-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.js
More file actions
63 lines (47 loc) · 1.95 KB
/
chat.js
File metadata and controls
63 lines (47 loc) · 1.95 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
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { MemoryVectorStore } from "langchain/vectorstores/memory";
import { RetrievalQAChain } from "langchain/chains";
import { ChatOpenAI } from "langchain/chat_models/openai";
import { PromptTemplate } from "langchain/prompts";
import { PDFLoader } from "langchain/document_loaders/fs/pdf";
const chat = async (query, filePath = "./uploads/CV.pdf") => {
// step 1: document loading
const loader = new PDFLoader(filePath);
const data = await loader.load();
// step 2: splitting
const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: 500,
chunkOverlap: 0,
});
const splitDocs = await textSplitter.splitDocuments(data); // chunks of text
// step 3: embedding (text -> vector)
const embeddings = new OpenAIEmbeddings({
openAIApiKey: process.env.REACT_APP_OPENAI_API_KEY,
});
const vectorStore = await MemoryVectorStore.fromDocuments(
splitDocs,
embeddings
);
// step 4: retrival (optional), you can check the relevant splits it retrieved
// const relevantDocs = await vectorStore.similaritySearch("What is this article about?");
// step 5: question & answer
const model = new ChatOpenAI({
modelName: "gpt-3.5-turbo",
openAIApiKey: process.env.REACT_APP_OPENAI_API_KEY,
});
const template = `Use the following pieces of context to answer the question at the end.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
Use three sentences maximum and keep the answer as concise as possible.
{context}
Question: {question}
Helpful Answer:`;
const chain = RetrievalQAChain.fromLLM(model, vectorStore.asRetriever(), {
prompt: PromptTemplate.fromTemplate(template),
});
const response = await chain.call({
query,
});
return response;
};
export default chat;