forked from typesense/typesense-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ts
More file actions
133 lines (124 loc) · 3.45 KB
/
setup.ts
File metadata and controls
133 lines (124 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import "dotenv/config";
import { Client } from "../src/Typesense";
import type { CollectionCreateSchema } from "../src/Typesense/Collections";
import { ObjectAlreadyExists } from "../src/Typesense/Errors";
import { essays } from "./essays";
import { ConversationModelCreateSchema } from "../src/Typesense/ConversationModel";
export const client = new Client({
nodes: [
{
host: "localhost",
port: 8108,
protocol: "http",
},
],
apiKey: "xyz",
connectionTimeoutSeconds: 180,
});
export const collection = {
name: "pg-essays",
fields: [
{
name: "title",
type: "string",
facet: false,
},
{
name: "text",
type: "string",
facet: false,
},
{
name: "embedding",
type: "float[]",
embed: {
from: ["title", "text"],
model_config: {
model_name: "ts/snowflake-arctic-embed-m",
},
},
},
],
} as const satisfies CollectionCreateSchema;
export const model = {
id: "pg-essays-model",
model_name: "openai/gpt-4-turbo",
api_key: process.env.OPENAI_API_KEY ?? "",
history_collection: "conversation_store",
system_prompt:
"You are an assistant for question-answering like Paul Graham. You can only make conversations based on the provided context. If a response cannot be formed strictly using the context, politely say you don't have knowledge about that topic.",
max_bytes: 65536,
} as const satisfies ConversationModelCreateSchema;
export async function setup() {
try {
console.log("Creating collection");
await client.collections().create(collection);
console.log("Collection created");
} catch (error) {
if (error instanceof ObjectAlreadyExists) {
console.log("PG essays collection already exists");
} else {
console.error(error);
}
}
try {
console.log("Creating conversation store collection");
await client.collections().create({
name: "conversation_store",
fields: [
{
name: "conversation_id",
type: "string",
},
{
name: "model_id",
type: "string",
},
{
name: "timestamp",
type: "int32",
},
{
name: "role",
type: "string",
index: false,
},
{
name: "message",
type: "string",
index: false,
},
],
});
console.log("Conversation store collection created");
} catch (error) {
if (error instanceof ObjectAlreadyExists) {
console.log("Conversation store collection already exists");
} else {
console.error(error);
}
}
// if it fails, we're failing
console.log("Importing essays");
await client.collections(collection.name).documents().import(essays);
console.log("Essays imported");
try {
console.log("Creating conversation model");
await client.conversations().models().create(model);
console.log("Conversation model created");
} catch (error) {
if (error instanceof ObjectAlreadyExists) {
console.log("Conversation model already exists");
} else {
console.error(error);
}
}
}
export async function teardown() {
console.log("Deleting collection");
await client.collections(collection.name).delete();
console.log("Collection deleted");
console.log("Deleting conversation store collection");
await client.collections("conversation_store").delete();
console.log("Conversation store collection deleted");
}