forked from Archives/langchain
parent
f16c1fb6df
commit
aff33d52c5
@ -0,0 +1,128 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"attachments": {},
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "245a954a",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# OpenWeatherMap API\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook goes over how to use the OpenWeatherMap component to fetch weather information.\n",
|
||||||
|
"\n",
|
||||||
|
"First, you need to sign up for an OpenWeatherMap API key:\n",
|
||||||
|
"\n",
|
||||||
|
"1. Go to OpenWeatherMap and sign up for an API key [here](https://openweathermap.org/api/)\n",
|
||||||
|
"2. pip install pyowm\n",
|
||||||
|
"\n",
|
||||||
|
"Then we will need to set some environment variables:\n",
|
||||||
|
"1. Save your API KEY into OPENWEATHERMAP_API_KEY env variable"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "961b3689",
|
||||||
|
"metadata": {
|
||||||
|
"vscode": {
|
||||||
|
"languageId": "shellscript"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"pip install pyowm"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 35,
|
||||||
|
"id": "34bb5968",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"os.environ[\"OPENWEATHERMAP_API_KEY\"] = \"\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 36,
|
||||||
|
"id": "ac4910f8",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain.utilities import OpenWeatherMapAPIWrapper"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 37,
|
||||||
|
"id": "84b8f773",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"weather = OpenWeatherMapAPIWrapper()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 38,
|
||||||
|
"id": "9651f324-e74a-4f08-a28a-89db029f66f8",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"weather_data = weather.run(\"London,GB\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 39,
|
||||||
|
"id": "028f4cba",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"In London,GB, the current weather is as follows:\n",
|
||||||
|
"Detailed status: overcast clouds\n",
|
||||||
|
"Wind speed: 4.63 m/s, direction: 150°\n",
|
||||||
|
"Humidity: 67%\n",
|
||||||
|
"Temperature: \n",
|
||||||
|
" - Current: 5.35°C\n",
|
||||||
|
" - High: 6.26°C\n",
|
||||||
|
" - Low: 3.49°C\n",
|
||||||
|
" - Feels like: 1.95°C\n",
|
||||||
|
"Rain: {}\n",
|
||||||
|
"Heat index: None\n",
|
||||||
|
"Cloud cover: 100%\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"print(weather_data)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"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.11.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
"""OpenWeatherMap API toolkit."""
|
@ -0,0 +1,29 @@
|
|||||||
|
"""Tool for the OpenWeatherMap API."""
|
||||||
|
|
||||||
|
from langchain.tools.base import BaseTool
|
||||||
|
from langchain.utilities import OpenWeatherMapAPIWrapper
|
||||||
|
|
||||||
|
|
||||||
|
class OpenWeatherMapQueryRun(BaseTool):
|
||||||
|
"""Tool that adds the capability to query using the OpenWeatherMap API."""
|
||||||
|
|
||||||
|
api_wrapper: OpenWeatherMapAPIWrapper
|
||||||
|
|
||||||
|
name = "OpenWeatherMap"
|
||||||
|
description = (
|
||||||
|
"A wrapper around OpenWeatherMap API. "
|
||||||
|
"Useful for fetching current weather information for a specified location. "
|
||||||
|
"Input should be a location string (e.g. 'London,GB')."
|
||||||
|
)
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.api_wrapper = OpenWeatherMapAPIWrapper()
|
||||||
|
return
|
||||||
|
|
||||||
|
def _run(self, location: str) -> str:
|
||||||
|
"""Use the OpenWeatherMap tool."""
|
||||||
|
return self.api_wrapper.run(location)
|
||||||
|
|
||||||
|
async def _arun(self, location: str) -> str:
|
||||||
|
"""Use the OpenWeatherMap tool asynchronously."""
|
||||||
|
raise NotImplementedError("OpenWeatherMapQueryRun does not support async")
|
@ -0,0 +1,79 @@
|
|||||||
|
"""Util that calls OpenWeatherMap using PyOWM."""
|
||||||
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
|
from pydantic import Extra, root_validator
|
||||||
|
|
||||||
|
from langchain.tools.base import BaseModel
|
||||||
|
from langchain.utils import get_from_dict_or_env
|
||||||
|
|
||||||
|
|
||||||
|
class OpenWeatherMapAPIWrapper(BaseModel):
|
||||||
|
"""Wrapper for OpenWeatherMap API using PyOWM.
|
||||||
|
|
||||||
|
Docs for using:
|
||||||
|
|
||||||
|
1. Go to OpenWeatherMap and sign up for an API key
|
||||||
|
3. Save your API KEY into OPENWEATHERMAP_API_KEY env variable
|
||||||
|
4. pip install wolframalpha
|
||||||
|
"""
|
||||||
|
|
||||||
|
owm: Any
|
||||||
|
openweathermap_api_key: Optional[str] = None
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
"""Configuration for this pydantic object."""
|
||||||
|
|
||||||
|
extra = Extra.forbid
|
||||||
|
|
||||||
|
@root_validator(pre=True)
|
||||||
|
def validate_environment(cls, values: Dict) -> Dict:
|
||||||
|
"""Validate that api key exists in environment."""
|
||||||
|
openweathermap_api_key = get_from_dict_or_env(
|
||||||
|
values, "openweathermap_api_key", "OPENWEATHERMAP_API_KEY"
|
||||||
|
)
|
||||||
|
values["openweathermap_api_key"] = openweathermap_api_key
|
||||||
|
|
||||||
|
try:
|
||||||
|
import pyowm
|
||||||
|
|
||||||
|
except ImportError:
|
||||||
|
raise ImportError(
|
||||||
|
"pyowm is not installed. " "Please install it with `pip install pyowm`"
|
||||||
|
)
|
||||||
|
|
||||||
|
owm = pyowm.OWM(openweathermap_api_key)
|
||||||
|
values["owm"] = owm
|
||||||
|
|
||||||
|
return values
|
||||||
|
|
||||||
|
def _format_weather_info(self, location: str, w: Any) -> str:
|
||||||
|
detailed_status = w.detailed_status
|
||||||
|
wind = w.wind()
|
||||||
|
humidity = w.humidity
|
||||||
|
temperature = w.temperature("celsius")
|
||||||
|
rain = w.rain
|
||||||
|
heat_index = w.heat_index
|
||||||
|
clouds = w.clouds
|
||||||
|
|
||||||
|
return (
|
||||||
|
f"In {location}, the current weather is as follows:\n"
|
||||||
|
f"Detailed status: {detailed_status}\n"
|
||||||
|
f"Wind speed: {wind['speed']} m/s, direction: {wind['deg']}°\n"
|
||||||
|
f"Humidity: {humidity}%\n"
|
||||||
|
f"Temperature: \n"
|
||||||
|
f" - Current: {temperature['temp']}°C\n"
|
||||||
|
f" - High: {temperature['temp_max']}°C\n"
|
||||||
|
f" - Low: {temperature['temp_min']}°C\n"
|
||||||
|
f" - Feels like: {temperature['feels_like']}°C\n"
|
||||||
|
f"Rain: {rain}\n"
|
||||||
|
f"Heat index: {heat_index}\n"
|
||||||
|
f"Cloud cover: {clouds}%"
|
||||||
|
)
|
||||||
|
|
||||||
|
def run(self, location: str) -> str:
|
||||||
|
"""Get the current weather information for a specified location."""
|
||||||
|
mgr = self.owm.weather_manager()
|
||||||
|
observation = mgr.weather_at_place(location)
|
||||||
|
w = observation.weather
|
||||||
|
|
||||||
|
return self._format_weather_info(location, w)
|
@ -0,0 +1,24 @@
|
|||||||
|
from langchain.utilities.openweathermap import OpenWeatherMapAPIWrapper
|
||||||
|
|
||||||
|
|
||||||
|
def test_openweathermap_api_wrapper() -> None:
|
||||||
|
"""Test that OpenWeatherMapAPIWrapper returns correct data for London, GB."""
|
||||||
|
|
||||||
|
weather = OpenWeatherMapAPIWrapper()
|
||||||
|
weather_data = weather.run("London,GB")
|
||||||
|
|
||||||
|
assert weather_data is not None
|
||||||
|
assert "London" in weather_data
|
||||||
|
assert "GB" in weather_data
|
||||||
|
assert "Detailed status:" in weather_data
|
||||||
|
assert "Wind speed:" in weather_data
|
||||||
|
assert "direction:" in weather_data
|
||||||
|
assert "Humidity:" in weather_data
|
||||||
|
assert "Temperature:" in weather_data
|
||||||
|
assert "Current:" in weather_data
|
||||||
|
assert "High:" in weather_data
|
||||||
|
assert "Low:" in weather_data
|
||||||
|
assert "Feels like:" in weather_data
|
||||||
|
assert "Rain:" in weather_data
|
||||||
|
assert "Heat index:" in weather_data
|
||||||
|
assert "Cloud cover:" in weather_data
|
Loading…
Reference in New Issue