langchain/docs/modules/agents/examples/intermediate_steps.ipynb

193 lines
5.1 KiB
Plaintext
Raw Normal View History

2022-12-20 01:32:45 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "5436020b",
"metadata": {},
"source": [
"# Intermediate Steps\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 1,
2022-12-20 01:32:45 +00:00
"id": "b2b0d119",
"metadata": {},
"outputs": [],
"source": [
"from langchain.agents import load_tools\n",
"from langchain.agents import initialize_agent\n",
"from langchain.llms import OpenAI"
]
},
{
"cell_type": "markdown",
"id": "1b440b8a",
"metadata": {},
"source": [
"Initialize the components needed for the agent."
]
},
{
"cell_type": "code",
"execution_count": 3,
2022-12-20 01:32:45 +00:00
"id": "36ed392e",
"metadata": {},
"outputs": [],
"source": [
"llm = OpenAI(temperature=0, model_name='text-davinci-002')\n",
"tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm)"
]
},
{
"cell_type": "markdown",
"id": "1d329c3d",
"metadata": {},
"source": [
"Initialize the agent with `return_intermediate_steps=True`"
]
},
{
"cell_type": "code",
"execution_count": 4,
2022-12-20 01:32:45 +00:00
"id": "6abf3b08",
"metadata": {},
"outputs": [],
"source": [
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True, return_intermediate_steps=True)"
]
},
{
"cell_type": "code",
"execution_count": 5,
2022-12-20 01:32:45 +00:00
"id": "837211e8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m I should look up Olivia Wilde's boyfriend's age\n",
"Action: Search\n",
"Action Input: \"Olivia Wilde's boyfriend's age\"\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3m28 years\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I should use the calculator to raise that number to the 0.23 power\n",
2022-12-20 01:32:45 +00:00
"Action: Calculator\n",
"Action Input: 28^0.23\u001b[0m\n",
2022-12-20 01:32:45 +00:00
"Observation: \u001b[33;1m\u001b[1;3mAnswer: 2.1520202182226886\n",
"\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
"Final Answer: 2.1520202182226886\u001b[0m\n",
2022-12-20 01:32:45 +00:00
"\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n"
]
}
],
"source": [
"response = agent({\"input\":\"How old is Olivia Wilde's boyfriend? What is that number raised to the 0.23 power?\"})"
]
},
{
"cell_type": "code",
"execution_count": 6,
2022-12-20 01:32:45 +00:00
"id": "e1a39a23",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(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')]\n"
2022-12-20 01:32:45 +00:00
]
}
],
"source": [
"# The actual return type is a NamedTuple for the agent action, and then an observation\n",
"print(response[\"intermediate_steps\"])"
]
},
{
"cell_type": "code",
"execution_count": 7,
2022-12-20 01:32:45 +00:00
"id": "6365bb69",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[\n",
" [\n",
" [\n",
" \"Search\",\n",
" \"Olivia Wilde's boyfriend's age\",\n",
" \" I should look up Olivia Wilde's boyfriend's age\\nAction: Search\\nAction Input: \\\"Olivia Wilde's boyfriend's age\\\"\"\n",
" ],\n",
" \"28 years\"\n",
2022-12-20 01:32:45 +00:00
" ],\n",
" [\n",
" [\n",
" \"Calculator\",\n",
" \"28^0.23\",\n",
" \" I should use the calculator to raise that number to the 0.23 power\\nAction: Calculator\\nAction Input: 28^0.23\"\n",
2022-12-20 01:32:45 +00:00
" ],\n",
" \"Answer: 2.1520202182226886\\n\"\n",
" ]\n",
"]\n"
]
}
],
"source": [
"import json\n",
"print(json.dumps(response[\"intermediate_steps\"], indent=2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e7776981",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "8dc69fc3",
"metadata": {},
"outputs": [],
"source": []
2022-12-20 01:32:45 +00:00
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.0 64-bit ('llm-env')",
2022-12-20 01:32:45 +00:00
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.0"
},
"vscode": {
"interpreter": {
"hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103"
}
2022-12-20 01:32:45 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}