Implementing OpenAI Chat Conversations in ASP.NET Core Minimal API#
This guide demonstrates how to build a robust, production-ready chat conversation API using ASP.NET Core Minimal API, OpenAI (or Azure OpenAI), and Microsoft.Extensions.AI. It covers secure key management with Azure Key Vault, advanced function calling, and outlines next steps for integrating Semantic Kernel, Retrieval-Augmented Generation (RAG), and server-side tool/function execution.
Prerequisites#
- .NET 8 SDK or later
- Azure Subscription (for Key Vault and Azure OpenAI)
- Access to OpenAI or Azure OpenAI API
NuGet Packages#
Install the following packages:
# Core AI and OpenAI integration
dotnet add package Microsoft.Extensions.AI.OpenAI
dotnet add package Microsoft.Extensions.AI.OpenAI.Azure
# Key Vault integration
dotnet add package Azure.Identity
dotnet add package Azure.Security.KeyVault.Secrets
# For advanced scenarios (Semantic Kernel, RAG, etc.)
dotnet add package Microsoft.SemanticKernel
1. Store API Keys and Endpoints in Azure Key Vault#
- Create a Key Vault in Azure.
- Add secrets for your OpenAI or Azure OpenAI keys and endpoints, e.g.:
-
OpenAI__ApiKey
-OpenAI__Endpoint
-OpenAI__Model
2. Configure Your Minimal API Project#
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Microsoft.Extensions.AI.OpenAI;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
// Load secrets from Azure Key Vault
var keyVaultUrl = builder.Configuration["KeyVaultUrl"];
if (!string.IsNullOrEmpty(keyVaultUrl))
{
builder.Configuration.AddAzureKeyVault(new Uri(keyVaultUrl), new DefaultAzureCredential());
}
// Register OpenAI or Azure OpenAI as a service
builder.Services.AddOpenAIChatCompletion(
provider: "openai", // or "azure"
configure: options =>
{
options.ApiKey = builder.Configuration["OpenAI:ApiKey"];
options.Endpoint = builder.Configuration["OpenAI:Endpoint"];
options.Model = builder.Configuration["OpenAI:Model"] ?? "gpt-3.5-turbo";
});
var app = builder.Build();
// Example: Minimal API endpoint for chat
app.MapPost("/api/chat", async (OpenAIChatCompletionService chat, ChatRequest req) =>
{
var messages = new List<ChatMessage>
{
new("system", req.SystemPrompt ?? "You are a helpful assistant."),
new("user", req.UserMessage)
};
// Optionally add function/tool definitions here
var result = await chat.GetChatCompletionsAsync(messages, req.Functions);
return Results.Ok(new { response = result.Choices[0].Message.Content });
});
app.Run();
public record ChatRequest(string UserMessage, string? SystemPrompt = null, List<OpenAIFunctionDef>? Functions = null);
// Define your function/tool schema for OpenAI function calling
public record OpenAIFunctionDef(string Name, string Description, object Parameters);
3. Advanced: Securely Accessing Key Vault at Runtime#
// Example: Fetching secrets at runtime
var secretClient = new SecretClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
KeyVaultSecret apiKeySecret = await secretClient.GetSecretAsync("OpenAI__ApiKey");
string apiKey = apiKeySecret.Value;
4. Advanced: Function Calling and Tool Use#
- Define your functions/tools as C# methods.
- Register them and expose their schema to OpenAI via the API.
- When OpenAI requests a function call, invoke the corresponding C# method and return the result as a function message.
5. Next Steps: Semantic Kernel, RAG, and Server-Side Tools#
- Semantic Kernel: Integrate with Microsoft.SemanticKernel for advanced orchestration, memory, and planning.
- Retrieval-Augmented Generation (RAG): Use Semantic Kernel or custom code to retrieve relevant documents from a vector database (e.g., Azure Cognitive Search, Pinecone) and inject them into the chat context.
- Server-Side Function Execution: Implement a dispatcher that maps OpenAI function calls to C# methods, executes them, and streams results back to the client.
Example: Semantic Kernel Integration#
using Microsoft.SemanticKernel;
var kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletionService("openai", o =>
{
o.ApiKey = builder.Configuration["OpenAI:ApiKey"];
o.Endpoint = builder.Configuration["OpenAI:Endpoint"];
o.Model = builder.Configuration["OpenAI:Model"];
})
.Build();
// Use kernel for advanced chat, RAG, and tool orchestration
References#
- Microsoft.Extensions.AI
- Microsoft.SemanticKernel
- Azure Key Vault Documentation
- OpenAI Function Calling
With this setup, you can build a secure, extensible, and production-grade chat API in ASP.NET Core, ready for advanced AI scenarios.