langchain icon indicating copy to clipboard operation
langchain copied to clipboard

can't seem to add filters in ConversationalRetrievalChain

Open ruturajgh opened this issue 1 year ago • 3 comments

Issue you'd like to raise.

i have a chromadb store that contains 3 to 4 pdfs stored, and i need to search the database for documents with metadata by the filter={'source':'PDFname'}, so it doesnt return with different docs containing similar data, the same is done with using similaritysearch() without any problems,

chain = ConversationalRetrievalChain.from_llm(OpenAI(temperature=0),
                                            docsearch.as_retriever(),
                                              memory=memory)
print(chain({'question':query}))

but i dont understand how to, when trying to use filters with ConversationalRetrievalchain, i have tried doing

docsearch.as_retriever(kwargs={'filter':{'source':'pdfname'}), but it doesnt seem to work

i saw something

retriever = vector_store.as_retriever()
retriever.search_kwargs = {'k':1}

but it doesnt seem to recognise the [dot]search_kwargs any help would be appreciated

Suggestion:

No response

ruturajgh avatar May 12 '23 11:05 ruturajgh

Try this

chain = ConversationalRetrievalChain.from_llm(
    OpenAI(temperature=0),
    docsearch.as_retriever(search_kwargs={'filter': {'source':'pdfname'}}),
    memory=memory
)
print(chain({'question':query}))

imeckr avatar May 15 '23 18:05 imeckr

hey thanks, your solution works,

but i made some changes with the conversational chain imports and passed the filter arguments along with the inputs object, incase i have to search between mutiple pdfs at once, this just propagates to the similarity search function and passes the filters and return docs from given pdfnames

` filter = [ {'source':'pdf_name'}, {'source':'pdf_name2} ]

print(chain ({"question" : question , 'filter' : filter}) `

its not as clean but it does the job

ruturajgh avatar May 16 '23 08:05 ruturajgh

Try this

chain = ConversationalRetrievalChain.from_llm(
    OpenAI(temperature=0),
    docsearch.as_retriever(search_kwargs={'filter': {'source':'pdfname'}}),
    memory=memory
)
print(chain({'question':query}))

hey thanks, your solution works,

but i made some changes with the conversational chain imports and passed the filter arguments along with the inputs object, incase i have to search between mutiple pdfs at once, this just propagates to the similarity search function and passes the filters and return docs from given pdfnames

 filter = [ {'source':'pdf_name'}, {'source':'pdf_name2} ]

print(chain ({"question" : question , 'filter' : filter}) 

its not as clean but it does the job

ruturajgh avatar May 16 '23 08:05 ruturajgh

Try this

chain = ConversationalRetrievalChain.from_llm(
    OpenAI(temperature=0),
    docsearch.as_retriever(search_kwargs={'filter': {'source':'pdfname'}}),
    memory=memory
)
print(chain({'question':query}))

hey thanks, your solution works,

but i made some changes with the conversational chain imports and passed the filter arguments along with the inputs object, incase i have to search between mutiple pdfs at once, this just propagates to the similarity search function and passes the filters and return docs from given pdfnames

 filter = [ {'source':'pdf_name'}, {'source':'pdf_name2} ]

print(chain ({"question" : question , 'filter' : filter}) 

its not as clean but it does the job

hi there, i think im trying to do something similar here. could you explain more on how you achieved multi doc filtering?

ItsJustSmellz avatar Jun 16 '23 23:06 ItsJustSmellz

what do you pass in for 'source'?

ItsJustSmellz avatar Jun 19 '23 21:06 ItsJustSmellz

I am using below chain. I want to use multiple categories in the filters. My logic is to bring the results from category=c1 OR category=c2 OR category=c3. How can we modify below code the achieve the objective

chain = ConversationalRetrievalChain.from_llm(OpenAI(temperature=0), retriever= vectorstore.as_retriever(search_kwargs={'filter': {'category':category}}), memory=memory, return_source_documents = True)

javedafroz avatar Jul 17 '23 07:07 javedafroz