.NET Aspire Azure AI OpenAI component

In this article, you learn how to use the .NET Aspire Azure AI OpenAI client. The Aspire.Azure.AI.OpenAI library is used to register an OpenAIClient in the dependency injection (DI) container for consuming Azure AI OpenAI or OpenAI functionality. It enables corresponding logging and telemetry.

For more information on using the OpenAIClient, see Quickstart: Get started generating text using Azure OpenAI Service.

Get started

To get started with the .NET Aspire Azure AI OpenAI component, install the Aspire.Azure.AI.OpenAI NuGet package.

dotnet add package Aspire.Azure.AI.OpenAI --prerelease

For more information, see dotnet add package or Manage package dependencies in .NET applications.

Example usage

In the Program.cs file of your component-consuming project, call the extension method to register an OpenAIClient for use via the dependency injection container. The method takes a connection name parameter.

builder.AddAzureOpenAIClient("openAiConnectionName");

In the preceding code, the AddAzureOpenAIClient method adds an OpenAIClient to the DI container. The openAiConnectionName parameter is the name of the connection string in the configuration. You can then retrieve the OpenAIClient instance using dependency injection. For example, to retrieve the connection from an example service:

public class ExampleService(OpenAIClient client)
{
    // Use client...
}

App host usage

To add Azure AI hosting support to your IDistributedApplicationBuilder, install the Aspire.Hosting.Azure.CognitiveServices NuGet package.

dotnet add package Aspire.Hosting.Azure.CognitiveServices --prerelease

In your app host project, register an Azure AI OpenAI resource using the following methods, such as AddAzureOpenAI:

// Service registration
var openai = builder.ExecutionContext.IsPublishMode
    ? builder.AddAzureOpenAI("openAiConnectionName")
    : builder.AddConnectionString("openAiConnectionName");

// Service consumption
builder.AddProject<Projects.ExampleProject>()
       .WithReference(openai);

The AddAzureAIOpenAI method will read connection information from the app host's configuration (for example, from "user secrets") under the ConnectionStrings:openAiConnectionName config key. The WithReference method passes that connection information into a connection string named openAiConnectionName in the ExampleProject project. In the Program.cs file of ExampleProject, the connection can be consumed using:

builder.AddAzureAIOpenAI("openAiConnectionName");

Configuration

The .NET Aspire Azure AI OpenAI component provides multiple options to configure the connection based on the requirements and conventions of your project.

Use a connection string

When using a connection string from the ConnectionStrings configuration section, you can provide the name of the connection string when calling builder.AddAzureAIOpenAI:

builder.AddAzureAIOpenAI("openAiConnectionName");

And then the connection string will be retrieved from the ConnectionStrings configuration section:

Account endpoint

The recommended approach is to use an Endpoint, which works with the AzureOpenAISettings.Credential property to establish a connection. If no credential is configured, the DefaultAzureCredential is used.

{
  "ConnectionStrings": {
    "openAiConnectionName": "https://{account_name}.openai.azure.com/"
  }
}

Connection string

Alternatively, a custom connection string can be used.

{
  "ConnectionStrings": {
    "openaiConnectionName": "Endpoint=https://{account_name}.openai.azure.com/;Key={account_key};"
  }
}

In order to connect to the non-Azure OpenAI service, drop the Endpoint property and only set the Key property to set the API key.

Use configuration providers

The .NET Aspire Azure AI OpenAI component supports Microsoft.Extensions.Configuration. It loads the AzureOpenAISettings from configuration by using the Aspire:Azure:AI:OpenAI key. Example appsettings.json that configures some of the options:

{
  "Aspire": {
    "Azure": {
      "AI": {
        "OpenAI": {
          "Tracing": true,
        }
      }
    }
  }
}

Use inline delegates

Also you can pass the Action<AzureOpenAISettings> configureSettings delegate to set up some or all the options inline, for example to disable health checks from code:

builder.AddAzureAIOpenAI(
    "openAiConnectionName",
    static settings => settings.Tracing = false);

You can also setup the OpenAIClientOptions using the optional Action<IAzureClientBuilder<OpenAIClient, OpenAIClientOptions>> configureClientBuilder parameter of the AddAzureAIOpenAI method. For example, to set the client ID for this client:

builder.AddAzureAIOpenAI(
    "openAiConnectionName",
    configureClientBuilder: builder => builder.ConfigureOptions(
        options => options.Diagnostics.ApplicationId = "CLIENT_ID"));

Observability and telemetry

.NET Aspire components automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. For more information about component observability and telemetry, see .NET Aspire components overview. Depending on the backing service, some components may only support some of these features. For example, some components support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.

Logging

The .NET Aspire Azure AI OpenAI component uses the following log categories:

  • Azure
  • Azure.Core
  • Azure.Identity

See also