Tool Implementation Example

This guide provides practical tool implementations for SEA-LION models, including working code, parsing logic, and execution frameworks. The implementations cover common patterns that you can adapt for your specific use cases.

What's Included

This example includes the components typically needed for tool calling:

  • Tool Functions: Working implementations for weather, time, and web search

  • Schema Definitions: OpenAI-compatible tool schemas for model integration

  • System Prompt: Sample prompt configured for model to execute tool-calling if it is not equipped to do so via chat template

  • Response Parsing: Parsing logic for text-based tool calls

  • Execution Framework: Tool execution system with error handling

  • Customization Guide: Instructions for adapting these examples to your own tools

The three example tools (weather, time, web search) demonstrate different common patterns—external API integration, system functions, and data processing. You can use these directly or as templates when building your own tools.

Tool Functions

Weather Tool Implementation

async def get_weather(location, session):
    """Get weather information using Open-Meteo API."""
    if not location or not isinstance(location, str):
        raise ValueError("Invalid location")

    # Geocoding request to resolve location
    geocode_url = "https://geocoding-api.open-meteo.com/v1/search"
    geocode_params = {
        "name": location,
        "count": 1,
        "language": "en",
        "format": "json",
    }
    
    async with session.get(geocode_url, params=geocode_params, timeout=20) as response:
        geo_data = await response.json()
    
    results = geo_data.get("results", [])
    if not results:
        raise ValueError(f"No results found for location: {location}")
    
    first = results[0]
    latitude = first["latitude"]
    longitude = first["longitude"]
    resolved = {
        "name": first["name"],
        "country": first["country"],
        "admin1": first.get("admin1"),
        "latitude": latitude,
        "longitude": longitude,
        "timezone": first.get("timezone"),
    }

    # Weather forecast request
    forecast_url = "https://api.open-meteo.com/v1/forecast"
    forecast_params = {
        "latitude": latitude,
        "longitude": longitude,
        "current": "temperature_2m,apparent_temperature,relative_humidity_2m,weather_code,wind_speed_10m",
        "wind_speed_unit": "kmh",
        "timezone": "auto",
    }
    
    async with session.get(forecast_url, params=forecast_params, timeout=20) as response:
        forecast_data = await response.json()

    current = forecast_data.get("current")
    units = forecast_data.get("current_units")
    if not current:
        raise ValueError("Weather data unavailable")

    return {
        "locationQueried": location,
        "resolvedLocation": resolved,
        "current": current,
        "units": units,
        "source": "open-meteo.com",
    }

Time Tool Implementation

Web Search Tool Implementation

Tool Schema Definition

System Prompt

Response Parsing

Tool Execution Framework

Customizing Tools for Your Application

The example tools above demonstrate different types of functionality:

  • get_weather: External API integration with error handling

  • get_time: System/library function usage with timezone handling

  • search_web: Complex data processing and result formatting

To create your own tools:

  1. Define your function: Create an async function that takes parameters and returns structured data

  2. Add error handling: Always validate inputs and handle exceptions gracefully

  3. Update the tool schema: Define the function signature for the model

  4. Modify the execution framework: Add your function to the execute_tool_calls function

  5. Update system prompts: Include your tool in the system message

Example of a custom tool:

Last updated