mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
* .NET: Delete AgentResponse.{Try}Deserialize<T> methods (#3518)
* delete deserialize method of agent response
* order usings
* Update dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step05_StructuredOutput/Program.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update dotnet/samples/GettingStarted/Workflows/_Foundational/08_WriterCriticWorkflow/Program.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update dotnet/samples/GettingStarted/AGUI/Step05_StateManagement/Server/SharedStateAgent.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update dotnet/samples/AGUIClientServer/AGUIDojoServer/SharedState/SharedStateAgent.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update dotnet/samples/M365Agent/Agents/WeatherForecastAgent.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* .NET:[Breaking] Add support for structured output (#3658)
* add support for so
* restore lost xml comment part
* fix using ordering
* Update dotnet/src/Microsoft.Agents.AI.Abstractions/AIAgentStructuredOutput.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update dotnet/src/Microsoft.Agents.AI.Abstractions/AIAgentStructuredOutput.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_SO_WithFormatResponseTests.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* addressw pr review comments
* address pr review feedback
* address pr review comments
* fix compilation issues after the latest merge with main
* remove unnecessry options
* remove RunAsync<object> methods
* address code review feedback
* address pr review feedback
* make copy constructor protected
* address pr review feedback
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* .NET: Add decorator for structured output support (#3694)
* add decorator that adds structured output support to agents that don't natively support it.
* Update dotnet/src/Microsoft.Agents.AI/StructuredOutput/StructuredOutputAgentResponse.cs
Co-authored-by: westey <164392973+westey-m@users.noreply.github.com>
* Update dotnet/samples/GettingStarted/Agents/Agent_Step05_StructuredOutput/Program.cs
Co-authored-by: westey <164392973+westey-m@users.noreply.github.com>
* address pr review feedback
---------
Co-authored-by: westey <164392973+westey-m@users.noreply.github.com>
* .NET: Support primitives and arrays for SO (#3696)
* wrap primitives and arrays
* fix file encoding
* address review comments
* add adr
* add missed change
* fix compilation issue
* address review comments
* rename adr file name
* reflect decision to have SO decorator as a reference implementation in samples
* .NET: Move SO agent to samples (#3820)
* move SO agent to samples
* change file encoding
* fix files encoding
* .NET: Preserve caller context (#3803)
* fix stuck orchestration
* add previously removed RunAsync<T> method to DurableAIAgent
* suppress IDE0005 warning
* update changelog and remove unused constructor of AgentResponse<T>
* updatge the changelog
* address PR review feedback
* .NET: Disable irrelevant integration test (#3913)
* disable irrelevant integration test
* Update dotnet/tests/AzureAI.IntegrationTests/AIProjectClientAgentStructuredOutputRunTests.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* forgotten change
* address pr review feedback
* disable intermittently failing integration test.
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: westey <164392973+westey-m@users.noreply.github.com>
141 lines
8.4 KiB
C#
141 lines
8.4 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.AI;
|
|
using Microsoft.Shared.Diagnostics;
|
|
|
|
namespace Microsoft.Agents.AI;
|
|
|
|
/// <summary>
|
|
/// Provides structured output methods for <see cref="AIAgent"/> that enable requesting responses in a specific type format.
|
|
/// </summary>
|
|
public abstract partial class AIAgent
|
|
{
|
|
/// <summary>
|
|
/// Run the agent with no message assuming that all required instructions are already provided to the agent or on the session, and requesting a response of the specified type <typeparamref name="T"/>.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of structured output to request.</typeparam>
|
|
/// <param name="session">
|
|
/// The conversation session to use for this invocation. If <see langword="null"/>, a new session will be created.
|
|
/// The session will be updated with any response messages generated during invocation.
|
|
/// </param>
|
|
/// <param name="serializerOptions">Optional JSON serializer options to use for deserializing the response.</param>
|
|
/// <param name="options">Optional configuration parameters for controlling the agent's invocation behavior.</param>
|
|
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
|
/// <returns>A task that represents the asynchronous operation. The task result contains an <see cref="AgentResponse{T}"/> with the agent's output.</returns>
|
|
/// <remarks>
|
|
/// This overload is useful when the agent has sufficient context from previous messages in the session
|
|
/// or from its initial configuration to generate a meaningful response without additional input.
|
|
/// </remarks>
|
|
public Task<AgentResponse<T>> RunAsync<T>(
|
|
AgentSession? session = null,
|
|
JsonSerializerOptions? serializerOptions = null,
|
|
AgentRunOptions? options = null,
|
|
CancellationToken cancellationToken = default) =>
|
|
this.RunAsync<T>([], session, serializerOptions, options, cancellationToken);
|
|
|
|
/// <summary>
|
|
/// Runs the agent with a text message from the user, requesting a response of the specified type <typeparamref name="T"/>.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of structured output to request.</typeparam>
|
|
/// <param name="message">The user message to send to the agent.</param>
|
|
/// <param name="session">
|
|
/// The conversation session to use for this invocation. If <see langword="null"/>, a new session will be created.
|
|
/// The session will be updated with the input message and any response messages generated during invocation.
|
|
/// </param>
|
|
/// <param name="serializerOptions">Optional JSON serializer options to use for deserializing the response.</param>
|
|
/// <param name="options">Optional configuration parameters for controlling the agent's invocation behavior.</param>
|
|
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
|
/// <returns>A task that represents the asynchronous operation. The task result contains an <see cref="AgentResponse{T}"/> with the agent's output.</returns>
|
|
/// <exception cref="ArgumentException"><paramref name="message"/> is <see langword="null"/>, empty, or contains only whitespace.</exception>
|
|
/// <remarks>
|
|
/// The provided text will be wrapped in a <see cref="ChatMessage"/> with the <see cref="ChatRole.User"/> role
|
|
/// before being sent to the agent. This is a convenience method for simple text-based interactions.
|
|
/// </remarks>
|
|
public Task<AgentResponse<T>> RunAsync<T>(
|
|
string message,
|
|
AgentSession? session = null,
|
|
JsonSerializerOptions? serializerOptions = null,
|
|
AgentRunOptions? options = null,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
_ = Throw.IfNullOrWhitespace(message);
|
|
|
|
return this.RunAsync<T>(new ChatMessage(ChatRole.User, message), session, serializerOptions, options, cancellationToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Runs the agent with a single chat message, requesting a response of the specified type <typeparamref name="T"/>.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of structured output to request.</typeparam>
|
|
/// <param name="message">The chat message to send to the agent.</param>
|
|
/// <param name="session">
|
|
/// The conversation session to use for this invocation. If <see langword="null"/>, a new session will be created.
|
|
/// The session will be updated with the input message and any response messages generated during invocation.
|
|
/// </param>
|
|
/// <param name="serializerOptions">Optional JSON serializer options to use for deserializing the response.</param>
|
|
/// <param name="options">Optional configuration parameters for controlling the agent's invocation behavior.</param>
|
|
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
|
/// <returns>A task that represents the asynchronous operation. The task result contains an <see cref="AgentResponse{T}"/> with the agent's output.</returns>
|
|
/// <exception cref="ArgumentNullException"><paramref name="message"/> is <see langword="null"/>.</exception>
|
|
public Task<AgentResponse<T>> RunAsync<T>(
|
|
ChatMessage message,
|
|
AgentSession? session = null,
|
|
JsonSerializerOptions? serializerOptions = null,
|
|
AgentRunOptions? options = null,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
_ = Throw.IfNull(message);
|
|
|
|
return this.RunAsync<T>([message], session, serializerOptions, options, cancellationToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Runs the agent with a collection of chat messages, requesting a response of the specified type <typeparamref name="T"/>.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of structured output to request.</typeparam>
|
|
/// <param name="messages">The collection of messages to send to the agent for processing.</param>
|
|
/// <param name="session">
|
|
/// The conversation session to use for this invocation. If <see langword="null"/>, a new session will be created.
|
|
/// The session will be updated with the input messages and any response messages generated during invocation.
|
|
/// </param>
|
|
/// <param name="serializerOptions">Optional JSON serializer options to use for deserializing the response.</param>
|
|
/// <param name="options">Optional configuration parameters for controlling the agent's invocation behavior.</param>
|
|
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
|
/// <returns>A task that represents the asynchronous operation. The task result contains an <see cref="AgentResponse{T}"/> with the agent's output.</returns>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// This method handles collections of messages, allowing for complex conversational scenarios including
|
|
/// multi-turn interactions, function calls, and context-rich conversations.
|
|
/// </para>
|
|
/// <para>
|
|
/// The messages are processed in the order provided and become part of the conversation history.
|
|
/// The agent's response will also be added to <paramref name="session"/> if one is provided.
|
|
/// </para>
|
|
/// </remarks>
|
|
public async Task<AgentResponse<T>> RunAsync<T>(
|
|
IEnumerable<ChatMessage> messages,
|
|
AgentSession? session = null,
|
|
JsonSerializerOptions? serializerOptions = null,
|
|
AgentRunOptions? options = null,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
serializerOptions ??= AgentAbstractionsJsonUtilities.DefaultOptions;
|
|
|
|
var responseFormat = ChatResponseFormat.ForJsonSchema<T>(serializerOptions);
|
|
|
|
(responseFormat, bool isWrappedInObject) = StructuredOutputSchemaUtilities.WrapNonObjectSchema(responseFormat);
|
|
|
|
options = options?.Clone() ?? new AgentRunOptions();
|
|
options.ResponseFormat = responseFormat;
|
|
|
|
AgentResponse response = await this.RunAsync(messages, session, options, cancellationToken).ConfigureAwait(false);
|
|
|
|
return new AgentResponse<T>(response, serializerOptions) { IsWrappedInObject = isWrappedInObject };
|
|
}
|
|
}
|