Skip to main content
Tasks in Upsonic are created directly in code using the Task class constructor. Each task can be customized with specific tools, response formats, caching options, and validation rules to meet your exact requirements.

Basic Task Creation

from upsonic import Agent, Task

# Simple task with just a description
agent = Agent(model="openai/gpt-4o")
task = Task(description="What is the capital of France?")

# Execute task
result = agent.do(task)
print(result)

Task with Tools

from upsonic import Agent, Task
from upsonic.tools import tool

# Create a simple tool
@tool
def calculator(operation: str, a: float, b: float) -> str:
    """Perform basic mathematical operations."""
    if operation == "add":
        return f"Result: {a + b}"
    elif operation == "multiply":
        return f"Result: {a * b}"
    return "Invalid operation"

# Create agent and task with tools
agent = Agent(model="openai/gpt-4o")
task = Task(
    description="Calculate 10 + 5 using the calculator tool",
    tools=[calculator]
)

# Execute task
result = agent.do(task)
print(result)

Task with Response Format

from upsonic import Agent, Task
from pydantic import BaseModel

class AnalysisResult(BaseModel):
    summary: str
    confidence: float
    recommendations: list[str]

# Create agent and task with structured response
agent = Agent(model="openai/gpt-4o")
task = Task(
    description="Analyze the current state of renewable energy and provide structured results",
    response_format=AnalysisResult
)

# Execute task
result = agent.do(task)
print(f"Summary: {result.summary}")
print(f"Confidence: {result.confidence}")
print(f"Recommendations: {result.recommendations}")

Task with Advanced Configuration

from upsonic import Agent, Task

def validate_output(result):
    """Custom guardrail function."""
    if isinstance(result, str) and len(result) > 10:
        return True
    return False

# Create agent and task with advanced features
agent = Agent(model="openai/gpt-4o")
task = Task(
    description="Generate a comprehensive report on AI trends",
    enable_cache=True,
    cache_method="vector_search",
    cache_threshold=0.8,
    cache_duration_minutes=120,
    guardrail=validate_output,
    guardrail_retries=3,
    enable_thinking_tool=True
)

# Execute task
result = agent.do(task)
print(result)

Best Practices

  • Start Simple: Begin with basic tasks and gradually add complexity
  • Clear Descriptions: Write descriptive task descriptions that clearly state the objective
  • Tool Selection: Choose appropriate tools for the task requirements
  • Error Handling: Use guardrails for tasks that require specific output validation
  • Caching Strategy: Enable caching for tasks that might be repeated with similar inputs