Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
OurBuilding Ambient Agents with LangGraph course is now available on LangChain Academy!
Open In ColabOpen on GitHub

OpenWeatherMap

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

First, you need to sign up for anOpenWeatherMap API key:

  1. Go to OpenWeatherMap and sign up for an API keyhere
  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

%pip install--upgrade--quiet pyowm
Note: you may need to restart the kernel to use updated packages.
import os

from langchain_community.utilitiesimport OpenWeatherMapAPIWrapper

os.environ["OPENWEATHERMAP_API_KEY"]=""

weather= OpenWeatherMapAPIWrapper()
weather_data= weather.run("London,GB")
print(weather_data)
In London,GB, the current weather is as follows:
Detailed status: overcast clouds
Wind speed: 4.12 m/s, direction: 10°
Humidity: 51%
Temperature:
- Current: 12.82°C
- High: 13.98°C
- Low: 12.01°C
- Feels like: 11.49°C
Rain: {}
Heat index: None
Cloud cover: 100%

Use the tool

import os

from langgraph.prebuiltimport create_react_agent

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

tools=[weather.run]
agent= create_react_agent("openai:gpt-4.1-mini", tools)
API Reference:create_react_agent
input_message={
"role":"user",
"content":"What's the weather like in London?",
}

for stepin agent.stream(
{"messages":[input_message]},
stream_mode="values",
):
step["messages"][-1].pretty_print()
================================ Human Message =================================

What's the weather like in London?
================================== Ai Message ==================================
Tool Calls:
run (call_6vPq9neyy7oOnht29ExidE2g)
Call ID: call_6vPq9neyy7oOnht29ExidE2g
Args:
location: London
================================= Tool Message =================================
Name: run

In London, the current weather is as follows:
Detailed status: overcast clouds
Wind speed: 4.12 m/s, direction: 10°
Humidity: 51%
Temperature:
- Current: 12.82°C
- High: 13.98°C
- Low: 12.01°C
- Feels like: 11.49°C
Rain: {}
Heat index: None
Cloud cover: 100%
================================== Ai Message ==================================

The weather in London is currently overcast with 100% cloud cover. The temperature is around 12.82°C, feeling like 11.49°C. The wind is blowing at 4.12 m/s from the direction of 10°. Humidity is at 51%. The high for the day is 13.98°C, and the low is 12.01°C.

Related


[8]ページ先頭

©2009-2025 Movatter.jp