-
Notifications
You must be signed in to change notification settings - Fork 1.6k
.NET: Add inline skills API #4951
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3675264
add inline skills
SergeyMenshykh c0439c8
Fix IDE1006 and IDE0004 formatting errors in test files
SergeyMenshykh 50999da
address issues
SergeyMenshykh 01ca209
address comments
SergeyMenshykh ce951a6
make inline skills script and resource model classes internal
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
Some comments aren't visible on the classic Files Changed page.
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
21 changes: 21 additions & 0 deletions
21
...2-agents/AgentSkills/Agent_Step02_CodeDefinedSkills/Agent_Step02_CodeDefinedSkills.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> |
90 changes: 90 additions & 0 deletions
90
dotnet/samples/02-agents/AgentSkills/Agent_Step02_CodeDefinedSkills/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,90 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| // This sample demonstrates how to define Agent Skills entirely in code using AgentInlineSkill. | ||
| // No SKILL.md files are needed — skills, resources, and scripts are all defined programmatically. | ||
| // | ||
| // Three approaches are shown using a unit-converter skill: | ||
| // 1. Static resources — inline content provided via AddResource | ||
| // 2. Dynamic resources — computed at runtime via a factory delegate | ||
| // 3. Code scripts — executable delegates the agent can invoke directly | ||
|
|
||
| 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"; | ||
|
|
||
| // --- Build the code-defined skill --- | ||
| var unitConverterSkill = new AgentInlineSkill( | ||
| name: "unit-converter", | ||
| description: "Convert between common units using a multiplication factor. Use when asked to convert miles, kilometers, pounds, or kilograms.", | ||
| 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. Check the conversion-policy resource for rounding and formatting rules. | ||
| 3. Use the convert script, passing the value and factor from the table. | ||
| """) | ||
| // 1. Static Resource: conversion tables | ||
| .AddResource( | ||
| "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 | | ||
| """) | ||
| // 2. Dynamic Resource: conversion policy (computed at runtime) | ||
| .AddResource("conversion-policy", () => | ||
| { | ||
| const int Precision = 4; | ||
| return $""" | ||
| # Conversion Policy | ||
|
|
||
| **Decimal places:** {Precision} | ||
| **Format:** Always show both the original and converted values with units | ||
| **Generated at:** {DateTime.UtcNow:O} | ||
| """; | ||
| }) | ||
| // 3. Code Script: convert | ||
| .AddScript("convert", (double value, double factor) => | ||
| { | ||
| double result = Math.Round(value * factor, 4); | ||
| return JsonSerializer.Serialize(new { value, factor, result }); | ||
| }); | ||
|
|
||
| // --- Skills Provider --- | ||
| var skillsProvider = new AgentSkillsProvider(unitConverterSkill); | ||
|
|
||
| // --- 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 code-defined 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}"); |
52 changes: 52 additions & 0 deletions
52
dotnet/samples/02-agents/AgentSkills/Agent_Step02_CodeDefinedSkills/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,52 @@ | ||
| # Code-Defined Agent Skills Sample | ||
|
|
||
| This sample demonstrates how to define **Agent Skills entirely in code** using `AgentInlineSkill`. | ||
|
|
||
| ## What it demonstrates | ||
|
|
||
| - Creating skills programmatically with `AgentInlineSkill` — no SKILL.md files needed | ||
| - **Static resources** via `AddResource` with inline content | ||
| - **Dynamic resources** via `AddResource` with a factory delegate (computed at runtime) | ||
| - **Code scripts** via `AddScript` with a delegate handler | ||
| - Using the `AgentSkillsProvider` constructor with inline skills | ||
|
|
||
| ## Skills Included | ||
|
|
||
| ### unit-converter (code-defined) | ||
|
|
||
| Converts between common units using multiplication factors. Defined entirely in C# code: | ||
|
|
||
| - `conversion-table` — Static resource with factor table | ||
| - `conversion-policy` — Dynamic resource with formatting rules (generated at runtime) | ||
| - `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 code-defined skills | ||
| ------------------------------------------------------------ | ||
| Agent: Here are your conversions: | ||
|
|
||
| 1. **26.2 miles → 42.16 km** (a marathon distance) | ||
| 2. **75 kg → 165.35 lbs** | ||
| ``` |
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 |
|---|---|---|
| @@ -1,7 +1,24 @@ | ||
| # AgentSkills Samples | ||
|
|
||
| Samples demonstrating Agent Skills capabilities. | ||
| Samples demonstrating Agent Skills capabilities. Each sample shows a different way to define and use skills. | ||
|
|
||
| | Sample | Description | | ||
| |--------|-------------| | ||
| | [Agent_Step01_FileBasedSkills](Agent_Step01_FileBasedSkills/) | Define skills as `SKILL.md` files on disk with reference documents. Uses a unit-converter skill. | | ||
| | [Agent_Step02_CodeDefinedSkills](Agent_Step02_CodeDefinedSkills/) | Define skills entirely in C# code using `AgentInlineSkill`, with static/dynamic resources and scripts. | | ||
|
|
||
| ## Key Concepts | ||
|
|
||
| ### File-Based vs Code-Defined Skills | ||
|
|
||
| | Aspect | File-Based | Code-Defined | | ||
| |--------|-----------|--------------| | ||
| | Definition | `SKILL.md` files on disk | `AgentInlineSkill` instances in C# | | ||
| | Resources | All files in skill directory (filtered by extension) | `AddResource` (static value or delegate-backed) | | ||
| | Scripts | Supported via script executor delegate | `AddScript` delegates | | ||
| | Discovery | Automatic from directory path | Explicit via constructor | | ||
| | Dynamic content | No (static files only) | Yes (factory delegates) | | ||
| | Reusability | Copy skill directory | Inline or shared instances | | ||
|
|
||
| For single-source scenarios, use the `AgentSkillsProvider` constructors directly. To combine multiple skill types, use the `AgentSkillsProviderBuilder`. | ||
|
|
35 changes: 35 additions & 0 deletions
35
dotnet/src/Microsoft.Agents.AI/Skills/AgentInMemorySkillsSource.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,35 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Shared.DiagnosticIds; | ||
| using Microsoft.Shared.Diagnostics; | ||
|
|
||
| namespace Microsoft.Agents.AI; | ||
|
|
||
| /// <summary> | ||
| /// A skill source that holds <see cref="AgentSkill"/> instances in memory. | ||
| /// </summary> | ||
| [Experimental(DiagnosticIds.Experiments.AgentsAIExperiments)] | ||
| internal sealed class AgentInMemorySkillsSource : AgentSkillsSource | ||
| { | ||
| private readonly List<AgentSkill> _skills; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="AgentInMemorySkillsSource"/> class. | ||
| /// </summary> | ||
| /// <param name="skills">The skills to include in this source.</param> | ||
| public AgentInMemorySkillsSource(IEnumerable<AgentSkill> skills) | ||
| { | ||
| this._skills = Throw.IfNull(skills).ToList(); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override Task<IList<AgentSkill>> GetSkillsAsync(CancellationToken cancellationToken = default) | ||
| { | ||
| return Task.FromResult<IList<AgentSkill>>(this._skills); | ||
|
SergeyMenshykh marked this conversation as resolved.
|
||
| } | ||
| } | ||
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
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.