-
Notifications
You must be signed in to change notification settings - Fork 1.2k
.NET: Support hosted code interpreter for skill script execution #4192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SergeyMenshykh
wants to merge
12
commits into
microsoft:main
Choose a base branch
from
SergeyMenshykh:script-execution-by-code-interpreter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+701
−65
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
361fcaf
support script execution by code interpretor
SergeyMenshykh fe25aef
improve the instruction prompt
SergeyMenshykh ddc85c9
Merge branch 'main' into script-execution-by-code-interpreter
SergeyMenshykh c67746f
Add DefaultAzureCredential production warning to AgentSkills samples
SergeyMenshykh fad82f1
address pr review comments
SergeyMenshykh 1b7092f
Merge branch 'main' into script-execution-by-code-interpreter
SergeyMenshykh eb16b1a
address feedback
SergeyMenshykh ac61a46
rename Skill* types to FileAgentSkill* prefix for consistency
SergeyMenshykh f73dd58
reorder usings
SergeyMenshykh a4ca6e2
use set for props initialization instead of init
SergeyMenshykh 2a93a0e
rename HostedCodeInterpreterSkillScriptExecutor
SergeyMenshykh c377733
Merge branch 'main' into script-execution-by-code-interpreter
SergeyMenshykh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
28 changes: 28 additions & 0 deletions
28
...ScriptExecutionWithCodeInterpreter/Agent_Step02_ScriptExecutionWithCodeInterpreter.csproj
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFrameworks>net10.0</TargetFrameworks> | ||
|
|
||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <NoWarn>$(NoWarn);MAAI001</NoWarn> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Azure.AI.OpenAI" /> | ||
| <PackageReference Include="Azure.Identity" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.OpenAI\Microsoft.Agents.AI.OpenAI.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <!-- Copy skills directory to output --> | ||
| <ItemGroup> | ||
| <None Include="skills\**\*.*"> | ||
| <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
49 changes: 49 additions & 0 deletions
49
.../samples/02-agents/AgentSkills/Agent_Step02_ScriptExecutionWithCodeInterpreter/Program.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| // This sample demonstrates how to use Agent Skills with script execution via the hosted code interpreter. | ||
| // When FileAgentSkillScriptExecutor.HostedCodeInterpreter() is configured, the agent can load and execute scripts | ||
| // from skill resources using the LLM provider's built-in code interpreter. | ||
| // | ||
| // This sample includes the password-generator skill: | ||
| // - A Python script for generating secure passwords | ||
|
|
||
| using Azure.AI.OpenAI; | ||
| using Azure.Identity; | ||
| using Microsoft.Agents.AI; | ||
| using OpenAI.Responses; | ||
|
|
||
| // --- Configuration --- | ||
| string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") | ||
| ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); | ||
| string deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; | ||
|
|
||
| // --- Skills Provider with Script Execution --- | ||
| // Discovers skills and enables script execution via the hosted code interpreter | ||
| var skillsProvider = new FileAgentSkillsProvider( | ||
| skillPath: Path.Combine(AppContext.BaseDirectory, "skills"), | ||
| options: new FileAgentSkillsProviderOptions | ||
| { | ||
| ScriptExecutor = FileAgentSkillScriptExecutor.HostedCodeInterpreter() | ||
| }); | ||
|
|
||
| // --- Agent Setup --- | ||
| // WARNING: DefaultAzureCredential is convenient for development but requires careful consideration in production. | ||
| // In production, consider using a specific credential (e.g., ManagedIdentityCredential) to avoid | ||
| // latency issues, unintended credential probing, and potential security risks from fallback mechanisms. | ||
| AIAgent agent = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential()) | ||
| .GetResponsesClient(deploymentName) | ||
| .AsAIAgent(new ChatClientAgentOptions | ||
| { | ||
| Name = "SkillsAgent", | ||
| ChatOptions = new() | ||
| { | ||
| Instructions = "You are a helpful assistant that can generate secure passwords.", | ||
| }, | ||
| AIContextProviders = [skillsProvider], | ||
| }); | ||
|
|
||
| // --- Example: Password generation with script execution --- | ||
| Console.WriteLine("Example: Generating a password with a skill script"); | ||
| Console.WriteLine("---------------------------------------------------"); | ||
| AgentResponse response = await agent.RunAsync("Generate a secure password for my database account."); | ||
| Console.WriteLine($"Agent: {response.Text}\n"); | ||
72 changes: 72 additions & 0 deletions
72
...02-agents/AgentSkills/Agent_Step02_ScriptExecutionWithCodeInterpreter/README.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # Script Execution with Code Interpreter | ||
|
|
||
| This sample demonstrates how to use **Agent Skills** with **script execution** via the hosted code interpreter. | ||
|
|
||
| ## What's Different from Step01? | ||
|
|
||
| In the [basic skills sample](../Agent_Step01_BasicSkills/), skills only provide instructions and resources as text. This sample adds **script execution** — the agent can load Python scripts from skill resources and execute them using the LLM provider's built-in code interpreter. | ||
|
|
||
| This is enabled by configuring `FileAgentSkillScriptExecutor.HostedCodeInterpreter()` on the skills provider options: | ||
|
|
||
| ```csharp | ||
| var skillsProvider = new FileAgentSkillsProvider( | ||
| skillPath: Path.Combine(AppContext.BaseDirectory, "skills"), | ||
| options: new FileAgentSkillsProviderOptions | ||
| { | ||
| ScriptExecutor = FileAgentSkillScriptExecutor.HostedCodeInterpreter() | ||
| }); | ||
| ``` | ||
|
|
||
| ## Skills Included | ||
|
|
||
| ### password-generator | ||
| Generates secure passwords using a Python script with configurable length and complexity. | ||
| - `scripts/generate.py` — Password generation script | ||
| - `references/PASSWORD_GUIDELINES.md` — Recommended length and symbol sets by use case | ||
|
|
||
| ## Project Structure | ||
|
|
||
| ``` | ||
| Agent_Step02_ScriptExecutionWithCodeInterpreter/ | ||
| ├── Program.cs | ||
| ├── Agent_Step02_ScriptExecutionWithCodeInterpreter.csproj | ||
| └── skills/ | ||
| └── password-generator/ | ||
| ├── SKILL.md | ||
| ├── scripts/ | ||
| │ └── generate.py | ||
| └── references/ | ||
| └── PASSWORD_GUIDELINES.md | ||
| ``` | ||
|
|
||
| ## Running the Sample | ||
|
|
||
| ### Prerequisites | ||
| - .NET 10.0 SDK | ||
| - Azure OpenAI endpoint with a deployed model that supports code interpreter | ||
|
|
||
| ### Setup | ||
| 1. Set environment variables: | ||
| ```bash | ||
| export AZURE_OPENAI_ENDPOINT="https://your-endpoint.openai.azure.com/" | ||
| export AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4o-mini" | ||
| ``` | ||
|
|
||
| 2. Run the sample: | ||
| ```bash | ||
| dotnet run | ||
| ``` | ||
|
|
||
| ### Example | ||
|
|
||
| The sample asks the agent to generate a secure password. The agent: | ||
| 1. Loads the password-generator skill | ||
| 2. Reads the `generate.py` script via `read_skill_resource` | ||
| 3. Executes the script using the code interpreter with appropriate parameters | ||
| 4. Returns the generated password | ||
|
|
||
| ## Learn More | ||
|
|
||
| - [Agent Skills Specification](https://agentskills.io/) | ||
| - [Step01: Basic Skills](../Agent_Step01_BasicSkills/) — Skills without script execution | ||
| - [Microsoft Agent Framework Documentation](../../../../../docs/) |
16 changes: 16 additions & 0 deletions
16
...nt_Step02_ScriptExecutionWithCodeInterpreter/skills/password-generator/SKILL.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| --- | ||
| name: password-generator | ||
| description: Generate secure passwords using a Python script. Use when asked to create passwords or credentials. | ||
| --- | ||
|
|
||
| # Password Generator | ||
|
|
||
| This skill generates secure passwords using a Python script. | ||
|
|
||
| ## Usage | ||
|
|
||
| When the user requests a password: | ||
| 1. First, review `references/PASSWORD_GUIDELINES.md` to determine the recommended password length and character sets for the user's use case | ||
| 2. Load `scripts/generate.py` and adjust its parameters (length, character set) based on the guidelines and user's requirements | ||
| 3. Execute the script | ||
| 4. Present the generated password clearly |
24 changes: 24 additions & 0 deletions
24
...WithCodeInterpreter/skills/password-generator/references/PASSWORD_GUIDELINES.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Password Generation Guidelines | ||
SergeyMenshykh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## General Rules | ||
|
|
||
| - Never reuse passwords across services. | ||
| - Always use cryptographically secure randomness (e.g., `random.SystemRandom()`). | ||
| - Avoid dictionary words, keyboard patterns, and personal information. | ||
|
|
||
| ## Recommended Settings by Use Case | ||
|
|
||
| | Use Case | Min Length | Character Set | Example | | ||
| |-----------------------|-----------|----------------------------------------|--------------------------| | ||
| | Web account | 16 | Upper + lower + digits + symbols | `G7!kQp@2xM#nW9$z` | | ||
| | Database credential | 24 | Upper + lower + digits + symbols | `aR3$vK8!mN2@pQ7&xL5#wY` | | ||
| | Wi-Fi / network key | 20 | Upper + lower + digits + symbols | `Ht4&jL9!rP2#mK7@xQ` | | ||
| | API key / token | 32 | Upper + lower + digits (no symbols) | `k8Rm3xQ7nW2pL9vT4jH6yA` | | ||
| | Encryption passphrase | 32 | Upper + lower + digits + symbols | `Xp4!kR8@mN2#vQ7&jL9$wT` | | ||
|
|
||
| ## Symbol Sets | ||
|
|
||
| - **Standard symbols**: `!@#$%^&*()-_=+` | ||
| - **Extended symbols**: `~`{}[]|;:'",.<>?/\` | ||
| - **Safe symbols** (URL/shell-safe): `!@#$&*-_=+` | ||
| - If the target system restricts symbols, use only the **safe** set. | ||
11 changes: 11 additions & 0 deletions
11
...t_Step02_ScriptExecutionWithCodeInterpreter/skills/password-generator/scripts/generate.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Password generator script | ||
| # Usage: Adjust 'length' as needed, then run | ||
|
|
||
| import random | ||
| import string | ||
|
|
||
| length = 16 # desired length | ||
|
|
||
| pool = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation | ||
| password = "".join(random.SystemRandom().choice(pool) for _ in range(length)) | ||
| print(f"Generated password ({length} chars): {password}") |
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
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
9 changes: 6 additions & 3 deletions
9
...soft.Agents.AI/Skills/SkillFrontmatter.cs → ...ts.AI/Skills/FileAgentSkillFrontmatter.cs
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.