Demonstrate concurrent execution of multiple AI agent tasks. This function creates multiple async tasks that execute concurrently rather than sequentially. Each task makes an independent API call to the AI model, and asyncio.gather() waits for all tasks to complete before returning
()
| 30 | |
| 31 | |
| 32 | async def demonstrate_async_operations(): |
| 33 | """ |
| 34 | Demonstrate concurrent execution of multiple AI agent tasks. |
| 35 | |
| 36 | This function creates multiple async tasks that execute concurrently rather than sequentially. |
| 37 | Each task makes an independent API call to the AI model, and asyncio.gather() |
| 38 | waits for all tasks to complete before returning results. |
| 39 | |
| 40 | Performance benefit: Instead of 3 sequential calls taking ~90 seconds total, |
| 41 | concurrent execution typically completes in ~30 seconds. |
| 42 | """ |
| 43 | tracer = agentops.start_trace(trace_name="Agno Async Operations Example") |
| 44 | |
| 45 | try: |
| 46 | # Initialize AI agent with specified model |
| 47 | agent = Agent(model=OpenAIChat(id="gpt-4o-mini")) |
| 48 | |
| 49 | async def task1(): |
| 50 | """Query AI about Python programming language.""" |
| 51 | response = await agent.arun("Explain Python programming language in one paragraph") |
| 52 | return f"Python: {response.content}" |
| 53 | |
| 54 | async def task2(): |
| 55 | """Query AI about JavaScript programming language.""" |
| 56 | response = await agent.arun("Explain JavaScript programming language in one paragraph") |
| 57 | return f"JavaScript: {response.content}" |
| 58 | |
| 59 | async def task3(): |
| 60 | """Query AI for comparison between programming languages.""" |
| 61 | response = await agent.arun("Compare Python and JavaScript briefly") |
| 62 | return f"Comparison: {response.content}" |
| 63 | |
| 64 | # Execute all tasks concurrently using asyncio.gather() |
| 65 | results = await asyncio.gather(task1(), task2(), task3()) |
| 66 | |
| 67 | for i, result in enumerate(results, 1): |
| 68 | print(f"\nTask {i} Result:") |
| 69 | print(result) |
| 70 | print("-" * 50) |
| 71 | |
| 72 | agentops.end_trace(tracer, end_state="Success") |
| 73 | |
| 74 | except Exception as e: |
| 75 | print(f"An error occurred: {e}") |
| 76 | agentops.end_trace(tracer, end_state="Error") |
| 77 | |
| 78 | # Let's check programmatically that spans were recorded in AgentOps |
| 79 | print("\n" + "=" * 50) |
| 80 | print("Now let's verify that our LLM calls were tracked properly...") |
| 81 | try: |
| 82 | agentops.validate_trace_spans(trace_context=tracer) |
| 83 | print("\n✅ Success! All LLM spans were properly recorded in AgentOps.") |
| 84 | except agentops.ValidationError as e: |
| 85 | print(f"\n❌ Error validating spans: {e}") |
| 86 | raise |
| 87 | |
| 88 | |
| 89 | if __name__ == "__main__": |
no test coverage detected
searching dependent graphs…