![]() |
VOOZH | about |
dotnet add package Microsoft.Extensions.AI.OpenAI --version 10.7.0
NuGet\Install-Package Microsoft.Extensions.AI.OpenAI -Version 10.7.0
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="10.7.0" />
<PackageVersion Include="Microsoft.Extensions.AI.OpenAI" Version="10.7.0" />Directory.Packages.props
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />Project file
paket add Microsoft.Extensions.AI.OpenAI --version 10.7.0
#r "nuget: Microsoft.Extensions.AI.OpenAI, 10.7.0"
#:package Microsoft.Extensions.AI.OpenAI@10.7.0
#addin nuget:?package=Microsoft.Extensions.AI.OpenAI&version=10.7.0Install as a Cake Addin
#tool nuget:?package=Microsoft.Extensions.AI.OpenAI&version=10.7.0Install as a Cake Tool
Provides an implementation of the IChatClient interface for the OpenAI package and OpenAI-compatible endpoints.
From the command-line:
dotnet add package Microsoft.Extensions.AI.OpenAI
Or directly in the C# project file:
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="[CURRENTVERSION]" />
</ItemGroup>
using Microsoft.Extensions.AI;
IChatClient client =
new OpenAI.Chat.ChatClient("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
.AsIChatClient();
Console.WriteLine(await client.GetResponseAsync("What is AI?"));
using Microsoft.Extensions.AI;
IChatClient client =
new OpenAI.Chat.ChatClient("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
.AsIChatClient();
Console.WriteLine(await client.GetResponseAsync(
[
new ChatMessage(ChatRole.System, "You are a helpful AI assistant"),
new ChatMessage(ChatRole.User, "What is AI?"),
]));
using Microsoft.Extensions.AI;
IChatClient client =
new OpenAI.Chat.ChatClient("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
.AsIChatClient();
await foreach (var update in client.GetStreamingResponseAsync("What is AI?"))
{
Console.Write(update);
}
using System.ComponentModel;
using Microsoft.Extensions.AI;
IChatClient openaiClient =
new OpenAI.Chat.ChatClient("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
.AsIChatClient();
IChatClient client = new ChatClientBuilder(openaiClient)
.UseFunctionInvocation()
.Build();
ChatOptions chatOptions = new()
{
Tools = [AIFunctionFactory.Create(GetWeather)]
};
await foreach (var message in client.GetStreamingResponseAsync("Do I need an umbrella?", chatOptions))
{
Console.Write(message);
}
[Description("Gets the weather")]
static string GetWeather() => Random.Shared.NextDouble() > 0.5 ? "It's sunny" : "It's raining";
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
IDistributedCache cache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
IChatClient openaiClient =
new OpenAI.Chat.ChatClient("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
.AsIChatClient();
IChatClient client = new ChatClientBuilder(openaiClient)
.UseDistributedCache(cache)
.Build();
for (int i = 0; i < 3; i++)
{
await foreach (var message in client.GetStreamingResponseAsync("In less than 100 words, what is AI?"))
{
Console.Write(message);
}
Console.WriteLine();
Console.WriteLine();
}
using Microsoft.Extensions.AI;
using OpenTelemetry.Trace;
// Configure OpenTelemetry exporter
var sourceName = Guid.NewGuid().ToString();
var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder()
.AddSource(sourceName)
.AddConsoleExporter()
.Build();
IChatClient openaiClient =
new OpenAI.Chat.ChatClient("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
.AsIChatClient();
IChatClient client = new ChatClientBuilder(openaiClient)
.UseOpenTelemetry(sourceName: sourceName, configure: c => c.EnableSensitiveData = true)
.Build();
Console.WriteLine(await client.GetResponseAsync("What is AI?"));
using System.ComponentModel;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using OpenTelemetry.Trace;
// Configure telemetry
var sourceName = Guid.NewGuid().ToString();
var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder()
.AddSource(sourceName)
.AddConsoleExporter()
.Build();
// Configure caching
IDistributedCache cache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
// Configure tool calling
var chatOptions = new ChatOptions
{
Tools = [AIFunctionFactory.Create(GetPersonAge)]
};
IChatClient openaiClient =
new OpenAI.Chat.ChatClient("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
.AsIChatClient();
IChatClient client = new ChatClientBuilder(openaiClient)
.UseDistributedCache(cache)
.UseFunctionInvocation()
.UseOpenTelemetry(sourceName: sourceName, configure: c => c.EnableSensitiveData = true)
.Build();
for (int i = 0; i < 3; i++)
{
Console.WriteLine(await client.GetResponseAsync("How much older is Alice than Bob?", chatOptions));
}
[Description("Gets the age of a person specified by name.")]
static int GetPersonAge(string personName) =>
personName switch
{
"Alice" => 42,
"Bob" => 35,
_ => 26,
};
using Microsoft.Extensions.AI;
IEmbeddingGenerator<string, Embedding<float>> generator =
new OpenAI.Embeddings.EmbeddingClient("text-embedding-3-small", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
.AsIEmbeddingGenerator();
var embeddings = await generator.GenerateAsync("What is AI?");
Console.WriteLine(string.Join(", ", embeddings[0].Vector.ToArray()));
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
IDistributedCache cache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
IEmbeddingGenerator<string, Embedding<float>> openAIGenerator =
new OpenAI.Embeddings.EmbeddingClient("text-embedding-3-small", Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
.AsIEmbeddingGenerator();
IEmbeddingGenerator<string, Embedding<float>> generator = new EmbeddingGeneratorBuilder<string, Embedding<float>>(openAIGenerator)
.UseDistributedCache(cache)
.Build();
foreach (var prompt in new[] { "What is AI?", "What is .NET?", "What is AI?" })
{
var embeddings = await generator.GenerateAsync(prompt);
Console.WriteLine(string.Join(", ", embeddings[0].Vector.ToArray()));
}
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
// App Setup
var builder = Host.CreateApplicationBuilder();
builder.Services.AddDistributedMemoryCache();
builder.Services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Trace));
builder.Services.AddChatClient(services =>
new OpenAI.Chat.ChatClient("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY")).AsIChatClient())
.UseDistributedCache()
.UseLogging();
var app = builder.Build();
// Elsewhere in the app
var chatClient = app.Services.GetRequiredService<IChatClient>();
Console.WriteLine(await chatClient.GetResponseAsync("What is AI?"));
using Microsoft.Extensions.AI;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddChatClient(services =>
new OpenAI.Chat.ChatClient("gpt-4o-mini", builder.Configuration["OPENAI_API_KEY"]).AsIChatClient());
builder.Services.AddEmbeddingGenerator(services =>
new OpenAI.Embeddings.EmbeddingClient("text-embedding-3-small", builder.Configuration["OPENAI_API_KEY"]).AsIEmbeddingGenerator());
var app = builder.Build();
app.MapPost("/chat", async (IChatClient client, string message) =>
{
var response = await client.GetResponseAsync(message);
return response.Message;
});
app.MapPost("/embedding", async (IEmbeddingGenerator<string, Embedding<float>> client, string message) =>
{
var response = await client.GenerateAsync(message);
return response[0].Vector;
});
app.Run();
Learn how to create a conversational .NET console chat app using an OpenAI or Azure OpenAI model with the Quickstart - Build an AI chat app with .NET documentation.
We welcome feedback and contributions in our GitHub repo.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 is compatible. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on Microsoft.Extensions.AI.OpenAI:
| Package | Downloads |
|---|---|
|
Microsoft.SemanticKernel.Connectors.OpenAI
Semantic Kernel connectors for OpenAI. Contains clients for chat completion, embedding and DALL-E text to image. |
|
|
Microsoft.Agents.AI.OpenAI
Provides Microsoft Agent Framework support for OpenAI. |
|
|
Aspire.Azure.AI.OpenAI
A client for Azure OpenAI that integrates with Aspire, including logging and telemetry. |
|
|
ImmediaC.SimpleCms
ASP.NET Core based CMS |
|
|
Aspire.OpenAI
A client for OpenAI that integrates with Aspire, including metrics and telemetry. |
Showing the top 20 popular GitHub repositories that depend on Microsoft.Extensions.AI.OpenAI:
| Repository | Stars |
|---|---|
|
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
|
|
|
dotnet/maui
.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
|
|
|
microsoft/aspire
Aspire is the tool for code-first, extensible, observable dev and deploy.
|
|
|
Sylinko/Everywhere
On-screen aware AI assistant for your desktop. Uses current app context, multiple LLMs, and MCP tools to help you act across apps.
|
|
|
modelcontextprotocol/csharp-sdk
The official C# SDK for Model Context Protocol servers and clients. Maintained in collaboration with Microsoft.
|
|
|
dotnet/maui-samples
Samples for .NET Multi-Platform App UI (.NET MAUI)
|
|
|
dotnet/skills
Repository for skills to assist AI coding agents with .NET and C#
|
|
|
microsoft/mcp
Catalog of official Microsoft MCP (Model Context Protocol) server implementations for AI-powered data access and tool integration
|
|
|
microsoft/Generative-AI-for-beginners-dotnet
Five lessons, learn how to really apply AI to your .NET Applications
|
|
|
phongnguyend/Practical.CleanArchitecture
Full-stack .Net 10 Clean Architecture (Microservices, Modular Monolith, Monolith), Blazor, Angular 21, React 19, Vue 3.5, BFF with YARP, NextJs 16, Domain-Driven Design, CQRS, SOLID, Asp.Net Core Identity Custom Storage, OpenID Connect, EF Core, OpenTelemetry, SignalR, Background Services, Health Checks, Rate Limiting, Clouds (Azure, AWS, GCP), ...
|
|
|
RazorConsole/RazorConsole
Build agentic TUI applications with .NET Razor and Spectre.Console
|
|
|
microsoft/ai-dev-gallery
An open-source project for Windows developers to learn how to add AI with local models and APIs to Windows apps.
|
|
|
bitfoundation/bitplatform
Build all of your apps using what you already know and love ❤️
|
|
|
abpframework/abp-samples
Sample solutions built with the ABP Framework
|
|
|
tryAGI/LangChain
C# implementation of LangChain. We try to be as close to the original as possible in terms of abstractions, but are open to new entities.
|
|
|
microsoft/testfx
This repository holds the source code of Microsoft.Testing.Platform (MTP), a lightweight alternative to VSTest, as well as MSTest adapter and framework.
|
|
|
getcellm/cellm
Use LLMs in Excel formulas
|
|
|
VladislavAntonyuk/MauiSamples
.NET MAUI Samples
|
|
| dotnet/ai-samples | |
|
getsentry/sentry-dotnet
Sentry SDK for .NET
|
| Version | Downloads | Last Updated |
|---|---|---|
| 10.7.0 | 33,554 | 6/9/2026 |
| 10.6.0 | 254,867 | 5/12/2026 |
| 10.5.2 | 88,481 | 5/5/2026 |
| 10.5.1 | 265,491 | 5/2/2026 |
| 10.5.0 | 795,665 | 4/15/2026 |
| 10.4.1 | 512,666 | 3/18/2026 |
| 10.4.0 | 995,840 | 3/10/2026 |
| 10.3.0 | 1,152,223 | 2/10/2026 |
| 10.2.0-preview.1.26063.2 | 331,809 | 1/13/2026 |
| 10.1.1-preview.1.25612.2 | 406,787 | 12/12/2025 |
| 10.1.0-preview.1.25608.1 | 98,215 | 12/9/2025 |
| 10.0.1-preview.1.25571.5 | 2,039,584 | 11/21/2025 |
| 10.0.0-preview.1.25560.10 | 85,734 | 11/11/2025 |
| 10.0.0-preview.1.25559.3 | 82,352 | 11/11/2025 |
| 9.10.2-preview.1.25552.1 | 161,268 | 11/2/2025 |
| 9.10.1-preview.1.25521.4 | 113,831 | 10/22/2025 |
| 9.10.0-preview.1.25513.3 | 848,768 | 10/14/2025 |
| 9.9.1-preview.1.25474.6 | 552,008 | 9/25/2025 |
| 9.9.0-preview.1.25458.4 | 925,722 | 9/9/2025 |
| 9.8.0-preview.1.25412.6 | 820,661 | 8/12/2025 |