-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathchatbot.js
More file actions
482 lines (455 loc) · 22.5 KB
/
Copy pathchatbot.js
File metadata and controls
482 lines (455 loc) · 22.5 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
const { OpenAI } = require("langchain/llms/openai");
const { ChatOpenAI } = require("langchain/chat_models/openai");
const { BufferWindowMemory, CombinedMemory, ChatMessageHistory } = require("langchain/memory");
const { MemoryVectorStore } = require("langchain/vectorstores/memory");
const { OpenAIEmbeddings } = require("langchain/embeddings/openai");
const { TextLoader } = require("langchain/document_loaders/fs/text");
const { Ollama } = require("langchain/llms/ollama");
const { HumanMessage, AIMessage, SystemMessage } = require("langchain/schema");
const DriveUtils = require("./gdrive");
const { HuggingFaceInference } = require("langchain/llms/hf");
const { Calculator } = require("langchain/tools/calculator");
const { initializeAgentExecutorWithOptions } = require("langchain/agents");
const { DynamicTool } = require("langchain/tools");
const { VectorStoreRetrieverMemory } = require("langchain/memory");
const { RecursiveCharacterTextSplitter } = require("langchain/text_splitter");
//const { HNSWLib } = require("langchain/vectorstores/hnswlib");
const { FaissStore } = require("langchain/vectorstores/faiss");
const { DEFAULT_PREFIX, DEFAULT_SUFFIX } = require("../example/server/prompts");
const fs = require("fs");
const { ChatPromptTemplate, MessagesPlaceholder, StringPromptValue } = require("langchain/prompts");
const { spawn } = require("child_process");
//
// https://myaccount.google.com/connections?filters=3,4&hl=en
// https://developers.google.com/oauthplayground/
// https://console.cloud.google.com/apis/credentials/oauthclient/
//
/*
//
// TODO: request_headers: {} // Example: {'Bearer Token: <API KEY>}
// MaybeDO ? - this.drive_chatbot.generate_OAuth2Link()
// ['buffer', 'conversation_buffer_window', 'buffer_window', 'entity', 'multiple', 'conversation_summary', 'vector_store']
*/
class Chatbot {
constructor(props) {
this.verbose = props.verbose || false;
this.model = props.model || false;
this.model_service = props.model.service;
this.model_config = this.model.model_config;
const useOpenAiModel = !!this.model_config.openAIApiKey;
const useHfAiModel = !!this.model_config.huggingFaceApiKey;
if (!this.model_service && useOpenAiModel) this.model_service = "chatOpenAi";
if (!this.model_service && useHfAiModel) this.model_service = "huggingFace";
if (!this.model_service && !openAiModel && !useOpenAiModel) {
// Install the huggingface Flant t5 small model and spin up a local endpoint for that.
this.model_service = "Endpoint";
this.model_config = { baseUrl: "http://localhost:8912/", model: "meta-llama/Llama-2-7b" };
const model_server = spawn("node", [__dirname + "/ollama_server.js", "--model", "meta-llama/Llama-2-7b"]);
model_server.stdout.on("data", data => console.log(`stdout: ${data}`));
model_server.stderr.on("data", data => console.error(`stderr: ${data}`));
model_server.on("close", code => console.log(`child process exited with code ${code}`));
console.debug(
"WARNING: Using Default model. PLEASE update using props.model: ['openAi', 'huggingFace', 'Endpoint']."
);
}
if (!this.model_config) {
console.debug(
"WARNING: Using Default model config. PLEASE update props.model_config to correspond with a valid langchain configuration object \
EXAMPLE: chatBot({ \
model:'openAi', \
model_config:{ \
modelName: 'gpt-3.5-turbo', \
maxTokens: 256, \
openAIApiKey: OPENAI_API_KEY, \
temperature: 0.9 })"
);
}
if (this.model_service.includes("openAi")) this.model = new OpenAI(this.model_config);
else if (this.model_service.includes("chatOpenAi") || useOpenAiModel)
this.model = new ChatOpenAI(this.model_config);
else if (this.model_service.includes("huggingFace") || useHfAiModel) {
this.model_config.model_id = this.model_config.model_id || this.model_config.modelName || "meta-llama/Llama-2-7b";
this.model = new HuggingFaceInference(this.model_config);
} else if (this.model === "Endpoint") this.model = new Ollama(this.model_config);
else
console.error(
"Error: At least one model must be specified. Values: ['chatOpenAi', 'openAi', 'huggingFace', 'Endpoint']"
);
this.chat_history_id = null;
this.pastMessages = [];
this.memory_length = props.memory_length || 5;
this.vector_length = props.vector_length || 2;
this.chat_embeddings_id = null;
this.chat_embeddings_data = [];
this.embeddings_id = null;
this.embeddings_data = [];
this.bufferMemory = [];
this.memory = [];
this.agent = props.agent.type || "chat-conversational-react-description";
this.agent_config = props.agent.agent_config || {};
this.agent_verbose = props.agent.verbose || false;
this.DEFAULT_PREFIX = props.agent.prefix || DEFAULT_PREFIX;
this.DEFAULT_SUFFIX = props.agent.suffix || DEFAULT_SUFFIX;
this.tools = [
new Calculator(),
...(this.driveClient
? [
new DynamicTool({
name: "Drive Util: List Files",
description: "Call this to get a list of all files of a mimeType in a users drive. Inputs: mimeType",
func: async () => {
this.verbose && console.log("Drive Util: List Files");
try {
let response = await this.driveClient.listFiles({});
let filenames = response.data.files.map(file => file.name).slice(0, 10);
return filenames;
} catch (error) {
let msg = "Error Connecting to Drive.";
this.verbose && console.log(error);
return msg;
}
}
}),
new DynamicTool({
name: "Drive Util: Read Contents",
description: "Call this to get a single file in a users drive. Inputs: filename.ext",
func: async (filename = "test.txt") => {
try {
this.verbose && console.log("Drive Util: Read Contents");
let response = await this.driveClient.getFileByName(filename);
let innerContent = response.data;
fs.writeFileSync(filename, innerContent, "utf8");
const loader = new TextLoader(filename);
const docs = await loader.load();
fs.unlinkSync(filename);
return "The File has been read.";
} catch (error) {
let msg = "Error Connecting to Drive.";
this.verbose && console.log(error);
return msg;
}
}
}),
new DynamicTool({
name: "Fetch From Knowledge Base",
description: "Call this to fetch a file from a single file in a users drive. Inputs: filename.ext",
func: async (filename = "test.txt") => {
try {
this.verbose && console.log("Drive Util: Read Contents");
let response = await this.driveClient.getFileByName(filename);
let innerContent = response.data;
fs.writeFileSync(filename, innerContent, "utf8");
const loader = new TextLoader(filename);
const docs = await loader.load();
fs.unlinkSync(filename);
return "The File has been read.";
} catch (error) {
let msg = "Error Connecting to Drive.";
this.verbose && console.log(error);
return msg;
}
}
})
]
: []),
...(props.agent.tools || [])
];
this.driveClient = props.drive.web
? new DriveUtils({ ...props.drive.web, verbose: props.drive?.web?.verbose !== undefined ? props.drive.web.verbose : props.drive?.verbose !== undefined ? props.drive.verbose : this.verbose })
: false;
this.drive_chat_history_filename = props?.drive?.web?.chat_history_filename || "chatbot_chat_history.json";
this.drive_chat_history_filepath = props?.drive?.web?.chat_history_filepath || "chatbot/memory";
this.drive_chat_embeddings_filename = props?.drive?.web?.chat_embeddings_filename || "chatbot_chat_embeddings.json";
this.drive_chat_embeddings_filepath = props?.drive?.web?.chat_embeddings_filepath || "chatbot/memory";
this.drive_document_directory_filename = props?.drive?.web?.documents_filename || "chatbot_documents.json";
this.drive_document_directory_filepath = props?.drive?.web?.document_directory_filepath || "chatbot/memory";
this.vectorStore = new MemoryVectorStore(new OpenAIEmbeddings());
this.vectorStoreRetrieverMemory = new VectorStoreRetrieverMemory({
vectorStoreRetriever: this.vectorStore.asRetriever(this.vector_length), // Return top n docs
memoryKey: "chat_vector",
inputKey: "input"
});
this.driveServer = props.drive.server
? new DriveUtils({ ...props.drive.server, verbose: props.drive?.server?.verbose !== undefined ? props.drive.server.verbose : props.drive?.verbose !== undefined ? props.drive.verbose : this.verbose })
: false;
this.drive_embed_from_folder = props.drive?.server?.embed_from_folder || "chatbot";
this.drive_embed_to_folder = props.drive?.server?.embed_to_folder || "chatbot";
this.drive_embeddings_filename = props.drive?.server?.embeddings_filename || "embeddings.json";
this.kBVectorStore = new MemoryVectorStore(new OpenAIEmbeddings());
this.kBVectorStoreRetrieverMemory = new VectorStoreRetrieverMemory({
vectorStoreRetriever: this.kBVectorStore.asRetriever(this.vector_length), // Return top n docs
memoryKey: "server_vector",
inputKey: "input"
});
// Fetch data from drive and initialize the chatbot using user-given props
this.init();
}
// Fetch data from drive and initialize the chatbot using user-given props
async init() {
this.getMemory();
this.bufferMemory = new BufferWindowMemory({
chatHistory: new ChatMessageHistory(this.pastMessages),
k: this.memory_length,
returnMessages: true,
memoryKey: "chat_history",
inputKey: "input"
});
this.memory = new CombinedMemory({
memories: [this.bufferMemory, this.vectorStoreRetrieverMemory, this.kBVectorStoreRetrieverMemory]
});
//
// Initialize the agent. ConversationChain is used as the default agent w a BufferMemory.
// Always use this one for now.
//
let config = {
agentType: this.agent,
memory: this.memory,
verbose: this.agent_verbose,
...this.agent_config,
agentArgs: {
systemMessage: this.DEFAULT_PREFIX,
humanMessage: this.DEFAULT_SUFFIX
}
}
this.verbose && console.log("Chatbot: Initializing Agent Executor with config: ");
this.agentExecutor = await initializeAgentExecutorWithOptions(this.tools, this.model, config);
let chain = this.agentExecutor.agent.llmChain;
let prompt_messages = chain.prompt.promptMessages;
// This will insert our chat_vector memory into the model
prompt_messages = [
new MessagesPlaceholder("chat_vector"),
new MessagesPlaceholder("server_vector"),
...prompt_messages
];
// const response = await this.knowledgeBaseVectorStore.similaritySearch(prompt, 1);
let memory = this.agentExecutor.memory;
// uses the originals result but then modifies it to work w the agent.
// when agentExecutor.call is made, base chain calls _formatValues which retrieves data from memory.
memory.loadMemoryVariables_original = memory.loadMemoryVariables;
memory.loadMemoryVariables = async values => {
let returnThis = await memory.loadMemoryVariables_original(values);
returnThis.server_vector = [
new SystemMessage(
"## Document Excerpts: \n\n The following is a random text extract you may use to help you: \n" +
returnThis.server_vector
)
];
returnThis.chat_vector = [
new SystemMessage(
"## Past Conversations: \n\n The following is an text extract from past conversations that LangDrive can use: \n" +
returnThis.chat_vector
)
];
return returnThis;
};
/*
// This would add a new input to the human message?
let systemPrompt = prompt_messages[2].prompt;
systemPrompt.inputVariables = [...systemPrompt.inputVariables, "chat_vector"];
systemPrompt.template = systemPrompt.template + "{chat_vector}\n";
*/
let template = ChatPromptTemplate.fromPromptMessages(prompt_messages);
this.agentExecutor.agent.llmChain.prompt = template;
return true;
}
async sendMessage(message) {
this.verbose && console.log('Chatbot: Message: ', message, '\n');
let response = await this.agentExecutor.call({ input: message });
this.verbose && console.log('\nChatbot: Response: \n', response.output, '\n');
this.saveMemory();
return response.output;
}
async getMemory() {
if (this.driveClient) {
try {
console.log('this.drive', this.driveClient)
let chatpath = this.drive_chat_history_filepath + "/" + this.drive_chat_history_filename;
this.verbose && this.driveClient.verbose && console.log("Chatbot: getMemory: chat history: START");
let response = await this.driveClient.createAndOrGetContent({
path: chatpath,
mimeType: "application/json",
message: `[]`
});
// Update our Agent
this.chat_history_id = response.data.metadata.id;
this.pastMessages = this.mapMessages(response.data.file);
this.agentExecutor.memory.memories[0] = new BufferWindowMemory({
chatHistory: new ChatMessageHistory(this.pastMessages),
k: this.memory_length,
returnMessages: true,
memoryKey: "chat_history",
inputKey: "input"
});
this.verbose && this.driveClient.verbose && console.log("Chatbot: getMemory: chat history: SUCCESS"); // { past_msgs: this.pastMessages });
} catch (error) {
this.verbose && this.driveClient.verbose && console.error("Chatbot: getMemory: chat history: ERROR: Failed to get message history:", error);
}
try {
this.verbose && this.driveClient.verbose && console.log("Chatbot: getMemory: Embed history: START");
let embedPath = this.drive_chat_embeddings_filepath + "/" + this.drive_chat_embeddings_filename;
// console.log("\n\n RETRIEVING MEMORY chat embeddings", embedPath);
let response = await this.driveClient.createAndOrGetContent({
path: embedPath,
mimeType: "application/json",
message: `[]`
});
// Update our Agent
let data = response.data;
this.chat_embeddings_id = data.metadata && data.metadata.id;
this.chat_embeddings_data = data.file;
// console.log(this.chat_embeddings_data)
let docText = [data];
this.verbose && this.driveClient.verbose && console.log("Chatbot: getMemory: Embed history: SUCCESS");
let ids = docText.map((doc, index) => ({ id: index + 1 }));
this.verbose && this.driveClient.verbose && console.log("Chatbot: getMemory: Embed history: FaissStore.fromTexts: START");
console.log('WEB', { docText, ids })
/*
this.vectorStore = await FaissStore.fromTexts(
["Hello world", "Bye bye", "hello nice world"],
[{ id: 2 }, { id: 1 }, { id: 3 }],
new OpenAIEmbeddings()
);
this.verbose && this.driveClient.verbose && console.log("Chatbot: getMemory: Embed history: FaissStore.fromTexts: SUCCESS");
this.verbose && this.driveClient.verbose && console.log("Chatbot: getMemory: Embed history: CREATING vectorStoreRetrieverMemory");
this.vectorStoreRetrieverMemory = new VectorStoreRetrieverMemory({
vectorStoreRetriever: this.kBVectorStore.asRetriever(this.vector_length), // Return top n docs
memoryKey: "chat_vector",
inputKey: "input"
});
this.verbose && this.driveClient.verbose && console.log("Chatbot: getMemory: Embed history: ASSIGNING TO AGENTEXECUTOR");
this.agentExecutor.memory.memories[1] = this.VectorStoreRetrieverMemory;
this.verbose && this.driveClient.verbosev && console.log("Chatbot: getMemory: Embed history: SUCCESS");
*/
//
//
//
} catch (error) {
this.verbose && this.driveClient.verbose && console.error("Chatbot: getMemory: Client Embed history: ERROR: Failed to get chat embeddings data:", error);
}
} else {
this.verbose &&
console.log(
"Google Drive - driveClient - Not Configured. For data persistance, please provide: CLIENT_ID, CLIENT_SECRET, ACCESS_TOKEN*"
);
}
if (this.driveServer) {
try {
this.verbose && this.driveServer.verbose && console.error("Chatbot: getMemory: Server Embeddings: START");
// first make sure embeddings path is ok
if (typeof embedPath === "string") {
embedPath =
embedPath.indexOf("./") === 0
? embedPath.substring(2)
: embedPath.indexOf("/") === 0
? embedPath.substring(1)
: embedPath;
}
let embeddingsFile = await this.driveServer.createAndOrGetContent({
path: this.drive_embed_to_folder + "/" + this.drive_embeddings_filename,
mimeType: "application/json",
message: `[]`
});
let fromDirectory = await this.driveServer.createAndOrGetContent({
path: this.drive_embed_from_folder,
mimeType: "folder"
});
// For each file in folder, get file contents
let files = fromDirectory.data.files;
this.verbose && this.driveServer.verbose && console.error("Chatbot: getMemory: Server Embeddings: GETTING FILES FROM DIRECTORY", this.drive_embed_from_folder, files);
let docText = (await files)
.filter(file => file.name.endsWith(".txt"))
.map(async file => {
let fileContents = await this.driveServer.getFileById({ id: file.id });
if (fileContents.status == 400) return error;
return fileContents.data;
});
docText = await Promise.all(docText);
const ids = docText.map((doc, index) => ({ id: index + 1 }));
let uploadThis = { texts: docText, ids };
this.verbose && this.driveServer.verbose && console.error("Chatbot: getMemory: Server Embeddings: UPDATING EMBED FILE", uploadThis);
this.driveServer.updateFile({
fileId: embeddingsFile.data.metadata.id,
mimeType: "application/json",
message: uploadThis
});
// Update our Agent
this.verbose && this.driveServer.verbose && console.error("Chatbot: getMemory: Server Embeddings: FaissStore.fromTexts: START");
console.log('SERVER ',{ docText, ids })
this.kBVectorStore = await FaissStore.fromTexts(docText, ids,
new OpenAIEmbeddings()
);
this.verbose && this.driveServer.verbose && console.error("Chatbot: getMemory: Server Embeddings: FaissStore.fromTexts: SUCCESS");
this.verbose && this.driveServer.verbose && console.error("Chatbot: getMemory: Server Embeddings: kBVectorStoreRetrieverMemory: START");
this.kBVectorStoreRetrieverMemory = new VectorStoreRetrieverMemory({
vectorStoreRetriever: this.kBVectorStore.asRetriever(this.vector_length), // Return top n docs
memoryKey: "server_vector",
inputKey: "input"
});
this.verbose && this.driveServer.verbose && console.error("Chatbot: getMemory: Server Embeddings: kBVectorStoreRetrieverMemory: SUCCESS");
this.verbose && this.driveServer.verbose && console.error("Chatbot: getMemory: Server Embeddings: memories[2]: START");
this.agentExecutor.memory.memories[2] = this.kBVectorStoreRetrieverMemory;
this.verbose && this.driveServer.verbose && console.error("Chatbot: getMemory: Server Embeddings: memories[2]: SUCCESS");
//
//
//
} catch (error) {
this.verbose && console.error("Chatbot: getMemory: Server Embeddings: ERROR: Failed to get embeddings data:", error);
}
} else {
this.verbose &&
console.log(
"Google Drive - driveServer - Not Configured. For data persistance, please provide: CLIENT_ID, CLIENT_SECRET, ACCESS_TOKEN*"
);
}
}
// called after every message to Update the google drive(s) each time.
async saveMemory() {
if (this.driveClient) {
// for the page-visitors drive.
try {
this.verbose && console.log("ChatBot: saveMemory: Client Chat Buffer: START");
let bufferMemory = this.agentExecutor.memory.memories[0];
let status = await this.driveClient.updateFile({
fileId: this.chat_history_id,
mimeType: "application/json",
message: bufferMemory.chatHistory.messages
});
this.verbose && console.log("ChatBot: saveMemory: Client Chat Buffer: SUCCESS")
} catch (error) { this.verbose && console.log("ERROR: Could not Save Chat History: ", error); }
try {
this.verbose && console.log("ChatBot: saveMemory: Client Embeddings: START");
let memory = this.agentExecutor.memory.memories[1];
let vectors = memory.vectorStoreRetriever.vectorStore.memoryVectors;
let status = await this.driveClient.updateFile({
fileId: this.chat_embeddings_id,
mimeType: "application/json",
message: vectors
});
this.verbose && console.log("ChatBot: saveMemory: Client Embeddings: SUCCESS")
} catch (error) { this.verbose && console.log("ERROR: Could not Save Chat Embeddings: ", error); }
}
if (this.driveServer) {
this.verbose && console.log("ChatBot: saveMemory: Server Embeddings: Status: START");
// No need to upload anything to the server.. but.
// We need to overwrite this memory vector store to prevent the chat appending.
this.agentExecutor.memory.memories[2] = this.kBVectorStoreRetrieverMemory;
this.verbose && console.log("ChatBot: saveMemory: Server Embeddings: Status: Success");
}
}
mapMessages(content) {
return (
content.map(msg => {
const content = msg.kwargs.content;
const additional_kwargs = msg.kwargs.additional_kwargs;
if (msg.id.includes("HumanMessage")) {
return new HumanMessage(content, additional_kwargs);
} else if (msg.id.includes("AIMessage")) {
return new AIMessage(content, additional_kwargs);
} else if (msg.id.includes("SystemMessage")) {
return new SystemMessage(content, additional_kwargs);
}
}) || []
);
}
}
module.exports = Chatbot;