Aspire.OpenAI
9.0.0-preview.5.24551.3
Prefix Reserved
dotnet add package Aspire.OpenAI --version 9.0.0-preview.5.24551.3
NuGet\Install-Package Aspire.OpenAI -Version 9.0.0-preview.5.24551.3
<PackageReference Include="Aspire.OpenAI" Version="9.0.0-preview.5.24551.3" />
paket add Aspire.OpenAI --version 9.0.0-preview.5.24551.3
#r "nuget: Aspire.OpenAI, 9.0.0-preview.5.24551.3"
// Install Aspire.OpenAI as a Cake Addin #addin nuget:?package=Aspire.OpenAI&version=9.0.0-preview.5.24551.3&prerelease // Install Aspire.OpenAI as a Cake Tool #tool nuget:?package=Aspire.OpenAI&version=9.0.0-preview.5.24551.3&prerelease
Aspire.OpenAI library
Registers OpenAIClient as a singleton in the DI container for using the OpenAI REST API. Enables corresponding metrics, logging and telemetry.
Getting started
Prerequisites
- An OpenAI REST API compatible service like OpenAI.com, ollama.com, and others.
- An API key.
Install the package
Install the .NET Aspire OpenAI library with NuGet:
dotnet add package Aspire.OpenAI
Usage example
In the Program.cs file of your project, call the AddOpenAIClient
extension method to register an OpenAIClient
for use via the dependency injection container. The method takes a connection name parameter.
builder.AddOpenAIClient("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 ChatController(OpenAIClient client)
{
_client = client;
}
To learn how to use the OpenAI client library refer to Using the OpenAIClient class.
Configuration
The .NET Aspire OpenAI library provides multiple options to configure the 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 an Endpoint and a Key value with the format Endpoint={endpoint};Key={key};
. If the Endpoint
value is omitted the default OpenAI endpoint will be used. You can provide the name of the connection string when calling builder.AddOpenAIClient()
:
builder.AddOpenAIClient("openaiConnectionName");
Connection string
Alternatively, a custom connection string can be used.
{
"ConnectionStrings": {
"openaiConnectionName": "Endpoint=https://{openai_rest_api_url};Key={account_key};"
}
}
Use configuration providers
The .NET Aspire OpenAI library supports Microsoft.Extensions.Configuration. It loads the OpenAISettings
and OpenAIClientOptions
from configuration by using the Aspire:OpenAI
key. Example appsettings.json
that configures some of the options:
{
"Aspire": {
"OpenAI": {
"DisableTracing": false,
"ClientOptions": {
"UserAgentApplicationId": "myapp"
}
}
}
}
Use inline delegates
You can also pass the Action<OpenAISettings> configureSettings
delegate to set up some or all the options inline, for example to disable tracing from code:
builder.AddOpenAIClient("openaiConnectionName", settings => settings.DisableTracing = true);
You can also setup the OpenAIClientOptions
using the optional Action<OpenAIClientOptions>? configureOptions
parameter of the AddOpenAIClient
method. For example, to set a custom NetworkTimeout
value for this client:
builder.AddOpenAIClient("openaiConnectionName", configureOptions: options => options.NetworkTimeout = TimeSpan.FromSeconds(2));
AppHost extensions
There is no specific AppHost extension corresponding to the OpenAI integration. Instead a connection string resource can be registered:
In the Program.cs file of AppHost
, add an OpenAI REST API connection string name:
var openai = builder.AddConnectionString("openai");
var myService = builder.AddProject<Projects.MyService>()
.WithReference(openai);
The 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.AddOpenAIClient("openai");
Experimental Telemetry
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://github.com/openai/openai-dotnet
- 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. |
-
net8.0
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.Extensions.Primitives (>= 8.0.0)
- OpenAI (>= 2.0.0)
- OpenTelemetry.Extensions.Hosting (>= 1.9.0)
- System.Text.Json (>= 8.0.5)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Aspire.OpenAI:
Package | Downloads |
---|---|
Aspire.Azure.AI.OpenAI
A client for Azure OpenAI that integrates with Aspire, including logging and telemetry. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
9.0.0-preview.5.24551.3 | 288 | 11/12/2024 |
9.0.0-preview.4.24511.1 | 2,758 | 10/15/2024 |