DEV Community

Cover image for Building AI Agents with smolagents and a Gaia Node
Harish Kotra (he/him) for Gaia

Posted on

Building AI Agents with smolagents and a Gaia Node

What is covered in this example

  1. How to configure smolagents with a Gaia Node.
  2. How to define reusable tools with the @tool decorator.
  3. How to create an agent that responds to natural language queries.

Get your Gaia Developer API key here.

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://llama3b.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() 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)