Aspire.Hosting.OpenAI 13.1.0

Prefix Reserved
dotnet add package Aspire.Hosting.OpenAI --version 13.1.0
                    
NuGet\Install-Package Aspire.Hosting.OpenAI -Version 13.1.0
                    
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.Hosting.OpenAI" Version="13.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Aspire.Hosting.OpenAI" Version="13.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Aspire.Hosting.OpenAI" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Aspire.Hosting.OpenAI --version 13.1.0
                    
#r "nuget: Aspire.Hosting.OpenAI, 13.1.0"
                    
#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.
#:package Aspire.Hosting.OpenAI@13.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Aspire.Hosting.OpenAI&version=13.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Aspire.Hosting.OpenAI&version=13.1.0
                    
Install as a Cake Tool

Aspire.Hosting.OpenAI library

Provides extension methods and resource definitions for an Aspire AppHost to configure OpenAI resources and models.

Getting started

Prerequisites

  • An OpenAI account with access to the OpenAI API
  • OpenAI API key

Install the package

In your AppHost project, install the Aspire OpenAI Hosting library with NuGet:

dotnet add package Aspire.Hosting.OpenAI

Usage example

Then, in the AppHost.cs file of AppHost, add an OpenAI resource and one or more model resources, and consume connections as needed:

var builder = DistributedApplication.CreateBuilder(args);

var openai = builder.AddOpenAI("openai");
var chat = openai.AddModel("chat", "gpt-4o-mini");

var myService = builder.AddProject<Projects.MyService>()
                       .WithReference(chat);

The WithReference method passes that connection information into a connection string named chat in the MyService project. Multiple models can share the same OpenAI API key via the parent OpenAIResource.

In the Program.cs file of MyService, the connection can be consumed using the client library Aspire.OpenAI:

OpenAI client usage
builder.AddOpenAIClient("chat");

When no model resources are defined, clients can be created with the OpenAI resource only:

builder.AddOpenAIClient("openai")
       .AddChatClient("gpt-4o-mini");

Configuration

The OpenAI resources can be configured with the following options:

API Key

The API key is configured on the parent OpenAI resource via a parameter named {resource_name}-openai-apikey or the OPENAI_API_KEY environment variable.

Then in user secrets:

{
    "Parameters": 
    {
        "openai-openai-apikey": "YOUR_OPENAI_API_KEY_HERE"
    }
}

You can replace the parent API key with a custom parameter on the parent resource:

var apiKey = builder.AddParameter("my-api-key", secret: true);
var openai = builder.AddOpenAI("openai").WithApiKey(apiKey);

// share a single key across multiple models
var chat = openai.AddModel("chat", "gpt-4o-mini");
var embeddings = openai.AddModel("embeddings", "text-embedding-3-small");

Then in user secrets:

{
    "Parameters": 
    {
        "my-api-key": "YOUR_OPENAI_API_KEY_HERE",
        "alt-key": "ANOTHER_OPENAI_API_KEY"
    }
}

Available Models

OpenAI supports various AI models. Some popular options include:

  • gpt-4o-mini
  • gpt-4o
  • gpt-4-turbo
  • gpt-3.5-turbo
  • text-embedding-3-small
  • text-embedding-3-large
  • dall-e-3
  • whisper-1

Check the OpenAI Models documentation for the most up-to-date list of available models.

Custom endpoint

By default, the OpenAI service endpoint is https://api.openai.com/v1. To use an OpenAI-compatible gateway or self-hosted endpoint, set a custom endpoint on the parent resource:

var openai = builder.AddOpenAI("openai")
                    .WithEndpoint("https://my-gateway.example.com/v1");

var chat = openai.AddModel("chat", "gpt-4o-mini");

Both the parent and model connection strings will include the custom endpoint.

Connection Properties

When you reference an OpenAI resource using WithReference, the following connection properties are made available to the consuming project:

OpenAIResource

The OpenAI resource exposes the following connection properties:

Property Name Description
Endpoint The base endpoint URI for the OpenAI API, with the format https://api.openai.com/v1
Uri The endpoint URI (same as Endpoint), with the format https://api.openai.com/v1
Key The API key for authentication

OpenAI model

The OpenAI model resource combines the parent properties above and adds the following connection property:

Property Name Description
ModelName The model identifier for inference requests, for instance gpt-4o-mini

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called chat becomes CHAT_URI.

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.  net9.0 was computed.  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 was computed.  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. 
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.