langchain/docs/modules/agents/getting_started.ipynb

184 lines
5.4 KiB
Plaintext
Raw Normal View History

2022-11-22 14:16:26 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "5436020b",
"metadata": {},
"source": [
"# Getting Started\n",
2022-11-23 02:02:20 +00:00
"\n",
"Agents use an LLM to determine which actions to take and in what order.\n",
"An action can either be using a tool and observing its output, or returning to the user.\n",
2022-11-22 14:16:26 +00:00
"\n",
2022-12-20 01:32:45 +00:00
"When used correctly agents can be extremely powerful. The purpose of this notebook is to show you how to easily use agents through the simplest, highest level API."
2022-11-22 14:16:26 +00:00
]
},
{
"cell_type": "markdown",
"id": "3c6226b9",
"metadata": {},
"source": [
2022-11-23 02:02:20 +00:00
"In order to load agents, you should understand the following concepts:\n",
2022-11-22 14:16:26 +00:00
"\n",
"- Tool: A function that performs a specific duty. This can be things like: Google Search, Database lookup, Python REPL, other chains. The interface for a tool is currently a function that is expected to have a string as an input, with a string as an output.\n",
"- LLM: The language model powering the agent.\n",
"- Agent: The agent to use. This should be a string that references a support agent class. Because this notebook focuses on the simplest, highest level API, this only covers using the standard supported agents. If you want to implement a custom agent, see the documentation for custom agents (coming soon).\n",
2022-11-23 02:02:20 +00:00
"\n",
"**Agents**: For a list of supported agents and their specifications, see [here](agents.md).\n",
2022-12-19 02:51:23 +00:00
"\n",
"**Tools**: For a list of predefined tools and their specifications, see [here](tools.md)."
2022-11-22 14:16:26 +00:00
]
},
{
"cell_type": "code",
"execution_count": 1,
2022-12-20 01:32:45 +00:00
"id": "d01216c0",
2022-11-22 14:16:26 +00:00
"metadata": {},
"outputs": [],
"source": [
2022-12-20 01:32:45 +00:00
"from langchain.agents import load_tools\n",
"from langchain.agents import initialize_agent\n",
2022-11-22 14:16:26 +00:00
"from langchain.llms import OpenAI"
]
},
{
2022-12-20 01:32:45 +00:00
"cell_type": "markdown",
"id": "ef965094",
2022-11-22 14:16:26 +00:00
"metadata": {},
"source": [
2022-12-20 01:32:45 +00:00
"First, let's load the language model we're going to use to control the agent."
2022-11-22 14:16:26 +00:00
]
},
{
"cell_type": "code",
"execution_count": 2,
2022-12-20 01:32:45 +00:00
"id": "0728f0d9",
2022-11-22 14:16:26 +00:00
"metadata": {},
"outputs": [],
"source": [
2022-12-20 01:32:45 +00:00
"llm = OpenAI(temperature=0)"
2022-12-19 02:51:23 +00:00
]
},
{
"cell_type": "markdown",
2022-12-20 01:32:45 +00:00
"id": "fb29d592",
2022-12-19 02:51:23 +00:00
"metadata": {},
"source": [
2022-12-20 01:32:45 +00:00
"Next, let's load some tools to use. Note that the `llm-math` tool uses an LLM, so we need to pass that in."
2022-11-22 14:16:26 +00:00
]
},
{
"cell_type": "code",
"execution_count": 3,
2022-12-20 01:32:45 +00:00
"id": "ba4e7618",
2022-11-22 14:16:26 +00:00
"metadata": {},
"outputs": [],
2022-12-19 02:51:23 +00:00
"source": [
2022-12-20 01:32:45 +00:00
"tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm)"
2022-12-19 02:51:23 +00:00
]
},
{
"cell_type": "markdown",
2022-12-20 01:32:45 +00:00
"id": "0b50fc9b",
2022-12-19 02:51:23 +00:00
"metadata": {},
"source": [
2022-12-20 01:32:45 +00:00
"Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use."
2022-12-19 02:51:23 +00:00
]
},
{
"cell_type": "code",
"execution_count": 6,
2022-12-20 01:32:45 +00:00
"id": "03208e2b",
2022-12-19 02:51:23 +00:00
"metadata": {},
"outputs": [],
"source": [
2022-12-20 01:32:45 +00:00
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
2022-12-19 02:51:23 +00:00
]
},
{
2022-12-20 01:32:45 +00:00
"cell_type": "markdown",
"id": "373361d5",
2022-12-19 02:51:23 +00:00
"metadata": {},
"source": [
2022-12-20 01:32:45 +00:00
"Now let's test it out!"
2022-12-19 02:51:23 +00:00
]
},
{
"cell_type": "code",
"execution_count": 13,
2022-12-19 02:51:23 +00:00
"id": "244ee75c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
2022-12-20 01:32:45 +00:00
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m I need to find out who Leo DiCaprio's girlfriend is and then calculate her age raised to the 0.43 power.\n",
2022-12-19 02:51:23 +00:00
"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 need to find out Camila Morrone's age\n",
2022-12-19 02:51:23 +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 need to calculate 25 raised to the 0.43 power\n",
2022-12-19 02:51:23 +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-19 02:51:23 +00:00
"\u001b[0m\n",
2022-12-20 01:32:45 +00:00
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
"Final Answer: Camila Morrone is Leo DiCaprio's girlfriend and her current age raised to the 0.43 power is 3.991298452658078.\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
2022-12-19 02:51:23 +00:00
]
},
{
"data": {
"text/plain": [
"\"Camila Morrone is Leo DiCaprio's girlfriend and her current age raised to the 0.43 power is 3.991298452658078.\""
2022-12-19 02:51:23 +00:00
]
},
"execution_count": 13,
2022-12-19 02:51:23 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent.run(\"Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?\")"
2022-12-19 02:51:23 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5901695b",
"metadata": {},
"outputs": [],
"source": []
2022-11-22 14:16:26 +00:00
}
],
"metadata": {
"kernelspec": {
2023-01-23 07:37:01 +00:00
"display_name": "Python 3 (ipykernel)",
2022-11-22 14:16:26 +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.1"
2022-11-22 14:16:26 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}