Skip to main content

Quickstart

Build your first AI agent with Upsonic in under 2 minutes! 🦔

1. Installation

pip install upsonic

2. Basic Agent Usage

from upsonic.tasks.tasks import Task
from upsonic.agent.agent import Agent

# Create a task
task = Task(
    description="Research latest news in AI and summarize the key points",
    tools=[]  # We'll add tools next
)

# Create an agent
agent = Agent(
    model="openai/gpt-4o",
    name="Research Assistant"
)

# Execute the task
result = agent.print_do(task)

3. Adding Tools

Built-in Tools (Provider-Native)

from upsonic.tools.builtin_tools import WebSearchTool, CodeExecutionTool

# Web search (works with Anthropic, OpenAI, Google, Groq)
web_search = WebSearchTool(
    search_context_size='high',
    max_uses=5
)

# Code execution (works with Anthropic, OpenAI, Google)
code_execution = CodeExecutionTool()

task = Task(
    description="Search for Python best practices and write a sample function",
    tools=[web_search, code_execution]
)

Common Tools (Third-Party)

from upsonic.tools.common_tools import tavily_search_tool, duckduckgo_search_tool

# Tavily search (requires API key)
tavily_search = tavily_search_tool(api_key="your-tavily-key")

# DuckDuckGo search (free)
ddg_search = duckduckgo_search_tool()

task = Task(
    description="Get current stock price for AAPL and search for recent news",
    tools=[tavily_search, ddg_search]
)

Custom Tools

from upsonic.tools import tool

@tool
def calculate_tip(bill_amount: float, tip_percentage: float = 15.0) -> str:
    """Calculate tip amount and total bill.
    
    Args:
        bill_amount: The total bill amount
        tip_percentage: Tip percentage (default 15%)
    
    Returns:
        Formatted string with tip and total
    """
    tip = bill_amount * (tip_percentage / 100)
    total = bill_amount + tip
    return f"Tip: ${tip:.2f}, Total: ${total:.2f}"

@tool(requires_confirmation=True, cache_results=True)
def send_email(to: str, subject: str, body: str) -> str:
    """Send an email (requires confirmation).
    
    Args:
        to: Recipient email address
        subject: Email subject
        body: Email body content
    
    Returns:
        Confirmation message
    """
    # Your email sending logic here
    return f"Email sent to {to}"

task = Task(
    description="Calculate tip for a $50 bill and send a thank you email",
    tools=[calculate_tip, send_email]
)

4. Advanced Agent Configuration

agent = Agent(
    model="openai/gpt-4o",
    name="Advanced Assistant",
    
    # Agent personality
    role="Senior Software Engineer",
    goal="Help developers write better code",
    instructions="Always provide code examples and best practices",
    
    # Advanced features
    enable_thinking_tool=True,      # Multi-step planning
    enable_reasoning_tool=True,     # Self-reflection
    tool_call_limit=10,            # Max tool calls per task
    show_tool_calls=True,          # Display tool execution
    
    # Memory and context
    memory=memory_instance,        # Conversation history
    compression_strategy="simple", # Context compression
    
    # Safety and reliability
    user_policy=safety_policy,     # Input validation
    agent_policy=output_policy,    # Output filtering
    retry=3,                       # Retry attempts
)

5. Tool Configuration Options

@tool(
    requires_user_input=True,      # Prompt for additional input
    user_input_fields=['api_key'], # Fields to collect
    show_result=True,              # Display result to user
    stop_after_tool_call=True,     # Stop agent after this tool
    sequential=True,               # No parallel execution
    cache_results=True,            # Cache results
    cache_ttl=3600,               # Cache for 1 hour
    max_retries=3,                # Retry on failure
    timeout=30.0                  # 30 second timeout
)
def advanced_tool(param: str) -> str:
    """An advanced tool with full configuration."""
    return f"Processed: {param}"

6. MCP (Model Context Protocol) Integration

# Connect to any MCP server
class FetchMCP:
    command = "uvx"
    args = ["mcp-server-fetch"]

class GitHubMCP:
    url = "https://github.com/modelcontextprotocol/servers"
    # Additional MCP configuration

task = Task(
    description="Fetch data from GitHub and process it",
    tools=[FetchMCP, GitHubMCP]
)

7. Complete Example

from upsonic.tasks.tasks import Task
from upsonic.agent.agent import Agent
from upsonic.tools import tool
from upsonic.tools.builtin_tools import WebSearchTool
from upsonic.tools.common_tools import YFinanceTools


# Custom tool
@tool(cache_results=True)
def analyze_sentiment(text: str) -> str:
    """Analyze sentiment of given text."""
    positive_words = ['good', 'great', 'excellent', 'amazing']
    negative_words = ['bad', 'terrible', 'awful', 'horrible']
    
    text_lower = text.lower()
    positive_count = sum(1 for word in positive_words if word in text_lower)
    negative_count = sum(1 for word in negative_words if word in text_lower)
    
    if positive_count > negative_count:
        return "Positive sentiment"
    elif negative_count > positive_count:
        return "Negative sentiment"
    else:
        return "Neutral sentiment"

# Create tools
web_search = WebSearchTool()
finance_tools = YFinanceTools(stock_price=True, company_news=True)
finance_tools.enable_all_tools()

# Create task
task = Task(
    description="Get current AAPL stock price, search for recent news, and analyze sentiment",
    tools=[finance_tools, web_search, analyze_sentiment]
)

# Create agent
agent = Agent(
    model="openai/gpt-4o",
    name="Financial Analyst",
    role="Senior Financial Analyst",
    enable_thinking_tool=True,
    tool_call_limit=8
)

# Execute
result = agent.print_do(task)

8. Key Features

  • Multi-Model Support: OpenAI, Anthropic, Google, Groq, and more
  • Advanced Tool System: Built-in, custom, and MCP tools
  • Intelligent Planning: Multi-step reasoning and orchestration
  • Memory Management: Conversation history and context
  • Safety Policies: Input/output validation and filtering
  • Caching: Built-in result caching for efficiency
  • Streaming: Real-time response streaming
  • External Execution: Defer tools for external processing

9. Next Steps


Ready to build amazing AI agents? Start with the basic example above and gradually add more sophisticated tools and configurations as you explore the framework!
I