|
1 | 1 | { |
2 | | - "cells": [ |
3 | | - { |
4 | | - "cell_type": "markdown", |
5 | | - "id": "813b80b7", |
6 | | - "metadata": {}, |
7 | | - "source": [ |
8 | | - "\n", |
9 | | - "# Tool Integration with RAG (Retrieval-Augmented Generation) in Agno\n", |
10 | | - "\n", |
11 | | - "This example demonstrates how to enhance Agno agents with RAG capabilities, allowing them to access and reason over external knowledge bases for more accurate and source-backed responses.\n", |
12 | | - "\n", |
13 | | - "**Overview**\n", |
14 | | - "\n", |
15 | | - "This example shows how to integrate RAG with Agno agents where we:\n", |
16 | | - "\n", |
17 | | - "1. **Set up a knowledge base** with documents, URLs, and other external sources\n", |
18 | | - "2. **Configure vector databases** (like Pinecone, Weaviate, or ChromaDB) for efficient semantic search\n", |
19 | | - "3. **Implement retrieval** using embeddings and reranking for accurate information access\n", |
20 | | - "4. **Create RAG-enabled agents** that can search, retrieve, and reason over the knowledge base\n", |
21 | | - "\n", |
22 | | - "By using RAG, agents can provide responses backed by external sources rather than relying solely on their training data, significantly improving accuracy and verifiability of their outputs.\n", |
23 | | - "\n", |
24 | | - "RAG enables agents to access and reason over large knowledge bases,\n" |
25 | | - ] |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Tool Integration Example with Agno\n", |
| 8 | + "\n", |
| 9 | + "This example demonstrates how to integrate and use various tools with Agno agents, showing how AgentOps automatically tracks tool usage and agent interactions.\n", |
| 10 | + "\n", |
| 11 | + "## Overview\n", |
| 12 | + "\n", |
| 13 | + "This example demonstrates:\n", |
| 14 | + "\n", |
| 15 | + "1. **Using built-in Agno tools** like GoogleSearch, DuckDuckGo, and Arxiv\n", |
| 16 | + "2. **Creating agents with tools** and seeing how they use them\n", |
| 17 | + "3. **Tool execution tracking** with AgentOps\n", |
| 18 | + "4. **Combining multiple tools** for comprehensive research\n", |
| 19 | + "\n", |
| 20 | + "## Key Concepts\n", |
| 21 | + "\n", |
| 22 | + "### Tool Integration\n", |
| 23 | + "Agno provides various built-in tools that agents can use to access external information and services. These tools are automatically invoked by agents based on the task at hand.\n", |
| 24 | + "\n", |
| 25 | + "### Available Tools\n", |
| 26 | + "- **GoogleSearchTools**: Search the web using Google\n", |
| 27 | + "- **DuckDuckGoTools**: Alternative web search engine\n", |
| 28 | + "- **ArxivTools**: Search academic papers on arXiv\n", |
| 29 | + "- **HackerNewsTools**: Access HackerNews content\n", |
| 30 | + "- And many more...\n", |
| 31 | + "\n", |
| 32 | + "### Automatic Tool Selection\n", |
| 33 | + "Agents intelligently select which tools to use based on their instructions and the user's query. They can also combine multiple tools to provide comprehensive responses." |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "code", |
| 38 | + "execution_count": null, |
| 39 | + "metadata": {}, |
| 40 | + "outputs": [], |
| 41 | + "source": [ |
| 42 | + "# Install the required dependencies\n", |
| 43 | + "%pip install agentops\n", |
| 44 | + "%pip install agno\n", |
| 45 | + "%pip install python-dotenv" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": null, |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "import os\n", |
| 55 | + "from dotenv import load_dotenv\n", |
| 56 | + "\n", |
| 57 | + "import agentops\n", |
| 58 | + "from agno.agent import Agent\n", |
| 59 | + "from agno.models.openai import OpenAIChat\n", |
| 60 | + "from agno.tools.googlesearch import GoogleSearchTools\n", |
| 61 | + "from agno.tools.duckduckgo import DuckDuckGoTools\n", |
| 62 | + "from agno.tools.arxiv import ArxivTools" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "code", |
| 67 | + "execution_count": null, |
| 68 | + "metadata": {}, |
| 69 | + "outputs": [], |
| 70 | + "source": [ |
| 71 | + "load_dotenv()\n", |
| 72 | + "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\", \"your_openai_api_key_here\")\n", |
| 73 | + "os.environ[\"AGENTOPS_API_KEY\"] = os.getenv(\"AGENTOPS_API_KEY\", \"your_agentops_api_key_here\")" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": null, |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [], |
| 81 | + "source": [ |
| 82 | + "agentops.init(auto_start_session=False, tags=[\"agno-tools\", \"tool-integration\", \"demo\"])" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "markdown", |
| 87 | + "metadata": {}, |
| 88 | + "source": [ |
| 89 | + "## Example 1: Agent with Single Tool\n", |
| 90 | + "\n", |
| 91 | + "Let's start with a simple example where we create an agent that uses Google Search to find information. This demonstrates:\n", |
| 92 | + "- How to create an agent with a specific tool\n", |
| 93 | + "- How the agent automatically uses the tool when needed\n", |
| 94 | + "- How AgentOps tracks the tool execution" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": null, |
| 100 | + "metadata": {}, |
| 101 | + "outputs": [], |
| 102 | + "source": [ |
| 103 | + "# Start an AgentOps trace for tracking\n", |
| 104 | + "tracer = agentops.start_trace(trace_name=\"Example 1: Single Tool Agent\")\n", |
| 105 | + "\n", |
| 106 | + "try:\n", |
| 107 | + " # Create an agent with Google Search tool\n", |
| 108 | + " search_agent = Agent(\n", |
| 109 | + " name=\"Search Agent\",\n", |
| 110 | + " role=\"Research information using Google Search\",\n", |
| 111 | + " model=OpenAIChat(id=\"gpt-4o-mini\"),\n", |
| 112 | + " tools=[GoogleSearchTools()],\n", |
| 113 | + " instructions=\"You are a research assistant. Use Google Search to find accurate, up-to-date information.\"\n", |
| 114 | + " )\n", |
| 115 | + " \n", |
| 116 | + " # Run the agent with a query\n", |
| 117 | + " response = search_agent.run(\"What are the latest developments in AI agents?\")\n", |
| 118 | + " \n", |
| 119 | + " print(\"Search Agent Response:\")\n", |
| 120 | + " print(\"-\" * 60)\n", |
| 121 | + " print(response.content)\n", |
| 122 | + " print(\"-\" * 60)\n", |
| 123 | + " \n", |
| 124 | + " agentops.end_trace(tracer, end_state=\"Success\")\n", |
| 125 | + " \n", |
| 126 | + "except Exception as e:\n", |
| 127 | + " print(f\"An error occurred: {e}\")\n", |
| 128 | + " agentops.end_trace(tracer, end_state=\"Error\")" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "markdown", |
| 133 | + "metadata": {}, |
| 134 | + "source": [ |
| 135 | + "## Example 2: Agent with Multiple Tools\n", |
| 136 | + "\n", |
| 137 | + "Now let's create an agent that has access to multiple tools. The agent will intelligently decide which tool(s) to use based on the query. This demonstrates:\n", |
| 138 | + "- How to provide multiple tools to an agent\n", |
| 139 | + "- How agents select appropriate tools\n", |
| 140 | + "- How to combine information from different sources" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "code", |
| 145 | + "execution_count": null, |
| 146 | + "metadata": {}, |
| 147 | + "outputs": [], |
| 148 | + "source": [ |
| 149 | + "# Start a new trace for Example 2\n", |
| 150 | + "tracer = agentops.start_trace(trace_name=\"Example 2: Multi-Tool Agent\")\n", |
| 151 | + "\n", |
| 152 | + "try:\n", |
| 153 | + " # Create an agent with multiple research tools\n", |
| 154 | + " research_agent = Agent(\n", |
| 155 | + " name=\"Research Agent\",\n", |
| 156 | + " role=\"Comprehensive research using multiple tools\",\n", |
| 157 | + " model=OpenAIChat(id=\"gpt-4o-mini\"),\n", |
| 158 | + " tools=[\n", |
| 159 | + " GoogleSearchTools(),\n", |
| 160 | + " ArxivTools(),\n", |
| 161 | + " DuckDuckGoTools()\n", |
| 162 | + " ],\n", |
| 163 | + " instructions=\"\"\"You are a comprehensive research assistant. \n", |
| 164 | + " Use Google Search for general information, Arxiv for academic papers, \n", |
| 165 | + " and DuckDuckGo as an alternative search engine. \n", |
| 166 | + " Provide well-researched, balanced information from multiple sources.\"\"\"\n", |
| 167 | + " )\n", |
| 168 | + " \n", |
| 169 | + " # Run the agent with a complex query\n", |
| 170 | + " response = research_agent.run(\n", |
| 171 | + " \"Find information about recent advances in tool-use for AI agents. \"\n", |
| 172 | + " \"Include both academic research and practical implementations.\"\n", |
| 173 | + " )\n", |
| 174 | + " \n", |
| 175 | + " print(\"Research Agent Response:\")\n", |
| 176 | + " print(\"-\" * 60)\n", |
| 177 | + " print(response.content)\n", |
| 178 | + " print(\"-\" * 60)\n", |
| 179 | + " \n", |
| 180 | + " agentops.end_trace(tracer, end_state=\"Success\")\n", |
| 181 | + " \n", |
| 182 | + "except Exception as e:\n", |
| 183 | + " print(f\"An error occurred: {e}\")\n", |
| 184 | + " agentops.end_trace(tracer, end_state=\"Error\")" |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "cell_type": "markdown", |
| 189 | + "metadata": {}, |
| 190 | + "source": [ |
| 191 | + "## Example 3: Specialized Tool Usage\n", |
| 192 | + "\n", |
| 193 | + "Let's create an agent specialized in academic research using the Arxiv tool. This demonstrates:\n", |
| 194 | + "- How to create domain-specific agents\n", |
| 195 | + "- How specialized tools provide focused results\n", |
| 196 | + "- The value of tool specialization for specific tasks" |
| 197 | + ] |
| 198 | + }, |
| 199 | + { |
| 200 | + "cell_type": "code", |
| 201 | + "execution_count": null, |
| 202 | + "metadata": {}, |
| 203 | + "outputs": [], |
| 204 | + "source": [ |
| 205 | + "# Start a new trace for Example 3\n", |
| 206 | + "tracer = agentops.start_trace(trace_name=\"Example 3: Academic Research Agent\")\n", |
| 207 | + "\n", |
| 208 | + "try:\n", |
| 209 | + " # Create an agent specialized in academic research\n", |
| 210 | + " academic_agent = Agent(\n", |
| 211 | + " name=\"Academic Agent\",\n", |
| 212 | + " role=\"Find and summarize academic papers\",\n", |
| 213 | + " model=OpenAIChat(id=\"gpt-4o-mini\"),\n", |
| 214 | + " tools=[ArxivTools()],\n", |
| 215 | + " instructions=\"You are an academic research assistant. Use Arxiv to find relevant papers and provide concise summaries.\"\n", |
| 216 | + " )\n", |
| 217 | + " \n", |
| 218 | + " # Run the agent with an academic query\n", |
| 219 | + " response = academic_agent.run(\n", |
| 220 | + " \"Find recent papers about tool augmented language models\"\n", |
| 221 | + " )\n", |
| 222 | + " \n", |
| 223 | + " print(\"Academic Agent Response:\")\n", |
| 224 | + " print(\"-\" * 60)\n", |
| 225 | + " print(response.content)\n", |
| 226 | + " print(\"-\" * 60)\n", |
| 227 | + " \n", |
| 228 | + " agentops.end_trace(tracer, end_state=\"Success\")\n", |
| 229 | + " \n", |
| 230 | + "except Exception as e:\n", |
| 231 | + " print(f\"An error occurred: {e}\")\n", |
| 232 | + " agentops.end_trace(tracer, end_state=\"Error\")" |
| 233 | + ] |
| 234 | + }, |
| 235 | + { |
| 236 | + "cell_type": "markdown", |
| 237 | + "metadata": {}, |
| 238 | + "source": [ |
| 239 | + "## Example 4: Comparing Different Search Tools\n", |
| 240 | + "\n", |
| 241 | + "Finally, let's create an agent that can compare results from different search engines. This demonstrates:\n", |
| 242 | + "- How agents can use multiple tools strategically\n", |
| 243 | + "- How to compare and synthesize information from different sources\n", |
| 244 | + "- The benefits of having alternative data sources" |
| 245 | + ] |
| 246 | + }, |
| 247 | + { |
| 248 | + "cell_type": "code", |
| 249 | + "execution_count": null, |
| 250 | + "metadata": {}, |
| 251 | + "outputs": [], |
| 252 | + "source": [ |
| 253 | + "# Start a new trace for Example 4\n", |
| 254 | + "tracer = agentops.start_trace(trace_name=\"Example 4: Tool Comparison Agent\")\n", |
| 255 | + "\n", |
| 256 | + "try:\n", |
| 257 | + " # Create an agent that compares different search tools\n", |
| 258 | + " comparison_agent = Agent(\n", |
| 259 | + " name=\"Comparison Agent\",\n", |
| 260 | + " role=\"Compare results from different search engines\",\n", |
| 261 | + " model=OpenAIChat(id=\"gpt-4o-mini\"),\n", |
| 262 | + " tools=[\n", |
| 263 | + " GoogleSearchTools(),\n", |
| 264 | + " DuckDuckGoTools()\n", |
| 265 | + " ],\n", |
| 266 | + " instructions=\"\"\"Compare search results from Google and DuckDuckGo. \n", |
| 267 | + " Note any differences in results, ranking, or information quality.\n", |
| 268 | + " Be objective in your comparison.\"\"\"\n", |
| 269 | + " )\n", |
| 270 | + " \n", |
| 271 | + " # Run the agent with a comparison task\n", |
| 272 | + " response = comparison_agent.run(\n", |
| 273 | + " \"Search for 'AgentOps observability platform' on both search engines and compare the results\"\n", |
| 274 | + " )\n", |
| 275 | + " \n", |
| 276 | + " print(\"Comparison Agent Response:\")\n", |
| 277 | + " print(\"-\" * 60)\n", |
| 278 | + " print(response.content)\n", |
| 279 | + " print(\"-\" * 60)\n", |
| 280 | + " \n", |
| 281 | + " agentops.end_trace(tracer, end_state=\"Success\")\n", |
| 282 | + " \n", |
| 283 | + "except Exception as e:\n", |
| 284 | + " print(f\"An error occurred: {e}\")\n", |
| 285 | + " agentops.end_trace(tracer, end_state=\"Error\")" |
| 286 | + ] |
| 287 | + }, |
| 288 | + { |
| 289 | + "cell_type": "markdown", |
| 290 | + "metadata": {}, |
| 291 | + "source": [ |
| 292 | + "## Key Takeaways\n", |
| 293 | + "\n", |
| 294 | + "This notebook demonstrated several important concepts about tool integration in Agno:\n", |
| 295 | + "\n", |
| 296 | + "1. **Automatic Tool Usage**: Agents automatically determine when and how to use their available tools based on the query.\n", |
| 297 | + "\n", |
| 298 | + "2. **Tool Selection**: When given multiple tools, agents intelligently select the most appropriate ones for the task.\n", |
| 299 | + "\n", |
| 300 | + "3. **Information Synthesis**: Agents can combine information from multiple tools to provide comprehensive responses.\n", |
| 301 | + "\n", |
| 302 | + "4. **AgentOps Tracking**: All tool executions are automatically tracked by AgentOps, providing observability into:\n", |
| 303 | + " - Which tools were used\n", |
| 304 | + " - What parameters were passed\n", |
| 305 | + " - Tool execution times\n", |
| 306 | + " - Success/failure rates\n", |
| 307 | + "\n", |
| 308 | + "5. **Specialized vs. General Agents**: You can create both specialized agents (with specific tools) and general-purpose agents (with multiple tools).\n", |
| 309 | + "\n", |
| 310 | + "## Viewing Results in AgentOps\n", |
| 311 | + "\n", |
| 312 | + "After running these examples, you can visit [AgentOps Dashboard](https://app.agentops.ai/) to see:\n", |
| 313 | + "- Detailed traces of each agent execution\n", |
| 314 | + "- Tool usage patterns\n", |
| 315 | + "- Performance metrics\n", |
| 316 | + "- Error tracking and debugging information\n", |
| 317 | + "\n", |
| 318 | + "Each trace URL printed during execution provides direct access to that specific session's details." |
| 319 | + ] |
| 320 | + } |
| 321 | + ], |
| 322 | + "metadata": { |
| 323 | + "kernelspec": { |
| 324 | + "display_name": "Python 3", |
| 325 | + "language": "python", |
| 326 | + "name": "python3" |
| 327 | + }, |
| 328 | + "language_info": { |
| 329 | + "codemirror_mode": { |
| 330 | + "name": "ipython", |
| 331 | + "version": 3 |
| 332 | + }, |
| 333 | + "file_extension": ".py", |
| 334 | + "mimetype": "text/x-python", |
| 335 | + "name": "python", |
| 336 | + "nbconvert_exporter": "python", |
| 337 | + "pygments_lexer": "ipython3", |
| 338 | + "version": "3.9.0" |
| 339 | + } |
26 | 340 | }, |
27 | 341 | { |
28 | 342 | "cell_type": "code", |
|
0 commit comments