mirror of
https://github.com/hwchase17/langchain
synced 2024-11-10 01:10:59 +00:00
c2a3021bb0
Signed-off-by: ChengZi <chen.zhang@zilliz.com> Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com> Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Dan O'Donovan <dan.odonovan@gmail.com> Co-authored-by: Tom Daniel Grande <tomdgrande@gmail.com> Co-authored-by: Grande <Tom.Daniel.Grande@statsbygg.no> Co-authored-by: Bagatur <baskaryan@gmail.com> Co-authored-by: ccurme <chester.curme@gmail.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com> Co-authored-by: Tomaz Bratanic <bratanic.tomaz@gmail.com> Co-authored-by: ZhangShenao <15201440436@163.com> Co-authored-by: Friso H. Kingma <fhkingma@gmail.com> Co-authored-by: ChengZi <chen.zhang@zilliz.com> Co-authored-by: Nuno Campos <nuno@langchain.dev> Co-authored-by: Morgante Pell <morgantep@google.com>
78 lines
2.4 KiB
Python
78 lines
2.4 KiB
Python
"""Util that calls OpenWeatherMap using PyOWM."""
|
|
|
|
from typing import Any, Dict, Optional
|
|
|
|
from langchain_core.utils import get_from_dict_or_env
|
|
from pydantic import BaseModel, ConfigDict, model_validator
|
|
|
|
|
|
class OpenWeatherMapAPIWrapper(BaseModel):
|
|
"""Wrapper for OpenWeatherMap API using PyOWM.
|
|
|
|
Docs for using:
|
|
|
|
1. Go to OpenWeatherMap and sign up for an API key
|
|
2. Save your API KEY into OPENWEATHERMAP_API_KEY env variable
|
|
3. pip install pyowm
|
|
"""
|
|
|
|
owm: Any = None
|
|
openweathermap_api_key: Optional[str] = None
|
|
|
|
model_config = ConfigDict(
|
|
extra="forbid",
|
|
)
|
|
|
|
@model_validator(mode="before")
|
|
@classmethod
|
|
def validate_environment(cls, values: Dict) -> Any:
|
|
"""Validate that api key exists in environment."""
|
|
openweathermap_api_key = get_from_dict_or_env(
|
|
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)
|