langchain: `initialize_agent` does not work with `return_intermediate_steps=True`
E.g. running
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
llm = OpenAI(temperature=0)
tools = load_tools(["llm-math"], llm=llm)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True, return_intermediate_steps=True)
agent.run("What is 2 raised to the 0.43 power?")
gives the error
203 """Run the chain as text in, text out or multiple variables, text out."""
204 if len(self.output_keys) != 1:
--> 205 raise ValueError(
206 f"`run` not supported when there is not exactly "
207 f"one output key. Got {self.output_keys}."
208 )
210 if args and not kwargs:
211 if len(args) != 1:
ValueError: `run` not supported when there is not exactly one output key. Got ['output', 'intermediate_steps'].
Is this supposed to be called differently or how else can the intermediate outputs (“Observations”) be retrieved?
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 6
- Comments: 18 (1 by maintainers)
Define the input and output keys in your memory when you initialize the agent like this:
Call the agent directly on the input like this:
agent("What is 2 raised to the 0.43 power?")
This will return a dict with keys “input”, “output”, and “intermediate_steps”.
Another evidence:
This raises the same error. The problem seems to be the memory.
I just tried running
agent("test")
,agent(input='test')
,agent(dict(input='test'))
and all of them raise errors.agent("test")
andagent(dict(input='test'))
raise:And
agent(input='test')
raises:I faced the same issue while using the
chat-conversational-react-description
agent. I tried overriding the ConversationBufferMemory as suggested in #3091Solution that worked for me: My Langchain execution utilizes the
ConversationBufferMemory
with memory key aschat-history
initialize_agent
returns anAgentExecutor
object so it’s important to have thereturn_intermediate_steps = True
for the executorinput_key
andoutput_key
helps to get a dictionary output with intermediate_steps. Also make sure thatreturn_messages=True
memory = ConversationBufferMemory(memory_key="chat_history", input_key="input", output_key="output",return_messages=True)
Hope this helps!
I’ve the same issue with create_pandas_dataframe_agent (that unfortunaltely cannot be solved with your call to initialize_agent @wct432
I tried by replacing the AgentExecutor.from_agent_and_tools in create_pandas_dataframe_agent with a initialize_agent, but I don’t know how to pass the prompt into initialize_agent
cc @hwchase17
Is there any way to make this work with
SQLDatabaseChain.from_llm()
? When settingreturn_intermediate_steps=True
in the chain, returns the errorERROR:root:'run' not supported when there is not exactly one output key. Got ['result', 'intermediate_steps']
. The suggested workarounds in the thread still don’t work 😦Hey @ogabrielluiz, you better change that API key 😉
I tested it too and it works. Thanks, @wct432!
Thanks @wct432 ! Is it in the docs? I could not find the section mentioning that approach