Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Microsoft.Extensions.AI;

/// <summary>Represents the options for a chat request.</summary>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai#provide-options">Provide options.</related>
public class ChatOptions
{
/// <summary>Gets or sets an optional identifier used to associate a request with an existing conversation.</summary>
Expand All @@ -20,6 +21,7 @@ public string? ChatThreadId
}

/// <summary>Gets or sets an optional identifier used to associate a request with an existing conversation.</summary>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai#stateless-vs-stateful-clients">Stateless vs. stateful clients.</related>
public string? ConversationId { get; set; }

/// <summary>Gets or sets the temperature for generating chat responses.</summary>
Expand Down Expand Up @@ -115,6 +117,7 @@ public string? ChatThreadId
public ChatToolMode? ToolMode { get; set; }

/// <summary>Gets or sets the list of tools to include with a chat request.</summary>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai#tool-calling">Tool calling.</related>
[JsonIgnore]
public IList<AITool>? Tools { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public string? ChatThreadId
/// or may not differ on every response, depending on whether the underlying provider uses a fixed ID for each conversation
/// or updates it for each message.
/// </remarks>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai#stateless-vs-stateful-clients">Stateless vs. stateful clients.</related>
public string? ConversationId { get; set; }

/// <summary>Gets or sets the model ID used in the creation of the chat response.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Microsoft.Extensions.AI;
/// This is recommended as a base type when building clients that can be chained around an underlying <see cref="IChatClient"/>.
/// The default implementation simply passes each call to the inner client instance.
/// </remarks>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai#custom-ichatclient-middleware">Custom IChatClient middleware.</related>
public class DelegatingChatClient : IChatClient
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace Microsoft.Extensions.AI;
/// </para>
/// </remarks>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/quickstarts/build-chat-app">Build an AI chat app with .NET.</related>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai#the-ichatclient-interface">The IChatClient interface.</related>
public interface IChatClient : IDisposable
{
/// <summary>Sends chat messages and returns the response.</summary>
Expand All @@ -32,6 +33,7 @@ public interface IChatClient : IDisposable
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>The response messages generated by the client.</returns>
/// <exception cref="ArgumentNullException"><paramref name="messages"/> is <see langword="null"/>.</exception>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai#request-a-chat-response">Request a chat response.</related>
Task<ChatResponse> GetResponseAsync(
IEnumerable<ChatMessage> messages,
ChatOptions? options = null,
Expand All @@ -43,6 +45,7 @@ Task<ChatResponse> GetResponseAsync(
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>The response messages generated by the client.</returns>
/// <exception cref="ArgumentNullException"><paramref name="messages"/> is <see langword="null"/>.</exception>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai#request-a-streaming-chat-response">Request a streaming chat response.</related>
IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(
IEnumerable<ChatMessage> messages,
ChatOptions? options = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace Microsoft.Extensions.AI;
/// no <see cref="IEmbeddingGenerator{TInput, TEmbedding}"/> instances are used which might employ such mutation.
/// </para>
/// </remarks>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai#the-iembeddinggenerator-interface">The IEmbeddingGenerator interface.</related>
public interface IEmbeddingGenerator<in TInput, TEmbedding> : IEmbeddingGenerator
where TEmbedding : Embedding
{
Expand All @@ -33,6 +34,7 @@ public interface IEmbeddingGenerator<in TInput, TEmbedding> : IEmbeddingGenerato
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>The generated embeddings.</returns>
/// <exception cref="ArgumentNullException"><paramref name="values"/> is <see langword="null"/>.</exception>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai#create-embeddings">Create embeddings.</related>
Task<GeneratedEmbeddings<TEmbedding>> GenerateAsync(
IEnumerable<TInput> values,
EmbeddingGenerationOptions? options = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public IChatClient Build(IServiceProvider? services = null)
/// <param name="clientFactory">The client factory function.</param>
/// <returns>The updated <see cref="ChatClientBuilder"/> instance.</returns>
/// <exception cref="ArgumentNullException"><paramref name="clientFactory"/> is <see langword="null"/>.</exception>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai#functionality-pipelines">Pipelines of functionality.</related>
public ChatClientBuilder Use(Func<IChatClient, IChatClient> clientFactory)
{
_ = Throw.IfNull(clientFactory);
Expand All @@ -77,6 +78,7 @@ public ChatClientBuilder Use(Func<IChatClient, IChatClient> clientFactory)
/// <param name="clientFactory">The client factory function.</param>
/// <returns>The updated <see cref="ChatClientBuilder"/> instance.</returns>
/// <exception cref="ArgumentNullException"><paramref name="clientFactory"/> is <see langword="null"/>.</exception>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai#functionality-pipelines">Pipelines of functionality.</related>
public ChatClientBuilder Use(Func<IChatClient, IServiceProvider, IChatClient> clientFactory)
{
_ = Throw.IfNull(clientFactory);
Expand All @@ -98,10 +100,11 @@ public ChatClientBuilder Use(Func<IChatClient, IServiceProvider, IChatClient> cl
/// </param>
/// <returns>The updated <see cref="ChatClientBuilder"/> instance.</returns>
/// <remarks>
/// This overload may be used when the anonymous implementation needs to provide pre- and/or post-processing, but doesn't
/// This overload can be used when the anonymous implementation needs to provide pre-processing and/or post-processing, but doesn't
/// need to interact with the results of the operation, which will come from the inner client.
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="sharedFunc"/> is <see langword="null"/>.</exception>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai#functionality-pipelines">Pipelines of functionality.</related>
public ChatClientBuilder Use(Func<IEnumerable<ChatMessage>, ChatOptions?, Func<IEnumerable<ChatMessage>, ChatOptions?, CancellationToken, Task>, CancellationToken, Task> sharedFunc)
{
_ = Throw.IfNull(sharedFunc);
Expand All @@ -125,7 +128,7 @@ public ChatClientBuilder Use(Func<IEnumerable<ChatMessage>, ChatOptions?, Func<I
/// </param>
/// <returns>The updated <see cref="ChatClientBuilder"/> instance.</returns>
/// <remarks>
/// One or both delegates may be provided. If both are provided, they will be used for their respective methods:
/// One or both delegates can be provided. If both are provided, they will be used for their respective methods:
/// <paramref name="getResponseFunc"/> will provide the implementation of <see cref="IChatClient.GetResponseAsync"/>, and
/// <paramref name="getStreamingResponseFunc"/> will provide the implementation of <see cref="IChatClient.GetStreamingResponseAsync"/>.
/// If only one of the delegates is provided, it will be used for both methods. That means that if <paramref name="getResponseFunc"/>
Expand All @@ -135,6 +138,7 @@ public ChatClientBuilder Use(Func<IEnumerable<ChatMessage>, ChatOptions?, Func<I
/// <see cref="IChatClient.GetResponseAsync"/> will be implemented by combining the updates from <paramref name="getStreamingResponseFunc"/>.
/// </remarks>
/// <exception cref="ArgumentNullException">Both <paramref name="getResponseFunc"/> and <paramref name="getStreamingResponseFunc"/> are <see langword="null"/>.</exception>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai#functionality-pipelines">Pipelines of functionality.</related>
public ChatClientBuilder Use(
Func<IEnumerable<ChatMessage>, ChatOptions?, IChatClient, CancellationToken, Task<ChatResponse>>? getResponseFunc,
Func<IEnumerable<ChatMessage>, ChatOptions?, IChatClient, CancellationToken, IAsyncEnumerable<ChatResponseUpdate>>? getStreamingResponseFunc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Microsoft.Extensions.AI;
/// <summary>
/// Provides extension methods on <see cref="IChatClient"/> that simplify working with structured output.
/// </summary>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/quickstarts/structured-output">Request a response with structured output.</related>
public static class ChatClientStructuredOutputExtensions
{
private static readonly AIJsonSchemaCreateOptions _inferenceOptions = new()
Expand All @@ -33,9 +34,8 @@ public static class ChatClientStructuredOutputExtensions
/// <param name="messages">The chat content to send.</param>
/// <param name="options">The chat options to configure the request.</param>
/// <param name="useJsonSchema">
/// Optionally specifies whether to set a JSON schema on the <see cref="ChatResponseFormat"/>.
/// This improves reliability if the underlying model supports native structured output with a schema, but may cause an error if the model does not support it.
/// If not specified, the default value is <see langword="true" />.
/// <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>The response messages generated by the client.</returns>
Expand All @@ -53,14 +53,15 @@ public static Task<ChatResponse<T>> GetResponseAsync<T>(
/// <param name="chatMessage">The text content for the chat message to send.</param>
/// <param name="options">The chat options to configure the request.</param>
/// <param name="useJsonSchema">
/// Optionally specifies whether to set a JSON schema on the <see cref="ChatResponseFormat"/>.
/// This improves reliability if the underlying model supports native structured output with a schema, but may cause an error if the model does not support it.
/// <see langword="true" /> to set a JSON schema on the <see cref="ChatResponseFormat"/>; otherwise, <see langword="false" />.
/// 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.
/// If not specified, the default value is determined by the implementation.
/// If a specific value is required, it must be specified by the caller.
/// </param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>The response messages generated by the client.</returns>
/// <typeparam name="T">The type of structured output to request.</typeparam>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/quickstarts/structured-output">Request a response with structured output.</related>
public static Task<ChatResponse<T>> GetResponseAsync<T>(
this IChatClient chatClient,
string chatMessage,
Expand All @@ -74,9 +75,8 @@ public static Task<ChatResponse<T>> GetResponseAsync<T>(
/// <param name="chatMessage">The chat message to send.</param>
/// <param name="options">The chat options to configure the request.</param>
/// <param name="useJsonSchema">
/// Optionally specifies whether to set a JSON schema on the <see cref="ChatResponseFormat"/>.
/// This improves reliability if the underlying model supports native structured output with a schema, but may cause an error if the model does not support it.
/// If not specified, the default value is <see langword="true" />.
/// <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>The response messages generated by the client.</returns>
Expand All @@ -95,9 +95,8 @@ public static Task<ChatResponse<T>> GetResponseAsync<T>(
/// <param name="serializerOptions">The JSON serialization options to use.</param>
/// <param name="options">The chat options to configure the request.</param>
/// <param name="useJsonSchema">
/// Optionally specifies whether to set a JSON schema on the <see cref="ChatResponseFormat"/>.
/// This improves reliability if the underlying model supports native structured output with a schema, but may cause an error if the model does not support it.
/// If not specified, the default value is <see langword="true" />.
/// <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>The response messages generated by the client.</returns>
Expand All @@ -117,9 +116,8 @@ public static Task<ChatResponse<T>> GetResponseAsync<T>(
/// <param name="serializerOptions">The JSON serialization options to use.</param>
/// <param name="options">The chat options to configure the request.</param>
/// <param name="useJsonSchema">
/// Optionally specifies whether to set a JSON schema on the <see cref="ChatResponseFormat"/>.
/// This improves reliability if the underlying model supports native structured output with a schema, but may cause an error if the model does not support it.
/// If not specified, the default value is <see langword="true" />.
/// <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>The response messages generated by the client.</returns>
Expand All @@ -139,16 +137,13 @@ public static Task<ChatResponse<T>> GetResponseAsync<T>(
/// <param name="serializerOptions">The JSON serialization options to use.</param>
/// <param name="options">The chat options to configure the request.</param>
/// <param name="useJsonSchema">
/// Optionally specifies whether to set a JSON schema on the <see cref="ChatResponseFormat"/>.
/// This improves reliability if the underlying model supports native structured output with a schema, but may cause an error if the model does not support it.
/// If not specified, the default value is <see langword="true" />.
/// <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>The response messages generated by the client.</returns>
/// <typeparam name="T">The type of structured output to request.</typeparam>
/// <exception cref="ArgumentNullException"><paramref name="chatClient"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="messages"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="serializerOptions"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="chatClient"/> or <paramref name="messages"/> or <paramref name="serializerOptions"/> is <see langword="null"/>.</exception>
public static async Task<ChatResponse<T>> GetResponseAsync<T>(
this IChatClient chatClient,
IEnumerable<ChatMessage> messages,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static class ConfigureOptionsChatClientBuilderExtensions
/// <returns>The <paramref name="builder"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="configure"/> is <see langword="null"/>.</exception>
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai#provide-options">Provide options.</related>
public static ChatClientBuilder ConfigureOptions(
this ChatClientBuilder builder, Action<ChatOptions> configure)
{
Expand Down
Loading