mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
c79f886dc3
* dotnet: refresh Foundry sample guidance Carry forward the still-relevant sample guidance and Foundry-specific documentation fixes from the old stacked sample migration work, adapted to the current repo layout and policy. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * dotnet: rename Foundry sample env vars Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * dotnet: remove persistent provider sample Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * dotnet: drop SAMPLE_GUIDELINES.md from this PR Defer the guidelines doc and its cross-link to a follow-on PR to avoid broken-link failures in CI. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * dotnet: add DefaultAzureCredential warning to remaining samples Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * dotnet: address PR review feedback Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
53 lines
2.4 KiB
C#
53 lines
2.4 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
// This sample demonstrates how to use file-based Agent Skills with a ChatClientAgent.
|
|
// Skills are discovered from SKILL.md files on disk and follow the progressive disclosure pattern:
|
|
// 1. Advertise — skill names and descriptions in the system prompt
|
|
// 2. Load — full instructions loaded on demand via load_skill tool
|
|
// 3. Read resources — reference files read via read_skill_resource tool
|
|
// 4. Run scripts — scripts executed via run_skill_script tool with a subprocess executor
|
|
//
|
|
// This sample uses a unit-converter skill that converts between miles, kilometers, pounds, and kilograms.
|
|
|
|
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-5.4-mini";
|
|
|
|
// --- Skills Provider ---
|
|
// Discovers skills from the 'skills' directory containing SKILL.md files.
|
|
// The script runner runs file-based scripts (e.g. Python) as local subprocesses.
|
|
var skillsProvider = new AgentSkillsProvider(
|
|
Path.Combine(AppContext.BaseDirectory, "skills"),
|
|
SubprocessScriptRunner.RunAsync);
|
|
|
|
// --- 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()
|
|
.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 file-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}");
|