Get started with the Azure OpenAI integration
Questi contenuti non sono ancora disponibili nella tua lingua.
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.
Set up hosting integration
Section titled “Set up hosting integration”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 add azure-cognitiveservicesLa CLI Aspire è interattiva; seleziona il risultato corretto quando richiesto:
Select an integration to add:
> azure-cognitiveservices (Aspire.Hosting.Azure.CognitiveServices)> Other results listed as selectable options...#:package Aspire.Hosting.Azure.CognitiveServices@*<PackageReference Include="Aspire.Hosting.Azure.CognitiveServices" Version="*" />Next, in the AppHost project, create an Azure OpenAI resource and pass it to the consuming client projects:
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.
Set up client integration
Section titled “Set up client integration”To use Azure OpenAI from your client applications, install the Aspire Azure OpenAI client integration in your client project:
dotnet add package Aspire.Azure.AI.OpenAI#:package Aspire.Azure.AI.OpenAI@*<PackageReference Include="Aspire.Azure.AI.OpenAI" Version="*" />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");Use injected Azure OpenAI properties
Section titled “Use injected Azure OpenAI properties”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:
string openaiUri = builder.Configuration.GetValue<string>("OPENAI_URI");Use Azure OpenAI client in client code
Section titled “Use Azure OpenAI client in client code”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.