2022-12-25 14:52:48 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "87455ddb",
"metadata": {},
"source": [
2023-04-03 21:13:48 +00:00
"# Multi-Input Tools\n",
2022-12-25 14:52:48 +00:00
"\n",
2023-05-03 16:31:02 +00:00
"This notebook shows how to use a tool that requires multiple inputs with an agent. The recommended way to do so is with the `StructuredTool` class.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "113c8805",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import os\n",
"os.environ[\"LANGCHAIN_TRACING\"] = \"true\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9c257017",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain import OpenAI\n",
"from langchain.agents import initialize_agent, AgentType\n",
2022-12-25 14:52:48 +00:00
"\n",
2023-05-03 16:31:02 +00:00
"llm = OpenAI(temperature=0)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "21623e8f",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain.tools import StructuredTool\n",
2022-12-25 14:52:48 +00:00
"\n",
2023-05-03 16:31:02 +00:00
"def multiplier(a: float, b: float) -> float:\n",
" \"\"\"Multiply the provided floats.\"\"\"\n",
" return a * b\n",
"\n",
"tool = StructuredTool.from_function(multiplier)"
2022-12-25 14:52:48 +00:00
]
},
{
"cell_type": "code",
2023-05-03 16:31:02 +00:00
"execution_count": 4,
"id": "ae7e8e07",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Structured tools are compatible with the STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION agent type. \n",
"agent_executor = initialize_agent([tool], llm, agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6cfa22d7",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m\n",
"Thought: I need to multiply 3 and 4\n",
"Action:\n",
"```\n",
"{\n",
" \"action\": \"multiplier\",\n",
" \"action_input\": {\"a\": 3, \"b\": 4}\n",
"}\n",
"```\n",
"\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3m12\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I know what to respond\n",
"Action:\n",
"```\n",
"{\n",
" \"action\": \"Final Answer\",\n",
" \"action_input\": \"3 times 4 is 12\"\n",
"}\n",
"```\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'3 times 4 is 12'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent_executor.run(\"What is 3 times 4\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "e643b307",
"metadata": {},
"source": [
"## Multi-Input Tools with a string format\n",
"\n",
"An alternative to the structured tool would be to use the regular `Tool` class and accept a single string. The tool would then have to handle the parsing logic to extract the relavent values from the text, which tightly couples the tool representation to the agent prompt. This is still useful if the underlying language model can't reliabl generate structured schema. \n",
"\n",
"Let's take the multiplication function as an example. In order to use this, we will tell the agent to generate the \"Action Input\" as a comma-separated list of length two. We will then write a thin wrapper that takes a string, splits it into two around a comma, and passes both parsed sides as integers to the multiplication function."
]
},
{
"cell_type": "code",
"execution_count": 6,
2022-12-25 14:52:48 +00:00
"id": "291149b6",
"metadata": {},
"outputs": [],
"source": [
"from langchain.llms import OpenAI\n",
2023-04-04 04:56:20 +00:00
"from langchain.agents import initialize_agent, Tool\n",
2023-04-04 14:21:50 +00:00
"from langchain.agents import AgentType"
2022-12-25 14:52:48 +00:00
]
},
{
"cell_type": "markdown",
"id": "71b6bead",
"metadata": {},
"source": [
"Here is the multiplication function, as well as a wrapper to parse a string as input."
]
},
{
"cell_type": "code",
2023-05-03 16:31:02 +00:00
"execution_count": 7,
2022-12-25 14:52:48 +00:00
"id": "f0b82020",
"metadata": {},
"outputs": [],
"source": [
"def multiplier(a, b):\n",
" return a * b\n",
"\n",
"def parsing_multiplier(string):\n",
" a, b = string.split(\",\")\n",
" return multiplier(int(a), int(b))"
]
},
{
"cell_type": "code",
2023-05-03 16:31:02 +00:00
"execution_count": 8,
2022-12-25 14:52:48 +00:00
"id": "6db1d43f",
"metadata": {},
"outputs": [],
"source": [
"llm = OpenAI(temperature=0)\n",
"tools = [\n",
" Tool(\n",
" name = \"Multiplier\",\n",
" func=parsing_multiplier,\n",
" description=\"useful for when you need to multiply two numbers together. The input to this tool should be a comma separated list of numbers of length two, representing the two numbers you want to multiply together. For example, `1,2` would be the input if you wanted to multiply 1 by 2.\"\n",
" )\n",
"]\n",
2023-04-04 04:56:20 +00:00
"mrkl = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
2022-12-25 14:52:48 +00:00
]
},
{
"cell_type": "code",
2023-05-03 16:31:02 +00:00
"execution_count": 9,
2022-12-25 14:52:48 +00:00
"id": "aa25d0ca",
"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 need to multiply two numbers\n",
"Action: Multiplier\n",
"Action Input: 3,4\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3m12\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
"Final Answer: 3 times 4 is 12\u001b[0m\n",
2023-02-18 21:40:43 +00:00
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
2022-12-25 14:52:48 +00:00
]
},
{
"data": {
"text/plain": [
"'3 times 4 is 12'"
]
},
2023-05-03 16:31:02 +00:00
"execution_count": 9,
2022-12-25 14:52:48 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mrkl.run(\"What is 3 times 4\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ea340c0",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
2023-02-18 21:40:43 +00:00
"display_name": "Python 3 (ipykernel)",
2022-12-25 14:52:48 +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-05-03 16:31:02 +00:00
"version": "3.11.2"
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-25 14:52:48 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}