from upsonic import Agent, Task
from upsonic.tools import tool, ToolHooks
# Define hooks
def before_search(query: str):
print(f"Searching for: {query}")
return {"logged_query": query}
def after_search(result):
print(f"Found {len(result)} results")
return {"result_count": len(result)}
hooks = ToolHooks(before=before_search, after=after_search)
@tool(
requires_confirmation=True,
timeout=30,
max_retries=2,
cache_results=True,
cache_ttl=3600,
tool_hooks=hooks,
sequential=True
)
def search_database(query: str, limit: int = 10) -> list:
"""
Search the database for matching records.
Args:
query: Search query string
limit: Maximum number of results to return
Returns:
List of matching records
"""
return [f"Record {i}: Found '{query}' in database" for i in range(1, limit + 1)]
# Create agent and task
agent = Agent("openai/gpt-4o")
task = Task(
description="Search the database for 'Upsonic' and return the top 5 results",
tools=[search_database]
)
# Execute
result = agent.do(task)
print(result)