mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
e224f06e60
* Update models used in dotnet samples to gpt-5.4-mini * Fix additional missed sample
59 lines
2.4 KiB
C#
59 lines
2.4 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
// This sample shows how to load an AI agent from a YAML file and process a prompt using Azure OpenAI as the backend.
|
|
|
|
using System.ComponentModel;
|
|
using Azure.AI.OpenAI;
|
|
using Azure.Identity;
|
|
using Microsoft.Agents.AI;
|
|
using Microsoft.Extensions.AI;
|
|
|
|
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
|
|
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-5.4-mini";
|
|
|
|
// Create the chat client
|
|
// 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.
|
|
IChatClient chatClient = new AzureOpenAIClient(
|
|
new Uri(endpoint),
|
|
new DefaultAzureCredential())
|
|
.GetChatClient(deploymentName)
|
|
.AsIChatClient();
|
|
|
|
// Read command-line arguments
|
|
if (args.Length < 2)
|
|
{
|
|
Console.WriteLine("Usage: DeclarativeAgents <yaml-file-path> <prompt>");
|
|
Console.WriteLine(" <yaml-file-path>: The path to the YAML file containing the agent definition");
|
|
Console.WriteLine(" <prompt>: The prompt to send to the agent");
|
|
return;
|
|
}
|
|
|
|
var yamlFilePath = args[0];
|
|
var prompt = args[1];
|
|
|
|
// Verify the YAML file exists
|
|
if (!File.Exists(yamlFilePath))
|
|
{
|
|
Console.WriteLine($"Error: File not found: {yamlFilePath}");
|
|
return;
|
|
}
|
|
|
|
// Read the YAML content from the file
|
|
var text = await File.ReadAllTextAsync(yamlFilePath);
|
|
|
|
// Example function tool that can be used by the agent.
|
|
[Description("Get the weather for a given location.")]
|
|
static string GetWeather(
|
|
[Description("The city and state, e.g. San Francisco, CA")] string location,
|
|
[Description("The unit of temperature. Possible values are 'celsius' and 'fahrenheit'.")] string unit)
|
|
=> $"The weather in {location} is cloudy with a high of {(unit.Equals("celsius", StringComparison.Ordinal) ? "15°C" : "59°F")}.";
|
|
|
|
// Create the agent from the YAML definition.
|
|
var agentFactory = new ChatClientPromptAgentFactory(chatClient, [AIFunctionFactory.Create(GetWeather, "GetWeather")]);
|
|
var agent = await agentFactory.CreateFromYamlAsync(text);
|
|
|
|
// Invoke the agent and output the text result.
|
|
Console.WriteLine(await agent!.RunAsync(prompt));
|