DEV Community

Cover image for Integrating Azure OpenAI with .NET Applications Using Microsoft.Extensions.AI
hazhayder
hazhayder

Posted on

Integrating Azure OpenAI with .NET Applications Using Microsoft.Extensions.AI

Integrating Azure OpenAI with .NET Applications Using Microsoft.Extensions.AI

Introduction to LLMs and Azure OpenAI

Large Language Models (LLMs) are at the forefront of AI innovation, enabling sophisticated natural language processing capabilities. Azure OpenAI, Microsoft's cloud-based service, provides access to OpenAI models like GPT-3 and GPT-4, tailored for enterprise use. By integrating Azure OpenAI with .NET applications, developers can leverage Azure's infrastructure to build powerful AI-driven solutions.

Understanding the Building Blocks

Azure OpenAI

Azure OpenAI offers scalable, secure, and reliable AI capabilities, integrated with Azure's ecosystem. It provides access to advanced models for enterprise applications, ensuring high performance and reliability.

Microsoft.Extensions.AI

This library simplifies AI service integration into .NET applications, offering a unified interface for various AI providers, including Azure OpenAI. It abstracts away the complexities of different AI providers, making it easier to switch between them.

Getting Started

Prerequisites

  • An Azure account with access to Azure OpenAI services.
  • Azure OpenAI resource set up and an API key obtained.
  • .NET 6 or later installed.
  • Necessary NuGet packages installed.

Setting up Azure OpenAI

  1. Create an Azure OpenAI resource in the Azure portal.
  2. Deploy a model and obtain the API key and endpoint URL for authentication.

Installing NuGet Packages

Install the following packages via the NuGet Package Manager:

Install-Package Microsoft.Extensions.AI.AzureOpenAI
Install-Package Microsoft.Extensions.Hosting
Enter fullscreen mode Exit fullscreen mode

Basic Integration Example

var builder = Host.CreateApplicationBuilder();

builder.Services.AddAzureOpenAIClient(new AzureOpenAIClientOptions
{
    Endpoint = new Uri("https://your-openai-endpoint.azure.com"),
    ApiKey = "your-api-key"
});

var app = builder.Build();

var client = app.Services.GetRequiredService<IAzureOpenAIClient>();

var completion = await client.GetCompletionAsync("What is the capital of France?", "text-davinci-003");
Console.WriteLine(completion.Choices[0].Text);
Enter fullscreen mode Exit fullscreen mode

Advanced Usage

Maintain chat history for context-aware interactions:

var messages = new List<ChatMessage>
{
    new ChatMessage(ChatRole.System, "You are a helpful assistant."),
    new ChatMessage(ChatRole.User, "What is the capital of France?")
};

var chat = await client.GetChatCompletionsAsync(messages, "gpt-3.5-turbo");
Console.WriteLine(chat.Choices[0].Message.Content);
Enter fullscreen mode Exit fullscreen mode

Practical Applications

Building a Q&A Bot for Documentation

string documentation = File.ReadAllText("documentation.txt");
string userQuery = "What are the company's core values?";
string prompt = $"Given the following documentation:\n{documentation}\n\nAnswer the question: {userQuery}";

var completion = await client.GetCompletionAsync(prompt, "text-davinci-003");
Console.WriteLine(completion.Choices[0].Text);
Enter fullscreen mode Exit fullscreen mode

Flexibility and Scalability

Switching AI providers is easy with Microsoft.Extensions.AI. For example, switching to OpenAI requires only configuration changes without altering application code.

Best Practices and Considerations

  • Error Handling: Gracefully handle exceptions and errors.
  • Cost Management: Be mindful of API rate limits and costs.
  • Security: Ensure secure storage and handling of API keys.
  • Ethical Guidelines: Adhere to ethical guidelines and model usage policies.

Conclusion

Integrating Azure OpenAI with .NET applications using Microsoft.Extensions.AI is straightforward, offering vast possibilities for AI-driven features. By following the steps outlined, developers can quickly build intelligent applications. Experiment with different models, enhance applications with sophisticated AI features, and optimize for performance and scalability.

Feel free to share your projects or experiences in the comments!

Top comments (0)