forked from remix-run/indie-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.server.ts
217 lines (185 loc) · 5.29 KB
/
chat.server.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
214
215
216
217
import { HNSWLib } from "@langchain/community/vectorstores/hnswlib";
import { CallbackManager } from "@langchain/core/callbacks/manager";
import { StringOutputParser } from "@langchain/core/output_parsers";
import {
ChatPromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate,
} from "@langchain/core/prompts";
import { RunnableSequence } from "@langchain/core/runnables";
import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai";
import { Note, User } from "@prisma/client";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import { formatDocumentsAsString } from "langchain/util/document";
import { Subject } from "rxjs";
import invariant from "tiny-invariant";
import { getAllNotes } from "~/models/note.server";
invariant(
typeof process.env.OPEN_AI_KEY === "string",
"OPEN_AI_KEY must be set.",
);
invariant(
typeof process.env.HNSW_INDEX_PATH === "string",
"HNSW_INDEX_PATH must be set.",
);
const _model = new ChatOpenAI({
streaming: true,
openAIApiKey: process.env.OPEN_AI_KEY,
modelName: process.env.OPEN_AI_MODEL_NAME ?? "gpt-3.5-turbo",
});
const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: 250,
chunkOverlap: 0,
});
const createDocument = ({
title,
id,
body,
userId,
}: Pick<Note, "id" | "body" | "title" | "userId">) => ({
pageContent: title + "\n" + body,
metadata: { id, title, userId },
});
const initVectorStore = async () => {
const embeddings = new OpenAIEmbeddings({
openAIApiKey: process.env.OPEN_AI_KEY,
modelName: "text-embedding-ada-002",
});
try {
const store = await HNSWLib.load(process.env.HNSW_INDEX_PATH!, embeddings);
return store;
} catch {
const notes = await getAllNotes();
const splitDocs = await textSplitter.splitDocuments(
notes.map(createDocument),
);
console.log("🤘 Creating embeddings...");
const store = await HNSWLib.fromDocuments(splitDocs, embeddings);
await store.save(process.env.HNSW_INDEX_PATH!);
return store;
}
};
const _vectorStore = initVectorStore();
async function addDocument(
note: Pick<Note, "id" | "body" | "title" | "userId">,
) {
const store = await _vectorStore;
const splitDocs = await textSplitter.splitDocuments([createDocument(note)]);
console.log("✨ Adding document to index.");
await store.addDocuments(splitDocs);
await store.save(process.env.HNSW_INDEX_PATH!);
return store;
}
async function removeDocument(id: Note["id"]) {
const store = await _vectorStore;
console.log("❌ Removing document from index.");
const document = Array.from(store.docstore._docs.entries()).find(
([, doc]) => doc.metadata.id === id,
);
if (!document) {
console.error("Error deleting document from index. Not found.");
return store;
}
store.index.markDelete(parseInt(document[0]));
await store.save(process.env.HNSW_INDEX_PATH!);
return store;
}
const SYSTEM_TEMPLATE = `Use the following pieces of context to answer the users question.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
The following context originates from the users notes.
----------------
{context}`;
const messages = [
SystemMessagePromptTemplate.fromTemplate(SYSTEM_TEMPLATE),
HumanMessagePromptTemplate.fromTemplate("{question}"),
];
const prompt = ChatPromptTemplate.fromMessages(messages);
const chat$ = new Subject<ChatResponse>();
async function askQuestion({ question, userId }: ChatRequest) {
const store = await _vectorStore;
const sources = await store.similaritySearch(
question,
5,
(doc) =>
// --- This filter will look different depending on the vector store you choose
typeof doc.metadata.userId === "string" && doc.metadata.userId === userId,
);
const chain = RunnableSequence.from([
{
context: (input) => formatDocumentsAsString(input.sources),
question: (input) => input.question,
},
prompt,
_model,
new StringOutputParser(),
]);
chain.invoke(
{ sources, question },
{
callbacks: CallbackManager.fromHandlers({
handleLLMNewToken: (text) =>
chat$.next({ type: "token", text, userId }),
handleLLMError: (e) =>
chat$.next({
type: "error",
userId,
error:
typeof e === "string"
? e
: e instanceof Error
? e.message
: "An error occurred please try again.",
}),
handleLLMStart: () =>
chat$.next({
type: "start",
userId,
}),
handleLLMEnd: () =>
chat$.next({
type: "end",
userId,
}),
}),
},
);
return sources;
}
//
// --- Chat Types
//
interface ChatTokenResponse {
type: "token";
text: string;
userId: User["id"];
}
interface ChatStartResponse {
type: "start";
userId: User["id"];
}
interface ChatEndResponse {
type: "end";
userId: User["id"];
}
interface ChatErrorResponse {
type: "error";
error: string;
userId: User["id"];
}
type ChatResponse =
| ChatTokenResponse
| ChatStartResponse
| ChatEndResponse
| ChatErrorResponse;
interface ChatRequest {
question: string;
userId: User["id"];
}
export {
askQuestion,
chat$,
addDocument,
removeDocument,
type ChatRequest,
type ChatResponse,
};