title | description | ms.topic | ms.date |
---|---|---|---|
.NET Aspire Azure OpenAI integration (Preview) |
Learn how to use the .NET Aspire Azure OpenAI integration. |
how-to |
02/14/2025 |
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.
For more information on using the OpenAIClient
, see Quickstart: Get started generating text using Azure OpenAI Service.
- Azure subscription: create one for free.
- Azure OpenAI or OpenAI account: create an Azure OpenAI Service resource.
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.
dotnet add package Aspire.Azure.AI.OpenAI
<PackageReference Include="Aspire.Azure.AI.OpenAI"
Version="*" />
For more information, see dotnet add package or Manage package dependencies in .NET applications.
In the :::no-loc text="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.
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...
}
To add Azure hosting support to your xref:Aspire.Hosting.IDistributedApplicationBuilder, install the 📦 Aspire.Hosting.Azure.CognitiveServices NuGet package in the app host project.
dotnet add package Aspire.Hosting.Azure.CognitiveServices
<PackageReference Include="Aspire.Hosting.Azure.CognitiveServices"
Version="*" />
In your app host project, register an Azure OpenAI resource using the following methods, such as xref:Aspire.Hosting.AzureOpenAIExtensions.AddAzureOpenAI%2A:
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 xref:Aspire.Hosting.ResourceBuilderExtensions.WithReference%2A method passes that connection information into a connection string named openAiConnectionName
in the ExampleProject
project. In the :::no-loc text="Program.cs"::: file of ExampleProject, the connection can be consumed using:
builder.AddAzureAIOpenAI("openAiConnectionName");
The .NET Aspire Azure OpenAI integration provides multiple options to configure the connection based on the requirements and conventions of your project.
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.
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 xref:Azure.Identity.DefaultAzureCredential is used.
{
"ConnectionStrings": {
"openAiConnectionName": "https://{account_name}.openai.azure.com/"
}
}
For more information, see Use Azure OpenAI without keys.
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.
The .NET Aspire Azure OpenAI integration supports xref:Microsoft.Extensions.Configuration. It loads the AzureOpenAISettings
from configuration by using the Aspire:Azure:AI:OpenAI
key. Example :::no-loc text="appsettings.json"::: that configures some of the options:
{
"Aspire": {
"Azure": {
"AI": {
"OpenAI": {
"DisableTracing": false,
}
}
}
}
}
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:
builder.AddAzureAIOpenAI(
"openAiConnectionName",
static settings => settings.DisableTracing = true);
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"));
[!INCLUDE integration-observability-and-telemetry]
The .NET Aspire Azure OpenAI integration uses the following log categories:
Azure
Azure.Core
Azure.Identity