Skip to content

Commit 955f4f9

Browse files
Ethan0456victordibiaCopilot
authored
Add support for specifying the languages to parse from the CodeExecutorAgent response (#6592)
## Why are these changes needed? The `CodeExecutorAgent` can generate code blocks in various programming languages, some of which may not be supported by the executor environment. Adding support for specifying languages to be parsed helps users ignore unnecessary code blocks, preventing potential execution errors. ## Related issue number Closes #6471 ## Checks - [x] I've included any doc changes needed for <https://microsoft.github.io/autogen/>. See <https://github.com/microsoft/autogen/blob/main/CONTRIBUTING.md> to build and test documentation locally. - [x] I've added tests (if relevant) corresponding to the changes introduced in this PR. - [x] I've made sure all auto checks have passed. --------- Signed-off-by: Abhijeetsingh Meena <abhijeet040403@gmail.com> Co-authored-by: Victor Dibia <victordibia@microsoft.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 03394a4 commit 955f4f9

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

python/packages/autogen-agentchat/src/autogen_agentchat/agents/_code_executor_agent.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class CodeExecutorAgentConfig(BaseModel):
5454
system_message: str | None = None
5555
model_client_stream: bool = False
5656
model_context: ComponentModel | None = None
57+
supported_languages: List[str] | None = None
5758

5859

5960
class RetryDecision(BaseModel):
@@ -110,6 +111,8 @@ class CodeExecutorAgent(BaseChatAgent, Component[CodeExecutorAgentConfig]):
110111
This is only used if `model_client` is not provided.
111112
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.
112113
If the code execution fails after this number of retries, the agent will yield a reflection result.
114+
supported_languages (List[str], optional): List of programming languages that will be parsed and executed from agent response;
115+
others will be ignored. Defaults to DEFAULT_SUPPORTED_LANGUAGES.
113116
114117
115118
.. note::
@@ -329,6 +332,7 @@ async def main() -> None:
329332
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."
330333
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."
331334
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)."
335+
DEFAULT_SUPPORTED_LANGUAGES = ["python", "bash"]
332336

333337
component_config_schema = CodeExecutorAgentConfig
334338
component_provider_override = "autogen_agentchat.agents.CodeExecutorAgent"
@@ -345,6 +349,7 @@ def __init__(
345349
description: str | None = None,
346350
system_message: str | None = DEFAULT_SYSTEM_MESSAGE,
347351
sources: Sequence[str] | None = None,
352+
supported_languages: List[str] | None = None,
348353
) -> None:
349354
if description is None:
350355
if model_client is None:
@@ -358,6 +363,13 @@ def __init__(
358363
self._model_client_stream = model_client_stream
359364
self._max_retries_on_error = max_retries_on_error
360365

366+
if supported_languages is not None:
367+
self._supported_languages = supported_languages
368+
else:
369+
self._supported_languages = CodeExecutorAgent.DEFAULT_SUPPORTED_LANGUAGES
370+
371+
self._supported_languages_regex = "|".join(re.escape(lang) for lang in self._supported_languages)
372+
361373
self._model_client = None
362374
if model_client is not None:
363375
self._model_client = model_client
@@ -589,7 +601,7 @@ async def on_reset(self, cancellation_token: CancellationToken) -> None:
589601
pass
590602

591603
def _extract_markdown_code_blocks(self, markdown_text: str) -> List[CodeBlock]:
592-
pattern = re.compile(r"```(?:\s*([\w\+\-]+))?\n([\s\S]*?)```")
604+
pattern = re.compile(rf"```(?:\s*({self._supported_languages_regex}))\n([\s\S]*?)```", re.IGNORECASE)
593605
matches = pattern.findall(markdown_text)
594606
code_blocks: List[CodeBlock] = []
595607
for match in matches:
@@ -612,6 +624,7 @@ def _to_config(self) -> CodeExecutorAgentConfig:
612624
),
613625
model_client_stream=self._model_client_stream,
614626
model_context=self._model_context.dump_component(),
627+
supported_languages=self._supported_languages,
615628
)
616629

617630
@classmethod
@@ -627,6 +640,7 @@ def _from_config(cls, config: CodeExecutorAgentConfig) -> Self:
627640
system_message=config.system_message,
628641
model_client_stream=config.model_client_stream,
629642
model_context=ChatCompletionContext.load_component(config.model_context) if config.model_context else None,
643+
supported_languages=config.supported_languages,
630644
)
631645

632646
@staticmethod

0 commit comments

Comments
 (0)