AspireAzureAIInferenceExtensions Methods
AddAzureChatCompletionsClient(IHostApplicationBuilder, string, Action<ChatCompletionsClientSettings>, Action<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>>)Section titled AddAzureChatCompletionsClient(IHostApplicationBuilder, string, Action<ChatCompletionsClientSettings>, Action<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>>)extensionAspireChatCompletionsClientBuilderInference.ChatCompletionsClient to the application and configures it with the specified settings. public static class AspireAzureAIInferenceExtensions{ public static AspireChatCompletionsClientBuilder AddAzureChatCompletionsClient( this IHostApplicationBuilder builder, string connectionName, Action<ChatCompletionsClientSettings>? configureSettings = null, Action<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>>? configureClientBuilder = null) { // ... }}Parameters
builderIHostApplicationBuilderThe Hosting.IHostApplicationBuilder to add the client to.connectionNamestringThe name of the client. This is used to retrieve the connection string from configuration.configureSettingsAction<ChatCompletionsClientSettings>optionalAn optional callback to configure the ChatCompletionsClientSettings.configureClientBuilderAction<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>>optionalAn optional callback to configure the Extensions.IAzureClientBuilder`2 for the client.Returns
AspireChatCompletionsClientBuilderAn AspireChatCompletionsClientBuilder that can be used to further configure the client.Exceptions
InvalidOperationExceptionThrown when endpoint is missing from settings.Remarks
The client is registered as a singleton.
Configuration is loaded from the "Aspire:Azure:AI:Inference" section, and can be supplemented with a connection string named after the connectionName parameter.
Examples
The following example shows how to register a and use it in a minimal API endpoint:
var builder = WebApplication.CreateBuilder(args);builder.AddAzureChatCompletionsClient("chat");
var app = builder.Build();app.MapGet("/chat", async (ChatCompletionsClient client) =>{ var response = await client.CompleteAsync("Hello, how are you?"); return response.Value.Content;});AddAzureEmbeddingsClient(IHostApplicationBuilder, string, Action<ChatCompletionsClientSettings>, Action<IAzureClientBuilder<EmbeddingsClient, AzureAIInferenceClientOptions>>)Section titled AddAzureEmbeddingsClient(IHostApplicationBuilder, string, Action<ChatCompletionsClientSettings>, Action<IAzureClientBuilder<EmbeddingsClient, AzureAIInferenceClientOptions>>)extensionAspireEmbeddingsClientBuilderInference.EmbeddingsClient to the application and configures it with the specified settings. public static class AspireAzureAIInferenceExtensions{ public static AspireEmbeddingsClientBuilder AddAzureEmbeddingsClient( this IHostApplicationBuilder builder, string connectionName, Action<ChatCompletionsClientSettings>? configureSettings = null, Action<IAzureClientBuilder<EmbeddingsClient, AzureAIInferenceClientOptions>>? configureClientBuilder = null) { // ... }}Parameters
builderIHostApplicationBuilderThe Hosting.IHostApplicationBuilder to add the client to.connectionNamestringThe name of the client. This is used to retrieve the connection string from configuration.configureSettingsAction<ChatCompletionsClientSettings>optionalAn optional callback to configure the ChatCompletionsClientSettings.configureClientBuilderAction<IAzureClientBuilder<EmbeddingsClient, AzureAIInferenceClientOptions>>optionalAn optional callback to configure the Extensions.IAzureClientBuilder`2 for the client.Returns
AspireEmbeddingsClientBuilderAn AspireEmbeddingsClientBuilder that can be used to further configure the client.Exceptions
InvalidOperationExceptionThrown when endpoint is missing from settings.Remarks
The client is registered as a singleton.
Configuration is loaded from the "Aspire:Azure:AI:Inference" section, and can be supplemented with a connection string named after the connectionName parameter.
Examples
The following example shows how to register an and use it in a minimal API endpoint:
var builder = WebApplication.CreateBuilder(args);builder.AddAzureEmbeddingsClient("embeddings");
var app = builder.Build();app.MapGet("/embed", async (EmbeddingsClient client) =>{ var response = await client.EmbedAsync("Hello, world!"); return response.Value.Data;});AddChatClient(AspireChatCompletionsClientBuilder, string?)Section titled AddChatClient(AspireChatCompletionsClientBuilder, string?)extensionChatClientBuilderAI.IChatClient from the Inference.ChatCompletionsClient registered in the service collection. public static class AspireAzureAIInferenceExtensions{ public static ChatClientBuilder AddChatClient( this AspireChatCompletionsClientBuilder builder, string? deploymentName = null) { // ... }}Parameters
deploymentNamestring?optionalOptionally specifies which model deployment to use. If not specified, a value will be taken from the connection string.Returns
ChatClientBuilderA AI.ChatClientBuilder.Examples
The following example shows how to register an and use it in a minimal API endpoint:
var builder = WebApplication.CreateBuilder(args);builder.AddAzureChatCompletionsClient("chat").AddChatClient();
var app = builder.Build();app.MapGet("/chat", async (IChatClient client) =>{ var response = await client.GetResponseAsync("Hello, how are you?"); return response.Text;});AddEmbeddingGenerator(AspireEmbeddingsClientBuilder, string?)Section titled AddEmbeddingGenerator(AspireEmbeddingsClientBuilder, string?)extensionEmbeddingGeneratorBuilder<string, Embedding<float>>AI.IEmbeddingGenerator`2 from the Inference.EmbeddingsClient registered in the service collection. public static class AspireAzureAIInferenceExtensions{ public static EmbeddingGeneratorBuilder<string, Embedding<float>> AddEmbeddingGenerator( this AspireEmbeddingsClientBuilder builder, string? deploymentName = null) { // ... }}Parameters
deploymentNamestring?optionalOptionally specifies which model deployment to use. If not specified, a value will be taken from the connection string.Returns
EmbeddingGeneratorBuilder<string, Embedding<float>>An AI.EmbeddingGeneratorBuilder`2.Examples
The following example shows how to register an and use it in a minimal API endpoint:
var builder = WebApplication.CreateBuilder(args);builder.AddAzureEmbeddingsClient("embeddings").AddEmbeddingGenerator();
var app = builder.Build();app.MapGet("/embed", async (IEmbeddingGenerator<string, Embedding<float>> generator) =>{ var embedding = await generator.GenerateEmbeddingAsync("Hello, world!"); return embedding.Vector.ToArray();});AddKeyedAzureChatCompletionsClient(IHostApplicationBuilder, string, Action<ChatCompletionsClientSettings>, Action<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>>)Section titled AddKeyedAzureChatCompletionsClient(IHostApplicationBuilder, string, Action<ChatCompletionsClientSettings>, Action<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>>)extensionAspireChatCompletionsClientBuilderInference.ChatCompletionsClient to the application and configures it with the specified settings. public static class AspireAzureAIInferenceExtensions{ public static AspireChatCompletionsClientBuilder AddKeyedAzureChatCompletionsClient( this IHostApplicationBuilder builder, string name, Action<ChatCompletionsClientSettings>? configureSettings = null, Action<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>>? configureClientBuilder = null) { // ... }}Parameters
builderIHostApplicationBuilderThe Hosting.IHostApplicationBuilder to add the client to.namestringThe name of the component, which is used as the ServiceDescriptor.ServiceKey of the service and also to retrieve the connection string from the ConnectionStrings configuration section.configureSettingsAction<ChatCompletionsClientSettings>optionalAn optional callback to configure the ChatCompletionsClientSettings.configureClientBuilderAction<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>>optionalAn optional callback to configure the Extensions.IAzureClientBuilder`2 for the client.Returns
AspireChatCompletionsClientBuilderAn AspireChatCompletionsClientBuilder that can be used to further configure the client.Exceptions
InvalidOperationExceptionThrown when endpoint is missing from settings.Remarks
The client is registered as a singleton with a keyed service.
Configuration is loaded from the "Aspire:Azure:AI:Inference" section, and can be supplemented with a connection string named after the name parameter.
Examples
The following example shows how to register a keyed and use it in a minimal API endpoint:
var builder = WebApplication.CreateBuilder(args);builder.AddKeyedAzureChatCompletionsClient("chat");
var app = builder.Build();app.MapGet("/chat", async ([FromKeyedServices("chat")] ChatCompletionsClient client) =>{ var response = await client.CompleteAsync("Hello, how are you?"); return response.Value.Content;});AddKeyedAzureEmbeddingsClient(IHostApplicationBuilder, string, Action<ChatCompletionsClientSettings>, Action<IAzureClientBuilder<EmbeddingsClient, AzureAIInferenceClientOptions>>)Section titled AddKeyedAzureEmbeddingsClient(IHostApplicationBuilder, string, Action<ChatCompletionsClientSettings>, Action<IAzureClientBuilder<EmbeddingsClient, AzureAIInferenceClientOptions>>)extensionAspireEmbeddingsClientBuilderInference.EmbeddingsClient to the application and configures it with the specified settings. public static class AspireAzureAIInferenceExtensions{ public static AspireEmbeddingsClientBuilder AddKeyedAzureEmbeddingsClient( this IHostApplicationBuilder builder, string name, Action<ChatCompletionsClientSettings>? configureSettings = null, Action<IAzureClientBuilder<EmbeddingsClient, AzureAIInferenceClientOptions>>? configureClientBuilder = null) { // ... }}Parameters
builderIHostApplicationBuilderThe Hosting.IHostApplicationBuilder to add the client to.namestringThe name of the component, which is used as the ServiceDescriptor.ServiceKey of the service and also to retrieve the connection string from the ConnectionStrings configuration section.configureSettingsAction<ChatCompletionsClientSettings>optionalAn optional callback to configure the ChatCompletionsClientSettings.configureClientBuilderAction<IAzureClientBuilder<EmbeddingsClient, AzureAIInferenceClientOptions>>optionalAn optional callback to configure the Extensions.IAzureClientBuilder`2 for the client.Returns
AspireEmbeddingsClientBuilderAn AspireEmbeddingsClientBuilder that can be used to further configure the client.Exceptions
InvalidOperationExceptionThrown when endpoint is missing from settings.Remarks
The client is registered as a singleton with a keyed service.
Configuration is loaded from the "Aspire:Azure:AI:Inference" section, and can be supplemented with a connection string named after the name parameter.
Examples
The following example shows how to register a keyed and use it in a minimal API endpoint:
var builder = WebApplication.CreateBuilder(args);builder.AddKeyedAzureEmbeddingsClient("embeddings");
var app = builder.Build();app.MapGet("/embed", async ([FromKeyedServices("embeddings")] EmbeddingsClient client) =>{ var response = await client.EmbedAsync("Hello, world!"); return response.Value.Data;});AddKeyedChatClient(AspireChatCompletionsClientBuilder, string, string?)Section titled AddKeyedChatClient(AspireChatCompletionsClientBuilder, string, string?)extensionChatClientBuilderAI.IChatClient from the Inference.ChatCompletionsClient registered in the service collection. public static class AspireAzureAIInferenceExtensions{ public static ChatClientBuilder AddKeyedChatClient( this AspireChatCompletionsClientBuilder builder, string serviceKey, string? deploymentName = null) { // ... }}Parameters
serviceKeystringThe service key with which the AI.IChatClient will be registered.deploymentNamestring?optionalOptionally specifies which model deployment to use. If not specified, a value will be taken from the connection string.Returns
ChatClientBuilderA AI.ChatClientBuilder.Examples
The following example shows how to register a keyed and use it in a minimal API endpoint:
var builder = WebApplication.CreateBuilder(args);builder.AddKeyedAzureChatCompletionsClient("chat").AddKeyedChatClient("chat");
var app = builder.Build();app.MapGet("/chat", async ([FromKeyedServices("chat")] IChatClient client) =>{ var response = await client.GetResponseAsync("Hello, how are you?"); return response.Text;});AddKeyedEmbeddingGenerator(AspireEmbeddingsClientBuilder, string, string?)Section titled AddKeyedEmbeddingGenerator(AspireEmbeddingsClientBuilder, string, string?)extensionEmbeddingGeneratorBuilder<string, Embedding<float>>AI.IEmbeddingGenerator`2 from the Inference.EmbeddingsClient registered in the service collection. public static class AspireAzureAIInferenceExtensions{ public static EmbeddingGeneratorBuilder<string, Embedding<float>> AddKeyedEmbeddingGenerator( this AspireEmbeddingsClientBuilder builder, string serviceKey, string? deploymentName = null) { // ... }}Parameters
serviceKeystringThe service key with which the AI.IEmbeddingGenerator`2 will be registered.deploymentNamestring?optionalOptionally specifies which model deployment to use. If not specified, a value will be taken from the connection string.Returns
EmbeddingGeneratorBuilder<string, Embedding<float>>An AI.EmbeddingGeneratorBuilder`2.Examples
The following example shows how to register a keyed and use it in a minimal API endpoint:
var builder = WebApplication.CreateBuilder(args);builder.AddKeyedAzureEmbeddingsClient("embeddings").AddKeyedEmbeddingGenerator("embeddings");
var app = builder.Build();app.MapGet("/embed", async ([FromKeyedServices("embeddings")] IEmbeddingGenerator<string, Embedding<float>> generator) =>{ var embedding = await generator.GenerateEmbeddingAsync("Hello, world!"); return embedding.Vector.ToArray();});