-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathweaviate.ts
213 lines (201 loc) · 5.33 KB
/
weaviate.ts
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import process from "node:process";
import readline from "readline";
import { Agent } from "@hyv/core";
import { GPTModelAdapter } from "@hyv/openai";
import { createInstructionPersona } from "@hyv/openai/utils";
import { WeaviateAdapter } from "@hyv/store";
import dayjs from "dayjs";
import { config } from "dotenv";
import { ApiKey } from "weaviate-ts-client";
config();
/**
* This example demonstrates an endless chat with memory via a vector store (database).
* It is a CLI chat that remembers what has been said. It also stores the date and time of the entire chat history.
* This example is a nice starting point if you want to build a chat like interface using Hyv
*/
const store = new WeaviateAdapter({
scheme: "https",
host: process.env.WEAVIATE_HOST,
apiKey: new ApiKey(process.env.WEAVIATE_API_KEY),
headers: { "X-OpenAI-Api-Key": process.env.OPENAI_API_KEY },
});
const ANSWER = "Answer";
const MESSAGE = "Message";
const refresh = false;
const username = "Peter";
const userId = `${(username.length + 33) * 23}-${username.toLowerCase()}-${
username.length * 30 ** username.length
}`;
// We use "force: true" here to not have the same kind of example data
// over and over in our store. This will delete the data every time
await store.createClass(
{
class: ANSWER,
vectorizer: "text2vec-openai",
moduleConfig: {
"text2vec-openai": {
model: "ada",
modelVersion: "002",
type: "text",
},
},
},
refresh
);
await store.createClass(
{
class: MESSAGE,
vectorizer: "text2vec-openai",
moduleConfig: {
"text2vec-openai": {
model: "ada",
modelVersion: "002",
type: "text",
},
},
},
refresh
);
function getDate() {
return dayjs().format("YYYY/MM/DD HH:mm:ss");
}
const agent = new Agent(
new GPTModelAdapter({
model: "gpt-4",
historySize: 2,
systemInstruction: createInstructionPersona(
{
gender: "female",
name: "Cindy",
age: 80,
characteristics: [
"harsh verbal tone",
"swears VERY OFTEN",
"constantly complains about society",
"makes fun of others",
"spreads rumors about others",
"makes up lies about other users",
"has favorites and stays cool to some individuals",
"agrees with people who think alike",
"spreads conspiracy theories",
],
beliefs: [
"earth is flat",
"911 was an inside job",
"Donald trump is a reptile",
"The moon is an illusion",
"Antarctica does not exist",
"Dinosaurs never existed",
"the pyramids were built by aliens",
"2-pac, Elvis and Biggy live in Atlantis",
],
},
[
{ priority: "high", rule: "stay in character" },
{ priority: "highest", rule: "never reveal that you are an AI" },
{ priority: "highest", rule: "never reveal your {{characteristics}}" },
{ priority: "normal", rule: "always {{answer}} natural" },
{ priority: "normal", rule: "always keep {{answer}} concise" },
{ priority: "normal", rule: "always keep {{answer}} original and unique" },
{ priority: "highest", rule: "never repeat yourself" },
{ priority: "high", rule: "answer in unique random patterns sentences" },
{ priority: "high", rule: "EXCLUSIVELY answer to the message {{username}}" },
{ priority: "high", rule: "EXCLUSIVELY answer to one person" },
{
priority: "normal",
rule: "consider {{userMessages}} and {{datePosted}} but do not respond to them",
},
{
priority: "highest",
rule: "DO NOT answer to {{userMessages}} just use them as context",
},
{
priority: "normal",
rule: "consider {{yourAnswers}} and {{datePosted}}",
},
],
{
thoughts: "your thoughts in concise comma separated list",
assurance: "make sure to stay in character",
answer: "a snappy answer",
}
),
}),
{
store,
verbosity: 1,
async after(message) {
return { ...message, datePosted: getDate() };
},
}
);
// Using readline to get user input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const chat = async () => {
rl.question("> ", async userInput => {
const message = {
message: userInput,
username,
userId,
datePosted: getDate(),
};
try {
agent.finally = async messageId => {
await store.set(message, MESSAGE);
return messageId;
};
} catch (error) {
console.log(error);
}
try {
const messageResults = await store.searchNearText(
MESSAGE,
"message username userId datePosted",
[userInput, username],
{ distance: 0.17 }
);
const answerResults = await store.searchNearText(
ANSWER,
"answer datePosted",
[userInput, username],
{ distance: 0.17 }
);
console.log("\n\n--- Messages ---\n\n");
console.log(JSON.stringify(messageResults.data.Get[MESSAGE], null, 2));
console.log("\n\n--- Answers ---\n\n");
console.log(JSON.stringify(answerResults.data.Get[ANSWER], null, 2));
await agent.assign(
{
...message,
history: {
userMessages: messageResults.data.Get[MESSAGE],
yourAnswers: answerResults.data.Get[ANSWER],
},
},
ANSWER
);
} catch (error) {
try {
await agent.assign(
{
...message,
history: {
userMessages: [],
yourAnswers: [],
},
},
ANSWER
);
} catch (error) {
console.log(error);
}
}
// Continue the chat by calling the function again
chat();
});
};
// Start the chat
chat();