feat: update local code executor to support powershell#5884
Merged
ekzhu merged 12 commits intomicrosoft:mainfrom Mar 10, 2025
Merged
feat: update local code executor to support powershell#5884ekzhu merged 12 commits intomicrosoft:mainfrom
ekzhu merged 12 commits intomicrosoft:mainfrom
Conversation
Collaborator
Author
|
There is a new unit test, but should be skipped in CI. I tested it on the following script ( a variant of the one posted in the issue) import asyncio
from autogen_ext.models.openai import AzureOpenAIChatCompletionClient
from autogen_agentchat.agents import AssistantAgent, CodeExecutorAgent
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_ext.code_executors.local import LocalCommandLineCodeExecutor
import os
async def main() -> None:
# Set up the model client to use the local Ollama model "llama3.2:3b-instruct-fp16"
model_client = AzureOpenAIChatCompletionClient(
model="gpt-4o",
api_key=...,
azure_deployment="gpt-4o",
azure_endpoint=...,
api_version=...
model_info={
"vision": False,
"function_calling": True,
"json_output": True,
"family": "unknown",
},
)
# Create a local code executor that will execute code on the local machine.
# Note: There is no need to call start() or stop() on LocalCommandLineCodeExecutor.
code_executor = LocalCommandLineCodeExecutor(work_dir="coding")
# Create a CodeExecutorAgent that executes local code blocks.
code_agent = CodeExecutorAgent("CodeExecutor", code_executor=code_executor)
# Create an AssistantAgent specialized for Windows 11 settings changes.
# The system message instructs the agent to generate safe code (Python or shell)
# that will, for example, enable dark mode in Windows 11.
assistant_agent = AssistantAgent(
"WindowsAdmin",
model_client=model_client,
system_message=(
"You are a Windows 11 system administrator agent. Your task is to generate code that modifies Windows 11 settings using local code execution as a powershell script. Provide only the code, no other text or comments formatted in markdown. The code should be formatted in a way that is easy to copy and paste into a powershell script."
),
)
# Assemble the team using RoundRobinGroupChat to act as a round-robin orchestrator.
# Here, max_turns=5 allows for up to 5 rounds of interactions between agents.
team = RoundRobinGroupChat([assistant_agent, code_agent], max_turns=5)
# Define the specialized task: change a Windows 11 setting (e.g., enable dark mode).
task = "Write a powershell script that calculates the sum of all numbers from 1 to 100."
# Run the team using streaming output via the Console helper.
result = await Console(team.run_stream(task=task))
print(result)
if __name__ == "__main__":
asyncio.run(main())PS C:\Users\lpinheiro\Github\autogen\python\dev> python .\src\test_ps1_code_executor.py
---------- user ----------
Write a powershell script that calculates the sum of all numbers from 1 to 100.
---------- WindowsAdmin ----------
'''powershell
$sum = 0
for ($i = 1; $i -le 100; $i++) {
$sum += $i
}
Write-Output "The sum of all numbers from 1 to 100 is $sum"
'''
---------- CodeExecutor ----------
The sum of all numbers from 1 to 100 is 5050
---------- WindowsAdmin ----------
---------- CodeExecutor ----------
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).
---------- WindowsAdmin ----------
'''powershell
$sum = 0
for ($i = 1; $i -le 100; $i++) {
$sum += $i
}
'''
Write-Output "The sum of all numbers from 1 to 100 is $sum"
TaskResult(messages=[TextMessage(source='user', models_usage=None, metadata={}, content='Write a powershell script that calculates the sum of all numbers from 1 to 100.', type='TextMessage'), TextMessage(source='WindowsAdmin', models_usage=RequestUsage(prompt_tokens=98, completion_tokens=56), metadata={}, content='```powershell\n$sum = 0\nfor ($i = 1; $i -le 100; $i++) {\n $sum += $i\n}\nWrite-Output "The sum of all numbers from 1 to 100 is $sum"\n```', type='TextMessage'), TextMessage(source='CodeExecutor', models_usage=None, metadata={}, content='The sum of all numbers from 1 to 100 is 5050\r\n', type='TextMessage'), TextMessage(source='WindowsAdmin', models_usage=RequestUsage(prompt_tokens=184, completion_tokens=0), metadata={}, content='', type='TextMessage'), TextMessage(source='CodeExecutor', models_usage=None, metadata={}, content='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).', type='TextMessage'), TextMessage(source='WindowsAdmin', models_usage=RequestUsage(prompt_tokens=233, completion_tokens=56), metadata={}, content='```powershell\n$sum = 0\nfor ($i = 1; $i -le 100; $i++) {\n $sum += $i\n}\nWrite-Output "The sum of all numbers from 1 to 100 is $sum"\n```', type='TextMessage')], stop_reason='Maximum number of turns 5 reached.') |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5884 +/- ##
==========================================
- Coverage 75.65% 75.64% -0.02%
==========================================
Files 189 189
Lines 12784 12794 +10
==========================================
+ Hits 9672 9678 +6
- Misses 3112 3116 +4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
ekzhu
approved these changes
Mar 10, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why are these changes needed?
To support powershell on the local code executor.
Related issue number
Closes #5518
Checks