Lewati ke konten

Get started with the Azure OpenAI integration

Konten ini belum tersedia dalam bahasa Anda.

Azure OpenAI logo 🧪 Preview

Azure OpenAI Service provides access to OpenAI’s powerful language and embedding models with the security and enterprise promise of Azure. The Aspire Azure OpenAI integration enables you to connect to Azure OpenAI services from your applications.

In this introduction, you’ll see how to install and use the Aspire Azure OpenAI integrations in a simple configuration. If you already have this knowledge, see Azure OpenAI Hosting integration for full reference details.

To begin, install the Aspire Azure OpenAI Hosting integration in your Aspire AppHost project. This integration allows you to create and manage Azure OpenAI resources from your Aspire hosting projects:

Aspire CLI — Tambahkan paket Aspire.Hosting.Azure.CognitiveServices
aspire add azure-cognitiveservices

Aspire CLI interaktif; pilih hasil pencarian yang sesuai saat diminta:

Aspire CLI — Contoh keluaran
Select an integration to add:
> azure-cognitiveservices (Aspire.Hosting.Azure.CognitiveServices)
> Other results listed as selectable options...

Next, in the AppHost project, create an Azure OpenAI resource and pass it to the consuming client projects:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var openai = builder.AddAzureOpenAI("openai");
openai.AddDeployment(
name: "preview",
modelName: "gpt-4.5-preview",
modelVersion: "2025-02-27");
builder.AddProject<Projects.ExampleProject>()
.WithReference(openai)
.WaitFor(openai);
// After adding all resources, run the app...
builder.Build().Run();

The preceding code adds an Azure OpenAI resource named openai to the AppHost project, configures a deployment with a specific model, and passes the connection information to the ExampleProject project.

To use Azure OpenAI from your client applications, install the Aspire Azure OpenAI client integration in your client project:

.NET CLI — Add Aspire.Azure.AI.OpenAI package
dotnet add package Aspire.Azure.AI.OpenAI

In the Program.cs file of your client-consuming project, call the AddAzureOpenAIClient extension method to register an AzureOpenAIClient for use via the dependency injection container:

builder.AddAzureOpenAIClient(connectionName: "openai");

In the AppHost, when you used the WithReference method to pass an Azure OpenAI resource to a consuming client project, Aspire injects several configuration properties that you can use in the consuming project.

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

Use the GetValue() method to obtain these environment variables in consuming projects:

C# — Obtain configuration properties
string openaiUri = builder.Configuration.GetValue<string>("OPENAI_URI");

After adding the AzureOpenAIClient, you can retrieve the client instance using dependency injection:

public class ExampleService(AzureOpenAIClient client)
{
// Use client...
}

For full details on using the client integration, see Azure OpenAI Client integration.