Created
April 20, 2023 15:04
-
-
Save tgfjt/a82de8b16d954c50a905f79d2f309bdd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Document } from 'langchain/document'; | |
import { OpenAIChat } from 'langchain/llms/openai'; | |
import { OpenAIEmbeddings } from 'langchain/embeddings/openai'; | |
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter'; | |
import { MemoryVectorStore } from 'langchain/vectorstores/memory'; | |
import { VectorDBQAChain } from 'langchain/chains'; | |
export async function main(apiKey: string) { | |
const text = `some text`; | |
const splitter = new RecursiveCharacterTextSplitter({ | |
chunkSize: 900, | |
chunkOverlap: 200, | |
}); | |
const docs = await splitter.splitDocuments([ | |
new Document({ pageContent: text }), | |
]); | |
const vectorStore = await MemoryVectorStore.fromDocuments( | |
docs, | |
new OpenAIEmbeddings({ | |
openAIApiKey: apiKey, | |
}), | |
); | |
const model = new OpenAIChat({ | |
openAIApiKey: apiKey, | |
temperature: 0, | |
}); | |
const chain = VectorDBQAChain.fromLLM(model, vectorStore, { | |
k: 1, | |
returnSourceDocuments: true, | |
}); | |
const res = await chain.call({ | |
query: 'some prompt', | |
}); | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment