Unlocking OpenAI Integration with .NET 8
The Aspire.Azure.AI.OpenAI library lets you set up an OpenAIClient in the dependency injection (DI) container for consuming Azure AI OpenAI or OpenAI features. It also handles logging and telemetry.
In this guide, you’ll discover how to make the most of the .NET Aspire Azure AI OpenAI client.
To begin, you’ll need an Azure subscription, which can be created for free, and an Azure AI OpenAI or OpenAI account. Here’s how to get started with the .NET Aspire Azure AI OpenAI component:
Installation
Start by installing the Aspire.Azure.AI.OpenAI NuGet package. You can do this using the .NET CLI or PackageReference.
dotnet add package Aspire.Azure.AI.OpenAI --prereleaseExample Usage
In your project’s Program.cs file, you’ll register an OpenAIClient using an extension method. This method requires a connection name parameter.
builder.AddAzureOpenAI("openAiConnectionName");This snippet adds an OpenAIClient to the DI container. The parameter openAiConnectionName refers to the connection string in the configuration. You can then access the OpenAIClient instance using dependency injection, as shown in the example service below:
public class ExampleService(OpenAIClient client)
{
// Use client...
}App Host Usage: If you’re utilizing the .NET Aspire Azure hosting APIs, found in the Aspire.Hosting.Azure NuGet package, you’ll need to install it.
dotnet add package Aspire.Hosting.Azure --prereleaseThen, in your app host project, register an Azure AI OpenAI resource using methods like AddAzureOpenAI:
var openai = builder.AddAzureOpenAI("openAiConnectionName");
builder.AddProject<Projects.ExampleProject>()
.WithReference(openai);The AddAzureOpenAI method reads connection information from the app host's configuration, typically from "user secrets", under the ConnectionStrings:openAiConnectionName config key. The WithReference method passes this 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");By following these steps, you can seamlessly integrate OpenAI features into your .NET projects, enhancing their capabilities and providing new avenues for functionality. Let’s continue exploring more about the configuration and observability aspects:
Configuration
The .NET Aspire Azure AI OpenAI component offers various options to configure the connection based on your project’s needs and conventions.
- Using a Connection String: When calling
builder.AddAzureAIOpenAI, you can provide the name of the connection string. This connection string will then be retrieved from theConnectionStringsconfiguration section. - Account Endpoint: Opting for an Endpoint is recommended. It works with the
AzureOpenAISettings.Credentialproperty to establish a connection. Alternatively, you can use a custom connection string. - Configuration Providers: If you’re using Microsoft.Extensions.Configuration, the component supports loading
AzureOpenAISettingsfrom configuration using theAspire:Azure:AI:OpenAIkey. - Inline Delegates: Another option is to pass the
Action<AzureOpenAISettings> configureSettingsdelegate to set up options inline.
Observability and Telemetry
.NET Aspire components automatically configure Logging, Tracing, and Metrics, which are essential for observability. These components set up the pillars of observability, enabling you to monitor and debug your application effectively.
- Logging: The .NET Aspire Azure AI OpenAI component utilizes specific log categories, such as Azure, Azure.Core, and Azure.Identity.
- Tracing: Tracing helps you understand the flow of operations within your application. It provides insights into the performance and behavior of your application.
- Metrics: Metrics allow you to monitor various aspects of your application’s performance, such as response times, error rates, and resource utilization.
- Telemetry Features: Telemetry features can be disabled using the techniques presented in the Configuration section. This allows you to tailor the observability setup to your specific requirements.
By effectively configuring and utilizing the observability and telemetry features of the .NET Aspire Azure AI OpenAI component, you can gain valuable insights into the behaviour and performance of your application. This enables you to identify and address issues promptly, ensuring the smooth operation of your .NET projects.
More articles
Follow me on
C# Publication, LinkedIn, Instagram, Twitter, Dev.to






