Skip to content

Commit 293696e

Browse files
author
SRM
committed
Move the agent & tool to their respective dirs
1 parent 9316442 commit 293696e

File tree

3 files changed

+83
-57
lines changed

3 files changed

+83
-57
lines changed

agents/l4m_agent.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from langchain.agents import initialize_agent
2+
3+
4+
def base_agent(llm, tools):
5+
"""Base agent to perform xyz slippy map tiles operations.
6+
7+
llm: LLM object
8+
tools: List of tools to use by the agent
9+
"""
10+
agent = initialize_agent(
11+
llm=llm,
12+
tools=tools,
13+
agent="zero-shot-react-description",
14+
max_iterations=5,
15+
early_stopping_method="generate",
16+
verbose=True,
17+
)
18+
19+
return agent

nbs/23-05-18_mercantile-tool.ipynb

Lines changed: 41 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,35 @@
33
{
44
"cell_type": "code",
55
"execution_count": 1,
6-
"id": "f7075b83-b18e-402b-9754-c5059473cc72",
6+
"id": "699f101a-66bb-4ae7-9ed6-01a27d028266",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import sys\n",
11+
"sys.path.append(\"..\")"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 2,
17+
"id": "58134048-4a6e-4ab9-87ee-04947085c30e",
718
"metadata": {},
819
"outputs": [],
920
"source": [
1021
"import os\n",
1122
"\n",
1223
"from langchain.chat_models import ChatOpenAI\n",
1324
"from langchain.tools import BaseTool, DuckDuckGoSearchRun\n",
14-
"from langchain.agents import Tool, load_agent\n",
15-
"import mercantile"
25+
"from langchain.agents import Tool\n",
26+
"\n",
27+
"from tools.mercantile_tool import MercantileTool\n",
28+
"from agents.l4m_agent import base_agent"
1629
]
1730
},
1831
{
1932
"cell_type": "code",
20-
"execution_count": 2,
21-
"id": "65a565e1-62c1-4344-a825-a688ff672cb7",
33+
"execution_count": 3,
34+
"id": "3909647e-af00-4489-b073-4b792e250af2",
2235
"metadata": {},
2336
"outputs": [],
2437
"source": [
@@ -27,11 +40,12 @@
2740
},
2841
{
2942
"cell_type": "code",
30-
"execution_count": 3,
31-
"id": "3e0e769b-6283-43e4-a273-cff680a31ae5",
43+
"execution_count": 4,
44+
"id": "3c032737-e0e4-4b7a-9f6a-3b722fc0e032",
3245
"metadata": {},
3346
"outputs": [],
3447
"source": [
48+
"# pick a LLM\n",
3549
"llm = ChatOpenAI(\n",
3650
" temperature=0,\n",
3751
" openai_api_key=OPENAI_API_KEY,\n",
@@ -41,63 +55,38 @@
4155
},
4256
{
4357
"cell_type": "code",
44-
"execution_count": 59,
45-
"id": "deee7784-30c0-4411-b041-319c916ddf78",
46-
"metadata": {},
47-
"outputs": [],
48-
"source": [
49-
"class MercantileTool(BaseTool):\n",
50-
" name=\"mercantile\"\n",
51-
" description=\"Use this tool to get the xyz tiles given a lng,lat coordinate. \\\n",
52-
" To use this tool you need to provide lng,lat,zoom level separated by comma. \\\n",
53-
" Eg: `-105.24, 22.50, 5` is the input to get a tile for this (lng=-105.24, lat=22.50) at zoom level 5\"\n",
54-
"\n",
55-
" def _run(self, query):\n",
56-
" lng,lat,zoom = map(float, query.split(\",\"))\n",
57-
" return mercantile.tile(lng, lat, zoom)\n",
58-
"\n",
59-
" def _arun(self, query):\n",
60-
" raise NotImplementedError(\"Mercantile tool doesn't have an async implementation.\")"
61-
]
62-
},
63-
{
64-
"cell_type": "code",
65-
"execution_count": 73,
66-
"id": "d7a64005-19ec-4833-ae8b-d8b883b37864",
58+
"execution_count": 11,
59+
"id": "660da8b8-f422-4f31-a3aa-15296709033f",
6760
"metadata": {},
6861
"outputs": [],
6962
"source": [
63+
"# define a set of tools the agent has access to for queries\n",
7064
"duckduckgo_tool = Tool(\n",
7165
" name=\"DuckDuckGo\",\n",
72-
" description=\"Use this tool to answer questions about current events. \\\n",
66+
" description=\"Use this tool to answer questions about current events and places. \\\n",
7367
" Please ask targeted questions.\",\n",
7468
" func=DuckDuckGoSearchRun().run\n",
7569
")\n",
7670
"\n",
77-
"mercantile_tool = MercantileTool()"
71+
"mercantile_tool = MercantileTool()\n",
72+
"\n",
73+
"tools = [duckduckgo_tool, mercantile_tool]"
7874
]
7975
},
8076
{
8177
"cell_type": "code",
82-
"execution_count": 77,
83-
"id": "0eb851bb-f1c0-4a9b-badc-64b75e09b5c5",
78+
"execution_count": 12,
79+
"id": "b4c4291d-35c9-48b7-8b45-7da5ff155db7",
8480
"metadata": {},
8581
"outputs": [],
8682
"source": [
87-
"myagent = initialize_agent(\n",
88-
" agent=\"zero-shot-react-description\",\n",
89-
" tools=[duckduckgo_tool, mercantile_tool],\n",
90-
" llm=llm,\n",
91-
" verbose=True,\n",
92-
" max_iterations=5,\n",
93-
" early_stopping_method=\"generate\"\n",
94-
")"
83+
"agent = base_agent(llm, tools) "
9584
]
9685
},
9786
{
9887
"cell_type": "code",
99-
"execution_count": 78,
100-
"id": "0b63d42b-ecdf-486b-a34b-08d60c68812d",
88+
"execution_count": 13,
89+
"id": "6baab55c-892f-4938-8f18-c2b8c8712cf2",
10190
"metadata": {},
10291
"outputs": [
10392
{
@@ -107,17 +96,12 @@
10796
"\n",
10897
"\n",
10998
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
110-
"\u001b[32;1m\u001b[1;3mI need to use mercantile to get the xyz tile for London, but first I need to find the longitude and latitude coordinates for London.\n",
111-
"Action: DuckDuckGo\n",
112-
"Action Input: \"longitude and latitude coordinates for London\"\u001b[0m\n",
113-
"Observation: \u001b[36;1m\u001b[1;3mlatitude and longitude, coordinate system by means of which the position or location of any place on Earth's surface can be determined and described. Latitude is a measurement on a globe or map of location north or south of the Equator. ... London, England, the specially designated imaginary north-south line that passes through both ... The following table lists the latitude and longitude coordinates of major world cities in alphabetical order by city name. The time corresponding to 12:00 noon, Eastern Standard Time, is also included for each city. ... Latitude Longitude ... London, England: 51: 32 N: 0: 5 W: 5:00 p.m. Lyons, France: 45: 45 N: 4: 50 E: 6:00 p.m. Madrid, Spain ... Decimal degrees = Degrees + Minutes/60 + Seconds/3600. Let's convert the latitude and longitude coordinates of the Empire State Building. The coordinates are 40° 44′ 54.3″ N, 73° 59′ 9″ W. So its latitude in decimal degrees is: DD = 40 + 44/60 + 54.3/3600 = 40.748417. And longitude: To calculate the distance between two points given longitude and latitude coordinates: Write down each point's coordinates in degrees-only format. We'll call θ and φ to their respective latitude and longitude components. Input them in the haversine distance formula: d = 2R × sin⁻¹ (√ [sin² ( (θ₂ - θ₁)/2) + cosθ₁ × cosθ₂ × ... The Geocoding API is a service that accepts a list of places as addresses, latitude/longitude coordinates, or Place IDs. It converts each address into latitude/longitude coordinates or a Place ID, or converts each set of latitude/longitude coordinates or a Place ID into an address. Note: If you only want plus codes and do not want to use an API ...\u001b[0m\n",
114-
"Thought:\u001b[32;1m\u001b[1;3mNow that I have the longitude and latitude coordinates for London, I can use mercantile to get the xyz tile.\n",
99+
"\u001b[32;1m\u001b[1;3mI need to use the mercantile tool to get the xyz tile for London.\n",
115100
"Action: mercantile\n",
116-
"Action Input: \"0.1278,51.5074,10\"\u001b[0m0.1278,51.5074,10\n",
117-
"\n",
118-
"Observation: \u001b[33;1m\u001b[1;3mTile(x=512, y=340, z=10.0)\u001b[0m\n",
119-
"Thought:\u001b[32;1m\u001b[1;3mThe xyz tile for London is Tile(x=512, y=340, z=10.0)\n",
120-
"Final Answer: Tile(x=512, y=340, z=10.0)\u001b[0m\n",
101+
"Action Input: -0.1278, 51.5074, 10\u001b[0m\n",
102+
"Observation: \u001b[33;1m\u001b[1;3mTile(x=511, y=340, z=10.0)\u001b[0m\n",
103+
"Thought:\u001b[32;1m\u001b[1;3mI now know the xyz tile for London is Tile(x=511, y=340, z=10.0).\n",
104+
"Final Answer: Tile(x=511, y=340, z=10.0)\u001b[0m\n",
121105
"\n",
122106
"\u001b[1m> Finished chain.\u001b[0m\n"
123107
]
@@ -126,16 +110,16 @@
126110
"data": {
127111
"text/plain": [
128112
"{'input': 'What is the xyz tile for London?',\n",
129-
" 'output': 'Tile(x=512, y=340, z=10.0)'}"
113+
" 'output': 'Tile(x=511, y=340, z=10.0)'}"
130114
]
131115
},
132-
"execution_count": 78,
116+
"execution_count": 13,
133117
"metadata": {},
134118
"output_type": "execute_result"
135119
}
136120
],
137121
"source": [
138-
"myagent(\"What is the xyz tile for London?\")"
122+
"agent(\"What is the xyz tile for London?\")"
139123
]
140124
},
141125
{

tools/mercantile_tool.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import mercantile
2+
from langchain.tools import BaseTool
3+
4+
5+
class MercantileTool(BaseTool):
6+
"""Custom tool to perform mercantile operations.
7+
8+
This tool is used to get the xyz tiles given a lng,lat coordinate.
9+
"""
10+
11+
name = "mercantile"
12+
description = "Use this tool to get the xyz tiles given a lng,lat coordinate. \
13+
To use this tool you need to provide lng,lat,zoom level separated by comma. \
14+
Eg: `-105.24, 22.50, 5` is the input to get a tile for this (lng=-105.24, lat=22.50) at zoom level 5"
15+
16+
def _run(self, query):
17+
lng, lat, zoom = map(float, query.split(","))
18+
return mercantile.tile(lng, lat, zoom)
19+
20+
def _arun(self, query):
21+
raise NotImplementedError(
22+
"Mercantile tool doesn't have an async implementation."
23+
)

0 commit comments

Comments
 (0)