From ce6c0840e38a5ad0e5e200499b8d10bdebf34797 Mon Sep 17 00:00:00 2001 From: DEENUU1 Date: Tue, 23 Jan 2024 01:46:59 +0100 Subject: [PATCH] Create tool to get news using NewsAPI --- app/ai/agent.py | 8 ++++++++ app/ai/tools/news.py | 43 +++++++++++++++++++++++++++++++++++++++++ app/config/settings.py | 2 ++ requirements.txt | Bin 3350 -> 3396 bytes 4 files changed, 53 insertions(+) create mode 100644 app/ai/tools/news.py diff --git a/app/ai/agent.py b/app/ai/agent.py index df37468..64794e2 100644 --- a/app/ai/agent.py +++ b/app/ai/agent.py @@ -14,6 +14,7 @@ from langchain_community.utilities.openweathermap import OpenWeatherMapAPIWrapper from langchain.tools import WikipediaQueryRun from langchain_community.utilities import WikipediaAPIWrapper +from .tools import news langchain.debug = True @@ -62,7 +63,14 @@ def setup_agent(session_id: str, model: str) -> AgentExecutor: weather = OpenWeatherMapAPIWrapper(openweathermap_api_key=settings.OPENWEATHERAPP_API_KEY) wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper()) + news_tool = news.NewsTool() + tools = [ + Tool( + name="News", + func=news_tool.run, + description="Useful for when you need to answer questions about current news" + ), Tool( name="Wikipedia", func=wikipedia.run, diff --git a/app/ai/tools/news.py b/app/ai/tools/news.py new file mode 100644 index 0000000..31ed61b --- /dev/null +++ b/app/ai/tools/news.py @@ -0,0 +1,43 @@ +from typing import Optional + +from langchain.callbacks.manager import ( + AsyncCallbackManagerForToolRun, + CallbackManagerForToolRun, +) +from langchain_core.tools import BaseTool +from newsapi import NewsApiClient +from config.settings import settings + + +newsapi = NewsApiClient(api_key=settings.NEWS_API) + + +def get_top_headlines_formatted() -> str: + # Get top headlines + result = "" + + top_headlines = newsapi.get_top_headlines() + + articles = top_headlines["articles"] + for idx, article in enumerate(articles): + title = f"{idx} + {article['title']} + '\n' " + result += title + result += f"Description: {article["description"]} + '\n'" + + return result + + +class NewsTool(BaseTool): + name = "news_tool" + description = "Useful for when you need to answer questions about current news" + + def _run( + self, run_manager: Optional[CallbackManagerForToolRun] = None + ) -> str: + return get_top_headlines_formatted() + + async def _arun( + self, run_manager: Optional[AsyncCallbackManagerForToolRun] = None + ) -> str: + """Use the tool asynchronously.""" + raise NotImplementedError("custom_search does not support async") diff --git a/app/config/settings.py b/app/config/settings.py index a3f0d89..852a270 100644 --- a/app/config/settings.py +++ b/app/config/settings.py @@ -51,5 +51,7 @@ class Settings(BaseSettings): OPENWEATHERAPP_API_KEY: Optional[str] = os.getenv("OPENWEATHERAPP_API_KEY") + NEWS_API: Optional[str] = os.getenv("NEWS_API") + settings = Settings() diff --git a/requirements.txt b/requirements.txt index 8a41274b77a212e5d85536a95017787fee6c1c01..0f1ae933e7a426d9fa0b523a0adebcebe887a509 100644 GIT binary patch delta 52 zcmbOxbwp}|1iN}FLpeh+Ln1=~LnebRkgQ}VVaQ;}XUJo)1wsP`Jq9BnHs37B&dLS= DJirPf delta 12 TcmX>iHBD-R1p8(;_J6DZ9v=i@