In this article, you learn how to use the .NET Aspire Azure OpenAI client. The Aspire.Azure.AI.OpenAI library is used to register an OpenAIClient in the dependency injection (DI) container for consuming Azure OpenAI or OpenAI functionality. It enables corresponding logging and telemetry.
To get started with the .NET Aspire Azure OpenAI integration, install the 📦 Aspire.Azure.AI.OpenAI NuGet package in the client-consuming project, i.e., the project for the application that uses the Azure OpenAI client.
In the Program.cs file of your client-consuming project, call the extension method to register an OpenAIClient for use via the dependency injection container. The method takes a connection name parameter.
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...
}
In your app host project, register an Azure OpenAI resource using the following methods, such as AddAzureOpenAI:
var builder = DistributedApplication.CreateBuilder(args);
var openai = builder.ExecutionContext.IsPublishMode
? builder.AddAzureOpenAI("openAiConnectionName")
: builder.AddConnectionString("openAiConnectionName");
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 OpenAI integration 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");
The connection string is retrieved from the ConnectionStrings configuration section, and there are two supported formats, either the account endpoint used in conjunction with the default Azure credential or a connection string with the account key.
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.
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 OpenAI integration 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:
Also you can pass the Action<AzureOpenAISettings> configureSettings delegate to set up some or all the options inline, for example to disable tracing from code:
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:
.NET Aspire integrations automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. For more information about integration observability and telemetry, see .NET Aspire integrations overview. Depending on the backing service, some integrations may only support some of these features. For example, some integrations 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 OpenAI integration uses the following log categories:
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
.NET Aspire feedback
.NET Aspire is an open source project. Select a link to provide feedback: