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

Setup

  1. Create an environment variable AZURE_OPENAI_ENDPOINT and 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

  1. Open the AzureOpenAIExamples.sln solution.
  2. Set AzureOpenAIExamples as the startup project.
  3. Press F5.

Visual Studio Code

  1. Open your terminal

  2. Navigate to the AzureOpenAIExamples project directory

  3. Run the application using dotnet run

    dotnet run
    

Test your application

  1. When the application starts, select Choose sample.
  2. Select one of the samples from the dropdown to run it.
  3. After the selected sample runs, you can choose to run another sample or select Quit to stop the application.

Examples

ExampleDescription
ChatUse IChatClient to send and receive chat messages
Chat + Conversation HistoryUse IChatClient alongside conversation history to send and receive chat messages
StreamingUse IChatClient to send and receive a stream of chat messages
CachingUse prompt caching middleware
OpenTelemetryUse OpenTelemetry middleware
Tool CallingUse tool calling middleware
MiddlewareUse prompt caching, OpenTelemetry and tool calling middleware
Dependency InjectionRegister an IChatClient and middleware using Dependency Injection
Text EmbeddingUse text embedding generator
Text Embedding + CachingUse text embedding generator with caching middleware