Reputation: 625
I have some code pretty much copied verbatom from the langchain docs,
The code creates a vector store from a list of .txt documents. My assumption is the code that follows finds what it needs from the store relative to the question and uses the selected data to make the api call, instead of passing hundreds of lines of .txt into the payload.
This is pretty useful because I have many documents I need the AI to reference, but there is one document in particular which specifies some specific tasks I would like the AI to perform. I need to pass in this full document along with whatever comes back from the store to the req to openAI. Does anyone know if langchain offers support for this kind of task? I'm very new to this so my knowledge is very limited. Looking for some guidance
const docs = await textSplitter.createDocuments(txtFiles)
// Create a vector store from the documents.
const vectorStore = await HNSWLib.fromDocuments(docs, embeddings)
// Create a chain that uses the OpenAI LLM and HNSWLib vector store.
const chain = RetrievalQAChain.fromLLM(model, vectorStore.asRetriever())
// Call the chain with the prompt.
const chatGptRes = await chain.call({
query: prompt,
})
Upvotes: 1
Views: 1505
Reputation: 163
This is how the documentation suggests it, hope this helps.
The langchain discord is pretty good too, well worth a look
one example might be to use the directory loader to get all the .json files for example in ./docs
llm = //add yourr openai instance here
//get the query from the request
const userPrompt = "how big is the universe";
// your directory holding your docs ./docs
const directoryLoader = new DirectoryLoader("docs", {
".json": (path) => new JSONLoader(path),
});
// load the documents into the docs variable
const docs = await directoryLoader.load();
// Load the vector store from the directory
const directory = "./vectorstore";
const loadedVectorStore = await HNSWLib.load(
directory,
new OpenAIEmbeddings()
);
const chain = RetrievalQAChain.fromLLM(
llm,
loadedVectorStore.asRetriever()
);
const res = await chain.call({
input_documents: docs,
query: userPrompt,
});
Upvotes: 2