-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocumentQA.js
More file actions
76 lines (64 loc) · 2.31 KB
/
Copy pathdocumentQA.js
File metadata and controls
76 lines (64 loc) · 2.31 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
import { openai } from './openai.js'
import { Document } from '@langchain/core/documents'
import { MemoryVectorStore } from '@langchain/classic/vectorstores/memory'
import { OpenAIEmbeddings } from '@langchain/openai'
import { CharacterTextSplitter } from '@langchain/textsplitters'
import { PDFLoader } from '@langchain/community/document_loaders/fs/pdf'
import { YoutubeTranscript } from '@danielxceron/youtube-transcript'
const video = 'https://www.youtube.com/watch?v=zR_iuq2evXo'
const question = process.argv[2] || 'HI'
const createStore = (docs) =>
MemoryVectorStore.fromDocuments(docs, new OpenAIEmbeddings())
const docsFromYoutubeVideo = async (video) => {
const transcript = await YoutubeTranscript.fetchTranscript(video)
const text = transcript.map((t) => t.text).join(' ')
const youtubeDoc = new Document({
pageContent: text,
metadata: { source: 'youtube' },
})
const splitter = new CharacterTextSplitter({
separator: ' ',
chunkSize: 2500,
chunkOverlap: 100,
})
return splitter.splitDocuments([youtubeDoc])
}
const docsFromPDF = async () => {
const loader = new PDFLoader('xbox.pdf')
const pdfDoc = await loader.load()
const splitter = new CharacterTextSplitter({
separator: '. ',
chunkSize: 2500,
chunkOverlap: 200,
})
return splitter.splitDocuments(pdfDoc)
}
const loadStore = async () => {
const videoDocs = await docsFromYoutubeVideo(video)
const pdfDocs = await docsFromPDF()
return createStore([...videoDocs, ...pdfDocs])
}
const query = async () => {
const store = await loadStore()
const results = await store.similaritySearch(question, 2)
const response = await openai.chat.completions.create({
model: 'gpt-4',
temperature: 0,
messages: [
{
role: 'system',
content: 'You are an AI assistant. Answer questions to the best of your ability.',
},
{
role: 'user',
content: `Answer the following question using the provided context only. 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}\nSources: ${results.map((r) => r.metadata.source).join(', ')}`,
)
}
query()