-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion-answer.js
More file actions
83 lines (69 loc) · 2.3 KB
/
Copy pathquestion-answer.js
File metadata and controls
83 lines (69 loc) · 2.3 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
import openAI from './openai.js';
import { Document } from 'langchain/document';
import { MemoryVectorStore } from 'langchain/vectorstores/memory';
import { OpenAIEmbeddings } from '@langchain/openai';
import { CharacterTextSplitter } from 'langchain/text_splitter';
import { PDFLoader } from '@langchain/community/document_loaders/fs/pdf';
import { YoutubeLoader } from '@langchain/community/document_loaders/web/youtube';
const question = process.argv[2] || 'hi';
const video = 'https://www.youtube.com/watch?v=rIrNIzy6U_g';
const createStore = docs =>
MemoryVectorStore.fromDocuments(docs, new OpenAIEmbeddings());
const docsFromYTVideo = video => {
const loader = YoutubeLoader.createFromUrl(video, {
language: 'en',
addVideoInfo: true,
});
return loader.loadAndSplit(
new CharacterTextSplitter({
separator: ' ',
chunkSize: 1000,
chunkOverlap: 70,
})
);
};
const docsFromPDF = () => {
const loader = new PDFLoader('./assets/quick-start-playstation.pdf');
return loader.loadAndSplit(
new CharacterTextSplitter({
separator: '. ',
chunkSize: 300,
chunkOverlap: 30,
})
);
};
const loadStore = async () => {
const videoDocs = await docsFromYTVideo(video);
const pdfDocs = await docsFromPDF();
// console.log('VIDEO', videoDocs[0]);
// console.log('PDF', pdfDocs[0]);
return createStore([...videoDocs, ...pdfDocs]);
};
const query = async () => {
const store = await loadStore();
const results = await store.similaritySearch(question, 1);
// console.log(results);
const response = await openAI.chat.completions.create({
model: 'gpt-3.5-turbo',
temperature: 0,
messages: [
{
role: 'system',
content:
'You are a helpful AI assistant. Answer questions to your best ability.',
},
{
role: 'user',
content: `Answer the following question using the provided context. If you cannot answer the question with the context, don't lie and make up stuff. Just say you need more context.
Question: ${question}
Context: ${results.map(result => result.pageContent).join('\n ')}
`,
},
],
});
console.log(`
Answer: ${response.choices[0].message.content}\n
Sources: ${results.map(result => result.metadata.source).join(', ')}
`);
};
query();