generated from langchain-ai/langchain-nextjs-template
-
Notifications
You must be signed in to change notification settings - Fork 312
/
worker.ts
218 lines (191 loc) · 6.23 KB
/
worker.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
218
import { ChatWindowMessage } from "@/schema/ChatWindowMessage";
import { Voy as VoyClient } from "voy-search";
import { WebPDFLoader } from "langchain/document_loaders/web/pdf";
import { HuggingFaceTransformersEmbeddings } from "langchain/embeddings/hf_transformers";
import { VoyVectorStore } from "langchain/vectorstores/voy";
import { ChatOllama } from "langchain/chat_models/ollama";
import { Document } from "langchain/document";
import {
ChatPromptTemplate,
MessagesPlaceholder,
PromptTemplate,
} from "langchain/prompts";
import { BaseLanguageModel } from "langchain/base_language";
import { BaseRetriever } from "langchain/schema/retriever";
import { RunnableSequence } from "langchain/schema/runnable";
import { StringOutputParser } from "langchain/schema/output_parser";
import { AIMessage, BaseMessage, HumanMessage } from "langchain/schema";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
const embeddings = new HuggingFaceTransformersEmbeddings({
modelName: "Xenova/all-MiniLM-L6-v2",
});
const voyClient = new VoyClient();
const vectorstore = new VoyVectorStore(voyClient, embeddings);
const ollama = new ChatOllama({
baseUrl: "http://localhost:11435",
temperature: 0.3,
model: "mistral",
});
const REPHRASE_QUESTION_TEMPLATE = `Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.
Chat History:
{chat_history}
Follow Up Input: {question}
Standalone Question:`;
const rephraseQuestionChainPrompt = PromptTemplate.fromTemplate(
REPHRASE_QUESTION_TEMPLATE,
);
const RESPONSE_SYSTEM_TEMPLATE = `You are an experienced researcher, expert at interpreting and answering questions based on provided sources. Using the provided context, answer the user's question to the best of your ability using the resources provided.
Generate a concise answer for a given question based solely on the provided search results (URL and content). You must only use information from the provided search results. Use an unbiased and journalistic tone. Combine search results together into a coherent answer. Do not repeat text.
If there is nothing in the context relevant to the question at hand, just say "Hmm, I'm not sure." Don't try to make up an answer.
Anything between the following \`context\` html blocks is retrieved from a knowledge bank, not part of the conversation with the user.
<context>
{context}
<context/>
REMEMBER: If there is no relevant information within the context, just say "Hmm, I'm not sure." Don't try to make up an answer. Anything between the preceding 'context' html blocks is retrieved from a knowledge bank, not part of the conversation with the user.`;
const responseChainPrompt = ChatPromptTemplate.fromMessages<{
context: string;
chat_history: BaseMessage[];
question: string;
}>([
["system", RESPONSE_SYSTEM_TEMPLATE],
new MessagesPlaceholder("chat_history"),
["user", `{question}`],
]);
const formatDocs = (docs: Document[]) => {
return docs
.map((doc, i) => `<doc id='${i}'>${doc.pageContent}</doc>`)
.join("\n");
};
const createRetrievalChain = (
llm: BaseLanguageModel,
retriever: BaseRetriever,
chatHistory: ChatWindowMessage[],
) => {
if (chatHistory.length) {
return RunnableSequence.from([
rephraseQuestionChainPrompt,
llm,
new StringOutputParser(),
retriever,
formatDocs,
]);
} else {
return RunnableSequence.from([
(input) => input.question,
retriever,
formatDocs,
]);
}
};
const embedPDF = async (pdfBlob: Blob) => {
const pdfLoader = new WebPDFLoader(pdfBlob);
const docs = await pdfLoader.load();
const splitter = new RecursiveCharacterTextSplitter({
chunkSize: 500,
chunkOverlap: 50,
});
const splitDocs = await splitter.splitDocuments(docs);
self.postMessage({
type: "log",
data: splitDocs,
});
await vectorstore.addDocuments(splitDocs);
};
const _formatChatHistoryAsMessages = async (
chatHistory: ChatWindowMessage[],
) => {
return chatHistory.map((chatMessage) => {
if (chatMessage.role === "human") {
return new HumanMessage(chatMessage.content);
} else {
return new AIMessage(chatMessage.content);
}
});
};
const queryVectorStore = async (messages: ChatWindowMessage[]) => {
const text = messages[messages.length - 1].content;
const chatHistory: ChatWindowMessage[] = messages.slice(0, -1);
const retrievalChain = createRetrievalChain(
ollama,
vectorstore.asRetriever(),
chatHistory,
);
const responseChain = RunnableSequence.from([
responseChainPrompt,
ollama,
new StringOutputParser(),
]);
const fullChain = RunnableSequence.from([
{
question: (input) => input.question,
chat_history: RunnableSequence.from([
(input) => input.chat_history,
_formatChatHistoryAsMessages,
]),
context: RunnableSequence.from([
(input) => {
const formattedChatHistory = input.chat_history
.map(
(message: ChatWindowMessage) =>
`${message.role.toUpperCase()}: ${message.content}`,
)
.join("\n");
return {
question: input.question,
chat_history: formattedChatHistory,
};
},
retrievalChain,
]),
},
responseChain,
]);
const stream = await fullChain.stream({
question: text,
chat_history: chatHistory,
});
for await (const chunk of stream) {
if (chunk) {
self.postMessage({
type: "chunk",
data: chunk,
});
}
}
self.postMessage({
type: "complete",
data: "OK",
});
};
// Listen for messages from the main thread
self.addEventListener("message", async (event: any) => {
self.postMessage({
type: "log",
data: `Received data!`,
});
if (event.data.pdf) {
try {
await embedPDF(event.data.pdf);
} catch (e: any) {
self.postMessage({
type: "error",
error: e.message,
});
throw e;
}
} else {
try {
await queryVectorStore(event.data.messages);
} catch (e: any) {
self.postMessage({
type: "error",
error: `${e.message}. Make sure you are running Ollama.`,
});
throw e;
}
}
self.postMessage({
type: "complete",
data: "OK",
});
});