|
| 1 | +import asyncio |
| 2 | +from ai21 import AsyncAI21Client |
| 3 | +from ai21.models.agents import BudgetLevel, AgentType |
| 4 | + |
| 5 | +client = AsyncAI21Client() |
| 6 | + |
| 7 | + |
| 8 | +async def main(): |
| 9 | + """Example demonstrating async Agent operations with enhanced options""" |
| 10 | + |
| 11 | + # Create an agent |
| 12 | + print("Creating an agent...") |
| 13 | + agent = await client.beta.agents.create( |
| 14 | + name="Research Assistant", |
| 15 | + description="An AI assistant that can help with research and analysis", |
| 16 | + budget=BudgetLevel.MEDIUM, |
| 17 | + ) |
| 18 | + |
| 19 | + print(f"Created agent: {agent.name} (ID: {agent.id})") |
| 20 | + agent_id = agent.id |
| 21 | + |
| 22 | + try: |
| 23 | + # Run the agent with enhanced options |
| 24 | + print("\nRunning agent with research question...") |
| 25 | + input_messages = [{"role": "user", "content": "Explain the key benefits of renewable energy"}] |
| 26 | + |
| 27 | + run_response = await client.beta.agents.runs.create_and_poll( |
| 28 | + agent_id=agent_id, |
| 29 | + input=input_messages, |
| 30 | + verbose=True, |
| 31 | + include=["data_sources", "requirements_result"], |
| 32 | + response_language="english", |
| 33 | + poll_timeout_sec=180, # 3 minutes timeout |
| 34 | + ) |
| 35 | + |
| 36 | + print(f"Run ID: {run_response.id}") |
| 37 | + print(f"Run status: {run_response.status}") |
| 38 | + |
| 39 | + if run_response.status == "completed": |
| 40 | + print("Run completed successfully!") |
| 41 | + if run_response.result: |
| 42 | + print(f"Result: {run_response.result}") |
| 43 | + |
| 44 | + # Show additional information if available |
| 45 | + if hasattr(run_response, "data_sources") and run_response.data_sources: |
| 46 | + print(f"Data sources used: {len(run_response.data_sources)}") |
| 47 | + |
| 48 | + if hasattr(run_response, "requirements_result") and run_response.requirements_result: |
| 49 | + print(f"Requirements result: {run_response.requirements_result}") |
| 50 | + else: |
| 51 | + print(f"Run failed with status: {run_response.status}") |
| 52 | + |
| 53 | + # Demonstrate multiple runs concurrently |
| 54 | + print("\nRunning multiple questions concurrently...") |
| 55 | + questions = [ |
| 56 | + "What is photosynthesis?", |
| 57 | + "How do solar panels work?", |
| 58 | + "What are the main types of renewable energy?", |
| 59 | + ] |
| 60 | + |
| 61 | + tasks = [] |
| 62 | + for i, question in enumerate(questions): |
| 63 | + input_msgs = [{"role": "user", "content": question}] |
| 64 | + task = client.beta.agents.runs.create_and_poll( |
| 65 | + agent_id=agent_id, |
| 66 | + input=input_msgs, |
| 67 | + poll_timeout_sec=120, |
| 68 | + ) |
| 69 | + tasks.append(task) |
| 70 | + |
| 71 | + # Wait for all runs to complete |
| 72 | + results = await asyncio.gather(*tasks, return_exceptions=True) |
| 73 | + |
| 74 | + for i, result in enumerate(results): |
| 75 | + if isinstance(result, Exception): |
| 76 | + print(f"Question {i+1} failed: {result}") |
| 77 | + else: |
| 78 | + print(f"Question {i+1} status: {result.status}") |
| 79 | + |
| 80 | + except Exception as e: |
| 81 | + print(f"Error during agent operations: {e}") |
| 82 | + finally: |
| 83 | + # Clean up - delete the agent |
| 84 | + print(f"\nCleaning up - deleting agent {agent_id}...") |
| 85 | + await client.beta.agents.delete(agent_id) |
| 86 | + print("Agent deleted successfully") |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + asyncio.run(main()) |
0 commit comments