Files
langchain/docs/modules/agents/tools/examples/openweathermap.ipynb
T
Samuli Rauatmaa 66828ad231 add the existing OpenWeatherMap tool to the public api (#4292)
[OpenWeatherMapAPIWrapper](https://github.com/hwchase17/langchain/blob/f70e18a5b3a5c3205dfefd3c1470d42cd789f797/docs/modules/agents/tools/examples/openweathermap.ipynb)
works wonderfully, but the _tool_ itself can't be used in master branch.

- added OpenWeatherMap **tool** to the public api, to be loadable with
`load_tools` by using "openweathermap-api" tool name (that name is used
in the existing
[docs](https://github.com/hwchase17/langchain/blob/aff33d52c52f5130677a3b7935329ec0048f5491/docs/modules/agents/tools/getting_started.md),
at the bottom of the page)
- updated OpenWeatherMap tool's **description** to make the input format
match what the API expects (e.g. `London,GB` instead of `'London,GB'`)
- added [ecosystem documentation page for
OpenWeatherMap](https://github.com/hwchase17/langchain/blob/f9c41594fe209ea7a9b9faf04187d3a186f09fe8/docs/ecosystem/openweathermap.md)
- added tool usage example to [OpenWeatherMap's
notebook](https://github.com/hwchase17/langchain/blob/f9c41594fe209ea7a9b9faf04187d3a186f09fe8/docs/modules/agents/tools/examples/openweathermap.ipynb)

Let me know if there's something I missed or something needs to be
updated! Or feel free to make edits yourself if that makes it easier for
you 🙂
2023-05-14 18:50:45 -07:00

4.6 KiB

OpenWeatherMap API

This notebook goes over how to use the OpenWeatherMap component to fetch weather information.

First, you need to sign up for an OpenWeatherMap API key:

  1. Go to OpenWeatherMap and sign up for an API key here
  2. pip install pyowm

Then we will need to set some environment variables:

  1. Save your API KEY into OPENWEATHERMAP_API_KEY env variable

Use the wrapper

In [9]:
from langchain.utilities import OpenWeatherMapAPIWrapper
import os

os.environ["OPENWEATHERMAP_API_KEY"] = ""

weather = OpenWeatherMapAPIWrapper()
In [10]:
weather_data = weather.run("London,GB")
print(weather_data)
In London,GB, the current weather is as follows:
Detailed status: broken clouds
Wind speed: 2.57 m/s, direction: 240°
Humidity: 55%
Temperature: 
  - Current: 20.12°C
  - High: 21.75°C
  - Low: 18.68°C
  - Feels like: 19.62°C
Rain: {}
Heat index: None
Cloud cover: 75%

Use the tool

In [11]:
from langchain.llms import OpenAI
from langchain.agents import load_tools, initialize_agent, AgentType
import os

os.environ["OPENAI_API_KEY"] = ""
os.environ["OPENWEATHERMAP_API_KEY"] = ""

llm = OpenAI(temperature=0)

tools = load_tools(["openweathermap-api"], llm)

agent_chain = initialize_agent(
    tools=tools,
    llm=llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
)
In [12]:
agent_chain.run("What's the weather like in London?")
Out [12]:

> Entering new AgentExecutor chain...
 I need to find out the current weather in London.
Action: OpenWeatherMap
Action Input: London,GB
Observation: In London,GB, the current weather is as follows:
Detailed status: broken clouds
Wind speed: 2.57 m/s, direction: 240°
Humidity: 56%
Temperature: 
  - Current: 20.11°C
  - High: 21.75°C
  - Low: 18.68°C
  - Feels like: 19.64°C
Rain: {}
Heat index: None
Cloud cover: 75%
Thought: I now know the current weather in London.
Final Answer: The current weather in London is broken clouds, with a wind speed of 2.57 m/s, direction 240°, humidity of 56%, temperature of 20.11°C, high of 21.75°C, low of 18.68°C, and a heat index of None.

> Finished chain.
'The current weather in London is broken clouds, with a wind speed of 2.57 m/s, direction 240°, humidity of 56%, temperature of 20.11°C, high of 21.75°C, low of 18.68°C, and a heat index of None.'