cgoncalves/agentic-rag
0
1import requests2import os3 4from huggingface_hub import list_models5from langchain_community.tools import DuckDuckGoSearchResults6from langchain.tools import Tool7from dotenv import load_dotenv8 9# Make sure environment variables are loaded10load_dotenv()11 12 13def get_weather_info(location: str) -> str:14 """Fetches real weather information for a given location using WeatherAPI.com."""15 api_key = os.getenv("WEATHERAPI_KEY")16 if not api_key:17 return "Error: WeatherAPI key not found. Please set the WEATHERAPI_KEY environment variable."18 19 base_url = "http://api.weatherapi.com/v1/current.json"20 params = {21 "key": api_key,22 "q": location,23 "aqi": "no" # You can change this to "yes" if you want air quality info24 }25 26 try:27 response = requests.get(base_url, params=params)28 data = response.json()29 30 if response.status_code == 200:31 # Extract relevant information from the response32 location_data = data.get("location", {})33 current_data = data.get("current", {})34 35 location_name = location_data.get("name", "Unknown")36 region = location_data.get("region", "Unknown")37 country = location_data.get("country", "Unknown")38 temp_c = current_data.get("temp_c", "N/A")39 feelslike_c = current_data.get("feelslike_c", "N/A")40 condition_text = current_data.get("condition", {}).get("text", "N/A")41 humidity = current_data.get("humidity", "N/A")42 wind_kph = current_data.get("wind_kph", "N/A")43 44 return (45 f"Weather in {location_name}, {region}, {country}: {condition_text}, "46 f"{temp_c}°C (feels like {feelslike_c}°C). Humidity: {humidity}%, Wind: {wind_kph} kph"47 )48 else:49 error_message = data.get("error", {}).get("message", "Unknown error")50 return f"Error fetching weather data: {error_message}"51 52 except Exception as e:53 return f"An error occurred while fetching weather data: {str(e)}"54 55 56# Initialize the tool57weather_info_tool = Tool(58 name="get_weather_info",59 func=get_weather_info,60 description="Fetches real weather information for a given location. Provide the city name and optionally the country code (e.g., 'London' or 'London,UK')."61)62 63 64def get_hub_stats(author: str) -> str:65 """Fetches the most downloaded model from a specific author on the Hugging Face Hub."""66 try:67 # List models from the specified author, sorted by downloads68 models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))69 70 if models:71 model = models[0]72 return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."73 else:74 return f"No models found for author {author}."75 except Exception as e:76 return f"Error fetching models for {author}: {str(e)}"77 78# Initialize the tool79hub_stats_tool = Tool(80 name="get_hub_stats",81 func=get_hub_stats,82 description="Fetches the most downloaded model from a specific author on the Hugging Face Hub."83)84 85search_tool = DuckDuckGoSearchResults()