Tools
Tools are the hands of your Agent. They allow the LLM to interact with the outside world, such as searching the web, querying a database, or performing calculations.
Purpose
Use @tool to:
- Expose Python functions to the LLM.
- Define clear descriptions and type hints so the LLM knows when and how to use them.
Usage
Defining a Tool
Simply decorate a Python function with @tool. Type hints and docstrings are mandatory as they are used to generate the schema for the LLM.
from yosrai import tool
@tool
def calculate_tax(amount: float, rate: float = 0.1) -> float:
"""
Calculates the tax for a given amount.
Args:
amount: The total amount.
rate: The tax rate (default 0.1).
"""
return amount * rate
Async Tools
You can also define async tools for non-blocking I/O operations.
import asyncio
from yosrai import tool
@tool
async def fetch_data(url: str) -> str:
"""Fetches data from a URL asynchronously."""
await asyncio.sleep(1) # Simulate I/O
return "Data content"
API Reference
yosrai.engine.core.tools.tool(func)
Decorator to mark a function as an Agent Tool. Auto-generates .json_schema for the tool and enforces type validation.