mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
6232dd8305
* Point AgentOptions.Instructions to ChatOptions * Update tests and checks * Update xml docs * Removal of agentOptions.Instructions in favor of chatOptions.Instructions * Instructions and tool check consistency * Instructions and tool check consistency * Address comment * Update .github/upgrades/prompts/SemanticKernelToAgentFramework.md Co-authored-by: westey <164392973+westey-m@users.noreply.github.com> * Address PR Comment * Update latest changes to comply with the PR proposal * Address feedback * Update dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentTests.cs Co-authored-by: westey <164392973+westey-m@users.noreply.github.com> * Address instructions * Update declarative to use promptAgent.Instrucitons with chatOptions.Instructions --------- Co-authored-by: westey <164392973+westey-m@users.noreply.github.com> Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
73 lines
3.3 KiB
C#
73 lines
3.3 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
// This sample shows how to configure ChatClientAgent to produce structured output.
|
|
|
|
using System.ComponentModel;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using Azure.AI.OpenAI;
|
|
using Azure.Identity;
|
|
using Microsoft.Agents.AI;
|
|
using OpenAI;
|
|
using OpenAI.Chat;
|
|
using SampleApp;
|
|
|
|
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-4o-mini";
|
|
|
|
// Create chat client to be used by chat client agents.
|
|
ChatClient chatClient = new AzureOpenAIClient(
|
|
new Uri(endpoint),
|
|
new AzureCliCredential())
|
|
.GetChatClient(deploymentName);
|
|
|
|
// Create the ChatClientAgent with the specified name and instructions.
|
|
ChatClientAgent agent = chatClient.CreateAIAgent(name: "HelpfulAssistant", instructions: "You are a helpful assistant.");
|
|
|
|
// Set PersonInfo as the type parameter of RunAsync method to specify the expected structured output from the agent and invoke the agent with some unstructured input.
|
|
AgentRunResponse<PersonInfo> response = await agent.RunAsync<PersonInfo>("Please provide information about John Smith, who is a 35-year-old software engineer.");
|
|
|
|
// Access the structured output via the Result property of the agent response.
|
|
Console.WriteLine("Assistant Output:");
|
|
Console.WriteLine($"Name: {response.Result.Name}");
|
|
Console.WriteLine($"Age: {response.Result.Age}");
|
|
Console.WriteLine($"Occupation: {response.Result.Occupation}");
|
|
|
|
// Create the ChatClientAgent with the specified name, instructions, and expected structured output the agent should produce.
|
|
ChatClientAgent agentWithPersonInfo = chatClient.CreateAIAgent(new ChatClientAgentOptions()
|
|
{
|
|
Name = "HelpfulAssistant",
|
|
ChatOptions = new() { Instructions = "You are a helpful assistant.", ResponseFormat = Microsoft.Extensions.AI.ChatResponseFormat.ForJsonSchema<PersonInfo>() }
|
|
});
|
|
|
|
// Invoke the agent with some unstructured input while streaming, to extract the structured information from.
|
|
var updates = agentWithPersonInfo.RunStreamingAsync("Please provide information about John Smith, who is a 35-year-old software engineer.");
|
|
|
|
// Assemble all the parts of the streamed output, since we can only deserialize once we have the full json,
|
|
// then deserialize the response into the PersonInfo class.
|
|
PersonInfo personInfo = (await updates.ToAgentRunResponseAsync()).Deserialize<PersonInfo>(JsonSerializerOptions.Web);
|
|
|
|
Console.WriteLine("Assistant Output:");
|
|
Console.WriteLine($"Name: {personInfo.Name}");
|
|
Console.WriteLine($"Age: {personInfo.Age}");
|
|
Console.WriteLine($"Occupation: {personInfo.Occupation}");
|
|
|
|
namespace SampleApp
|
|
{
|
|
/// <summary>
|
|
/// Represents information about a person, including their name, age, and occupation, matched to the JSON schema used in the agent.
|
|
/// </summary>
|
|
[Description("Information about a person including their name, age, and occupation")]
|
|
public class PersonInfo
|
|
{
|
|
[JsonPropertyName("name")]
|
|
public string? Name { get; set; }
|
|
|
|
[JsonPropertyName("age")]
|
|
public int? Age { get; set; }
|
|
|
|
[JsonPropertyName("occupation")]
|
|
public string? Occupation { get; set; }
|
|
}
|
|
}
|