OpenAI resource types for .NET Aspire.
Deprecated Package Version
Reason: Other, Legacy
This version is out of support and is no longer maintained. Please upgrade to the latest version. See our support policy for details: https://aka.ms/dotnet/aspire/support
Install
| Product | Versions |
|---|---|
| .NET |
README
Aspire.Hosting.OpenAI library
Provides extension methods and resource definitions for a .NET 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 .NET 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-minigpt-4ogpt-4-turbogpt-3.5-turbotext-embedding-3-smalltext-embedding-3-largedall-e-3whisper-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.
Additional documentation
- https://platform.openai.com/docs/models
- https://github.com/dotnet/aspire/tree/main/src/Components/README.md
Feedback & contributing
Adoption Assessment
A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.