From 68f0d454857beb1bc68264d0af74f5840fc2b593 Mon Sep 17 00:00:00 2001 From: Adheeban Manoharan <69910091+iamadhee@users.noreply.github.com> Date: Wed, 24 May 2023 04:27:33 +0530 Subject: [PATCH] Adding Weather Loader (#5056) Co-authored-by: Tyler Hutcherson Co-authored-by: Dev 2049 --- docs/modules/indexes/document_loaders.rst | 1 + .../document_loaders/examples/weather.ipynb | 101 ++++++++++++++++++ langchain/document_loaders/__init__.py | 2 + langchain/document_loaders/weather.py | 50 +++++++++ langchain/utilities/openweathermap.py | 1 - 5 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 docs/modules/indexes/document_loaders/examples/weather.ipynb create mode 100644 langchain/document_loaders/weather.py diff --git a/docs/modules/indexes/document_loaders.rst b/docs/modules/indexes/document_loaders.rst index c1902f4e..8c7306f2 100644 --- a/docs/modules/indexes/document_loaders.rst +++ b/docs/modules/indexes/document_loaders.rst @@ -53,6 +53,7 @@ For detailed instructions on how to get set up with Unstructured, see installati ./document_loaders/examples/unstructured_file.ipynb ./document_loaders/examples/url.ipynb ./document_loaders/examples/web_base.ipynb + ./document_loaders/examples/weather.ipynb ./document_loaders/examples/whatsapp_chat.ipynb diff --git a/docs/modules/indexes/document_loaders/examples/weather.ipynb b/docs/modules/indexes/document_loaders/examples/weather.ipynb new file mode 100644 index 00000000..5b796bb7 --- /dev/null +++ b/docs/modules/indexes/document_loaders/examples/weather.ipynb @@ -0,0 +1,101 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "66a7777e", + "metadata": {}, + "source": [ + "# Weather\n", + "\n", + ">[OpenWeatherMap](https://openweathermap.org/) is an open source weather service provider\n", + "\n", + "This loader fetches the weather data from the OpenWeatherMap's OneCall API, using the pyowm Python package. You must initialize the loader with your OpenWeatherMap API token and the names of the cities you want the weather data for." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9ec8a3b3", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.document_loaders import WeatherDataLoader" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "43128d8d", + "metadata": {}, + "outputs": [], + "source": [ + "#!pip install pyowm" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "51b0f0db", + "metadata": {}, + "outputs": [], + "source": [ + "# Set API key either by passing it in to constructor directly\n", + "# or by setting the environment variable \"OPENWEATHERMAP_API_KEY\".\n", + "\n", + "from getpass import getpass\n", + "\n", + "OPENWEATHERMAP_API_KEY = getpass()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "35d6809a", + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "loader = WeatherDataLoader.from_params(['chennai','vellore'], openweathermap_api_key=OPENWEATHERMAP_API_KEY) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "05fe33b9", + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "documents = loader.load()\n", + "documents" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/langchain/document_loaders/__init__.py b/langchain/document_loaders/__init__.py index af964f60..6e693c21 100644 --- a/langchain/document_loaders/__init__.py +++ b/langchain/document_loaders/__init__.py @@ -100,6 +100,7 @@ from langchain.document_loaders.unstructured import ( from langchain.document_loaders.url import UnstructuredURLLoader from langchain.document_loaders.url_playwright import PlaywrightURLLoader from langchain.document_loaders.url_selenium import SeleniumURLLoader +from langchain.document_loaders.weather import WeatherDataLoader from langchain.document_loaders.web_base import WebBaseLoader from langchain.document_loaders.whatsapp_chat import WhatsAppChatLoader from langchain.document_loaders.wikipedia import WikipediaLoader @@ -212,6 +213,7 @@ __all__ = [ "UnstructuredRTFLoader", "UnstructuredURLLoader", "UnstructuredWordDocumentLoader", + "WeatherDataLoader", "WebBaseLoader", "WhatsAppChatLoader", "WikipediaLoader", diff --git a/langchain/document_loaders/weather.py b/langchain/document_loaders/weather.py new file mode 100644 index 00000000..958b6f39 --- /dev/null +++ b/langchain/document_loaders/weather.py @@ -0,0 +1,50 @@ +"""Simple reader that reads weather data from OpenWeatherMap API""" +from __future__ import annotations + +from datetime import datetime +from typing import Iterator, List, Optional, Sequence + +from langchain.docstore.document import Document +from langchain.document_loaders.base import BaseLoader +from langchain.utilities.openweathermap import OpenWeatherMapAPIWrapper + + +class WeatherDataLoader(BaseLoader): + """Weather Reader. + + Reads the forecast & current weather of any location using OpenWeatherMap's free + API. Checkout 'https://openweathermap.org/appid' for more on how to generate a free + OpenWeatherMap API. + """ + + def __init__( + self, + client: OpenWeatherMapAPIWrapper, + places: Sequence[str], + ) -> None: + """Initialize with parameters.""" + super().__init__() + self.client = client + self.places = places + + @classmethod + def from_params( + cls, places: Sequence[str], *, openweathermap_api_key: Optional[str] = None + ) -> WeatherDataLoader: + client = OpenWeatherMapAPIWrapper(openweathermap_api_key=openweathermap_api_key) + return cls(client, places) + + def lazy_load( + self, + ) -> Iterator[Document]: + """Lazily load weather data for the given locations.""" + for place in self.places: + metadata = {"queried_at": datetime.now()} + content = self.client.run(place) + yield Document(page_content=content, metadata=metadata) + + def load( + self, + ) -> List[Document]: + """Load weather data for the given locations.""" + return list(self.lazy_load()) diff --git a/langchain/utilities/openweathermap.py b/langchain/utilities/openweathermap.py index 8d753fe1..2b10fb38 100644 --- a/langchain/utilities/openweathermap.py +++ b/langchain/utilities/openweathermap.py @@ -31,7 +31,6 @@ class OpenWeatherMapAPIWrapper(BaseModel): 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