Python SDK for AI agent monitoring, LLM cost tracking, benchmarking, and more. Integrates with most LLMs and agent frameworks including CrewAI, Agno, OpenAI Agents SDK, Langchain, Autogen, AG2, and CamelAI
Python agentagentopsagents-sdkaianthropicautogencost-estimationcrewaievalsevaluation-metricsgroqlangchainllmmistralollamaopenaiopenai-agents
{
"cells": [
{
"cell_type": "markdown",
"id": "cdf47b93",
"metadata": {},
"source": [
"AG2 Multi-Agent Group Chat Example with AgentOps Integration\n",
"\n",
"This script demonstrates how to orchestrate a group of specialized AI agents collaborating on a task using AG2 and AgentOps.\n",
"\n",
"Overview\n",
"This example shows how to:\n",
"1. Initialize multiple AG2 agents with different roles (researcher, coder, critic, and user proxy)\n",
"2. Set up a group chat where agents interact and collaborate to solve a problem\n",
"3. Simulate a human participant using a user proxy agent\n",
"4. Limit the number of chat rounds and user turns for controlled execution\n",
"5. Track and monitor all agent interactions and LLM calls using AgentOps for full traceability\n",
"\n",
"By using group chat and specialized agents, you can model real-world collaborative workflows, automate complex problem solving, and analyze agent behavior in detail."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "49497415",
"metadata": {},
"outputs": [],
"source": [
"%pip install agentops\n",
"%pip install ag2\n",
"%pip install nest-asyncio"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e8f3321",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import agentops\n",
"import autogen"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "017dd674",
"metadata": {},
"outputs": [],
"source": [
"# Initialize AgentOps for tracing and monitoring\n",
"agentops.init(auto_start_session=False, trace_name=\"AG2 Group Chat\")\n",
"tracer = agentops.start_trace(trace_name=\"AG2 Group Chat\", tags=[\"ag2-group-chat\", \"agentops-example\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d23930de",
"metadata": {},
"outputs": [],
"source": [
"# Configure your AG2 agents with model and API key\n",
"config_list = [\n",
" {\n",
" \"model\": \"gpt-4\",\n",
" \"api_key\": os.getenv(\"OPENAI_API_KEY\"),\n",
" }\n",
"]\n",
"llm_config = {\n",
" \"config_list\": config_list,\n",
" \"timeout\": 60,\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eeee05a1",
"metadata": {},
"outputs": [],
"source": [
"# Create a team of agents with specialized roles\n",
"researcher = autogen.AssistantAgent(\n",
" name=\"researcher\",\n",
" llm_config=llm_config,\n",
" system_message=\"You are a researcher who specializes in finding accurate information.\",\n",
")\n",
"coder = autogen.AssistantAgent(\n",
" name=\"coder\", llm_config=llm_config, system_message=\"You are an expert programmer who writes clean, efficient code.\"\n",
")\n",
"critic = autogen.AssistantAgent(\n",
" name=\"critic\", llm_config=llm_config, system_message=\"You review solutions and provide constructive feedback.\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "abd6720b",
"metadata": {},
"outputs": [],
"source": [
"# The user proxy agent simulates a human participant in the chat\n",
"user_proxy = autogen.UserProxyAgent(\n",
" name=\"user_proxy\",\n",
" human_input_mode=\"TERMINATE\", # Stops when a message ends with 'TERMINATE'\n",
" max_consecutive_auto_reply=10, # Limits auto-replies before requiring termination\n",
" is_termination_msg=lambda x: x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE\"),\n",
" code_execution_config={\"last_n_messages\": 3, \"work_dir\": \"coding\"},\n",
")\n",
"# Create a group chat with all agents and set a maximum number of rounds\n",
"groupchat = autogen.GroupChat(\n",
" agents=[user_proxy, researcher, coder, critic],\n",
" messages=[],\n",
" max_round=4, # Limits the total number of chat rounds\n",
")\n",
"# The manager coordinates the group chat and LLM configuration\n",
"manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)\n",
"# Start the group chat with an initial task and a maximum number of user turns\n",
"user_proxy.initiate_chat(\n",
" manager,\n",
" message=\"Create a Python program to analyze sentiment from Twitter data.\",\n",
" max_turns=2, # Limits the number of user turns\n",
")\n",
"agentops.end_trace(tracer, end_state=\"Success\")"
]
}
],
"metadata": {
"jupytext": {
"cell_metadata_filter": "-all",
"main_language": "python",
"notebook_metadata_filter": "-all"
},
"kernelspec": {
"display_name": ".venv",
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}