-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (58 loc) · 2.21 KB
/
app.js
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
require('dotenv').config();
const { PDFLoader } = require("langchain/document_loaders/fs/pdf");
const { DirectoryLoader } = require("langchain/document_loaders/fs/directory");
const { RecursiveCharacterTextSplitter } = require("langchain/text_splitter");
const { OpenAIEmbeddings } = require("langchain/embeddings/openai");
const { FaissStore } = require("langchain/vectorstores/faiss");
const { ChatOpenAI } = require("langchain/chat_models/openai");
const { BufferMemory } = require("langchain/memory");
const { ConversationalRetrievalQAChain } = require("langchain/chains");
const getPDFs = async () => {
try {
const directoryLoader = new DirectoryLoader("./documents",
{ ".pdf": (path) => new PDFLoader(path, { splitPages: false }) }
);
const docs = await directoryLoader.load();
const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: 1000,
chunkOverlap: 200,
separators: ["\n"],
});
const splitDocs = await textSplitter.splitDocuments(docs);
const embeddings = new OpenAIEmbeddings();
const vectorStore = await FaissStore.fromDocuments(splitDocs, embeddings);
const llm = new ChatOpenAI();
const memory = new BufferMemory({ memoryKey: "chat_history", returnMessages: true });
const conversationChain = ConversationalRetrievalQAChain.fromLLM(llm, vectorStore.asRetriever(), { memory });
console.log('Documents are loaded...');
return conversationChain;
} catch (error) {
console.error(error);
}
}
// get question from console
const readline = require('readline');
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const listenConsole = (conversation) => {
rl.question('Question: ', (cmd) => {
try {
if (cmd.toLowerCase() === 'exit') {
console.log('Exit...');
rl.close();
} else {
conversation?.call({ question: cmd }).then((answer) => {
console.log(`Answer: ${answer?.text}`);
listenConsole(conversation);
});
}
} catch (error) {
console.error(error);
}
});
}
async function main() {
console.log('Documents are loading...');
const conversation = await getPDFs();
listenConsole(conversation);
}
main();