Aspire.Azure.AI.OpenAI 8.0.0-preview.7.24251.11

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
This is a prerelease version of Aspire.Azure.AI.OpenAI.
dotnet add package Aspire.Azure.AI.OpenAI --version 8.0.0-preview.7.24251.11
NuGet\Install-Package Aspire.Azure.AI.OpenAI -Version 8.0.0-preview.7.24251.11
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Aspire.Azure.AI.OpenAI" Version="8.0.0-preview.7.24251.11" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Aspire.Azure.AI.OpenAI --version 8.0.0-preview.7.24251.11
#r "nuget: Aspire.Azure.AI.OpenAI, 8.0.0-preview.7.24251.11"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Aspire.Azure.AI.OpenAI as a Cake Addin
#addin nuget:?package=Aspire.Azure.AI.OpenAI&version=8.0.0-preview.7.24251.11&prerelease

// Install Aspire.Azure.AI.OpenAI as a Cake Tool
#tool nuget:?package=Aspire.Azure.AI.OpenAI&version=8.0.0-preview.7.24251.11&prerelease

Aspire.Azure.AI.OpenAI library

Registers OpenAIClient as a singleton in the DI container for connecting to Azure OpenAI or OpenAI. Enables corresponding logging and telemetry.

Getting started

Prerequisites

Install the package

Install the .NET Aspire Azure AI OpenAI library with NuGet:

dotnet add package Aspire.Azure.AI.OpenAI

Usage example

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

builder.AddAzureAIOpenAIClient("openaiConnectionName");

You can then retrieve the OpenAIClient instance using dependency injection. For example, to retrieve the client from a Web API controller:

private readonly OpenAIClient _client;

public CognitiveController(OpenAIClient client)
{
    _client = client;
}

See the Azure OpenAI Service quickstarts for examples on using the OpenAIClient.

Configuration

The .NET Aspire Azure 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.AddAzureAIOpenAIClient():

builder.AddAzureAIOpenAIClient("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 .NET Aspire Azure AI OpenAI library supports Microsoft.Extensions.Configuration. It loads the AzureOpenAISettings and OpenAIClientOptions 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,
        }
      }
    }
  }
}

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.AddAzureAIOpenAIClient("openaiConnectionName", settings => settings.DisableTracing = true);

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

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

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 Program.cs file of AppHost, add an Azure AI 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");

Additional documentation

Feedback & contributing

https://github.com/dotnet/aspire

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.0-preview.7.24251.11 74 5/7/2024
8.0.0-preview.6.24214.1 1,428 4/23/2024
8.0.0-preview.5.24201.12 2,041 4/9/2024
8.0.0-preview.4.24156.9 3,146 3/12/2024
8.0.0-preview.3.24105.21 3,408 2/13/2024