如何访问中间步骤

为了更好地了解代理正在执行的操作,我们还可以返回中间步骤。这以额外的键值对形式呈现在返回值中,其中包含了一个由 (action, observation) 元组组成的列表。

from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI

Initialize the components needed for the agent.

llm = OpenAI(temperature=0, model_name='text-davinci-002')
tools = load_tools(["serpapi", "llm-math"], llm=llm)

Initialize the agent with return_intermediate_steps=True

agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, return_intermediate_steps=True)
response = agent({"input":"Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?"})
Entering new AgentExecutor chain...
I should look up who Leo DiCaprio is dating
Action: Search
Action Input: "Leo DiCaprio girlfriend"
Observation: Camila Morrone
Thought:I should look up how old Camila Morrone is
Action: Search
Action Input: "Camila Morrone age"
Observation: 25 years
Thought:I should calculate what 25 years raised to the 0.43 power is
Action: Calculator
Action Input: 25^0.43
Observation: Answer: 3.991298452658078

Thought:I now know the final answer
Final Answer: Camila Morrone is Leo DiCaprio's girlfriend and she is 3.991298452658078 years old.

Finished chain.
# The actual return type is a NamedTuple for the agent action, and then an observation
print(response["intermediate_steps"])
[(AgentAction(tool='Search', tool_input='Leo DiCaprio girlfriend', log=' I should look up who Leo DiCaprio is dating\nAction: Search\nAction Input: "Leo DiCaprio girlfriend"'), 'Camila Morrone'), (AgentAction(tool='Search', tool_input='Camila Morrone age', log=' I should look up how old Camila Morrone is\nAction: Search\nAction Input: "Camila Morrone age"'), '25 years'), (AgentAction(tool='Calculator', tool_input='25^0.43', log=' I should calculate what 25 years raised to the 0.43 power is\nAction: Calculator\nAction Input: 25^0.43'), 'Answer: 3.991298452658078\n')]
import json
print(json.dumps(response["intermediate_steps"], indent=2))
[
  [
    [
      "Search",
      "Leo DiCaprio girlfriend",
      " I should look up who Leo DiCaprio is dating\nAction: Search\nAction Input: \"Leo DiCaprio girlfriend\""
    ],
    "Camila Morrone"
  ],
  [
    [
      "Search",
      "Camila Morrone age",
      " I should look up how old Camila Morrone is\nAction: Search\nAction Input: \"Camila Morrone age\""
    ],
    "25 years"
  ],
  [
    [
      "Calculator",
      "25^0.43",
      " I should calculate what 25 years raised to the 0.43 power is\nAction: Calculator\nAction Input: 25^0.43"
    ],
    "Answer: 3.991298452658078\n"
  ]
]
Last Updated:
Contributors: 刘强