We’ll create a simple QA chat bot that indexes a PDF and a youtube video as it’s knowledge base. This will allow accurate answers.
Code Explanation:
const question = process.argv[2] || 'hi'
const video = `https://youtu.be/zR_iuq2evXo?si=cG8rODgRgXOx9_Cn`
question
: Grabs the first command-line argument as the question to be queried. If none is provided, defaults to 'hi'.video
: Defines a YouTube video URL.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,
})
)
}
YoutubeLoader
to fetch the video's content and CharacterTextSplitter
to divide the content.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])
}