Search code examples
pythonlangchain

How to combine ConversationalRetrievalChain with an agent?


I have a chain which is defined as:

convR_qa = ConversationalRetrievalChain(retriever=customRetriever, 
memory=memory, question_generator=question_generator_chain, 
combine_docs_chain=qa_chain, return_source_documents=True,  
return_generated_question=True, verbose=True )`

But now, I want to combine my chain with an agent, where agent can decide whether to retrieve or not depends on the user's intention. I know there is "Conversational Retrieval Agent" to handle this problem, but I have no idea how to combine my ConversationalRetrievalChain with an agent, as both question_generator_chain and qa_chain are important in my case, and I don't want to drop them. Thanks for your attention.

I have tried Conversational Retrieval Agent in langchain document. But it is hard to customize for me.


Solution

  • To handle with "How to decide to retrieve or not when using ConversationalRetrievalChain", I have a another solution rather than using "Conversational Retrieval Agent", which is token-consuming and not robust. A new LLMChain called "intention_detector" is defined in my ConversationalRetrievalChain, taking user's question as input. Then it will decide:

    intention = self.intention_detector.run(question=question)
    

    return true or false. The prompt template of intention_detector behaves like:

    PROMPT_FEW_SHOTS_INTENTION_DETECTION = FewShotPromptTemplate(
    examples=_intention_detection_examples, 
    example_prompt=_intention_detection_prompt_template, 
    prefix="Please judge does user's query be related to knowledge in specific domain. Return True or False.",
    suffix="Input: {question}\nOutput: ", 
    input_variables=["question"]
    

    )

    Fewshots examples are given above depending on your demand. Then, another LLMChain can handle with:

    PROMPT_CHAT_HISTORY = ChatPromptTemplate.from_messages([
    SystemMessage(content="Refer to the chat_history and answer the latest question."), 
    MessagesPlaceholder(variable_name="chat_history"),
    HumanMessagePromptTemplate.from_template("{question}"), 
    

    ])

    Therefore, our ConversationalRetrievalChain bahaves much more smart, like:

    Human: Who are you?
    AI(answer directly): I am an AI Assistant. How can I help you?
    

    And if you propose some professional issues:

    Human: Is biyadi worth investing in?
    (After retrieving the vectorDB)
    AI: Refer to the infomation provided by GF Securities, biyadi launches a lot new ......
    

    And finally, we can end the conversation:

    Human: Thanks for your help!
    AI(answer directly): You are welcome!
    

    Here our ConversationalRetrievalChain can distinguish human's intention, choosing to retrieve the vectorDB or not.