diff --git a/py-server/nodes/Agents/llms/run_agent_node.py b/py-server/nodes/Agents/llms/run_agent_node.py index fd6b973..b43cf5b 100644 --- a/py-server/nodes/Agents/llms/run_agent_node.py +++ b/py-server/nodes/Agents/llms/run_agent_node.py @@ -7,7 +7,9 @@ from utils.get_credentails import get_credentials from .get_llm import get_llm from ..tools.tools import WEBSEARCH_TOOL +from ..tools.weather import WEATHER_TOOL import json + def run_agent_node(node: Node, input_data: Dict[str, Any], user_id) -> Dict[str, Any]: print("in the run agent") db: Session = next(get_db()) @@ -50,13 +52,14 @@ def run_agent_node(node: Node, input_data: Dict[str, Any], user_id) -> Dict[str, available_tools = [] if "websearch" in tools_list: available_tools.append(WEBSEARCH_TOOL) + if "weather" in tools_list: + available_tools.append(WEATHER_TOOL) agent = create_agent( model=llm, - # tools=available_tools + tools=available_tools ) - final_prompt = prompt_template.format(input=prompt, **input_data) agent_input = {"messages": [{"role": "user", "content": final_prompt}]} @@ -82,7 +85,4 @@ def run_agent_node(node: Node, input_data: Dict[str, Any], user_id) -> Dict[str, except Exception as e: print(f"Error in agent node {node.id}: {e}") - return {"error ": str(e)} - - - + return {"error ": str(e)} \ No newline at end of file diff --git a/py-server/nodes/Agents/tools/tools.py b/py-server/nodes/Agents/tools/tools.py index f0dabd1..59e1918 100644 --- a/py-server/nodes/Agents/tools/tools.py +++ b/py-server/nodes/Agents/tools/tools.py @@ -3,18 +3,28 @@ from dotenv import load_dotenv from langchain_tavily import TavilySearch from langchain_core.tools import Tool +from langchain.tools.weather import WeatherTool + load_dotenv() + if not os.environ.get("TAVILY_API_KEY"): os.environ["TAVILY_API_KEY"] = getpass.getpass("Tavily API key:\n") +if not os.environ.get("OPENWEATHER_API_KEY"): + os.environ["OPENWEATHER_API_KEY"] = getpass.getpass("OpenWeather API key:\n") + def web_search_tool(query: str): search = TavilySearch(api_key=os.environ["TAVILY_API_KEY"]) result = search.run(query) print(result) return result -WEBSEARCH_TOOL=Tool( +WEBSEARCH_TOOL = Tool( name="WebSearch", func=web_search_tool, description="search the web for the latest information" -) \ No newline at end of file +) + +WEATHER_TOOL = WeatherTool() + +__all__ = ["WEBSEARCH_TOOL", "WEATHER_TOOL"] \ No newline at end of file diff --git a/py-server/nodes/Agents/tools/weather_tool.py b/py-server/nodes/Agents/tools/weather_tool.py new file mode 100644 index 0000000..ea0345b --- /dev/null +++ b/py-server/nodes/Agents/tools/weather_tool.py @@ -0,0 +1,74 @@ +import os +import requests +from typing import Optional + +API_URL = "http://api.openweathermap.org/data/2.5/weather" + + +class WeatherTool: + """ + Fetches current weather information for a given city using the OpenWeatherMap API. + + The API key must be set in the environment variable `OPENWEATHER_API_KEY`. + """ + + def __init__(self, api_key: Optional[str] = None): + self.api_key = api_key or os.getenv("OPENWEATHER_API_KEY") + if not self.api_key: + raise ValueError( + "OpenWeatherMap API key not found. Set OPENWEATHER_API_KEY environment variable." + ) + + def get_weather(self, city: str) -> str: + """ + Retrieve current weather for the specified city. + + Parameters + ---------- + city : str + Name of the city (e.g., "London", "New York"). + + Returns + ------- + str + Human‑readable summary of the current weather. + + Raises + ------ + requests.HTTPError + If the API request fails. + """ + params = { + "q": city, + "appid": self.api_key, + "units": "metric", + "lang": "en", + } + response = requests.get(API_URL, params=params, timeout=10) + try: + response.raise_for_status() + except requests.HTTPError as exc: + # Provide a clearer error message for common cases + if response.status_code == 404: + raise ValueError(f"City '{city}' not found.") from exc + raise + + data = response.json() + + temp = data["main"]["temp"] + description = data["weather"][0]["description"].capitalize() + humidity = data["main"]["humidity"] + wind_speed = data["wind"]["speed"] + + summary = ( + f"The current weather in {city} is {description} " + f"with a temperature of {temp}°C, humidity {humidity}%, " + f"and wind speed {wind_speed} m/s." + ) + return summary + + def __call__(self, city: str) -> str: + return self.get_weather(city) + + +__all__ = ["WeatherTool"] \ No newline at end of file