langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
91.98k stars 14.63k forks source link

Can't acces return_intermediate_steps of a tool when dealing with an agent #17182

Closed miquelmongeAZ closed 3 months ago

miquelmongeAZ commented 7 months ago

Checked other resources

Example Code

cypher_prompt = PromptTemplate.from_template(CYPHER_GENERATION_TEMPLATE)
cypher_qa = GraphCypherQAChain.from_llm(
    llm,
    graph=graph,
    cypher_prompt=cypher_prompt,
    verbose=True,
    return_intermediate_steps=True,
    return_direct=False
)

------ other part of the code ----

agent_prompt = hub.pull("hwchase17/react-chat")
agent = create_react_agent(llm, tools, agent_prompt)
agent_executor = AgentExecutor(
    agent=agent,
    tools=tools,
    verbose=True,
    memory=memory,
    return_intermediate_steps=True
)

Error Message and Stack Trace (if applicable)

No response

Description

GraphCypherQAChain tool is accesible for the react-chat agent an generate good cypher queries, but it do not return the cypher queries to the agent. What I expect is that with return_intermediate_steps on both the tool and the agent I can have both the cypher queries and the agent steps. Right now I only see the agents return_intermediate_steps, but there is any cypher query from the tool.

System Info

langchain==0.1.5 langchain-community==0.0.18 langchain-core==0.1.19 langchain-openai==0.0.5 langchainhub==0.1.14

mac

Python 3.9.6

dosubot[bot] commented 7 months ago

🤖

Hey @miquelmongeAZ! Great to see you diving into the intricacies of LangChain again. Let's crack this one together! 🛠️

Based on the code you've provided and the context, it seems like you've correctly set return_intermediate_steps=True for both the GraphCypherQAChain tool and the AgentExecutor. However, you're not seeing the intermediate steps from the GraphCypherQAChain tool in the agent's output.

This could be due to how the AgentExecutor handles the output of the tools it executes. If the AgentExecutor doesn't specifically handle the intermediate steps from the tools, it might not include them in its own output, even if return_intermediate_steps=True.

Here's a simplified example of how this might work:

class AgentExecutor:
    def __init__(self, agent, tools, return_intermediate_steps):
        self.agent = agent
        self.tools = tools
        self.return_intermediate_steps = return_intermediate_steps

    def execute(self):
        results = []
        for tool in self.tools:
            result = tool.execute()
            if self.return_intermediate_steps:
                results.append(result)
        return results

In this example, the AgentExecutor executes each tool and appends the result to a list if return_intermediate_steps=True. However, it doesn't specifically handle the intermediate steps from the tools, so they're not included in the final output.

To fix this, you might need to modify the AgentExecutor to handle the intermediate steps from the tools. However, without more information about how the AgentExecutor and GraphCypherQAChain are implemented, it's hard to provide a more specific solution.

If you could provide more details about these classes, especially how they handle the return_intermediate_steps parameter and the output of the tools, it would be easier to diagnose the issue. For example, the relevant parts of the AgentExecutor.execute() method and the GraphCypherQAChain.execute() method would be very helpful.

You can refer to the source code of the GraphCypherQAChain class in the LangChain repository here and the AgentExecutor class here for more details.

I hope this helps! If you have any more questions, 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.