Aspire.Azure.AI.OpenAI
13.1.0-preview.1.25616.3
Prefix Reserved
dotnet add package Aspire.Azure.AI.OpenAI --version 13.1.0-preview.1.25616.3
NuGet\Install-Package Aspire.Azure.AI.OpenAI -Version 13.1.0-preview.1.25616.3
<PackageReference Include="Aspire.Azure.AI.OpenAI" Version="13.1.0-preview.1.25616.3" />
<PackageVersion Include="Aspire.Azure.AI.OpenAI" Version="13.1.0-preview.1.25616.3" />
<PackageReference Include="Aspire.Azure.AI.OpenAI" />
paket add Aspire.Azure.AI.OpenAI --version 13.1.0-preview.1.25616.3
#r "nuget: Aspire.Azure.AI.OpenAI, 13.1.0-preview.1.25616.3"
#:package Aspire.Azure.AI.OpenAI@13.1.0-preview.1.25616.3
#addin nuget:?package=Aspire.Azure.AI.OpenAI&version=13.1.0-preview.1.25616.3&prerelease
#tool nuget:?package=Aspire.Azure.AI.OpenAI&version=13.1.0-preview.1.25616.3&prerelease
Aspire.Azure.AI.OpenAI library
Registers OpenAIClient as a singleton in the DI container for connecting to Azure OpenAI or OpenAI. Enables corresponding metrics, logging and telemetry.
Getting started
Prerequisites
- Azure subscription - create one for free
- Azure OpenAI or OpenAI account - create an Azure OpenAI Service resource
Install the package
Install the Aspire Azure OpenAI library with NuGet:
dotnet add package Aspire.Azure.AI.OpenAI
Usage example
In the AppHost.cs file of your project, call the AddAzureOpenAIClient extension method to register an OpenAIClient for use via the dependency injection container. The method takes a connection name parameter.
builder.AddAzureOpenAIClient("openaiConnectionName");
You can then retrieve the AzureOpenAIClient instance using dependency injection. For example, to retrieve the client from a Web API controller:
private readonly AzureOpenAIClient _client;
public CognitiveController(AzureOpenAIClient client)
{
_client = client;
}
See the Azure OpenAI Service quickstarts for examples on using the AzureOpenAIClient.
Azure-agnostic client resolution
You can retrieve the AzureOpenAIClient object using the base OpenAIClient service type. This allows for code that is not dependent on Azure OpenAI-specific features to not depend directly on Azure types.
Additionally this package provides the AddOpenAIClientFromConfiguration extension method to register an OpenAIClient instance based on the connection string that is provided. This allows your application
to register the best implementation for the OpenAI Rest API it connects. The following rules are followed:
- If the
Endpointattribute is empty or missing, the OpenAI service is used and anOpenAIClientinstance is registered, e.g.,Key={key};. - If the attribute
IsAzureis provided andtruethenAzureOpenAIClientis registered,OpenAIClientotherwise, e.g.,Endpoint={azure_endpoint};Key={key};IsAzure=truewould register anAzureOpenAIClient, whileEndpoint=https://localhost:18889;Key={key}would register anOpenAIClient. - If the
Endpointattribute contains".azure."thenAzureOpenAIClientis registered,OpenAIClientotherwise, e.g.,Endpoint=https://{account}.azure.com;Key={key};.
In any case a valid connection string must contain at least either an Endpoint or a Key.
Configuration
The Aspire Azure OpenAI library provides multiple options to configure the Azure OpenAI Service based on the requirements and conventions of your project. Note that either an Endpoint or a ConnectionString is required to be supplied.
Use a connection string
A connection can be constructed from the Keys and Endpoint tab with the format Endpoint={endpoint};Key={key};. You can provide the name of the connection string when calling builder.AddAzureOpenAIClient():
builder.AddAzureOpenAIClient("openaiConnectionName");
And then the connection string will be retrieved from the ConnectionStrings configuration section. Two connection formats are supported:
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 (https://platform.openai.com/account/api-keys).
Use configuration providers
The Aspire Azure OpenAI library supports Microsoft.Extensions.Configuration. It loads the AzureOpenAISettings and AzureOpenAIClientOptions from configuration by using the Aspire:Azure:AI:OpenAI key. Example appsettings.json that configures some of the options:
{
"Aspire": {
"Azure": {
"AI": {
"OpenAI": {
"DisableTracing": false,
"ClientOptions": {
"UserAgentApplicationId": "myapp"
}
}
}
}
}
}
Use inline delegates
You can also pass the Action<AzureOpenAISettings> configureSettings delegate to set up some or all the options inline, for example to disable tracing from code:
builder.AddAzureOpenAIClient("openaiConnectionName", settings => settings.DisableTracing = true);
You can also setup the AzureOpenAIClientOptions using the optional Action<IAzureClientBuilder<AzureOpenAIClient, AzureOpenAIClientOptions>> configureClientBuilder parameter of the AddAzureOpenAIClient method. For example, to set the client ID for this client:
builder.AddAzureOpenAIClient("openaiConnectionName", configureClientBuilder: configureClientBuilder: builder => builder.ConfigureOptions(options => options.NetworkTimeout = TimeSpan.FromSeconds(2)));
AppHost extensions
In your AppHost project, install the Aspire Azure Cognitive Services Hosting library with NuGet:
dotnet add package Aspire.Hosting.Azure.CognitiveServices
Then, in the AppHost.cs file of AppHost, add an Azure OpenAI service and consume the connection using the following methods:
var openai = builder.ExecutionContext.IsPublishMode
? builder.AddAzureOpenAI("openai")
: builder.AddConnectionString("openai");
var myService = builder.AddProject<Projects.MyService>()
.WithReference(openai);
The AddAzureOpenAI method adds an Azure OpenAI resource to the builder. Or AddConnectionString can be used to read connection information from the AppHost's configuration (for example, from "user secrets") under the ConnectionStrings:openai config key. The WithReference method passes that connection information into a connection string named openai in the MyService project. In the Program.cs file of MyService, the connection can be consumed using:
builder.AddAzureOpenAIClient("openai");
Experimental Telemetry
Azure AI OpenAI telemetry support is experimental, the shape of traces may change in the future without notice. It can be enabled by invoking
AppContext.SetSwitch("OpenAI.Experimental.EnableOpenTelemetry", true);
or by setting the "OPENAI_EXPERIMENTAL_ENABLE_OPEN_TELEMETRY" environment variable to "true".
Additional documentation
- https://learn.microsoft.com/dotnet/api/overview/azure/ai.openai-readme
- https://github.com/dotnet/aspire/tree/main/src/Components/README.md
Feedback & contributing
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Aspire.OpenAI (>= 13.1.0-preview.1.25616.3)
- Azure.AI.OpenAI (>= 2.5.0-beta.1)
- Azure.Core (>= 1.50.0)
- Azure.Identity (>= 1.17.0)
- Microsoft.Extensions.AI (>= 10.1.0)
- Microsoft.Extensions.AI.OpenAI (>= 10.1.0-preview.1.25608.1)
- Microsoft.Extensions.Azure (>= 1.13.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.1)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.1)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 10.0.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.1)
- Microsoft.Extensions.Options (>= 10.0.1)
- Microsoft.Extensions.Primitives (>= 10.0.1)
- OpenAI (>= 2.7.0)
- OpenTelemetry.Extensions.Hosting (>= 1.14.0)
-
net8.0
- Aspire.OpenAI (>= 13.1.0-preview.1.25616.3)
- Azure.AI.OpenAI (>= 2.5.0-beta.1)
- Azure.Core (>= 1.50.0)
- Azure.Identity (>= 1.17.0)
- Microsoft.Extensions.AI (>= 10.1.0)
- Microsoft.Extensions.AI.OpenAI (>= 10.1.0-preview.1.25608.1)
- Microsoft.Extensions.Azure (>= 1.13.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.1)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.1)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 10.0.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.1)
- Microsoft.Extensions.Options (>= 10.0.1)
- Microsoft.Extensions.Primitives (>= 10.0.1)
- OpenAI (>= 2.7.0)
- OpenTelemetry.Extensions.Hosting (>= 1.14.0)
- System.Text.Json (>= 10.0.1)
-
net9.0
- Aspire.OpenAI (>= 13.1.0-preview.1.25616.3)
- Azure.AI.OpenAI (>= 2.5.0-beta.1)
- Azure.Core (>= 1.50.0)
- Azure.Identity (>= 1.17.0)
- Microsoft.Extensions.AI (>= 10.1.0)
- Microsoft.Extensions.AI.OpenAI (>= 10.1.0-preview.1.25608.1)
- Microsoft.Extensions.Azure (>= 1.13.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.1)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.1)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 10.0.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.1)
- Microsoft.Extensions.Options (>= 10.0.1)
- Microsoft.Extensions.Primitives (>= 10.0.1)
- OpenAI (>= 2.7.0)
- OpenTelemetry.Extensions.Hosting (>= 1.14.0)
- System.Text.Json (>= 10.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (6)
Showing the top 6 popular GitHub repositories that depend on Aspire.Azure.AI.OpenAI:
| Repository | Stars |
|---|---|
|
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
|
|
|
dotnet/eShop
A reference .NET application implementing an eCommerce site
|
|
|
dotnet/extensions
This repository contains a suite of libraries that provide facilities commonly needed when creating production-ready applications.
|
|
|
thangchung/practical-dotnet-aspire
The practical .NET Aspire builds on the coffeeshop app business domain
|
|
|
Azure-Samples/eShopOnAzure
A variant of https://github.com/dotnet/eShop that uses Azure services
|
|
|
Azure-Samples/eShopLite
eShopLite is a set of reference .NET applications implementing an eCommerce site with features like Semantic Search, MCP, Reasoning models and more.
|