بنقرة واحدة
add-code-execution
// Add code execution capability to AG2 agents using LocalCommandLineCodeExecutor or Docker. Use when the user wants agents that can write and run Python code.
// Add code execution capability to AG2 agents using LocalCommandLineCodeExecutor or Docker. Use when the user wants agents that can write and run Python code.
| name | add-code-execution |
| description | Add code execution capability to AG2 agents using LocalCommandLineCodeExecutor or Docker. Use when the user wants agents that can write and run Python code. |
You are an expert at setting up code execution in AG2 agent workflows. When the user wants agents that can write and run code:
Ask the user:
import os
from autogen import ConversableAgent, LLMConfig
from autogen.coding import LocalCommandLineCodeExecutor
llm_config = LLMConfig(
{"model": "gpt-4o-mini", "api_key": os.environ["OPENAI_API_KEY"]}
)
# Coder writes code
coder = ConversableAgent(
name="coder",
system_message="""You are an expert Python developer.
Write code to solve the user's task. Always put code in ```python blocks.
When the task is complete, reply with TERMINATE.""",
llm_config=llm_config,
human_input_mode="NEVER",
)
# Executor runs code and returns output
executor = ConversableAgent(
name="executor",
llm_config=False,
human_input_mode="NEVER",
code_execution_config={
"executor": LocalCommandLineCodeExecutor(
work_dir="./coding_output",
timeout=60,
),
},
is_termination_msg=lambda msg: "TERMINATE" in msg.get("content", ""),
max_consecutive_auto_reply=10,
)
result = await executor.a_run(
coder,
message="Create a script that fetches the top 10 Python packages from PyPI and saves them to a CSV.",
)
await result.process()
from autogen.coding import DockerCommandLineCodeExecutor
executor = ConversableAgent(
name="executor",
llm_config=False,
human_input_mode="NEVER",
code_execution_config={
"executor": DockerCommandLineCodeExecutor(
image="python:3.11-slim",
work_dir="./coding_output",
timeout=120,
),
},
)
For workflows where a human should review code before running:
executor = ConversableAgent(
name="executor",
llm_config=False,
human_input_mode="ALWAYS", # Asks for approval before each execution
code_execution_config={
"executor": LocalCommandLineCodeExecutor(work_dir="./output"),
},
)
from autogen.agentchat import run_group_chat
from autogen.agentchat.group.patterns import RoundRobinPattern
planner = ConversableAgent(
name="planner",
system_message="You break tasks into coding steps.",
llm_config=llm_config,
human_input_mode="NEVER",
description="Plans the coding approach.",
)
coder = ConversableAgent(
name="coder",
system_message="You write Python code based on the plan. Put code in ```python blocks.",
llm_config=llm_config,
human_input_mode="NEVER",
description="Writes Python code.",
)
executor = ConversableAgent(
name="executor",
llm_config=False,
human_input_mode="NEVER",
code_execution_config={
"executor": LocalCommandLineCodeExecutor(work_dir="./output", timeout=60),
},
description="Runs Python code and returns results.",
)
user = ConversableAgent(name="user", llm_config=False, human_input_mode="NEVER")
result = run_group_chat(
pattern=RoundRobinPattern(
initial_agent=planner,
agents=[planner, coder, executor],
user_agent=user,
),
messages="Analyze the iris dataset and create a classification model.",
max_rounds=12,
)
DockerCommandLineCodeExecutor for untrusted code or productiontimeout to prevent infinite executionmax_consecutive_auto_reply to limit retry loopswork_dir to isolate output fileshuman_input_mode="ALWAYS" during development for safetyLocalCommandLineCodeExecutor (not Commandline)Add safety guardrails to AG2 agents using LLMGuardrail and RegexGuardrail. Use when the user wants to enforce safety constraints, filter PII, or redirect off-topic responses.
Wire a Model Context Protocol (MCP) server into AG2 agents using create_toolkit. Use when the user wants to connect external tools via MCP.
Build an AG2 handoff-driven workflow with DefaultPattern, agent handoffs, context variables, and routing. Use when the user wants customer service routing, state-machine workflows, or explicit agent-to-agent transitions.
Build a Retrieval-Augmented Generation (RAG) agent using AG2's RetrieveUserProxyAgent with vector database support. Use when the user wants agents that can query documents or knowledge bases.
Build an AG2 ReasoningAgent that uses tree-of-thought reasoning with beam search, MCTS, or LATS strategies. Use when the user needs advanced reasoning for complex problem solving.
Build a complete web research agent team using AG2 with search tools, web crawling, and structured output. Use when the user wants a practical research or information-gathering workflow.