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
- .NET 8 SDK
- Visual Studio or VS Code
- An Open AI API key. For more details, see the OpenAI documentation.
Setup
- Create an environment variable
OPENAI_API_KEYand set its value to your Open AI API key.
Quick Start
Visual Studio
- Open the OpenAIExamples.sln solution
- Set OpenAIExamples as the startup project.
- Press F5.
Visual Studio Code
-
Open your terminal
-
Navigate to the OpenAIExamples 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 |