langchain-ai / langchain

πŸ¦œπŸ”— Build context-aware reasoning applications
https://python.langchain.com
MIT License
94.31k stars 15.25k forks source link

Issue: Using Milvus partitions with ConversationalRetrievalChain #11153

Closed Quicksilver466 closed 1 year ago

Quicksilver466 commented 1 year ago

Issue you'd like to raise.

I am using langchain version 0.0.281 and using Milvus as the vector store. For now, I have a Milvus-lite instance on my device. I tried using paritions with base vectorstore operations like add_documents and similarity_search and its working fine i.e it inserts and searches from the mentioned partition only. But when I try to use partitions with ConversationalRetrievalChain and even RetrievalQA it doesn't work i.e it retrieves documents through all partitions. When using from_llm classmethod I tried giving the partition names like below: QA = ConversationalRetrievalChain.from_llm( llm=LLM, retriever=VECTOR_DB.vec_db.as_retriever(partition_names=partition_names), memory=MEMORY, return_source_documents=True ) But this doesn't ensure that the vector store will only search the mentioned partitions, rather the vectorestore searches through all the documents present regardless of partition. Is there anyway I can limit the retrieval of documents to only those present in mentioned partition.

Suggestion:

No response

dosubot[bot] commented 1 year ago

πŸ€–

Hello,

Thank you for bringing up this issue. It seems like you're trying to limit the retrieval of documents to a specific partition when using the ConversationalRetrievalChain and RetrievalQA with a Milvus vector store.

Based on a similar issue in the LangChain repository, you can extend the VectorStoreRetriever class and use it to filter the documents based on their source path. Here's how you can do it:

class FilteredRetriever(VectorStoreRetriever):
    vectorstore: VectorStoreRetriever
    search_type: str = "similarity"
    search_kwargs: dict = Field(default_factory=dict)
    filter_prefix: str

    def get_relevant_documents(self, query: str) -> List[Document]:
        results = self.vectorstore.get_relevant_documents(query=query)
        return [doc for doc in results if doc.metadata['source'].startswith(self.filter_prefix)]

And use it like this:

filtered_retriever = FilteredRetriever(vectorstore=store.as_retriever(), filter_prefix=source_filter)

chain = ConversationalRetrievalChain.from_llm(
    llm=llm,
    retriever=filtered_retriever,
    memory=st.session_state.memory,
    verbose=True,
    return_source_documents=True,
)

This solution was suggested in the issue Filtering retrieval with ConversationalRetrievalChain and received positive reactions.

Additionally, you can use the 'filter' option in the 'search_kwargs' argument of the 'as_retriever' method to limit the retrieval of documents to a specific partition. Here is an example:

docsearch.as_retriever(
    search_kwargs={'filter': {'partition_name':'Your Partition Name'}}
)

Replace 'Your Partition Name' with the name of the partition you want to limit the retrieval to. This is supported by the 'as_retriever' method documentation:

# Use a filter to only retrieve documents from a specific paper
docsearch.as_retriever(
    search_kwargs={'filter': {'paper_title':'GPT-4 Technical Report'}}
)

In this example, the filter is used to limit the retrieval to documents from a specific paper. You can use the same approach to limit the retrieval to a specific partition.

As for your question about whether LangChain version 0.0.281 supports partitioning in Milvus vector store, I wasn't able to find a definitive answer in the repository. It might be worth checking the documentation or release notes for that specific version.

I hope this helps! If you have any other questions or need further clarification, feel free to ask.

Sources


This response is meant to be useful and save you time. 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.

If you want to continue the conversation, start your reply with @dosu-bot.

Quicksilver466 commented 1 year ago

For now it seems to be working by using search_kwargs but without placing partition names in filter like:

QA = ConversationalRetrievalChain.from_llm( llm=LLM, retriever=VECTOR_DB.vec_db.as_retriever(search_kwargs={'partition_names': partition_names}), memory=MEMORY, return_source_documents=True, return_generated_question=True, combine_docs_chain_kwargs={"prompt": PROMPT} )

I haven't tried by placing it in filter, but the above should work as I remember the retriever passing search_kwargs as kwargs in milvus search method, and milvus search method handles the partition_names.