# AspireAzureAIInferenceExtensions Methods

- Package: [Aspire.Azure.AI.Inference](/reference/api/csharp/aspire.azure.ai.inference.md)
- Type: [AspireAzureAIInferenceExtensions](/reference/api/csharp/aspire.azure.ai.inference/aspireazureaiinferenceextensions.md)
- Kind: `Methods`
- Members: `8`

Extension methods for adding Azure AI Inference services to an Aspire application.

## AddAzureChatCompletionsClient(IHostApplicationBuilder, string, Action<ChatCompletionsClientSettings>, Action<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>>)

- Name: `AddAzureChatCompletionsClient(IHostApplicationBuilder, string, Action<ChatCompletionsClientSettings>, Action<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>>)`
- Modifiers: `extension`
- Returns: [AspireChatCompletionsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspirechatcompletionsclientbuilder.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Components/Aspire.Azure.AI.Inference/AspireAzureAIInferenceExtensions.cs#L65-L76)

Adds a `Inference.ChatCompletionsClient` to the application and configures it with the specified settings.

```csharp
public static class AspireAzureAIInferenceExtensions
{
    public static AspireChatCompletionsClientBuilder AddAzureChatCompletionsClient(
        this IHostApplicationBuilder builder,
        string connectionName,
        Action<ChatCompletionsClientSettings>? configureSettings = null,
        Action<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>>? configureClientBuilder = null)
    {
        // ...
    }
}
```

## Parameters

- `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](/reference/api/csharp/aspire.azure.ai.inference/chatcompletionsclientsettings.md).
- `configureClientBuilder` (`Action<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>>`) `optional`
  An optional callback to configure the `Extensions.IAzureClientBuilder`2` for the client.

## Returns

[AspireChatCompletionsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspirechatcompletionsclientbuilder.md) -- An [AspireChatCompletionsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspirechatcompletionsclientbuilder.md) that can be used to further configure the client.

## Exceptions

- `InvalidOperationException` -- Thrown 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:

```csharp
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>>)

- Name: `AddAzureEmbeddingsClient(IHostApplicationBuilder, string, Action<ChatCompletionsClientSettings>, Action<IAzureClientBuilder<EmbeddingsClient, AzureAIInferenceClientOptions>>)`
- Modifiers: `extension`
- Returns: [AspireEmbeddingsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspireembeddingsclientbuilder.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Components/Aspire.Azure.AI.Inference/AspireAzureAIInferenceExtensions.cs#L321-L332)

Adds a `Inference.EmbeddingsClient` to the application and configures it with the specified settings.

```csharp
public static class AspireAzureAIInferenceExtensions
{
    public static AspireEmbeddingsClientBuilder AddAzureEmbeddingsClient(
        this IHostApplicationBuilder builder,
        string connectionName,
        Action<ChatCompletionsClientSettings>? configureSettings = null,
        Action<IAzureClientBuilder<EmbeddingsClient, AzureAIInferenceClientOptions>>? configureClientBuilder = null)
    {
        // ...
    }
}
```

## Parameters

- `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](/reference/api/csharp/aspire.azure.ai.inference/chatcompletionsclientsettings.md).
- `configureClientBuilder` (`Action<IAzureClientBuilder<EmbeddingsClient, AzureAIInferenceClientOptions>>`) `optional`
  An optional callback to configure the `Extensions.IAzureClientBuilder`2` for the client.

## Returns

[AspireEmbeddingsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspireembeddingsclientbuilder.md) -- An [AspireEmbeddingsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspireembeddingsclientbuilder.md) that can be used to further configure the client.

## Exceptions

- `InvalidOperationException` -- Thrown 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:

```csharp
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?)

- Name: `AddChatClient(AspireChatCompletionsClientBuilder, string?)`
- Modifiers: `extension`
- Returns: `ChatClientBuilder`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Components/Aspire.Azure.AI.Inference/AspireAzureAIInferenceExtensions.cs#L226-L229)

Creates a `AI.IChatClient` from the `Inference.ChatCompletionsClient` registered in the service collection.

```csharp
public static class AspireAzureAIInferenceExtensions
{
    public static ChatClientBuilder AddChatClient(
        this AspireChatCompletionsClientBuilder builder,
        string? deploymentName = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` ([AspireChatCompletionsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspirechatcompletionsclientbuilder.md))
  An [AspireChatCompletionsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspirechatcompletionsclientbuilder.md).
- `deploymentName` (`string?`) `optional`
  Optionally specifies which model deployment to use. If not specified, a value will be taken from the connection string.

## Returns

`ChatClientBuilder` -- A `AI.ChatClientBuilder`.

## Examples

The following example shows how to register an and use it in a minimal API endpoint:

```csharp
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?)

- Name: `AddEmbeddingGenerator(AspireEmbeddingsClientBuilder, string?)`
- Modifiers: `extension`
- Returns: `EmbeddingGeneratorBuilder<string, Embedding<float>>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Components/Aspire.Azure.AI.Inference/AspireAzureAIInferenceExtensions.cs#L479-L482)

Creates a `AI.IEmbeddingGenerator`2` from the `Inference.EmbeddingsClient` registered in the service collection.

```csharp
public static class AspireAzureAIInferenceExtensions
{
    public static EmbeddingGeneratorBuilder<string, Embedding<float>> AddEmbeddingGenerator(
        this AspireEmbeddingsClientBuilder builder,
        string? deploymentName = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` ([AspireEmbeddingsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspireembeddingsclientbuilder.md))
  An [AspireEmbeddingsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspireembeddingsclientbuilder.md).
- `deploymentName` (`string?`) `optional`
  Optionally 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:

```csharp
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>>)

- Name: `AddKeyedAzureChatCompletionsClient(IHostApplicationBuilder, string, Action<ChatCompletionsClientSettings>, Action<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>>)`
- Modifiers: `extension`
- Returns: [AspireChatCompletionsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspirechatcompletionsclientbuilder.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Components/Aspire.Azure.AI.Inference/AspireAzureAIInferenceExtensions.cs#L116-L127)

Adds a `Inference.ChatCompletionsClient` to the application and configures it with the specified settings.

```csharp
public static class AspireAzureAIInferenceExtensions
{
    public static AspireChatCompletionsClientBuilder AddKeyedAzureChatCompletionsClient(
        this IHostApplicationBuilder builder,
        string name,
        Action<ChatCompletionsClientSettings>? configureSettings = null,
        Action<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>>? configureClientBuilder = null)
    {
        // ...
    }
}
```

## Parameters

- `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](/reference/api/csharp/aspire.azure.ai.inference/chatcompletionsclientsettings.md).
- `configureClientBuilder` (`Action<IAzureClientBuilder<ChatCompletionsClient, AzureAIInferenceClientOptions>>`) `optional`
  An optional callback to configure the `Extensions.IAzureClientBuilder`2` for the client.

## Returns

[AspireChatCompletionsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspirechatcompletionsclientbuilder.md) -- An [AspireChatCompletionsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspirechatcompletionsclientbuilder.md) that can be used to further configure the client.

## Exceptions

- `InvalidOperationException` -- Thrown 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:

```csharp
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>>)

- Name: `AddKeyedAzureEmbeddingsClient(IHostApplicationBuilder, string, Action<ChatCompletionsClientSettings>, Action<IAzureClientBuilder<EmbeddingsClient, AzureAIInferenceClientOptions>>)`
- Modifiers: `extension`
- Returns: [AspireEmbeddingsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspireembeddingsclientbuilder.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Components/Aspire.Azure.AI.Inference/AspireAzureAIInferenceExtensions.cs#L372-L383)

Adds a `Inference.EmbeddingsClient` to the application and configures it with the specified settings.

```csharp
public static class AspireAzureAIInferenceExtensions
{
    public static AspireEmbeddingsClientBuilder AddKeyedAzureEmbeddingsClient(
        this IHostApplicationBuilder builder,
        string name,
        Action<ChatCompletionsClientSettings>? configureSettings = null,
        Action<IAzureClientBuilder<EmbeddingsClient, AzureAIInferenceClientOptions>>? configureClientBuilder = null)
    {
        // ...
    }
}
```

## Parameters

- `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](/reference/api/csharp/aspire.azure.ai.inference/chatcompletionsclientsettings.md).
- `configureClientBuilder` (`Action<IAzureClientBuilder<EmbeddingsClient, AzureAIInferenceClientOptions>>`) `optional`
  An optional callback to configure the `Extensions.IAzureClientBuilder`2` for the client.

## Returns

[AspireEmbeddingsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspireembeddingsclientbuilder.md) -- An [AspireEmbeddingsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspireembeddingsclientbuilder.md) that can be used to further configure the client.

## Exceptions

- `InvalidOperationException` -- Thrown 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:

```csharp
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?)

- Name: `AddKeyedChatClient(AspireChatCompletionsClientBuilder, string, string?)`
- Modifiers: `extension`
- Returns: `ChatClientBuilder`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Components/Aspire.Azure.AI.Inference/AspireAzureAIInferenceExtensions.cs#L255-L261)

Creates a `AI.IChatClient` from the `Inference.ChatCompletionsClient` registered in the service collection.

```csharp
public static class AspireAzureAIInferenceExtensions
{
    public static ChatClientBuilder AddKeyedChatClient(
        this AspireChatCompletionsClientBuilder builder,
        string serviceKey,
        string? deploymentName = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` ([AspireChatCompletionsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspirechatcompletionsclientbuilder.md))
  An [AspireChatCompletionsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspirechatcompletionsclientbuilder.md).
- `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.

## Returns

`ChatClientBuilder` -- A `AI.ChatClientBuilder`.

## Examples

The following example shows how to register a keyed and use it in a minimal API endpoint:

```csharp
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?)

- Name: `AddKeyedEmbeddingGenerator(AspireEmbeddingsClientBuilder, string, string?)`
- Modifiers: `extension`
- Returns: `EmbeddingGeneratorBuilder<string, Embedding<float>>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Components/Aspire.Azure.AI.Inference/AspireAzureAIInferenceExtensions.cs#L508-L514)

Creates a `AI.IEmbeddingGenerator`2` from the `Inference.EmbeddingsClient` registered in the service collection.

```csharp
public static class AspireAzureAIInferenceExtensions
{
    public static EmbeddingGeneratorBuilder<string, Embedding<float>> AddKeyedEmbeddingGenerator(
        this AspireEmbeddingsClientBuilder builder,
        string serviceKey,
        string? deploymentName = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` ([AspireEmbeddingsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspireembeddingsclientbuilder.md))
  An [AspireEmbeddingsClientBuilder](/reference/api/csharp/aspire.azure.ai.inference/aspireembeddingsclientbuilder.md).
- `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.

## 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:

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