Passing filters to ConversationalRetrievalQAChain with Pinecone DB retriever #8810
Replies: 1 comment
-
Answer generated by a 🤖 AnswerHello, Based on the context provided, it seems like you're trying to filter the documents retrieved from the Pinecone DB based on the "productID" metadata field. Unfortunately, the However, there's a workaround that you can use. You can extend the class FilteredRetriever extends VectorStoreRetriever {
vectorstore: VectorStoreRetriever;
search_type: string = "similarity";
search_kwargs: any = {};
filter_prefix: string;
get_relevant_documents(query: string): Document[] {
const results = this.vectorstore.get_relevant_documents(query);
return results.filter(doc => doc.metadata['productID'].startsWith(this.filter_prefix));
}
}
const filtered_retriever = new FilteredRetriever(vectorstore.asRetriever(), productID);
const chain = ConversationalRetrievalQAChain.fromLLM(
model,
filtered_retriever,
{
memory: new BufferMemory({
memoryKey: 'chat_history',
}),
},
);
const generatedAnswer = await chain.call({
question: chatMessage,
});
return generatedAnswer.text; In this code, Please note that this is a workaround and may not be the most efficient solution, especially if you have a large number of documents in your Pinecone DB. The filtering is done after retrieving the documents from the DB, not during the retrieval. A similar issue was discussed in the LangChain repository: Filtering retrieval with ConversationalRetrievalChain. The solution provided there is similar to the one I've suggested here. I hope this helps! If you have any further questions, feel free to ask. Sources
This response is meant to be useful, save you time, and share context. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a function which generates answers, using Pinecone DB as a retriever, which I have based on this document. When generating this answer, only specific documents in the DB should be included. Documents should be included if the metadata field "productID" matches the user input.
It seems like VectorStore.asRetriever supports this, but when I run the code, no filter is applied. The retriever simply returns answers generated from all documents in the index. If I deliberately pass an invalid value like
productID: {"$eq": "BAD_DATA"}
I still get results, even though no documents have their metadata populated with this value. Here is my code:It feels like I'm putting the filter in the wrong place, but I can't find any examples of how to do this. I've considered using PineconeTranslator but I can't see how to do this.
Beta Was this translation helpful? Give feedback.
All reactions