agentops/examples/agno/agno_tool_integrations.ipynb

247 lines
9.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "intro-cell",
"metadata": {},
"source": [
"# Tool Integration Example with Agno\n",
"\n",
"This example demonstrates how to integrate and use various tools with Agno agents,\n",
"showing how AgentOps automatically tracks tool usage and agent interactions.\n",
"\n",
"## Overview\n",
"This example demonstrates:\n",
"\n",
"1. **Using built-in Agno tools** like GoogleSearch, DuckDuckGo, and Arxiv\n",
"2. **Creating agents with tools** and seeing how they use them\n",
"3. **Tool execution tracking** with AgentOps\n",
"4. **Combining multiple tools** for comprehensive research\n",
"\n",
"This example uses actual Agno components to show real tool integration patterns."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "setup-cell",
"metadata": {},
"outputs": [],
"source": [
"# Install the required dependencies:\n",
"%pip install agentops\n",
"%pip install \"agno[tools]\"\n",
"%pip install python-dotenv"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "imports-cell",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from dotenv import load_dotenv\n",
"import agentops\n",
"from agno.agent import Agent\n",
"from agno.models.openai import OpenAIChat\n",
"from agno.tools.googlesearch import GoogleSearchTools\n",
"from agno.tools.duckduckgo import DuckDuckGoTools\n",
"from agno.tools.arxiv import ArxivTools"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "config-cell",
"metadata": {},
"outputs": [],
"source": [
"# Load environment variables\n",
"load_dotenv()\n",
"\n",
"# Set environment variables if not already set\n",
"os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\", \"your_openai_api_key_here\")\n",
"os.environ[\"AGENTOPS_API_KEY\"] = os.getenv(\"AGENTOPS_API_KEY\", \"your_agentops_api_key_here\")\n",
"\n",
"# Initialize AgentOps\n",
"agentops.init(\n",
" auto_start_session=False, trace_name=\"Agno Tool Integrations\", tags=[\"agno-tools\", \"tool-integration\", \"demo\"]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "demo-function",
"metadata": {},
"outputs": [],
"source": [
"def demonstrate_tool_integration():\n",
" \"\"\"Demonstrate tool integration with Agno agents.\"\"\"\n",
" print(\"🚀 Agno Tool Integration Demonstration\")\n",
" print(\"=\" * 60)\n",
"\n",
" # Start AgentOps trace\n",
" tracer = agentops.start_trace(trace_name=\"Agno Tool Integration Demo\")\n",
"\n",
" try:\n",
" # Example 1: Single Tool Agent\n",
" print(\"\\n📌 Example 1: Agent with Google Search Tool\")\n",
" print(\"-\" * 40)\n",
"\n",
" search_agent = Agent(\n",
" name=\"Search Agent\",\n",
" role=\"Research information using Google Search\",\n",
" model=OpenAIChat(id=\"gpt-4o-mini\"),\n",
" tools=[GoogleSearchTools()],\n",
" instructions=\"You are a research assistant. Use Google Search to find accurate, up-to-date information.\",\n",
" )\n",
"\n",
" response = search_agent.run(\"What are the latest developments in AI agents?\")\n",
" print(f\"Search Agent Response:\\n{response.content}\")\n",
"\n",
" # Example 2: Multi-Tool Agent\n",
" print(\"\\n\\n📌 Example 2: Agent with Multiple Tools\")\n",
" print(\"-\" * 40)\n",
"\n",
" research_agent = Agent(\n",
" name=\"Research Agent\",\n",
" role=\"Comprehensive research using multiple tools\",\n",
" model=OpenAIChat(id=\"gpt-4o-mini\"),\n",
" tools=[GoogleSearchTools(), ArxivTools(), DuckDuckGoTools()],\n",
" instructions=\"\"\"You are a comprehensive research assistant. \n",
" Use Google Search for general information, Arxiv for academic papers, \n",
" and DuckDuckGo as an alternative search engine. \n",
" Provide well-researched, balanced information from multiple sources.\"\"\",\n",
" )\n",
"\n",
" response = research_agent.run(\n",
" \"Find information about recent advances in tool-use for AI agents. \"\n",
" \"Include both academic research and practical implementations.\"\n",
" )\n",
" print(f\"Research Agent Response:\\n{response.content}\")\n",
"\n",
" # Example 3: Specialized Tool Usage\n",
" print(\"\\n\\n📌 Example 3: Academic Research with Arxiv\")\n",
" print(\"-\" * 40)\n",
"\n",
" academic_agent = Agent(\n",
" name=\"Academic Agent\",\n",
" role=\"Find and summarize academic papers\",\n",
" model=OpenAIChat(id=\"gpt-4o-mini\"),\n",
" tools=[ArxivTools()],\n",
" instructions=\"You are an academic research assistant. Use Arxiv to find relevant papers and provide concise summaries.\",\n",
" )\n",
"\n",
" response = academic_agent.run(\"Find recent papers about tool augmented language models\")\n",
" print(f\"Academic Agent Response:\\n{response.content}\")\n",
"\n",
" # Example 4: Comparing Search Tools\n",
" print(\"\\n\\n📌 Example 4: Comparing Different Search Tools\")\n",
" print(\"-\" * 40)\n",
"\n",
" comparison_agent = Agent(\n",
" name=\"Comparison Agent\",\n",
" role=\"Compare results from different search engines\",\n",
" model=OpenAIChat(id=\"gpt-4o-mini\"),\n",
" tools=[GoogleSearchTools(), DuckDuckGoTools()],\n",
" instructions=\"\"\"Compare search results from Google and DuckDuckGo. \n",
" Note any differences in results, ranking, or information quality.\n",
" Be objective in your comparison.\"\"\",\n",
" )\n",
"\n",
" response = comparison_agent.run(\n",
" \"Search for 'AgentOps observability platform' on both search engines and compare the results\"\n",
" )\n",
" print(f\"Comparison Agent Response:\\n{response.content}\")\n",
"\n",
" print(\"\\n\\n✨ Demonstration Complete!\")\n",
" print(\"\\nKey Takeaways:\")\n",
" print(\"- Agno agents can use multiple tools seamlessly\")\n",
" print(\"- Tools are automatically invoked based on the agent's task\")\n",
" print(\"- AgentOps tracks all tool executions automatically\")\n",
" print(\"- Different tools serve different purposes (web search, academic search, etc.)\")\n",
" print(\"- Agents can compare and synthesize information from multiple tools\")\n",
"\n",
" # End the AgentOps trace successfully\n",
" print(\"\\n📊 View your tool execution traces in AgentOps:\")\n",
" print(\" Visit https://app.agentops.ai/ to see detailed analytics\")\n",
" agentops.end_trace(tracer, end_state=\"Success\")\n",
"\n",
" except Exception as e:\n",
" print(f\"\\n❌ An error occurred: {e}\")\n",
" agentops.end_trace(tracer, end_state=\"Error\")\n",
" raise\n",
"\n",
" # Let's check programmatically that spans were recorded in AgentOps\n",
" print(\"\\n\" + \"=\" * 50)\n",
" print(\"Now let's verify that our LLM calls were tracked properly...\")\n",
" try:\n",
" agentops.validate_trace_spans(trace_context=tracer)\n",
" print(\"\\n✅ Success! All LLM spans were properly recorded in AgentOps.\")\n",
" except agentops.ValidationError as e:\n",
" print(f\"\\n❌ Error validating spans: {e}\")\n",
" raise"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "run-demo",
"metadata": {},
"outputs": [],
"source": [
"demonstrate_tool_integration()"
]
},
{
"cell_type": "markdown",
"id": "summary-cell",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"This notebook demonstrated how to:\n",
"\n",
"- Set up Agno agents with various tools (Google Search, DuckDuckGo, Arxiv)\n",
"- Create single-tool and multi-tool agents\n",
"- Track tool usage with AgentOps\n",
"- Compare results from different search engines\n",
"- Validate that all operations are properly traced\n",
"\n",
"## Next Steps\n",
"\n",
"Visit the AgentOps dashboard to explore:\n",
"- Detailed tool execution metrics\n",
"- Agent performance analytics \n",
"- Error tracking and debugging information\n",
"\n",
"Each trace URL printed during execution provides direct access to that specific session's details."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}