Skip to content

feat: update local code executor to support powershell#5884

Merged
ekzhu merged 12 commits intomicrosoft:mainfrom
lspinheiro:feat-5518-support-powershell-scripts
Mar 10, 2025
Merged

feat: update local code executor to support powershell#5884
ekzhu merged 12 commits intomicrosoft:mainfrom
lspinheiro:feat-5518-support-powershell-scripts

Conversation

@lspinheiro
Copy link
Copy Markdown
Collaborator

Why are these changes needed?

To support powershell on the local code executor.

Related issue number

Closes #5518

Checks

@lspinheiro lspinheiro requested a review from ekzhu March 9, 2025 06:03
@lspinheiro
Copy link
Copy Markdown
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
Copy link
Copy Markdown

codecov bot commented Mar 9, 2025

Codecov Report

Attention: Patch coverage is 77.77778% with 4 lines in your changes missing coverage. Please review.

Project coverage is 75.64%. Comparing base (7d17b22) to head (efc9963).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...ogen-ext/src/autogen_ext/code_executors/_common.py 0.00% 2 Missing ⚠️
...t/src/autogen_ext/code_executors/local/__init__.py 87.50% 2 Missing ⚠️
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     
Flag Coverage Δ
unittests 75.64% <77.77%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ekzhu ekzhu merged commit a1858ef into microsoft:main Mar 10, 2025
56 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LocalCommandLineCodeExecutor to support PowerShell

3 participants