mirror of
https://github.com/Mintplex-Labs/langchain-python.git
synced 2026-07-21 00:35:23 -04:00
66828ad231
[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 🙂
42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
"""Tool for the OpenWeatherMap API."""
|
|
|
|
from typing import Optional
|
|
|
|
from pydantic import Field
|
|
|
|
from langchain.callbacks.manager import (
|
|
AsyncCallbackManagerForToolRun,
|
|
CallbackManagerForToolRun,
|
|
)
|
|
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 = Field(
|
|
default_factory=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 _run(
|
|
self, location: str, run_manager: Optional[CallbackManagerForToolRun] = None
|
|
) -> str:
|
|
"""Use the OpenWeatherMap tool."""
|
|
return self.api_wrapper.run(location)
|
|
|
|
async def _arun(
|
|
self,
|
|
location: str,
|
|
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
"""Use the OpenWeatherMap tool asynchronously."""
|
|
raise NotImplementedError("OpenWeatherMapQueryRun does not support async")
|