Skip to content
Docs Try Aspire

AspireAzureAIInferenceExtensions Methods

Class Methods 8 members
Extension methods for adding Azure AI Inference services to an Aspire application.
AddAzureChatCompletionsClient(IHostApplicationBuilder, string, Action<ChatCompletionsClientSettings>, Action<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>>) Section titled AddAzureChatCompletionsClient(IHostApplicationBuilder, string, Action<ChatCompletionsClientSettings>, Action<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>>) extension AspireChatCompletionsClientBuilder
Adds a Inference.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)
{
// ...
}
}
builder IHostApplicationBuilder The Hosting.IHostApplicationBuilder to add the client to.
connectionName string The name of the client. This is used to retrieve the connection string from configuration.
configureSettings Action<ChatCompletionsClientSettings> optional An optional callback to configure the ChatCompletionsClientSettings.
configureClientBuilder Action<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>> optional An optional callback to configure the Extensions.IAzureClientBuilder`2 for the client.
AspireChatCompletionsClientBuilder An AspireChatCompletionsClientBuilder that can be used to further configure the client.
InvalidOperationException Thrown when endpoint is missing from settings.

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.

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>>) extension AspireEmbeddingsClientBuilder
Adds a Inference.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)
{
// ...
}
}
builder IHostApplicationBuilder The Hosting.IHostApplicationBuilder to add the client to.
connectionName string The name of the client. This is used to retrieve the connection string from configuration.
configureSettings Action<ChatCompletionsClientSettings> optional An optional callback to configure the ChatCompletionsClientSettings.
configureClientBuilder Action<IAzureClientBuilder<EmbeddingsClient, AzureAIInferenceClientOptions>> optional An optional callback to configure the Extensions.IAzureClientBuilder`2 for the client.
AspireEmbeddingsClientBuilder An AspireEmbeddingsClientBuilder that can be used to further configure the client.
InvalidOperationException Thrown when endpoint is missing from settings.

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.

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?) extension ChatClientBuilder
Creates a AI.IChatClient from the Inference.ChatCompletionsClient registered in the service collection.
public static class AspireAzureAIInferenceExtensions
{
public static ChatClientBuilder AddChatClient(
this AspireChatCompletionsClientBuilder builder,
string? deploymentName = null)
{
// ...
}
}
deploymentName string? optional Optionally specifies which model deployment to use. If not specified, a value will be taken from the connection string.
ChatClientBuilder A AI.ChatClientBuilder.

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?) extension EmbeddingGeneratorBuilder<string, Embedding<float>>
Creates a 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)
{
// ...
}
}
deploymentName string? optional Optionally specifies which model deployment to use. If not specified, a value will be taken from the connection string.
EmbeddingGeneratorBuilder<string, Embedding<float>> An AI.EmbeddingGeneratorBuilder`2.

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>>) extension AspireChatCompletionsClientBuilder
Adds a Inference.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)
{
// ...
}
}
builder IHostApplicationBuilder The Hosting.IHostApplicationBuilder to add the client to.
name string The 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.
configureSettings Action<ChatCompletionsClientSettings> optional An optional callback to configure the ChatCompletionsClientSettings.
configureClientBuilder Action<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>> optional An optional callback to configure the Extensions.IAzureClientBuilder`2 for the client.
AspireChatCompletionsClientBuilder An AspireChatCompletionsClientBuilder that can be used to further configure the client.
InvalidOperationException Thrown when endpoint is missing from settings.

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.

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>>) extension AspireEmbeddingsClientBuilder
Adds a Inference.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)
{
// ...
}
}
builder IHostApplicationBuilder The Hosting.IHostApplicationBuilder to add the client to.
name string The 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.
configureSettings Action<ChatCompletionsClientSettings> optional An optional callback to configure the ChatCompletionsClientSettings.
configureClientBuilder Action<IAzureClientBuilder<EmbeddingsClient, AzureAIInferenceClientOptions>> optional An optional callback to configure the Extensions.IAzureClientBuilder`2 for the client.
AspireEmbeddingsClientBuilder An AspireEmbeddingsClientBuilder that can be used to further configure the client.
InvalidOperationException Thrown when endpoint is missing from settings.

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.

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?) extension ChatClientBuilder
Creates a AI.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)
{
// ...
}
}
serviceKey string The service key with which the AI.IChatClient will be registered.
deploymentName string? optional Optionally specifies which model deployment to use. If not specified, a value will be taken from the connection string.
ChatClientBuilder A AI.ChatClientBuilder.

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?) extension EmbeddingGeneratorBuilder<string, Embedding<float>>
Creates a 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)
{
// ...
}
}
serviceKey string The service key with which the AI.IEmbeddingGenerator`2 will be registered.
deploymentName string? optional Optionally specifies which model deployment to use. If not specified, a value will be taken from the connection string.
EmbeddingGeneratorBuilder<string, Embedding<float>> An AI.EmbeddingGeneratorBuilder`2.

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();
});