Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update tool use case #20404

Merged
merged 5 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 56 additions & 31 deletions docs/docs/use_cases/tool_use/agents.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
"jp-MarkdownHeadingCollapsed": true
},
"source": [
"## Agents\n",
"## Repeated tool use with agents\n",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a nit but repeated tool use is only one element of this right? There is also the decision of what tool to use, or whether to use any tools at all. I don't have a great alternative title, maybe something like "Agents and model-driven tool use"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, but was thinking that's handled by the "Choosing between multiple tools" section, what makes agents diff from just a routing chain is it chooses how many steps to take

"\n",
"Chains are great when we know the specific sequence of tool usage needed for any user input. But for certain use cases, how many times we use tools depends on the input. In these cases, we want to let the model itself decide how many times to use tools and in what order. [Agents](/docs/modules/agents/) let us do just this.\n",
"\n",
"LangChain comes with a number of built-in agents that are optimized for different use cases. Read about all the [agent types here](/docs/modules/agents/agent_types/).\n",
"\n",
"As an example, let's try out the OpenAI tools agent, which makes use of the new OpenAI tool-calling API (this is only available in the latest OpenAI models, and differs from function-calling in that the model can return multiple function invocations at once).\n",
"We'll use the [tool calling agent](/docs/modules/agents/agent_types/tool_calling/), which is generally the most reliable kind and the recommended one for most use cases. \"Tool calling\" in this case refers to a specific type of model API that allows for explicitly passing tool definitions to models and getting explicit tool invocations out. For more on tool calling models see [this guide].(/docs/modules/model_io/chat/function_calling/)\n",
"\n",
"![agent](../../../static/img/tool_agent.svg)"
]
Expand All @@ -45,30 +45,28 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install --upgrade --quiet langchain langchain-openai"
"%pip install --upgrade --quiet langchain langchainhub"
]
},
{
"cell_type": "markdown",
"id": "a33915ce-00c5-4379-8a83-c0053e471cdb",
"metadata": {},
"source": [
"And set these environment variables:"
"If you'd like to use LangSmith, set the environment variables below:"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "54667a49-c226-486d-a887-33120c90cc91",
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = getpass.getpass()\n",
"\n",
"# If you'd like to use LangSmith, uncomment the below\n",
"# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
"# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass()"
]
Expand All @@ -85,7 +83,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"id": "1c44ba79-6ab2-4d55-8247-82fca4d9b70c",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -124,19 +122,18 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"id": "e27a4e1a-938b-4b60-8e32-25e4ee530274",
"metadata": {},
"outputs": [],
"source": [
"from langchain import hub\n",
"from langchain.agents import AgentExecutor, create_openai_tools_agent\n",
"from langchain_openai import ChatOpenAI"
"from langchain.agents import AgentExecutor, create_tool_calling_agent"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 4,
"id": "bcc9536e-0328-4e29-9d3d-133f3e63e589",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -173,27 +170,45 @@
"id": "85e9875a-d8d4-4712-b3f0-b513c684451b",
"metadata": {},
"source": [
"## Create agent"
"## Create agent\n",
"\n",
"We'll need to use a model with tool calling capabilities. You can see which models support tool calling [here](/docs/integrations/chat/).\n",
"\n",
"```{=mdx}\n",
"import ChatModelTabs from \"@theme/ChatModelTabs\";\n",
"\n",
"<ChatModelTabs customVarName=\"llm\"/>\n",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might hideCohere pending langchain-ai/langchain-cohere#15

"```"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a1c5319d-6609-449d-8dd0-127e9a600656",
"execution_count": 5,
"id": "9583aef3-a2cf-461e-8506-8a22f4c730b8",
"metadata": {},
"outputs": [],
"source": [
"# Choose the LLM that will drive the agent\n",
"# Only certain models support this\n",
"model = ChatOpenAI(model=\"gpt-3.5-turbo-1106\", temperature=0)\n",
"# | echo: false\n",
"# | output: false\n",
"from langchain_anthropic import ChatAnthropic\n",
"\n",
"# Construct the OpenAI Tools agent\n",
"agent = create_openai_tools_agent(model, tools, prompt)"
"llm = ChatAnthropic(model=\"claude-3-sonnet-20240229\", temperature=0)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"id": "a1c5319d-6609-449d-8dd0-127e9a600656",
"metadata": {},
"outputs": [],
"source": [
"# Construct the tool calling agent\n",
"agent = create_tool_calling_agent(llm, tools, prompt)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c86bfe50-c5b3-49ed-86c8-1fe8dcd0c83a",
"metadata": {},
"outputs": [],
Expand All @@ -212,7 +227,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"id": "c098f8df-fd7f-4c13-963a-8e34194d3f84",
"metadata": {},
"outputs": [
Expand All @@ -225,21 +240,23 @@
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m\n",
"Invoking: `exponentiate` with `{'base': 3, 'exponent': 5}`\n",
"\n",
"responded: [{'text': \"Okay, let's break this down step-by-step:\", 'type': 'text'}, {'id': 'toolu_01CjdiDhDmMtaT1F4R7hSV5D', 'input': {'base': 3, 'exponent': 5}, 'name': 'exponentiate', 'type': 'tool_use'}]\n",
"\n",
"\u001b[0m\u001b[38;5;200m\u001b[1;3m243\u001b[0m\u001b[32;1m\u001b[1;3m\n",
"Invoking: `add` with `{'first_int': 12, 'second_int': 3}`\n",
"\n",
"responded: [{'text': '3 to the 5th power is 243.', 'type': 'text'}, {'id': 'toolu_01EKqn4E5w3Zj7bQ8s8xmi4R', 'input': {'first_int': 12, 'second_int': 3}, 'name': 'add', 'type': 'tool_use'}]\n",
"\n",
"\u001b[0m\u001b[33;1m\u001b[1;3m15\u001b[0m\u001b[32;1m\u001b[1;3m\n",
"Invoking: `multiply` with `{'first_int': 243, 'second_int': 15}`\n",
"\n",
"responded: [{'text': '12 + 3 = 15', 'type': 'text'}, {'id': 'toolu_017VZJgZBYbwMo2KGD6o6hsQ', 'input': {'first_int': 243, 'second_int': 15}, 'name': 'multiply', 'type': 'tool_use'}]\n",
"\n",
"\u001b[0m\u001b[36;1m\u001b[1;3m3645\u001b[0m\u001b[32;1m\u001b[1;3m\n",
"Invoking: `exponentiate` with `{'base': 3645, 'exponent': 2}`\n",
"Invoking: `multiply` with `{'first_int': 3645, 'second_int': 3645}`\n",
"responded: [{'text': '243 * 15 = 3645', 'type': 'text'}, {'id': 'toolu_01RtFCcQgbVGya3NVDgTYKTa', 'input': {'first_int': 3645, 'second_int': 3645}, 'name': 'multiply', 'type': 'tool_use'}]\n",
"\n",
"\u001b[0m\u001b[36;1m\u001b[1;3m13286025\u001b[0m\u001b[32;1m\u001b[1;3mSo 3645 squared is 13,286,025.\n",
"\n",
"\u001b[0m\u001b[38;5;200m\u001b[1;3m13286025\u001b[0m\u001b[32;1m\u001b[1;3mThe result of raising 3 to the fifth power and multiplying that by the sum of twelve and three, then squaring the whole result is 13,286,025.\u001b[0m\n",
"Therefore, the final result of taking 3 to the 5th power (243), multiplying by 12 + 3 (15), and then squaring the whole result is 13,286,025.\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
Expand All @@ -248,10 +265,10 @@
"data": {
"text/plain": [
"{'input': 'Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result',\n",
" 'output': 'The result of raising 3 to the fifth power and multiplying that by the sum of twelve and three, then squaring the whole result is 13,286,025.'}"
" 'output': 'So 3645 squared is 13,286,025.\\n\\nTherefore, the final result of taking 3 to the 5th power (243), multiplying by 12 + 3 (15), and then squaring the whole result is 13,286,025.'}"
]
},
"execution_count": 6,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -263,13 +280,21 @@
" }\n",
")"
]
},
{
"cell_type": "markdown",
"id": "4ecc190c-c133-493e-bd3e-f73e9690bae1",
"metadata": {},
"source": [
"You can see the [LangSmith trace here](https://smith.langchain.com/public/92694ff3-71b7-44ed-bc45-04bdf04d4689/r)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "poetry-venv",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "poetry-venv"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand Down
Loading
Loading