Microsoft.Extensions.AI.AzureAIInference
9.7.1-preview.1.25365.4
Prefix Reserved
dotnet add package Microsoft.Extensions.AI.AzureAIInference --version 9.7.1-preview.1.25365.4
NuGet\Install-Package Microsoft.Extensions.AI.AzureAIInference -Version 9.7.1-preview.1.25365.4
<PackageReference Include="Microsoft.Extensions.AI.AzureAIInference" Version="9.7.1-preview.1.25365.4" />
<PackageVersion Include="Microsoft.Extensions.AI.AzureAIInference" Version="9.7.1-preview.1.25365.4" />
<PackageReference Include="Microsoft.Extensions.AI.AzureAIInference" />
paket add Microsoft.Extensions.AI.AzureAIInference --version 9.7.1-preview.1.25365.4
#r "nuget: Microsoft.Extensions.AI.AzureAIInference, 9.7.1-preview.1.25365.4"
#:package Microsoft.Extensions.AI.AzureAIInference@9.7.1-preview.1.25365.4
#addin nuget:?package=Microsoft.Extensions.AI.AzureAIInference&version=9.7.1-preview.1.25365.4&prerelease
#tool nuget:?package=Microsoft.Extensions.AI.AzureAIInference&version=9.7.1-preview.1.25365.4&prerelease
Microsoft.Extensions.AI.AzureAIInference
Provides an implementation of the IChatClient
interface for the Azure.AI.Inference
package.
Install the package
From the command-line:
dotnet add package Microsoft.Extensions.AI.AzureAIInference
Or directly in the C# project file:
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.AI.AzureAIInference" Version="[CURRENTVERSION]" />
</ItemGroup>
Usage Examples
Chat
using Azure;
using Microsoft.Extensions.AI;
IChatClient client =
new Azure.AI.Inference.ChatCompletionsClient(
new("https://models.inference.ai.azure.com"),
new AzureKeyCredential(Environment.GetEnvironmentVariable("GH_TOKEN")!))
.AsIChatClient("gpt-4o-mini");
Console.WriteLine(await client.GetResponseAsync("What is AI?"));
Note: When connecting with Azure Open AI, the URL passed into the
ChatCompletionsClient
needs to includeopenai/deployments/{yourDeployment}
. For example:
new Azure.AI.Inference.ChatCompletionsClient( new("https://{your-resource-name}.openai.azure.com/openai/deployments/{yourDeployment}"), new AzureKeyCredential(Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY")!))
Chat + Conversation History
using Azure;
using Microsoft.Extensions.AI;
IChatClient client =
new Azure.AI.Inference.ChatCompletionsClient(
new("https://models.inference.ai.azure.com"),
new AzureKeyCredential(Environment.GetEnvironmentVariable("GH_TOKEN")!))
.AsIChatClient("gpt-4o-mini");
Console.WriteLine(await client.GetResponseAsync(
[
new ChatMessage(ChatRole.System, "You are a helpful AI assistant"),
new ChatMessage(ChatRole.User, "What is AI?"),
]));
Chat streaming
using Azure;
using Microsoft.Extensions.AI;
IChatClient client =
new Azure.AI.Inference.ChatCompletionsClient(
new("https://models.inference.ai.azure.com"),
new AzureKeyCredential(Environment.GetEnvironmentVariable("GH_TOKEN")!))
.AsIChatClient("gpt-4o-mini");
await foreach (var update in client.GetStreamingResponseAsync("What is AI?"))
{
Console.Write(update);
}
Tool calling
using System.ComponentModel;
using Azure;
using Microsoft.Extensions.AI;
IChatClient azureClient =
new Azure.AI.Inference.ChatCompletionsClient(
new("https://models.inference.ai.azure.com"),
new AzureKeyCredential(Environment.GetEnvironmentVariable("GH_TOKEN")!))
.AsIChatClient("gpt-4o-mini");
IChatClient client = new ChatClientBuilder(azureClient)
.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";
Caching
using Azure;
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 azureClient =
new Azure.AI.Inference.ChatCompletionsClient(
new("https://models.inference.ai.azure.com"),
new AzureKeyCredential(Environment.GetEnvironmentVariable("GH_TOKEN")!))
.AsIChatClient("gpt-4o-mini");
IChatClient client = new ChatClientBuilder(azureClient)
.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();
}
Telemetry
using Azure;
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 azureClient =
new Azure.AI.Inference.ChatCompletionsClient(
new("https://models.inference.ai.azure.com"),
new AzureKeyCredential(Environment.GetEnvironmentVariable("GH_TOKEN")!))
.AsIChatClient("gpt-4o-mini");
IChatClient client = new ChatClientBuilder(azureClient)
.UseOpenTelemetry(sourceName: sourceName, configure: c => c.EnableSensitiveData = true)
.Build();
Console.WriteLine(await client.GetResponseAsync("What is AI?"));
Telemetry, Caching, and Tool Calling
using System.ComponentModel;
using Azure;
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 azureClient =
new Azure.AI.Inference.ChatCompletionsClient(
new("https://models.inference.ai.azure.com"),
new AzureKeyCredential(Environment.GetEnvironmentVariable("GH_TOKEN")!))
.AsIChatClient("gpt-4o-mini");
IChatClient client = new ChatClientBuilder(azureClient)
.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,
};
Dependency Injection
using Azure;
using Azure.AI.Inference;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
// App Setup
var builder = Host.CreateApplicationBuilder();
builder.Services.AddSingleton(
new ChatCompletionsClient(
new("https://models.inference.ai.azure.com"),
new AzureKeyCredential(Environment.GetEnvironmentVariable("GH_TOKEN")!)));
builder.Services.AddDistributedMemoryCache();
builder.Services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Trace));
builder.Services.AddChatClient(services => services.GetRequiredService<ChatCompletionsClient>().AsIChatClient("gpt-4o-mini"))
.UseDistributedCache()
.UseLogging();
var app = builder.Build();
// Elsewhere in the app
var chatClient = app.Services.GetRequiredService<IChatClient>();
Console.WriteLine(await chatClient.GetResponseAsync("What is AI?"));
Minimal Web API
using Azure;
using Azure.AI.Inference;
using Microsoft.Extensions.AI;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton(new ChatCompletionsClient(
new("https://models.inference.ai.azure.com"),
new AzureKeyCredential(builder.Configuration["GH_TOKEN"]!)));
builder.Services.AddChatClient(services =>
services.GetRequiredService<ChatCompletionsClient>().AsIChatClient("gpt-4o-mini"));
var app = builder.Build();
app.MapPost("/chat", async (IChatClient client, string message) =>
{
var response = await client.GetResponseAsync(message);
return response.Message;
});
app.Run();
Documentation
Refer to the Microsoft.Extensions.AI libraries documentation for more information and API usage examples.
Feedback & Contributing
We welcome feedback and contributions in our GitHub repo.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- Azure.AI.Inference (>= 1.0.0-beta.4)
- Microsoft.Extensions.AI.Abstractions (>= 9.7.1)
- System.Memory.Data (>= 8.0.1)
- System.Text.Json (>= 8.0.6)
-
.NETStandard 2.0
- Azure.AI.Inference (>= 1.0.0-beta.4)
- Microsoft.Extensions.AI.Abstractions (>= 9.7.1)
- System.Memory.Data (>= 8.0.1)
- System.Text.Json (>= 8.0.6)
-
net8.0
- Azure.AI.Inference (>= 1.0.0-beta.4)
- Microsoft.Extensions.AI.Abstractions (>= 9.7.1)
- System.Memory.Data (>= 8.0.1)
- System.Text.Json (>= 8.0.6)
-
net9.0
- Azure.AI.Inference (>= 1.0.0-beta.4)
- Microsoft.Extensions.AI.Abstractions (>= 9.7.1)
- System.Memory.Data (>= 9.0.7)
- System.Text.Json (>= 9.0.7)
NuGet packages (10)
Showing the top 5 NuGet packages that depend on Microsoft.Extensions.AI.AzureAIInference:
Package | Downloads |
---|---|
Microsoft.SemanticKernel.Connectors.AzureAIInference
Semantic Kernel Model as a Service connectors for Azure AI Studio. Contains clients for chat completion, embeddings and text to image generation. |
|
CToolkit.Microsoft.Extensions.AI
Alpha-Version of the WinForms AI- and Modernization-Toolkit. You need to target TFM 'net9-windows10.0.22000.0' at the least: NET 9+ and Windows version 10.0.22000.0 or higher. Use on own risk - this is a personal Hobby project, provided without support neither from Microsoft nor from the Authors. |
|
CToolkit.DesktopUI.AI
Package Description |
|
CToolkit.Desktop.AI
Alpha-Version of the WinForms AI- and Modernization-Toolkit. You need to target TFM 'net9-windows10.0.22000.0' at the least: NET 9+ and Windows version 10.0.22000.0 or higher. Use on own risk - this is a personal Hobby project, provided without support neither from Microsoft nor from the Authors. |
|
WarpToolkit.WinForms.AI
Preview-Version of the Winforms Ai- and RevamP -Toolkit (WARP). You need to target TFM 'net9-windows10.0.22000.0' at the least. Note, that this is a preview version and may be unstable. **DISCLAIMER** – USE AT YOUR OWN RISK This NuGet package is a personal collection of experimental ideas and proof-of-concepts by Klaus Löffelmann. It is part of an AI learning ramp-up project focused on exploring modernization strategies for Windows Forms (WinForms). The package is not production-ready and may contain bugs, incomplete features, or other issues. It is intended solely for educational and conceptual exploration. Although the author is a full-time Microsoft employee, this project is a personal initiative and is not affiliated with, endorsed by, or supported by Microsoft. Versions 0.x are released under the MIT License. Licensing terms may change for future 1.x versions and beyond. |
GitHub repositories (5)
Showing the top 5 popular GitHub repositories that depend on Microsoft.Extensions.AI.AzureAIInference:
Repository | Stars |
---|---|
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
|
|
microsoft/Generative-AI-for-beginners-dotnet
Five lessons, learn how to really apply AI to your .NET Applications
|
|
dotnet/ai-samples
|
|
davidfowl/aspire-ai-chat-demo
Aspire AI Chat is a full-stack chat sample that combines modern technologies to deliver a ChatGPT-like experience.
|
|
csharpfritz/Fritz.StreamTools
Handy tools for managing my live stream, built with ASP.NET Core
|
Version | Downloads | Last Updated |
---|---|---|
9.7.1-preview.1.25365.4 | 614 | 11 days ago |
9.7.0-preview.1.25356.2 | 901 | 18 days ago |
9.6.0-preview.1.25310.2 | 5,336 | 2 months ago |
9.5.0-preview.1.25265.7 | 31,984 | 2 months ago |
9.5.0-preview.1.25262.9 | 3,187 | 2 months ago |
9.4.4-preview.1.25259.16 | 3,198 | 3 months ago |
9.4.3-preview.1.25230.7 | 12,979 | 3 months ago |
9.4.0-preview.1.25207.5 | 33,520 | 4 months ago |
9.3.0-preview.1.25161.3 | 27,250 | 4 months ago |
9.3.0-preview.1.25114.11 | 29,555 | 5 months ago |
9.1.0-preview.1.25064.3 | 18,272 | 6 months ago |
9.0.1-preview.1.24570.5 | 34,404 | 8 months ago |
9.0.0-preview.9.24556.5 | 4,035 | 8 months ago |
9.0.0-preview.9.24525.1 | 24,524 | 9 months ago |
9.0.0-preview.9.24507.7 | 2,475 | 10 months ago |