-
Notifications
You must be signed in to change notification settings - Fork 37
/
qa.js
77 lines (66 loc) · 2.14 KB
/
qa.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
import 'dotenv/config'
import { MemoryVectorStore } from 'langchain/vectorstores/memory'
import { OpenAIEmbeddings } from 'langchain/embeddings/openai'
import { YoutubeLoader } from 'langchain/document_loaders/web/youtube'
import { CharacterTextSplitter } from 'langchain/text_splitter'
import { PDFLoader } from 'langchain/document_loaders/fs/pdf'
import { openai } from './openai.js'
const question = process.argv[2] || 'hi'
const video = `https://youtu.be/zR_iuq2evXo?si=cG8rODgRgXOx9_Cn`
export const createStore = (docs) =>
MemoryVectorStore.fromDocuments(docs, new OpenAIEmbeddings())
export const docsFromYTVideo = async (video) => {
const loader = YoutubeLoader.createFromUrl(video, {
language: 'en',
addVideoInfo: true,
})
return loader.loadAndSplit(
new CharacterTextSplitter({
separator: ' ',
chunkSize: 2500,
chunkOverlap: 100,
})
)
}
export const docsFromPDF = () => {
const loader = new PDFLoader('xbox.pdf')
return loader.loadAndSplit(
new CharacterTextSplitter({
separator: '. ',
chunkSize: 2500,
chunkOverlap: 200,
})
)
}
const loadStore = async () => {
const videoDocs = await docsFromYTVideo(video)
const pdfDocs = await docsFromPDF()
return createStore([...videoDocs, ...pdfDocs])
}
const query = async () => {
const store = await loadStore()
const results = await store.similaritySearch(question, 1)
const response = await openai.chat.completions.create({
model: 'gpt-3.5-turbo-16k-0613',
temperature: 0,
messages: [
{
role: 'assistant',
content:
'You are a helpful AI assistant. Answser 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((r) => r.pageContent).join('\n')}`,
},
],
})
console.log(
`Answer: ${response.choices[0].message.content}\n\nSources: ${results
.map((r) => r.metadata.source)
.join(', ')}`
)
}
query()