Skip to content

Propose OpenAI Resource Integration for .NET Aspire (based on GitHub resource model) #10561

@davidfowl

Description

@davidfowl

Summary

Propose a new integration for OpenAI in .NET Aspire, inspired by the existing GitHub resource model. This integration allows developers to easily add OpenAI as a resource in their distributed applications, providing connection configuration, API key management, model selection, and support for custom endpoints (e.g., Azure OpenAI).

Motivation

OpenAI has become an essential service for modern AI-driven applications. A first-class Aspire resource for OpenAI would:

  • Simplify secure API key management and configuration
  • Standardize how OpenAI is referenced and consumed in .NET Aspire projects
  • Enable seamless support for multiple OpenAI-compatible providers (including Azure OpenAI)
  • Provide a consistent connection string pattern for client libraries

Proposed Implementation

A prototype (see below) demonstrates:

  • An OpenAIResource class that implements connection string logic, parameter handling, and optional endpoint configuration.
  • Extension methods for IDistributedApplicationBuilder to add OpenAI resources with simple or advanced (custom endpoint, custom key, custom model) configurations.
  • Fluent methods for further customization.
  • Example usage patterns for basic OpenAI, Azure OpenAI, and advanced customizations.

This model closely mirrors the GitHub resource integration, ensuring consistency across Aspire's resource ecosystem.

Example Usage

var openai = builder.AddOpenAI("openai", "gpt-4o");
var myService = builder.AddProject<Projects.MyService>().WithReference(openai);

Supports user secrets, environment variables, and custom endpoints:

var azureEndpoint = builder.AddParameter("azure-endpoint", "https://myazure.openai.azure.com");
var azureKey = builder.AddParameter("azure-key", secret: true);
var azureModel = builder.AddParameter("azure-model", "gpt-4");
var azureOpenai = builder.AddOpenAI("azure-openai", azureEndpoint, azureKey, azureModel);

Benefits

  • Secure, flexible, and standardized OpenAI integration for .NET Aspire apps
  • Supports both OpenAI and Azure OpenAI
  • Re-uses Aspire's robust resource and parameter system
  • Connection string format compatible with popular client libraries

Top-Level API Proposals

Basic Usage

// Adds an OpenAI resource named "openai" using the "gpt-4o" model.
// The API key is automatically sourced from configuration or environment variables.
var openai = builder.AddOpenAI("openai", "gpt-4o");

// Reference the OpenAI resource in a service.
var myService = builder.AddProject<Projects.MyService>()
                       .WithReference(openai);

Fluent Customization

// Customize the model and endpoint using fluent APIs.
var openai = builder.AddOpenAI("openai", "gpt-4")
    .WithModel("gpt-4o")
    .WithEndpoint("https://custom.openai.endpoint.com");

Explicit Parameter Usage

// Use explicit parameters for API key, model, and optionally endpoint.
var apiKey = builder.AddParameter("openai-key", secret: true);
var model = builder.AddParameter("openai-model", "gpt-4o");
var endpoint = builder.AddParameter("openai-endpoint", "https://api.openai.com");

var openai = builder.AddOpenAI("openai", endpoint, apiKey, model);

API Summary Table

Scenario API Example
Basic OpenAI builder.AddOpenAI("openai", "gpt-4o");
Custom Model builder.AddOpenAI("openai", "gpt-4").WithModel("gpt-4o");
Custom Endpoint builder.AddOpenAI("openai", "gpt-4").WithEndpoint("https://custom.endpoint.com");
Explicit Parameters builder.AddOpenAI("openai", endpoint, apiKey, model);

Notes

  • API key can be sourced via configuration, environment variable (OPENAI_API_KEY), or explicit parameter.
  • Parameters can be managed as secrets for security.
  • The connection string is injected using Aspire’s resource dependency system.
  • Supports both OpenAI and Azure OpenAI endpoints and models.

Additional Context

See the full prototype and README above for detailed implementation and examples.

Related Links

Metadata

Metadata

Assignees

No one assigned

    Labels

    aiarea-integrationsIssues pertaining to Aspire Integrations packages

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions