Microsoft.Extensions.AI - Azure OpenAI Examples
November 10, 2025 ยท View on GitHub
This project contains a set of samples that show how to use the OpenAI reference implementation in the Microsoft.Extensions.AI.OpenAI NuGet package with the Azure OpenAI service.
Prerequisites
- .NET 8 SDK
- Visual Studio or VS Code
- An Azure OpenAI Service resource with a chat completion and text embedding generation model deployments. For more details, see the Azure OpenAI resource deployment documentation.
Setup
-
Create an environment variable
AZURE_OPENAI_ENDPOINTand set its value to your Azure OpenAI resource deployment endpoint.For more details on where to find the endpoint, see the Azure OpenAI documentation.
Quick Start
Visual Studio
- Open the AzureOpenAIExamples.sln solution.
- Set AzureOpenAIExamples as the startup project.
- Press F5.
Visual Studio Code
-
Open your terminal
-
Navigate to the AzureOpenAIExamples project directory
-
Run the application using
dotnet rundotnet run
Test your application
- When the application starts, select Choose sample.
- Select one of the samples from the dropdown to run it.
- After the selected sample runs, you can choose to run another sample or select Quit to stop the application.
Examples
| Example | Description |
|---|---|
| Chat | Use IChatClient to send and receive chat messages |
| Chat + Conversation History | Use IChatClient alongside conversation history to send and receive chat messages |
| Streaming | Use IChatClient to send and receive a stream of chat messages |
| Caching | Use prompt caching middleware |
| OpenTelemetry | Use OpenTelemetry middleware |
| Tool Calling | Use tool calling middleware |
| Middleware | Use prompt caching, OpenTelemetry and tool calling middleware |
| Dependency Injection | Register an IChatClient and middleware using Dependency Injection |
| Text Embedding | Use text embedding generator |
| Text Embedding + Caching | Use text embedding generator with caching middleware |