|
| 1 | +""" |
| 2 | +This example shows how to use complex input types with tools. |
| 3 | +
|
| 4 | +Recommendations: |
| 5 | +- Specify fields with descriptions, these will be used in the JSON schema sent to the model and will increase accuracy. |
| 6 | +- Try not to nest the structures too deeply, the model will have a hard time understanding them. |
| 7 | +""" |
| 8 | + |
| 9 | +from datetime import datetime |
| 10 | +from enum import Enum |
| 11 | +from typing import List, Optional |
| 12 | + |
| 13 | +from agno.agent import Agent |
| 14 | +from agno.tools.decorator import tool |
| 15 | +from pydantic import BaseModel, Field |
| 16 | + |
| 17 | + |
| 18 | +# Define Pydantic models for our tools |
| 19 | +class UserProfile(BaseModel): |
| 20 | + """User profile information.""" |
| 21 | + |
| 22 | + name: str = Field(..., description="Full name of the user") |
| 23 | + email: str = Field(..., description="Valid email address") |
| 24 | + age: int = Field(..., ge=0, le=120, description="Age of the user") |
| 25 | + interests: List[str] = Field( |
| 26 | + default_factory=list, description="List of user interests" |
| 27 | + ) |
| 28 | + created_at: datetime = Field( |
| 29 | + default_factory=datetime.now, description="Account creation timestamp" |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +class TaskPriority(str, Enum): |
| 34 | + """Priority levels for tasks.""" |
| 35 | + |
| 36 | + LOW = "low" |
| 37 | + MEDIUM = "medium" |
| 38 | + HIGH = "high" |
| 39 | + URGENT = "urgent" |
| 40 | + |
| 41 | + |
| 42 | +class Task(BaseModel): |
| 43 | + """Task information.""" |
| 44 | + |
| 45 | + title: str = Field(..., min_length=1, max_length=100, description="Task title") |
| 46 | + description: Optional[str] = Field(None, description="Detailed task description") |
| 47 | + priority: TaskPriority = Field( |
| 48 | + default=TaskPriority.MEDIUM, description="Task priority level" |
| 49 | + ) |
| 50 | + due_date: Optional[datetime] = Field(None, description="Task due date") |
| 51 | + assigned_to: Optional[UserProfile] = Field( |
| 52 | + None, description="User assigned to the task" |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +# Custom tools using Pydantic models |
| 57 | +@tool |
| 58 | +def create_user(user_data: UserProfile) -> str: |
| 59 | + """Create a new user profile with validated information.""" |
| 60 | + # In a real application, this would save to a database |
| 61 | + return f"Created user profile for {user_data.name} with email {user_data.email}" |
| 62 | + |
| 63 | + |
| 64 | +@tool |
| 65 | +def create_task(task_data: Task) -> str: |
| 66 | + """Create a new task with priority and assignment.""" |
| 67 | + # In a real application, this would save to a database |
| 68 | + return f"Created task '{task_data.title}' with priority {task_data.priority}" |
| 69 | + |
| 70 | + |
| 71 | +# Create the agent |
| 72 | +agent = Agent( |
| 73 | + name="task_manager", |
| 74 | + description="An agent that manages users and tasks with proper validation", |
| 75 | + tools=[create_user, create_task], |
| 76 | +) |
| 77 | + |
| 78 | +# Example usage |
| 79 | +if __name__ == "__main__": |
| 80 | + # Example 1: Create a user |
| 81 | + agent.print_response( |
| 82 | + "Create a new user named John Doe with email john@example.com, age 30, and interests in Python and AI" |
| 83 | + ) |
| 84 | + |
| 85 | + # Example 2: Create a task |
| 86 | + agent.print_response( |
| 87 | + "Create a high priority task titled 'Implement API endpoints' due tomorrow" |
| 88 | + ) |
0 commit comments