168 lines
5.3 KiB
Plaintext
168 lines
5.3 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a35d851b",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"# Async Operations with Agno\n",
|
|
"\n",
|
|
"This notebook demonstrates how to leverage asynchronous programming with Agno agents to execute multiple AI tasks concurrently, significantly improving performance and efficiency.\n",
|
|
"\n",
|
|
"## Overview\n",
|
|
"This notebook demonstrates a practical example of concurrent AI operations where we:\n",
|
|
"\n",
|
|
"1. **Initialize an Agno agent** with OpenAI's GPT-4o-mini model\n",
|
|
"2. **Create multiple async tasks** that query the AI about different programming languages\n",
|
|
"3. **Compare performance** between concurrent and sequential execution\n",
|
|
"\n",
|
|
"By using async operations, you can run multiple AI queries simultaneously instead of waiting for each one to complete sequentially. This is particularly beneficial when dealing with I/O-bound operations like API calls to AI models.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "75767381",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Install the required dependencies:\n",
|
|
"%pip install agentops\n",
|
|
"%pip install agno\n",
|
|
"%pip install python-dotenv"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "fe7d8b83",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import os\n",
|
|
"import asyncio\n",
|
|
"from dotenv import load_dotenv\n",
|
|
"\n",
|
|
"import agentops\n",
|
|
"from agno.agent import Agent\n",
|
|
"from agno.models.openai import OpenAIChat"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "c6653555",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"load_dotenv()\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\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ac01eb8a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"agentops.init(auto_start_session=False, tags=[\"agno-example\", \"async-operation\"])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ca0f1a8a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"async def demonstrate_async_operations():\n",
|
|
" \"\"\"\n",
|
|
" Demonstrate concurrent execution of multiple AI agent tasks.\n",
|
|
"\n",
|
|
" This function creates multiple async tasks that execute concurrently rather than sequentially.\n",
|
|
" Each task makes an independent API call to the AI model, and asyncio.gather()\n",
|
|
" waits for all tasks to complete before returning results.\n",
|
|
"\n",
|
|
" Performance benefit: Instead of 3 sequential calls taking ~90 seconds total,\n",
|
|
" concurrent execution typically completes in ~30 seconds.\n",
|
|
" \"\"\"\n",
|
|
" tracer = agentops.start_trace(\n",
|
|
" trace_name=\"Agno Async Operations Example\",\n",
|
|
" )\n",
|
|
"\n",
|
|
" try:\n",
|
|
" # Initialize AI agent with specified model\n",
|
|
" agent = Agent(model=OpenAIChat(id=\"gpt-4o-mini\"))\n",
|
|
"\n",
|
|
" async def task1():\n",
|
|
" \"\"\"Query AI about Python programming language.\"\"\"\n",
|
|
" response = await agent.arun(\"Explain Python programming language in one paragraph\")\n",
|
|
" return f\"Python: {response.content}\"\n",
|
|
"\n",
|
|
" async def task2():\n",
|
|
" \"\"\"Query AI about JavaScript programming language.\"\"\"\n",
|
|
" response = await agent.arun(\"Explain JavaScript programming language in one paragraph\")\n",
|
|
" return f\"JavaScript: {response.content}\"\n",
|
|
"\n",
|
|
" async def task3():\n",
|
|
" \"\"\"Query AI for comparison between programming languages.\"\"\"\n",
|
|
" response = await agent.arun(\"Compare Python and JavaScript briefly\")\n",
|
|
" return f\"Comparison: {response.content}\"\n",
|
|
"\n",
|
|
" # Execute all tasks concurrently using asyncio.gather()\n",
|
|
" results = await asyncio.gather(task1(), task2(), task3())\n",
|
|
"\n",
|
|
" for i, result in enumerate(results, 1):\n",
|
|
" print(f\"\\nTask {i} Result:\")\n",
|
|
" print(result)\n",
|
|
" print(\"-\" * 50)\n",
|
|
"\n",
|
|
" agentops.end_trace(tracer, end_state=\"Success\")\n",
|
|
"\n",
|
|
" except Exception as e:\n",
|
|
" print(f\"An error occurred: {e}\")\n",
|
|
" agentops.end_trace(tracer, end_state=\"Error\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "0aa21331",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"await demonstrate_async_operations()"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"jupytext": {
|
|
"cell_metadata_filter": "-all",
|
|
"main_language": "python",
|
|
"notebook_metadata_filter": "-all"
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "agentops (3.11.11)",
|
|
"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.11.11"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|