Microsoft.Extensions.AI - 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.

Prerequisites

Setup

  1. Create an environment variable OPENAI_API_KEY and set its value to your Open AI API key.

Quick Start

Visual Studio

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

Visual Studio Code

  1. Open your terminal

  2. Navigate to the OpenAIExamples 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