Files
langchain/docs/modules/agents/examples/intermediate_steps.ipynb
Harrison ChaseandShreya Rajpal 985496f4be Docs refactor (#480)
Big docs refactor! Motivation is to make it easier for people to find
resources they are looking for. To accomplish this, there are now three
main sections:

- Getting Started: steps for getting started, walking through most core
functionality
- Modules: these are different modules of functionality that langchain
provides. Each part here has a "getting started", "how to", "key
concepts" and "reference" section (except in a few select cases where it
didnt easily fit).
- Use Cases: this is to separate use cases (like summarization, question
answering, evaluation, etc) from the modules, and provide a different
entry point to the code base.

There is also a full reference section, as well as extra resources
(glossary, gallery, etc)

Co-authored-by: Shreya Rajpal <ShreyaR@users.noreply.github.com>
2023-01-02 08:24:09 -08:00

5.1 KiB

Intermediate Steps

In order to get more visibility into what an agent is doing, we can also return intermediate steps. This comes in the form of an extra key in the return value, which is a list of (action, observation) tuples.

In [1]:
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI

Initialize the components needed for the agent.

In [3]:
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

In [4]:
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True, return_intermediate_steps=True)
In [5]:
response = agent({"input":"How old is Olivia Wilde's boyfriend? What is that number raised to the 0.23 power?"})

> Entering new AgentExecutor chain...
 I should look up Olivia Wilde's boyfriend's age
Action: Search
Action Input: "Olivia Wilde's boyfriend's age"
Observation: 28 years
Thought: I should use the calculator to raise that number to the 0.23 power
Action: Calculator
Action Input: 28^0.23
Observation: Answer: 2.1520202182226886

Thought: I now know the final answer
Final Answer: 2.1520202182226886
> Finished AgentExecutor chain.
In [6]:
# The actual return type is a NamedTuple for the agent action, and then an observation
print(response["intermediate_steps"])
[(AgentAction(tool='Search', tool_input="Olivia Wilde's boyfriend's age", log=' I should look up Olivia Wilde\'s boyfriend\'s age\nAction: Search\nAction Input: "Olivia Wilde\'s boyfriend\'s age"'), '28 years'), (AgentAction(tool='Calculator', tool_input='28^0.23', log=' I should use the calculator to raise that number to the 0.23 power\nAction: Calculator\nAction Input: 28^0.23'), 'Answer: 2.1520202182226886\n')]
In [7]:
import json
print(json.dumps(response["intermediate_steps"], indent=2))
[
  [
    [
      "Search",
      "Olivia Wilde's boyfriend's age",
      " I should look up Olivia Wilde's boyfriend's age\nAction: Search\nAction Input: \"Olivia Wilde's boyfriend's age\""
    ],
    "28 years"
  ],
  [
    [
      "Calculator",
      "28^0.23",
      " I should use the calculator to raise that number to the 0.23 power\nAction: Calculator\nAction Input: 28^0.23"
    ],
    "Answer: 2.1520202182226886\n"
  ]
]
In [ ]:
In [ ]: