-
Notifications
You must be signed in to change notification settings - Fork 1.4k
.NET: skill as class #5027
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
Merged
Merged
.NET: skill as class #5027
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3c1288b
add class-based skills
SergeyMenshykh 8b978ec
Merge branch 'main' into skill-as-class
SergeyMenshykh 9093230
address formating issues
SergeyMenshykh 1cff7bf
Remove generated filtered-unit.slnx and add to .gitignore
SergeyMenshykh 7886349
Remove generated filtered-unit.slnx and add to .gitignore
SergeyMenshykh 42cf8fb
Merge branch 'skill-as-class' of https://github.com/SergeyMenshykh/ag…
SergeyMenshykh 4b6094b
consolidate DI samples into one
SergeyMenshykh 56a625a
fix file encoding
SergeyMenshykh 7477195
Merge branch 'main' into skill-as-class
SergeyMenshykh b758b3c
suppress compatibility warning
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
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
21 changes: 21 additions & 0 deletions
21
.../02-agents/AgentSkills/Agent_Step03_ClassBasedSkills/Agent_Step03_ClassBasedSkills.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,21 @@ | ||
| <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> | ||
|
|
||
| </Project> |
102 changes: 102 additions & 0 deletions
102
dotnet/samples/02-agents/AgentSkills/Agent_Step03_ClassBasedSkills/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,102 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| // This sample demonstrates how to define Agent Skills as C# classes using AgentClassSkill. | ||
| // Class-based skills bundle all components into a single class implementation. | ||
|
|
||
| using System.Text.Json; | ||
| 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"; | ||
|
|
||
| // --- Class-Based Skill --- | ||
| // Instantiate the skill class. | ||
| var unitConverter = new UnitConverterSkill(); | ||
|
|
||
| // --- Skills Provider --- | ||
| var skillsProvider = new AgentSkillsProvider(unitConverter); | ||
|
|
||
| // --- Agent Setup --- | ||
| AIAgent agent = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential()) | ||
| .GetResponsesClient() | ||
| .AsAIAgent(new ChatClientAgentOptions | ||
| { | ||
| Name = "UnitConverterAgent", | ||
| ChatOptions = new() | ||
| { | ||
| Instructions = "You are a helpful assistant that can convert units.", | ||
| }, | ||
| AIContextProviders = [skillsProvider], | ||
| }, | ||
| model: deploymentName); | ||
|
|
||
| // --- Example: Unit conversion --- | ||
| Console.WriteLine("Converting units with class-based skills"); | ||
| Console.WriteLine(new string('-', 60)); | ||
|
|
||
| AgentResponse response = await agent.RunAsync( | ||
| "How many kilometers is a marathon (26.2 miles)? And how many pounds is 75 kilograms?"); | ||
|
|
||
| Console.WriteLine($"Agent: {response.Text}"); | ||
|
|
||
| /// <summary> | ||
| /// A unit-converter skill defined as a C# class. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Class-based skills bundle all components (name, description, body, resources, scripts) | ||
| /// into a single class. | ||
| /// </remarks> | ||
| internal sealed class UnitConverterSkill : AgentClassSkill | ||
| { | ||
| private IReadOnlyList<AgentSkillResource>? _resources; | ||
| private IReadOnlyList<AgentSkillScript>? _scripts; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override AgentSkillFrontmatter Frontmatter { get; } = new( | ||
| "unit-converter", | ||
| "Convert between common units using a multiplication factor. Use when asked to convert miles, kilometers, pounds, or kilograms."); | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override string Instructions => """ | ||
| Use this skill when the user asks to convert between units. | ||
|
|
||
| 1. Review the conversion-table resource to find the factor for the requested conversion. | ||
| 2. Use the convert script, passing the value and factor from the table. | ||
| 3. Present the result clearly with both units. | ||
| """; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override IReadOnlyList<AgentSkillResource>? Resources => this._resources ??= | ||
| [ | ||
| CreateResource( | ||
| "conversion-table", | ||
| """ | ||
| # Conversion Tables | ||
|
|
||
| Formula: **result = value × factor** | ||
|
|
||
| | From | To | Factor | | ||
| |-------------|-------------|----------| | ||
| | miles | kilometers | 1.60934 | | ||
| | kilometers | miles | 0.621371 | | ||
| | pounds | kilograms | 0.453592 | | ||
| | kilograms | pounds | 2.20462 | | ||
| """), | ||
| ]; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override IReadOnlyList<AgentSkillScript>? Scripts => this._scripts ??= | ||
| [ | ||
| CreateScript("convert", ConvertUnits), | ||
| ]; | ||
|
|
||
| private static string ConvertUnits(double value, double factor) | ||
| { | ||
| double result = Math.Round(value * factor, 4); | ||
| return JsonSerializer.Serialize(new { value, factor, result }); | ||
| } | ||
| } | ||
49 changes: 49 additions & 0 deletions
49
dotnet/samples/02-agents/AgentSkills/Agent_Step03_ClassBasedSkills/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,49 @@ | ||
| # Class-Based Agent Skills Sample | ||
|
|
||
| This sample demonstrates how to define **Agent Skills as C# classes** using `AgentClassSkill`. | ||
|
|
||
| ## What it demonstrates | ||
|
|
||
| - Creating skills as classes that extend `AgentClassSkill` | ||
| - Bundling name, description, body, resources, and scripts into a single class | ||
| - Using the `AgentSkillsProvider` constructor with class-based skills | ||
|
|
||
| ## Skills Included | ||
|
|
||
| ### unit-converter (class-based) | ||
|
|
||
| A `UnitConverterSkill` class that converts between common units. Defined in `Program.cs`: | ||
|
|
||
| - `conversion-table` — Static resource with factor table | ||
| - `convert` — Script that performs `value × factor` conversion | ||
|
|
||
| ## Running the Sample | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| - .NET 10.0 SDK | ||
| - Azure OpenAI endpoint with a deployed model | ||
|
|
||
| ### Setup | ||
|
|
||
| ```bash | ||
| export AZURE_OPENAI_ENDPOINT="https://your-endpoint.openai.azure.com/" | ||
| export AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4o-mini" | ||
| ``` | ||
|
|
||
| ### Run | ||
|
|
||
| ```bash | ||
| dotnet run | ||
| ``` | ||
|
|
||
| ### Expected Output | ||
|
|
||
| ``` | ||
| Converting units with class-based skills | ||
| ------------------------------------------------------------ | ||
| Agent: Here are your conversions: | ||
|
|
||
| 1. **26.2 miles → 42.16 km** (a marathon distance) | ||
| 2. **75 kg → 165.35 lbs** | ||
| ``` |
32 changes: 32 additions & 0 deletions
32
...et/samples/02-agents/AgentSkills/Agent_Step04_MixedSkills/Agent_Step04_MixedSkills.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,32 @@ | ||
| <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> | ||
| <Compile Include="..\SubprocessScriptRunner.cs" Link="SubprocessScriptRunner.cs" /> | ||
| </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> |
149 changes: 149 additions & 0 deletions
149
dotnet/samples/02-agents/AgentSkills/Agent_Step04_MixedSkills/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,149 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| // This sample demonstrates an advanced scenario: combining multiple skill types in a single agent | ||
| // using AgentSkillsProviderBuilder. The builder is designed for cases where the simple | ||
| // AgentSkillsProvider constructors are insufficient — for example, when you need to mix skill | ||
| // sources, apply filtering, or configure cross-cutting options in one place. | ||
| // | ||
| // Three different skill sources are registered here: | ||
| // 1. File-based: unit-converter (miles↔km, pounds↔kg) from SKILL.md on disk | ||
| // 2. Code-defined: volume-converter (gallons↔liters) using AgentInlineSkill | ||
| // 3. Class-based: temperature-converter (°F↔°C↔K) using AgentClassSkill | ||
| // | ||
| // For simpler, single-source scenarios, see the earlier steps in this sample series | ||
| // (e.g., Step01 for file-based, Step02 for code-defined, Step03 for class-based). | ||
|
|
||
| using System.Text.Json; | ||
| 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"; | ||
|
|
||
| // --- 1. Code-Defined Skill: volume-converter --- | ||
| var volumeConverterSkill = new AgentInlineSkill( | ||
| name: "volume-converter", | ||
| description: "Convert between gallons and liters using a multiplication factor.", | ||
| instructions: """ | ||
| Use this skill when the user asks to convert between gallons and liters. | ||
|
|
||
| 1. Review the volume-conversion-table resource to find the correct factor. | ||
| 2. Use the convert-volume script, passing the value and factor. | ||
| """) | ||
| .AddResource("volume-conversion-table", | ||
| """ | ||
| # Volume Conversion Table | ||
|
|
||
| Formula: **result = value × factor** | ||
|
|
||
| | From | To | Factor | | ||
| |---------|---------|---------| | ||
| | gallons | liters | 3.78541 | | ||
| | liters | gallons | 0.264172| | ||
| """) | ||
| .AddScript("convert-volume", (double value, double factor) => | ||
| { | ||
| double result = Math.Round(value * factor, 4); | ||
| return JsonSerializer.Serialize(new { value, factor, result }); | ||
| }); | ||
|
|
||
| // --- 2. Class-Based Skill: temperature-converter --- | ||
| var temperatureConverter = new TemperatureConverterSkill(); | ||
|
|
||
| // --- 3. Build provider combining all three source types --- | ||
| var skillsProvider = new AgentSkillsProviderBuilder() | ||
| .UseFileSkill(Path.Combine(AppContext.BaseDirectory, "skills")) // File-based: unit-converter | ||
| .UseSkill(volumeConverterSkill) // Code-defined: volume-converter | ||
| .UseSkill(temperatureConverter) // Class-based: temperature-converter | ||
| .UseFileScriptRunner(SubprocessScriptRunner.RunAsync) | ||
| .Build(); | ||
|
|
||
| // --- Agent Setup --- | ||
| AIAgent agent = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential()) | ||
| .GetResponsesClient() | ||
| .AsAIAgent(new ChatClientAgentOptions | ||
| { | ||
| Name = "MultiConverterAgent", | ||
| ChatOptions = new() | ||
| { | ||
| Instructions = "You are a helpful assistant that can convert units, volumes, and temperatures.", | ||
| }, | ||
| AIContextProviders = [skillsProvider], | ||
| }, | ||
| model: deploymentName); | ||
|
|
||
| // --- Example: Use all three skills --- | ||
| Console.WriteLine("Converting with mixed skills (file + code + class)"); | ||
| Console.WriteLine(new string('-', 60)); | ||
|
|
||
| AgentResponse response = await agent.RunAsync( | ||
| "I need three conversions: " + | ||
| "1) How many kilometers is a marathon (26.2 miles)? " + | ||
| "2) How many liters is a 5-gallon bucket? " + | ||
| "3) What is 98.6°F in Celsius?"); | ||
|
|
||
| Console.WriteLine($"Agent: {response.Text}"); | ||
|
|
||
| /// <summary> | ||
| /// A temperature-converter skill defined as a C# class. | ||
| /// </summary> | ||
| internal sealed class TemperatureConverterSkill : AgentClassSkill | ||
| { | ||
| private IReadOnlyList<AgentSkillResource>? _resources; | ||
| private IReadOnlyList<AgentSkillScript>? _scripts; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override AgentSkillFrontmatter Frontmatter { get; } = new( | ||
| "temperature-converter", | ||
| "Convert between temperature scales (Fahrenheit, Celsius, Kelvin)."); | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override string Instructions => """ | ||
| Use this skill when the user asks to convert temperatures. | ||
|
|
||
| 1. Review the temperature-conversion-formulas resource for the correct formula. | ||
| 2. Use the convert-temperature script, passing the value, source scale, and target scale. | ||
| 3. Present the result clearly with both temperature scales. | ||
| """; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override IReadOnlyList<AgentSkillResource>? Resources => this._resources ??= | ||
| [ | ||
| CreateResource( | ||
| "temperature-conversion-formulas", | ||
| """ | ||
| # Temperature Conversion Formulas | ||
|
|
||
| | From | To | Formula | | ||
| |-------------|-------------|---------------------------| | ||
| | Fahrenheit | Celsius | °C = (°F − 32) × 5/9 | | ||
| | Celsius | Fahrenheit | °F = (°C × 9/5) + 32 | | ||
| | Celsius | Kelvin | K = °C + 273.15 | | ||
| | Kelvin | Celsius | °C = K − 273.15 | | ||
| """), | ||
| ]; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override IReadOnlyList<AgentSkillScript>? Scripts => this._scripts ??= | ||
| [ | ||
| CreateScript("convert-temperature", ConvertTemperature), | ||
| ]; | ||
|
|
||
| private static string ConvertTemperature(double value, string from, string to) | ||
| { | ||
| double result = (from.ToUpperInvariant(), to.ToUpperInvariant()) switch | ||
| { | ||
| ("FAHRENHEIT", "CELSIUS") => Math.Round((value - 32) * 5.0 / 9.0, 2), | ||
| ("CELSIUS", "FAHRENHEIT") => Math.Round(value * 9.0 / 5.0 + 32, 2), | ||
| ("CELSIUS", "KELVIN") => Math.Round(value + 273.15, 2), | ||
| ("KELVIN", "CELSIUS") => Math.Round(value - 273.15, 2), | ||
| _ => throw new ArgumentException($"Unsupported conversion: {from} → {to}") | ||
| }; | ||
|
|
||
| return JsonSerializer.Serialize(new { value, from, to, result }); | ||
| } | ||
| } |
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.