Skip to content

Instantly share code, notes, and snippets.

@tgfjt
Created April 20, 2023 15:04
Show Gist options
  • Save tgfjt/a82de8b16d954c50a905f79d2f309bdd to your computer and use it in GitHub Desktop.
Save tgfjt/a82de8b16d954c50a905f79d2f309bdd to your computer and use it in GitHub Desktop.
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