|
| 1 | +""" |
| 2 | +This example demonstrates how to create a WorkflowAI agent that uses tools to enhance its capabilities. |
| 3 | +It showcases: |
| 4 | +
|
| 5 | +1. Using built-in WorkflowAI tools (@search-google) |
| 6 | +2. Creating and using custom tools |
| 7 | +3. Combining multiple tools in a single agent |
| 8 | +""" |
| 9 | + |
| 10 | +import asyncio |
| 11 | +from datetime import date, datetime |
| 12 | +from zoneinfo import ZoneInfo |
| 13 | + |
| 14 | +from pydantic import BaseModel, Field |
| 15 | + |
| 16 | +import workflowai |
| 17 | +from workflowai import Model |
| 18 | + |
| 19 | + |
| 20 | +def get_current_date() -> str: |
| 21 | + """Return today's date in ISO format (YYYY-MM-DD)""" |
| 22 | + return datetime.now(tz=ZoneInfo("UTC")).date().isoformat() |
| 23 | + |
| 24 | + |
| 25 | +def calculate_days_between(date1: str, date2: str) -> int: |
| 26 | + """Calculate the number of days between two dates in ISO format (YYYY-MM-DD)""" |
| 27 | + d1 = date.fromisoformat(date1) |
| 28 | + d2 = date.fromisoformat(date2) |
| 29 | + return abs((d2 - d1).days) |
| 30 | + |
| 31 | + |
| 32 | +class HistoricalEventInput(BaseModel): |
| 33 | + """Input model for querying historical events.""" |
| 34 | + query: str = Field( |
| 35 | + description="A query about a historical event", |
| 36 | + examples=[ |
| 37 | + "When was the first moon landing?", |
| 38 | + "When did World War II end?", |
| 39 | + "When was the Declaration of Independence signed?", |
| 40 | + ], |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +class HistoricalEventOutput(BaseModel): |
| 45 | + """Output model containing information about a historical event.""" |
| 46 | + event_date: str = Field( |
| 47 | + description="The date of the event in ISO format (YYYY-MM-DD)", |
| 48 | + examples=["1969-07-20", "1945-09-02", "1776-07-04"], |
| 49 | + ) |
| 50 | + event_description: str = Field( |
| 51 | + description="A brief description of the event", |
| 52 | + examples=[ |
| 53 | + "Apollo 11 astronauts Neil Armstrong and Buzz Aldrin became the first humans to land on the Moon", |
| 54 | + "Japan formally surrendered to the Allied Powers aboard the USS Missouri in Tokyo Bay", |
| 55 | + ], |
| 56 | + ) |
| 57 | + days_since_event: int = Field( |
| 58 | + description="Number of days between the event and today", |
| 59 | + examples=[19876, 28490, 90123], |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +@workflowai.agent( |
| 64 | + id="historical-event-analyzer", |
| 65 | + model=Model.GEMINI_1_5_FLASH_LATEST, |
| 66 | + tools=[get_current_date, calculate_days_between], |
| 67 | +) |
| 68 | +async def analyze_historical_event(event_input: HistoricalEventInput) -> HistoricalEventOutput: |
| 69 | + """ |
| 70 | + Find information about historical events and calculate days since they occurred. |
| 71 | +
|
| 72 | + You have access to these tools: |
| 73 | + 1. @search-google - Use this to find accurate information about events |
| 74 | + 2. get_current_date - Use this to get today's date for calculating days since the event |
| 75 | + 3. calculate_days_between - Calculate days between two dates (format: YYYY-MM-DD) |
| 76 | +
|
| 77 | + Guidelines: |
| 78 | + 1. Use @search-google to find accurate event information |
| 79 | + 2. Use get_current_date to get today's date |
| 80 | + 3. Use calculate_days_between to compute days since the event |
| 81 | + 4. Return dates in ISO format (YYYY-MM-DD) |
| 82 | + 5. Be precise with dates and descriptions |
| 83 | + """ |
| 84 | + ... |
| 85 | + |
| 86 | + |
| 87 | +async def main(): |
| 88 | + # Example: Query about the moon landing |
| 89 | + print("\nExample: First Moon Landing") |
| 90 | + print("-" * 50) |
| 91 | + run = await analyze_historical_event.run( |
| 92 | + HistoricalEventInput(query="When was the first moon landing?"), |
| 93 | + ) |
| 94 | + print(run) |
| 95 | + |
| 96 | + # Example: Query about World War II |
| 97 | + print("\nExample: End of World War II") |
| 98 | + print("-" * 50) |
| 99 | + run = await analyze_historical_event.run( |
| 100 | + HistoricalEventInput(query="When did World War II end?"), |
| 101 | + ) |
| 102 | + print(run) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + asyncio.run(main()) |
0 commit comments