Skip to content

A programming framework for agentic AI

Add support for specifying the languages to parse from the `CodeExecutorAgent` response (#6592)

Abhijeetsingh Meena955f4f9b9f8d
diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_code_executor_agent.py b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_code_executor_agent.py
index 2efa6d5a7..4dc3c3639 100644
--- a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_code_executor_agent.py
+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_code_executor_agent.py
@@ -54,6 +54,7 @@ class CodeExecutorAgentConfig(BaseModel):
system_message: str | None = None
model_client_stream: bool = False
model_context: ComponentModel | None = None
+ supported_languages: List[str] | None = None
class RetryDecision(BaseModel):
@@ -110,6 +111,8 @@ class CodeExecutorAgent(BaseChatAgent, Component[CodeExecutorAgentConfig]):
This is only used if `model_client` is not provided.
max_retries_on_error (int, optional): The maximum number of retries on error. If the code execution fails, the agent will retry up to this number of times.
If the code execution fails after this number of retries, the agent will yield a reflection result.
+ supported_languages (List[str], optional): List of programming languages that will be parsed and executed from agent response;
+ others will be ignored. Defaults to DEFAULT_SUPPORTED_LANGUAGES.
.. note::
@@ -329,6 +332,7 @@ class CodeExecutorAgent(BaseChatAgent, Component[CodeExecutorAgentConfig]):
DEFAULT_AGENT_DESCRIPTION = "A Code Execution Agent that generates and executes Python and shell scripts based on user instructions. It ensures correctness, efficiency, and minimal errors while gracefully handling edge cases."
DEFAULT_SYSTEM_MESSAGE = "You are a Code Execution Agent. Your role is to generate and execute Python code and shell scripts based on user instructions, ensuring correctness, efficiency, and minimal errors. Handle edge cases gracefully. Python code should be provided in ```python code blocks, and sh shell scripts should be provided in ```sh code blocks for execution."
NO_CODE_BLOCKS_FOUND_MESSAGE = "No code blocks found in the thread. Please provide at least one markdown-encoded code block to execute (i.e., quoting code in ```python or ```sh code blocks)."
+ DEFAULT_SUPPORTED_LANGUAGES = ["python", "bash"]
component_config_schema = CodeExecutorAgentConfig
component_provider_override = "autogen_agentchat.agents.CodeExecutorAgent"
@@ -345,6 +349,7 @@ class CodeExecutorAgent(BaseChatAgent, Component[CodeExecutorAgentConfig]):
description: str | None = None,
system_message: str | None = DEFAULT_SYSTEM_MESSAGE,
sources: Sequence[str] | None = None,
+ supported_languages: List[str] | None = None,
) -> None:
if description is None:
if model_client is None:
@@ -358,6 +363,13 @@ class CodeExecutorAgent(BaseChatAgent, Component[CodeExecutorAgentConfig]):
self._model_client_stream = model_client_stream
self._max_retries_on_error = max_retries_on_error
+ if supported_languages is not None:
+ self._supported_languages = supported_languages
+ else:
+ self._supported_languages = CodeExecutorAgent.DEFAULT_SUPPORTED_LANGUAGES
+
+ self._supported_languages_regex = "|".join(re.escape(lang) for lang in self._supported_languages)
+
self._model_client = None
if model_client is not None:
self._model_client = model_client
@@ -589,7 +601,7 @@ class CodeExecutorAgent(BaseChatAgent, Component[CodeExecutorAgentConfig]):
pass
def _extract_markdown_code_blocks(self, markdown_text: str) -> List[CodeBlock]:
- pattern = re.compile(r"```(?:\s*([\w\+\-]+))?\n([\s\S]*?)```")
+ pattern = re.compile(rf"```(?:\s*({self._supported_languages_regex}))\n([\s\S]*?)```", re.IGNORECASE)
matches = pattern.findall(markdown_text)
code_blocks: List[CodeBlock] = []
for match in matches:
@@ -612,6 +624,7 @@ class CodeExecutorAgent(BaseChatAgent, Component[CodeExecutorAgentConfig]):
),
model_client_stream=self._model_client_stream,
model_context=self._model_context.dump_component(),
+ supported_languages=self._supported_languages,
)
@classmethod
@@ -627,6 +640,7 @@ class CodeExecutorAgent(BaseChatAgent, Component[CodeExecutorAgentConfig]):
system_message=config.system_message,
model_client_stream=config.model_client_stream,
model_context=ChatCompletionContext.load_component(config.model_context) if config.model_context else None,
+ supported_languages=config.supported_languages,
)
@staticmethod