.NET Aspire Azure AI OpenAI component
source link: https://learn.microsoft.com/en-us/dotnet/aspire/openai/azureai-openai-component?tabs=dotnet-cli
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
.NET Aspire Azure AI OpenAI component
- Article
- 02/21/2024
In this article
In this article, you learn how to use the .NET Aspire Azure AI OpenAI client. The Aspire.Azure.AI.OpenAI
library is used to register an OpenAIClient in the dependency injection (DI) container for consuming Azure AI OpenAI or OpenAI functionality. It enables corresponding logging and telemetry.
For more information on using the OpenAIClient
, see Quickstart: Get started generating text using Azure OpenAI Service.
Get started
To get started with the .NET Aspire Azure AI OpenAI component, install the Aspire.Azure.AI.OpenAI NuGet package.
dotnet add package Aspire.Azure.AI.OpenAI --prerelease
For more information, see dotnet add package or Manage package dependencies in .NET applications.
Example usage
In the Program.cs file of your component-consuming project, call the extension method to register an OpenAIClient
for use via the dependency injection container. The method takes a connection name parameter.
builder.AddAzureOpenAI("openAiConnectionName");
In the preceding code, the AddAzureOpenAI method adds an OpenAIClient
to the DI container. The openAiConnectionName
parameter is the name of the connection string in the configuration. You can then retrieve the OpenAIClient
instance using dependency injection. For example, to retrieve the connection from an example service:
public class ExampleService(OpenAIClient client)
{
// Use client...
}
App host usage
The .NET Aspire Azure hosting APIs are available in the Aspire.Hosting.Azure
NuGet package. These APIs provide Azure-specific resources used within the App Host. To add Azure resources to your IDistributedApplicationBuilder, install the Aspire.Hosting.Azure NuGet package.
dotnet add package Aspire.Hosting.Azure --prerelease
In your app host project, register an Azure AI OpenAI resource using the following methods, such as AddAzureOpenAI:
// Service registration
var openai = builder.AddAzureOpenAI("openAiConnectionName");
// Service consumption
builder.AddProject<Projects.ExampleProject>()
.WithReference(openai);
The AddAzureAIOpenAI
method will read connection information from the app host's configuration (for example, from "user secrets") under the ConnectionStrings:openAiConnectionName
config key. The WithReference method passes that connection information into a connection string named openAiConnectionName
in the ExampleProject
project. In the Program.cs file of ExampleProject, the connection can be consumed using:
builder.AddAzureAIOpenAI("openAiConnectionName");
Configuration
The .NET Aspire Azure AI OpenAI component provides multiple options to configure the connection based on the requirements and conventions of your project.
Use a connection string
When using a connection string from the ConnectionStrings
configuration section, you can provide the name of the connection string when calling builder.AddAzureAIOpenAI
:
builder.AddAzureAIOpenAI("openAiConnectionName");
And then the connection string will be retrieved from the ConnectionStrings
configuration section:
Account endpoint
The recommended approach is to use an Endpoint
, which works with the AzureOpenAISettings.Credential
property to establish a connection. If no credential is configured, the DefaultAzureCredential is used.
{
"ConnectionStrings": {
"openAiConnectionName": "https://{account_name}.openai.azure.com/"
}
}
Connection string
Alternatively, a custom connection string can be used.
{
"ConnectionStrings": {
"openaiConnectionName": "Endpoint=https://{account_name}.openai.azure.com/;Key={account_key};"
}
}
In order to connect to the non-Azure OpenAI service, drop the Endpoint
property and only set the Key property to set the API key.
Use configuration providers
The .NET Aspire Azure AI OpenAI component supports Microsoft.Extensions.Configuration. It loads the AzureOpenAISettings
from configuration by using the Aspire:Azure:AI:OpenAI
key. Example appsettings.json
that configures some of the options:
{
"Aspire": {
"Azure": {
"AI": {
"OpenAI": {
"Tracing": true,
}
}
}
}
}
Use inline delegates
Also you can pass the Action<AzureOpenAISettings> configureSettings
delegate to set up some or all the options inline, for example to disable health checks from code:
builder.AddAzureAIOpenAI(
"openAiConnectionName",
static settings => settings.Tracing = false);
You can also setup the OpenAIClientOptions using the optional Action<IAzureClientBuilder<OpenAIClient, OpenAIClientOptions>> configureClientBuilder
parameter of the AddAzureAIOpenAI
method. For example, to set the client ID for this client:
builder.AddAzureAIOpenAI(
"openAiConnectionName",
configureClientBuilder: builder => builder.ConfigureOptions(
options => options.Diagnostics.ApplicationId = "CLIENT_ID"));
Observability and telemetry
.NET Aspire components automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. For more information about component observability and telemetry, see .NET Aspire components overview. Depending on the backing service, some components may only support some of these features. For example, some components support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.
Logging
The .NET Aspire Azure AI OpenAI component uses the following log categories:
Azure
Azure.Core
Azure.Identity
See also
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK