2022-12-20 01:32:45 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "5436020b",
"metadata": {},
"source": [
2023-06-16 18:52:56 +00:00
"# 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",
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 16:24:09 +00:00
"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",
2023-02-18 21:40:43 +00:00
"execution_count": 2,
2022-12-20 01:32:45 +00:00
"id": "36ed392e",
"metadata": {},
"outputs": [],
"source": [
2023-06-16 18:52:56 +00:00
"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`"
]
},
{
"cell_type": "code",
2023-02-18 21:40:43 +00:00
"execution_count": 3,
2022-12-20 01:32:45 +00:00
"id": "6abf3b08",
"metadata": {},
"outputs": [],
"source": [
2023-06-16 18:52:56 +00:00
"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",
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 16:24:09 +00:00
"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",
2023-02-18 21:40:43 +00:00
"\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",
2023-02-18 21:40:43 +00:00
"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",
2023-02-18 21:40:43 +00:00
"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",
2023-02-18 21:40:43 +00:00
"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": [
2023-06-16 18:52:56 +00:00
"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",
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 16:24:09 +00:00
"execution_count": 6,
2022-12-20 01:32:45 +00:00
"id": "e1a39a23",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2023-02-18 21:40:43 +00:00
"[(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",
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 16:24:09 +00:00
"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",
2023-02-18 21:40:43 +00:00
" \"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",
2023-02-18 21:40:43 +00:00
" \"25 years\"\n",
2022-12-20 01:32:45 +00:00
" ],\n",
" [\n",
" [\n",
" \"Calculator\",\n",
2023-02-18 21:40:43 +00:00
" \"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",
2023-02-18 21:40:43 +00:00
" \"Answer: 3.991298452658078\\n\"\n",
2022-12-20 01:32:45 +00:00
" ]\n",
"]\n"
]
}
],
"source": [
"import json\n",
2023-06-16 18:52:56 +00:00
"\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": []
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 16:24:09 +00:00
},
{
"cell_type": "code",
"execution_count": null,
"id": "8dc69fc3",
"metadata": {},
"outputs": [],
"source": []
2022-12-20 01:32:45 +00:00
}
],
"metadata": {
"kernelspec": {
2023-02-18 21:40:43 +00:00
"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",
2023-06-16 18:52:56 +00:00
"version": "3.11.3"
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 16:24:09 +00:00
},
"vscode": {
"interpreter": {
"hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103"
}
2022-12-20 01:32:45 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}