make tools editable (#445)

use dataclass instead of namedtuple, which makes it editable

add example in notebook
harrison/callback-updates
Harrison Chase 1 year ago committed by GitHub
parent 5cc6bf1a9c
commit 5208bb8c36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,7 +23,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"id": "1aaba18c",
"metadata": {},
"outputs": [],
@ -44,7 +44,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 2,
"id": "36ed392e",
"metadata": {},
"outputs": [],
@ -52,9 +52,18 @@
"llm = OpenAI(temperature=0)"
]
},
{
"cell_type": "markdown",
"id": "f8bc72c2",
"metadata": {},
"source": [
"## Completely New Tools\n",
"First, we show how to create completely new tools from scratch."
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"id": "56ff7670",
"metadata": {},
"outputs": [],
@ -78,7 +87,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 4,
"id": "5b93047d",
"metadata": {},
"outputs": [],
@ -91,7 +100,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 5,
"id": "6f96a891",
"metadata": {},
"outputs": [
@ -105,41 +114,133 @@
"\u001b[32;1m\u001b[1;3m I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.\n",
"Action: Search\n",
"Action Input: Olivia Wilde's boyfriend\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3mOlivia Wilde started dating Harry Styles after ending her years-long engagement to Jason Sudeikis — see their relationship timeline.\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I need to find out Harry Styles' age.\n",
"Action: Search\n",
"Action Input: Harry Styles age\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3m28 years\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I need to calculate 28 raised to the 0.23 power.\n",
"Observation: \u001b[36;1m\u001b[1;3mHarry Styles\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I need to calculate Harry Styles' age raised to the 0.23 power.\n",
"Action: Calculator\n",
"Action Input: 28^0.23\u001b[0m\n",
"Action Input: 23^0.23\u001b[0m\n",
"\n",
"\u001b[1m> Entering new LLMMathChain chain...\u001b[0m\n",
"28^0.23\u001b[32;1m\u001b[1;3m\n",
"23^0.23\u001b[32;1m\u001b[1;3m\n",
"\n",
"```python\n",
"import math\n",
"print(math.pow(28, 0.23))\n",
"print(math.pow(23, 0.23))\n",
"```\n",
"\u001b[0m\n",
"Answer: \u001b[33;1m\u001b[1;3m2.1520202182226886\n",
"Answer: \u001b[33;1m\u001b[1;3m2.0568252837687546\n",
"\u001b[0m\n",
"\u001b[1m> Finished LLMMathChain chain.\u001b[0m\n",
"\n",
"Observation: \u001b[33;1m\u001b[1;3mAnswer: 2.1520202182226886\n",
"Observation: \u001b[33;1m\u001b[1;3mAnswer: 2.0568252837687546\n",
"\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
"Final Answer: Harry Styles, Olivia Wilde's boyfriend, is 28 years old and his age raised to the 0.23 power is 2.1520202182226886.\u001b[0m\n",
"Final Answer: Harry Styles' age raised to the 0.23 power is 2.0568252837687546.\u001b[0m\n",
"\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"\"Harry Styles' age raised to the 0.23 power is 2.0568252837687546.\""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent.run(\"Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?\")"
]
},
{
"cell_type": "markdown",
"id": "1d0430d6",
"metadata": {},
"source": [
"## Modify existing tools\n",
"\n",
"Now, we show how to load existing tools and just modify them. In the example below, we do something really simple and change the Search tool to have the name `Google Search`."
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "79213f40",
"metadata": {},
"outputs": [],
"source": [
"from langchain.agents import load_tools"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "e1067dcb",
"metadata": {},
"outputs": [],
"source": [
"tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "6c66ffe8",
"metadata": {},
"outputs": [],
"source": [
"tools[0].name = \"Google Search\""
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "f45b5bc3",
"metadata": {},
"outputs": [],
"source": [
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "565e2b9b",
"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 find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.\n",
"Action: Google Search\n",
"Action Input: \"Olivia Wilde boyfriend\"\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3mHarry Styles\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I need to find out Harry Styles' age\n",
"Action: Google Search\n",
"Action Input: \"Harry Styles age\"\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3m28 years\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I need to calculate 28 raised to the 0.23 power\n",
"Action: Calculator\n",
"Action Input: 28^0.23\u001b[0m\n",
"Observation: \u001b[33;1m\u001b[1;3mAnswer: 2.1520202182226886\n",
"\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
"Final Answer: Harry Styles is Olivia Wilde's boyfriend and his current age raised to the 0.23 power is 2.1520202182226886.\u001b[0m\n",
"\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"\"Harry Styles, Olivia Wilde's boyfriend, is 28 years old and his age raised to the 0.23 power is 2.1520202182226886.\""
"\"Harry Styles is Olivia Wilde's boyfriend and his current age raised to the 0.23 power is 2.1520202182226886.\""
]
},
"execution_count": 7,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
@ -151,7 +252,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "e7776981",
"id": "3450512e",
"metadata": {},
"outputs": [],
"source": []

@ -1,8 +1,10 @@
"""Interface for tools."""
from typing import Callable, NamedTuple, Optional
from dataclasses import dataclass
from typing import Callable, Optional
class Tool(NamedTuple):
@dataclass
class Tool:
"""Interface for tools."""
name: str

Loading…
Cancel
Save