What is covered in this example How to configure smolagents with a Gaia Node. How to define reusable tools with the @tool decorator. How to create an agent that responds to natural language queries.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
from smolagents.agents import ToolCallingAgent from smolagents import tool, LiteLLMModel from typing import Optional import requests # Configure the model with Gaia's tooling node model = LiteLLMModel( model_id="openai/llama", # Using OpenAI format for Gaia compatibility api_base="https://llama70b.gaia.domains/v1", # Gaia's tooling node api_key="gaia" # API key for Gaia node ) @tool def get_weather(location: str, celsius: Optional[bool] = True) -> str: """ Get current weather conditions for a location using wttr.in service. This function fetches weather data in a human-readable format without requiring any API keys. It provides comprehensive weather information including temperature, conditions, and forecast. Args: location: The city name or location to get weather for celsius: Whether to return temperature in Celsius (True) or Fahrenheit (False) Returns: A formatted string containing current weather conditions """ try: # Construct the wttr.in URL with proper formatting # We use format=3 for a concise output and add language specification # The 'u' or 'm' parameter controls the unit (metric or USCS) unit = 'm' if celsius else 'u' url = f"https://wttr.in/{location}?format=3&{unit}" # Make the request to wttr.in response = requests.get(url, headers={'User-Agent': 'curl'}, timeout=10) response.raise_for_status() # Raise an exception for bad responses # Get the weather data and clean it up weather_data = response.text.strip() # Create a more detailed response using the weather data detailed_response = ( f"Weather Report for {location}:\n" f"{weather_data}\n\n" f"Temperature is shown in {'Celsius' if celsius else 'Fahrenheit'}" ) return detailed_response except requests.exceptions.RequestException as e: # Handle any network or service errors gracefully return f"I apologize, but I couldn't fetch the weather data: {str(e)}" # Create our weather-capable agent agent = ToolCallingAgent(tools=[get_weather], model=model) def demonstrate_weather_queries(): """ Demonstrate various weather queries to show the agent's capabilities. This function shows how the agent can handle different types of weather requests. """ print("Weather Agent Demonstration\n" + "="*25 + "\n") # Test various types of queries to show versatility test_queries = [ "What's the weather like in London?", "Tell me the current conditions in Tokyo", "How's the weather in New York City with temperatures in Fahrenheit?", "What's the weather like in Paris, France?" ] for query in test_queries: print(f"\nQuery: {query}") try: response = agent.run(query) print(f"Response: {response}") except Exception as e: print(f"Error processing query: {str(e)}") print("-" * 50) if __name__ == "__main__": # Run our demonstration demonstrate_weather_queries() |