langchain/docs/extras/modules/agents/how_to/intermediate_steps.ipynb

218 lines
6.0 KiB
Plaintext
Raw Normal View History

2022-12-20 01:32:45 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "5436020b",
"metadata": {},
"source": [
"# Access intermediate steps\n",
2022-12-20 01:32:45 +00:00
"\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",
2023-04-04 14:21:50 +00:00
"from langchain.agents import AgentType\n",
2022-12-20 01:32:45 +00:00
"from langchain.llms import OpenAI"
]
},
{
"cell_type": "markdown",
"id": "1b440b8a",
"metadata": {},
"source": [
"Initialize the components needed for the agent."
]
},
{
"cell_type": "code",
"execution_count": 2,
2022-12-20 01:32:45 +00:00
"id": "36ed392e",
"metadata": {},
"outputs": [],
"source": [
"llm = OpenAI(temperature=0, model_name=\"text-davinci-002\")\n",
2022-12-20 01:32:45 +00:00
"tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm)"
]
},
{
"cell_type": "markdown",
"id": "1d329c3d",
"metadata": {},
"source": [
"Initialize the agent with `return_intermediate_steps=True`:"
2022-12-20 01:32:45 +00:00
]
},
{
"cell_type": "code",
"execution_count": 3,
2022-12-20 01:32:45 +00:00
"id": "6abf3b08",
"metadata": {},
"outputs": [],
"source": [
"agent = initialize_agent(\n",
" tools,\n",
" llm,\n",
" agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
" verbose=True,\n",
" return_intermediate_steps=True,\n",
")"
2022-12-20 01:32:45 +00:00
]
},
{
"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 who Leo DiCaprio is dating\n",
"Action: Search\n",
"Action Input: \"Leo DiCaprio girlfriend\"\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3mCamila Morrone\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I should look up how old Camila Morrone is\n",
2022-12-20 01:32:45 +00:00
"Action: Search\n",
"Action Input: \"Camila Morrone age\"\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3m25 years\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I should calculate what 25 years raised to the 0.43 power is\n",
2022-12-20 01:32:45 +00:00
"Action: Calculator\n",
"Action Input: 25^0.43\u001b[0m\n",
"Observation: \u001b[33;1m\u001b[1;3mAnswer: 3.991298452658078\n",
2022-12-20 01:32:45 +00:00
"\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
"Final Answer: Camila Morrone is Leo DiCaprio's girlfriend and she is 3.991298452658078 years old.\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
2022-12-20 01:32:45 +00:00
]
}
],
"source": [
"response = agent(\n",
" {\n",
" \"input\": \"Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?\"\n",
" }\n",
")"
2022-12-20 01:32:45 +00:00
]
},
{
"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='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')]\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",
" \"Leo DiCaprio girlfriend\",\n",
" \" I should look up who Leo DiCaprio is dating\\nAction: Search\\nAction Input: \\\"Leo DiCaprio girlfriend\\\"\"\n",
" ],\n",
" \"Camila Morrone\"\n",
" ],\n",
" [\n",
" [\n",
" \"Search\",\n",
" \"Camila Morrone age\",\n",
" \" I should look up how old Camila Morrone is\\nAction: Search\\nAction Input: \\\"Camila Morrone age\\\"\"\n",
2022-12-20 01:32:45 +00:00
" ],\n",
" \"25 years\"\n",
2022-12-20 01:32:45 +00:00
" ],\n",
" [\n",
" [\n",
" \"Calculator\",\n",
" \"25^0.43\",\n",
" \" I should calculate what 25 years raised to the 0.43 power is\\nAction: Calculator\\nAction Input: 25^0.43\"\n",
2022-12-20 01:32:45 +00:00
" ],\n",
" \"Answer: 3.991298452658078\\n\"\n",
2022-12-20 01:32:45 +00:00
" ]\n",
"]\n"
]
}
],
"source": [
"import json\n",
"\n",
2022-12-20 01:32:45 +00:00
"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 (ipykernel)",
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.11.3"
},
"vscode": {
"interpreter": {
"hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103"
}
2022-12-20 01:32:45 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}