-
Notifications
You must be signed in to change notification settings - Fork 2k
.NET: Add Run overloads to expose ChatClientAgentRunOptions in IntelliSense #3115
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 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
af22bfc
Initial plan
Copilot ad61264
Add ChatClientAgentExtensions for improved discoverability of ChatCli…
Copilot 19c0bbe
Address code review feedback - use collection expression syntax
Copilot 495dac7
Apply suggestion from @westey-m
westey-m eef88f6
Fix issues with Copilot implementation
westey-m ee94529
Add additional tests for structured output overloads.
westey-m 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
There are no files selected for viewing
278 changes: 278 additions & 0 deletions
278
dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentExtensions.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,278 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Text.Json; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.AI; | ||
|
|
||
| namespace Microsoft.Agents.AI; | ||
|
|
||
| /// <summary> | ||
| /// Provides extension methods for <see cref="ChatClientAgent"/> to enable discoverability of <see cref="ChatClientAgentRunOptions"/>. | ||
| /// </summary> | ||
| public static class ChatClientAgentExtensions | ||
| { | ||
| /// <summary> | ||
| /// Run the agent with no message assuming that all required instructions are already provided to the agent or on the thread. | ||
| /// </summary> | ||
| /// <param name="agent">The <see cref="ChatClientAgent"/> to run.</param> | ||
| /// <param name="thread"> | ||
| /// The conversation thread to use for this invocation. If <see langword="null"/>, a new thread will be created. | ||
| /// The thread will be updated with any response messages generated during invocation. | ||
| /// </param> | ||
| /// <param name="options">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="AgentRunResponse"/> with the agent's output.</returns> | ||
| public static Task<AgentRunResponse> RunAsync( | ||
| this ChatClientAgent agent, | ||
| AgentThread? thread, | ||
| ChatClientAgentRunOptions options, | ||
| CancellationToken cancellationToken = default) => | ||
| agent.RunAsync(thread, (AgentRunOptions)options, cancellationToken); | ||
|
westey-m marked this conversation as resolved.
Outdated
|
||
|
|
||
| /// <summary> | ||
| /// Runs the agent with a text message from the user. | ||
| /// </summary> | ||
| /// <param name="agent">The <see cref="ChatClientAgent"/> to run.</param> | ||
| /// <param name="message">The user message to send to the agent.</param> | ||
| /// <param name="thread"> | ||
| /// The conversation thread to use for this invocation. If <see langword="null"/>, a new thread will be created. | ||
| /// The thread will be updated with the input message and any response messages generated during invocation. | ||
| /// </param> | ||
| /// <param name="options">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="AgentRunResponse"/> with the agent's output.</returns> | ||
| public static Task<AgentRunResponse> RunAsync( | ||
| this ChatClientAgent agent, | ||
| string message, | ||
| AgentThread? thread, | ||
| ChatClientAgentRunOptions options, | ||
| CancellationToken cancellationToken = default) => | ||
| agent.RunAsync(message, thread, (AgentRunOptions)options, cancellationToken); | ||
|
|
||
| /// <summary> | ||
| /// Runs the agent with a single chat message. | ||
| /// </summary> | ||
| /// <param name="agent">The <see cref="ChatClientAgent"/> to run.</param> | ||
| /// <param name="message">The chat message to send to the agent.</param> | ||
| /// <param name="thread"> | ||
| /// The conversation thread to use for this invocation. If <see langword="null"/>, a new thread will be created. | ||
| /// The thread will be updated with the input message and any response messages generated during invocation. | ||
| /// </param> | ||
| /// <param name="options">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="AgentRunResponse"/> with the agent's output.</returns> | ||
| public static Task<AgentRunResponse> RunAsync( | ||
| this ChatClientAgent agent, | ||
| ChatMessage message, | ||
| AgentThread? thread, | ||
| ChatClientAgentRunOptions options, | ||
| CancellationToken cancellationToken = default) => | ||
| agent.RunAsync(message, thread, (AgentRunOptions)options, cancellationToken); | ||
|
|
||
| /// <summary> | ||
| /// Runs the agent with a collection of chat messages. | ||
| /// </summary> | ||
| /// <param name="agent">The <see cref="ChatClientAgent"/> to run.</param> | ||
| /// <param name="messages">The collection of messages to send to the agent for processing.</param> | ||
| /// <param name="thread"> | ||
| /// The conversation thread to use for this invocation. If <see langword="null"/>, a new thread will be created. | ||
| /// The thread will be updated with the input messages and any response messages generated during invocation. | ||
| /// </param> | ||
| /// <param name="options">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="AgentRunResponse"/> with the agent's output.</returns> | ||
| public static Task<AgentRunResponse> RunAsync( | ||
| this ChatClientAgent agent, | ||
| IEnumerable<ChatMessage> messages, | ||
| AgentThread? thread, | ||
| ChatClientAgentRunOptions options, | ||
| CancellationToken cancellationToken = default) => | ||
| agent.RunAsync(messages, thread, (AgentRunOptions)options, cancellationToken); | ||
|
|
||
| /// <summary> | ||
| /// Runs the agent in streaming mode without providing new input messages, relying on existing context and instructions. | ||
| /// </summary> | ||
| /// <param name="agent">The <see cref="ChatClientAgent"/> to run.</param> | ||
| /// <param name="thread"> | ||
| /// The conversation thread to use for this invocation. If <see langword="null"/>, a new thread will be created. | ||
| /// The thread will be updated with any response messages generated during invocation. | ||
| /// </param> | ||
| /// <param name="options">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>An asynchronous enumerable of <see cref="AgentRunResponseUpdate"/> instances representing the streaming response.</returns> | ||
| public static IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync( | ||
| this ChatClientAgent agent, | ||
| AgentThread? thread, | ||
| ChatClientAgentRunOptions options, | ||
| CancellationToken cancellationToken = default) => | ||
| agent.RunStreamingAsync(thread, (AgentRunOptions)options, cancellationToken); | ||
|
|
||
| /// <summary> | ||
| /// Runs the agent in streaming mode with a text message from the user. | ||
| /// </summary> | ||
| /// <param name="agent">The <see cref="ChatClientAgent"/> to run.</param> | ||
| /// <param name="message">The user message to send to the agent.</param> | ||
| /// <param name="thread"> | ||
| /// The conversation thread to use for this invocation. If <see langword="null"/>, a new thread will be created. | ||
| /// The thread will be updated with the input message and any response messages generated during invocation. | ||
| /// </param> | ||
| /// <param name="options">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>An asynchronous enumerable of <see cref="AgentRunResponseUpdate"/> instances representing the streaming response.</returns> | ||
| public static IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync( | ||
| this ChatClientAgent agent, | ||
| string message, | ||
| AgentThread? thread, | ||
| ChatClientAgentRunOptions options, | ||
| CancellationToken cancellationToken = default) => | ||
| agent.RunStreamingAsync(message, thread, (AgentRunOptions)options, cancellationToken); | ||
|
|
||
| /// <summary> | ||
| /// Runs the agent in streaming mode with a single chat message. | ||
| /// </summary> | ||
| /// <param name="agent">The <see cref="ChatClientAgent"/> to run.</param> | ||
| /// <param name="message">The chat message to send to the agent.</param> | ||
| /// <param name="thread"> | ||
| /// The conversation thread to use for this invocation. If <see langword="null"/>, a new thread will be created. | ||
| /// The thread will be updated with the input message and any response messages generated during invocation. | ||
| /// </param> | ||
| /// <param name="options">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>An asynchronous enumerable of <see cref="AgentRunResponseUpdate"/> instances representing the streaming response.</returns> | ||
| public static IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync( | ||
| this ChatClientAgent agent, | ||
| ChatMessage message, | ||
| AgentThread? thread, | ||
| ChatClientAgentRunOptions options, | ||
| CancellationToken cancellationToken = default) => | ||
| agent.RunStreamingAsync(message, thread, (AgentRunOptions)options, cancellationToken); | ||
|
|
||
| /// <summary> | ||
| /// Runs the agent in streaming mode with a collection of chat messages. | ||
| /// </summary> | ||
| /// <param name="agent">The <see cref="ChatClientAgent"/> to run.</param> | ||
| /// <param name="messages">The collection of messages to send to the agent for processing.</param> | ||
| /// <param name="thread"> | ||
| /// The conversation thread to use for this invocation. If <see langword="null"/>, a new thread will be created. | ||
| /// The thread will be updated with the input messages and any response updates generated during invocation. | ||
| /// </param> | ||
| /// <param name="options">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>An asynchronous enumerable of <see cref="AgentRunResponseUpdate"/> instances representing the streaming response.</returns> | ||
| public static IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync( | ||
| this ChatClientAgent agent, | ||
| IEnumerable<ChatMessage> messages, | ||
| AgentThread? thread, | ||
| ChatClientAgentRunOptions options, | ||
| CancellationToken cancellationToken = default) => | ||
| agent.RunStreamingAsync(messages, thread, (AgentRunOptions)options, cancellationToken); | ||
|
|
||
| /// <summary> | ||
| /// Run the agent with no message assuming that all required instructions are already provided to the agent or on the thread, and requesting a response of the specified type <typeparamref name="T"/>. | ||
| /// </summary> | ||
| /// <param name="agent">The <see cref="ChatClientAgent"/> to run.</param> | ||
| /// <param name="thread"> | ||
| /// The conversation thread to use for this invocation. If <see langword="null"/>, a new thread will be created. | ||
| /// The thread will be updated with any response messages generated during invocation. | ||
| /// </param> | ||
| /// <param name="serializerOptions">The JSON serialization options to use.</param> | ||
| /// <param name="options">Configuration parameters for controlling the agent's invocation behavior.</param> | ||
| /// <param name="useJsonSchemaResponseFormat"> | ||
| /// <see langword="true" /> to set a JSON schema on the <see cref="ChatResponseFormat"/>; otherwise, <see langword="false" />. The default is <see langword="true" />. | ||
| /// Using a JSON schema improves reliability if the underlying model supports native structured output with a schema, but might cause an error if the model does not support it. | ||
| /// </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="ChatClientAgentRunResponse{T}"/> with the agent's output.</returns> | ||
| public static Task<ChatClientAgentRunResponse<T>> RunAsync<T>( | ||
| this ChatClientAgent agent, | ||
| AgentThread? thread, | ||
| JsonSerializerOptions? serializerOptions, | ||
| ChatClientAgentRunOptions options, | ||
| bool? useJsonSchemaResponseFormat = null, | ||
| CancellationToken cancellationToken = default) => | ||
| agent.RunAsync<T>(thread, serializerOptions, (AgentRunOptions)options, useJsonSchemaResponseFormat, cancellationToken); | ||
|
|
||
| /// <summary> | ||
| /// Runs the agent with a text message from the user, requesting a response of the specified type <typeparamref name="T"/>. | ||
| /// </summary> | ||
| /// <param name="agent">The <see cref="ChatClientAgent"/> to run.</param> | ||
| /// <param name="message">The user message to send to the agent.</param> | ||
| /// <param name="thread"> | ||
| /// The conversation thread to use for this invocation. If <see langword="null"/>, a new thread will be created. | ||
| /// The thread will be updated with the input message and any response messages generated during invocation. | ||
| /// </param> | ||
| /// <param name="serializerOptions">The JSON serialization options to use.</param> | ||
| /// <param name="options">Configuration parameters for controlling the agent's invocation behavior.</param> | ||
| /// <param name="useJsonSchemaResponseFormat"> | ||
| /// <see langword="true" /> to set a JSON schema on the <see cref="ChatResponseFormat"/>; otherwise, <see langword="false" />. The default is <see langword="true" />. | ||
| /// Using a JSON schema improves reliability if the underlying model supports native structured output with a schema, but might cause an error if the model does not support it. | ||
| /// </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="ChatClientAgentRunResponse{T}"/> with the agent's output.</returns> | ||
| public static Task<ChatClientAgentRunResponse<T>> RunAsync<T>( | ||
| this ChatClientAgent agent, | ||
| string message, | ||
| AgentThread? thread, | ||
| JsonSerializerOptions? serializerOptions, | ||
| ChatClientAgentRunOptions options, | ||
| bool? useJsonSchemaResponseFormat = null, | ||
| CancellationToken cancellationToken = default) => | ||
| agent.RunAsync<T>(message, thread, serializerOptions, (AgentRunOptions)options, useJsonSchemaResponseFormat, cancellationToken); | ||
|
|
||
| /// <summary> | ||
| /// Runs the agent with a single chat message, requesting a response of the specified type <typeparamref name="T"/>. | ||
| /// </summary> | ||
| /// <param name="agent">The <see cref="ChatClientAgent"/> to run.</param> | ||
| /// <param name="message">The chat message to send to the agent.</param> | ||
| /// <param name="thread"> | ||
| /// The conversation thread to use for this invocation. If <see langword="null"/>, a new thread will be created. | ||
| /// The thread will be updated with the input message and any response messages generated during invocation. | ||
| /// </param> | ||
| /// <param name="serializerOptions">The JSON serialization options to use.</param> | ||
| /// <param name="options">Configuration parameters for controlling the agent's invocation behavior.</param> | ||
| /// <param name="useJsonSchemaResponseFormat"> | ||
| /// <see langword="true" /> to set a JSON schema on the <see cref="ChatResponseFormat"/>; otherwise, <see langword="false" />. The default is <see langword="true" />. | ||
| /// Using a JSON schema improves reliability if the underlying model supports native structured output with a schema, but might cause an error if the model does not support it. | ||
| /// </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="ChatClientAgentRunResponse{T}"/> with the agent's output.</returns> | ||
| public static Task<ChatClientAgentRunResponse<T>> RunAsync<T>( | ||
| this ChatClientAgent agent, | ||
| ChatMessage message, | ||
| AgentThread? thread, | ||
| JsonSerializerOptions? serializerOptions, | ||
| ChatClientAgentRunOptions options, | ||
| bool? useJsonSchemaResponseFormat = null, | ||
| CancellationToken cancellationToken = default) => | ||
| agent.RunAsync<T>(message, thread, serializerOptions, (AgentRunOptions)options, useJsonSchemaResponseFormat, cancellationToken); | ||
|
|
||
| /// <summary> | ||
| /// Runs the agent with a collection of chat messages, requesting a response of the specified type <typeparamref name="T"/>. | ||
| /// </summary> | ||
| /// <param name="agent">The <see cref="ChatClientAgent"/> to run.</param> | ||
| /// <param name="messages">The collection of messages to send to the agent for processing.</param> | ||
| /// <param name="thread"> | ||
| /// The conversation thread to use for this invocation. If <see langword="null"/>, a new thread will be created. | ||
| /// The thread will be updated with the input messages and any response messages generated during invocation. | ||
| /// </param> | ||
| /// <param name="serializerOptions">The JSON serialization options to use.</param> | ||
| /// <param name="options">Configuration parameters for controlling the agent's invocation behavior.</param> | ||
| /// <param name="useJsonSchemaResponseFormat"> | ||
| /// <see langword="true" /> to set a JSON schema on the <see cref="ChatResponseFormat"/>; otherwise, <see langword="false" />. The default is <see langword="true" />. | ||
| /// Using a JSON schema improves reliability if the underlying model supports native structured output with a schema, but might cause an error if the model does not support it. | ||
| /// </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="ChatClientAgentRunResponse{T}"/> with the agent's output.</returns> | ||
| public static Task<ChatClientAgentRunResponse<T>> RunAsync<T>( | ||
| this ChatClientAgent agent, | ||
| IEnumerable<ChatMessage> messages, | ||
| AgentThread? thread, | ||
| JsonSerializerOptions? serializerOptions, | ||
| ChatClientAgentRunOptions options, | ||
| bool? useJsonSchemaResponseFormat = null, | ||
| CancellationToken cancellationToken = default) => | ||
| agent.RunAsync<T>(messages, thread, serializerOptions, (AgentRunOptions)options, useJsonSchemaResponseFormat, cancellationToken); | ||
| } | ||
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.