You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
langchain/docs/examples/agents/search_tools.ipynb

200 lines
6.5 KiB
Plaintext

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

{
"cells": [
{
"cell_type": "markdown",
"id": "6510f51c",
"metadata": {},
"source": [
"# Search Tools\n",
"\n",
"This notebook shows off usage of various search tools."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "e6860c2d",
"metadata": {},
"outputs": [],
"source": [
"from langchain.agents import load_tools\n",
"from langchain.agents import initialize_agent\n",
"from langchain.llms import OpenAI"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "dadbcfcd",
"metadata": {},
"outputs": [],
"source": [
"llm = OpenAI(temperature=0)"
]
},
{
"cell_type": "markdown",
"id": "a09ca013",
"metadata": {},
"source": [
"## SerpAPI\n",
"\n",
"First, let's use the SerpAPI tool."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "dd4ce6d9",
"metadata": {},
"outputs": [],
"source": [
"tools = load_tools([\"serpapi\"], llm=llm)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ef63bb84",
"metadata": {},
"outputs": [],
"source": [
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "53e24f5d",
"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 what the current weather is in Pomfret.\n",
"Action: Search\n",
"Action Input: \"weather in Pomfret\"\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3m10 Day Weather-Pomfret, CT ; Thu 29. 48° · 32°. 6% · SW 9 mph ; Fri 30. 53° · 39°. 8% · SW 8 mph ; Sat 31. 51° · 47°. 33% · SW 8 mph.\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know the current weather in Pomfret.\n",
"Final Answer: The current weather in Pomfret is 10 Day Weather-Pomfret, CT ; Thu 29. 48° · 32°. 6% · SW 9 mph ; Fri 30. 53° · 39°. 8% · SW 8 mph ; Sat 31. 51° · 47°. 33% · SW 8 mph.\u001b[0m\n",
"\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'The current weather in Pomfret is 10 Day Weather-Pomfret, CT ; Thu 29. 48° · 32°. 6% · SW 9 mph ; Fri 30. 53° · 39°. 8% · SW 8 mph ; Sat 31. 51° · 47°. 33% · SW 8 mph.'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent.run(\"What is the weather in Pomfret?\")"
]
},
{
"cell_type": "markdown",
"id": "8ef49137",
"metadata": {},
"source": [
"## GoogleSearchAPIWrapper\n",
"\n",
"Now, let's use the official Google Search API Wrapper."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "3e9c7c20",
"metadata": {},
"outputs": [],
"source": [
"tools = load_tools([\"google-search\"], llm=llm)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "b83624dc",
"metadata": {},
"outputs": [],
"source": [
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "9d5835e2",
"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 should look up the current weather conditions.\n",
"Action: Google Search\n",
"Action Input: \"weather in Pomfret\"\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3mBe prepared with the most accurate 10-day forecast for Pomfret, CT with highs, lows, chance of precipitation from The Weather Channel and Weather.com. Pomfret, CT Weather Forecast, with current conditions, wind, air quality, and what to expect for the next 3 days. Hourly Weather-Pomfret, CT. As of 6:05 am EST. Thursday, December 29. 6:00 am. Pomfret CT. Today. Today: Partly sunny, with a high near 40. ... National Digital Forecast Database Maximum Temperature Forecast. High Temperature. Hourly Weather-Pomfret, CT. As of 4:54 pm EST. Tuesday, December 27. 5:00 pm. Pomfret Center Weather Forecasts. Weather Underground provides local & long-range weather forecasts, weatherreports, maps & tropical weather conditions for ... South Pomfret VT ... Hazardous Weather Outlook · Regional Weather Conditions ... National Digital Forecast Database Maximum Temperature Forecast. North Pomfret Weather Forecasts. Weather Underground provides local & long-range weather forecasts, weatherreports, maps & tropical weather conditions for ... Pomfret, CT 12 hour by hour weather forecast includes precipitation, temperatures, sky conditions, rain chance, dew-point, relative humidity, wind direction ... Hazardous Weather Conditions ... Pomfret Center CT ... Temperature rising to near 53 by 8am, then falling to around 38 during the remainder of the day.\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know the current weather conditions in Pomfret.\n",
"Final Answer: The current weather in Pomfret is partly sunny with a high near 40. There is a chance of precipitation and temperatures will rise to near 53 by 8am, then fall to around 38 during the remainder of the day.\u001b[0m\n",
"\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'The current weather in Pomfret is partly sunny with a high near 40. There is a chance of precipitation and temperatures will rise to near 53 by 8am, then fall to around 38 during the remainder of the day.'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent.run(\"What is the weather in Pomfret?\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "68a05cfd",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}