English 中文(简体)
2. 与抗逆性再恢复同步进行动态过滤 链条
原标题:Dynamic filtering with ConversationalRetrievalChain
  • 时间:2023-08-14 08:46:25
  •  标签:
  • langchain

I m试图增加基本病媒储存的元数据过滤(chroma)。

db = Chroma.from_documents(texts, embeddings)

它这样做:

qa = ConversationalRetrievalChain.from_llm(
    OpenAI(openai_api_key=get_random_key(OPENAI_API_KEY_POOL), cache=True, temperature=0),
    VectorStoreRetriever(vectorstore=db, search_kwargs={"filter": {"source": "data/my.pdf"}}),
    verbose=True,
    return_source_documents=True) 
result = qa({"question": query, "chat_history": []})

但这意味着为每份文件建立一个单独的链条,似乎已经过。 然而,当我试图将过滤器送到现有的链条时,过滤器似乎没有任何效果,它使所有文件都得到回报。

qa = ConversationalRetrievalChain.from_llm(
    OpenAI(openai_api_key=get_random_key(OPENAI_API_KEY_POOL), cache=True, temperature=0),
    VectorStoreRetriever(vectorstore=db),
    verbose=True,
    return_source_documents=True)

filter = { source :  my.pdf }

result = qa({"question": query, "chat_history": [], "filter": filter})

我失踪了吗? 或者在不扩大现有类别/修改源代码<条码>兰链的情况下真正赢得t工作?

最佳回答

我最后扩大两门一流,用来通过过滤器:

class ConversationalRetrievalChainPassArgs(ConversationalRetrievalChain):

    def _get_docs(self, question: str, inputs: Dict[str, Any], *,
        run_manager: CallbackManagerForChainRun) -> List[Document]:
        """Get docs."""
        docs = self.retriever._get_relevant_documents(
            question, inputs[ filter ]
        )
        return self._reduce_tokens_below_limit(docs)


class VectorStoreRetrieverWithFiltering(VectorStoreRetriever):

    def _get_relevant_documents(self, query: str, filter: dict) -> List[Document]:
        if self.search_type == "similarity":
            docs = self.vectorstore.similarity_search(query, filter=filter, **self.search_kwargs)
        ...
        return docs

如果任何人的解决办法更可取,请分享。

问题回答

我的矢量数据库是OpenSearch,从过滤到。 缩略语 我不敢肯定这是否具有完全相关性,但无论如何,我看着类似的执行:

qa = ConversationalRetrievalChain.from_llm(
    llm=model,
    retriever=vectorStore.as_retriever(
        search_kwargs={
            "filter": [
                {
                    "term": {
                        "metadata.language": {
                            "value": query_string_params["language"],
                            "case_insensitive": True,
                        }
                    },
                },
            ],
        )
)




相关问题
Installing node-faiss & libomp when deploying

I m building AI website with Chagpt API & langchain. all using node.js, express now when I want to deploy the project on any platform, like vercel or renderjs I get this error telling me that ...

How to add conversational memory to pandas toolkit agent?

I want to add a ConversationBufferMemory to pandas_dataframe_agent but so far I was unsuccessful. I have tried adding the memory via construcor: create_pandas_dataframe_agent(llm, df, verbose=True, ...