Search code examples
langchain

Make LangChain agent ask clarifying question


I'm trying to implement a langchain agent that is able to ask clarifying questions in case some information is missing. Is this at all possible? A simple example would be

Input: "Please give me a recipe for a cake"
Agent: "Certainly. What kind of cake do you have in mind?"
Input: "A chocolate cake"
Agent: "Certainly, here is a recipe for a chocolate cake..."

Solution

  • Found it in the docs

    from langchain.chat_models import ChatOpenAI
    from langchain.llms import OpenAI
    from langchain.agents import load_tools, initialize_agent
    from langchain.agents import AgentType
    
    llm = ChatOpenAI(temperature=0.0)
    math_llm = OpenAI(temperature=0.0)
    tools = load_tools(
        ["human", "llm-math"], 
        llm=math_llm,
    )
    
    agent_chain = initialize_agent(
        tools,
        llm,
        agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
        verbose=True,
    )
    
    agent_chain.run("When's my friend Eric's surname?")
    

    which yields

    > Entering new AgentExecutor chain...
    I don't know Eric's surname, so I should ask a human for guidance.
    Action: Human
    Action Input: "What is Eric's surname?"
    
    What is Eric's surname?