from upsonic import Agent, Taskfrom upsonic.tools import tool@tooldef sum_tool(a: float, b: float) -> float: """ Add two numbers together. Args: a: First number b: Second number Returns: The sum of a and b """ return a + b# Create task with the tooltask = Task( description="Calculate 15 + 27", tools=[sum_tool])# Create agentagent = Agent(model="openai/gpt-4o", name="Calculator Agent")# Executeresult = agent.do(task)print(result)
Async functions are fully supported for I/O-bound operations:
Copy
from upsonic import Agent, Taskfrom upsonic.tools import toolimport httpx@toolasync def fetch_weather(city: str) -> dict: """ Fetch current weather for a city. Args: city: City name (e.g., "London", "New York") Returns: Weather data including temperature and conditions """ async with httpx.AsyncClient() as client: response = await client.get( f"https://api.weather.com/v1/current?city={city}" ) return response.json()task = Task( description="What's the weather in Tokyo?", tools=[fetch_weather])agent = Agent(model="openai/gpt-4o")result = await agent.do_async(task) # Use do_async for async toolsprint(result)
Configure tool behavior using decorator parameters:
Copy
from upsonic.tools import tool@tool( requires_confirmation=True, # Ask user before executing timeout=30, # 30 second timeout max_retries=3, # Retry up to 3 times on failure cache_results=True # Cache results)def expensive_operation(data: str) -> str: """Perform an expensive operation that should be cached.""" return process(data)