Skip to content

Commit c11ab29

Browse files
authored
.Net: New Azure AI Inference Connector (#7963)
# Motivation and Context This PR brings support for Azure AI Studio Model Catalogs also deployed thru GitHub Models, this Connector uses the `Azure AI Inference SDK` library client. Closes #3992 Closes #7958
1 parent f79eaaf commit c11ab29

File tree

40 files changed

+2871
-23
lines changed

40 files changed

+2871
-23
lines changed

.github/workflows/dotnet-build-and-test.yml

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ jobs:
125125
Bing__ApiKey: ${{ secrets.BING__APIKEY }}
126126
OpenAI__ApiKey: ${{ secrets.OPENAI__APIKEY }}
127127
OpenAI__ChatModelId: ${{ vars.OPENAI__CHATMODELID }}
128+
AzureAIInference__ApiKey: ${{ secrets.AZUREAIINFERENCE__APIKEY }}
129+
AzureAIInference__Endpoint: ${{ secrets.AZUREAIINFERENCE__ENDPOINT }}
128130

129131
# Generate test reports and check coverage
130132
- name: Generate test reports
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
# These are optional elements. Feel free to remove any of them.
3+
status: proposed
4+
contact: rogerbarreto
5+
date: 2024-08-07
6+
deciders: rogerbarreto, markwallace-microsoft
7+
consulted: taochen
8+
---
9+
10+
# Support Connector for .Net Azure Model-as-a-Service (Azure AI Studio)
11+
12+
## Context and Problem Statement
13+
14+
There has been a demand from customers to use and support natively models deployed in [Azure AI Studio - Serverless APIs](https://learn.microsoft.com/en-us/azure/ai-studio/how-to/model-catalog-overview#model-deployment-managed-compute-and-serverless-api-pay-as-you-go), This mode of consumption operates on a pay-as-you-go basis, typically using tokens for billing purposes. Clients can access the service via the [Azure AI Model Inference API](https://learn.microsoft.com/en-us/azure/ai-studio/reference/reference-model-inference-api?tabs=azure-studio) or client SDKs.
15+
16+
At present, there is no official support for [Azure AI Studio](https://learn.microsoft.com/en-us/azure/ai-studio/what-is-ai-studio). The purpose of this ADR is to examine the constraints of the service and explore potential solutions to enable support for the service via the development of a new AI connector.
17+
18+
## Azure Inference Client library for .NET
19+
20+
The Azure team has a new client library, namely [Azure.AI.Inference](https://github.com/Azure/azure-sdk-for-net/blob/Azure.AI.Inference_1.0.0-beta.1/sdk/ai/Azure.AI.Inference/README.md) in .Net, for effectively interacting with the service. While the service API is OpenAI-compatible, it is not permissible to use the OpenAI and the Azure OpenAI client libraries for interacting with the service as they are not independent with respect to both the models and their providers. This is because Azure AI Studio features a diverse range of open-source models, other than OpenAI models.
21+
22+
### Limitations
23+
24+
Currently is known that the first version of the client SDK will only support: `Chat Completion` and `Text Embedding Generation` and `Image Embedding Generation` with `TextToImage Generation` planned.
25+
26+
There are no current plans to support `Text Generation` modality.
27+
28+
## AI Connector
29+
30+
### Namespace options
31+
32+
- `Microsoft.SemanticKernel.Connectors.AzureAI`
33+
- `Microsoft.SemanticKernel.Connectors.AzureAIInference`
34+
- `Microsoft.SemanticKernel.Connectors.AzureAIModelInference`
35+
36+
Decision: `Microsoft.SemanticKernel.Connectors.AzureAIInference`
37+
38+
### Support for model-specific parameters
39+
40+
Models can possess supplementary parameters that are not part of the default API. The service API and the client SDK enable the provision of model-specific parameters. Users can provide model-specific settings via a dedicated argument along with other settings, such as `temperature` and `top_p`, among others.
41+
42+
Azure AI Inference specialized `PromptExecutionSettings`, will support those customizable parameters.
43+
44+
### Feature Branch
45+
46+
The development of the Azure AI Inference connector will be done in a feature branch named `feature-connectors-azureaiinference`.

dotnet/Directory.Packages.props

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
66
</PropertyGroup>
77
<ItemGroup>
8+
<PackageVersion Include="Azure.AI.Inference" Version="1.0.0-beta.1" />
89
<PackageVersion Include="OpenAI" Version="2.0.0-beta.10" />
910
<PackageVersion Include="System.ClientModel" Version="1.1.0-beta.7" />
1011
<PackageVersion Include="Azure.AI.ContentSafety" Version="1.0.0" />

dotnet/SK-dotnet.sln

+18
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Connectors.AzureOpenAI", "s
334334
EndProject
335335
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Connectors.AzureOpenAI.UnitTests", "src\Connectors\Connectors.AzureOpenAI.UnitTests\Connectors.AzureOpenAI.UnitTests.csproj", "{8CF06B22-50F3-4F71-A002-622DB49DF0F5}"
336336
EndProject
337+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Connectors.AzureAIInference", "src\Connectors\Connectors.AzureAIInference\Connectors.AzureAIInference.csproj", "{063044B2-A901-43C5-BFDF-5E4E71C7BC33}"
338+
EndProject
339+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Connectors.AzureAIInference.UnitTests", "src\Connectors\Connectors.AzureAIInference.UnitTests\Connectors.AzureAIInference.UnitTests.csproj", "{E0D45DDB-6D32-40FC-AC79-E1F342C4F513}"
340+
EndProject
337341
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OnnxSimpleRAG", "samples\Demos\OnnxSimpleRAG\OnnxSimpleRAG.csproj", "{8972254B-B8F0-4119-953B-378E3BACA59A}"
338342
EndProject
339343
Global
@@ -853,6 +857,18 @@ Global
853857
{8CF06B22-50F3-4F71-A002-622DB49DF0F5}.Publish|Any CPU.Build.0 = Debug|Any CPU
854858
{8CF06B22-50F3-4F71-A002-622DB49DF0F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
855859
{8CF06B22-50F3-4F71-A002-622DB49DF0F5}.Release|Any CPU.Build.0 = Release|Any CPU
860+
{063044B2-A901-43C5-BFDF-5E4E71C7BC33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
861+
{063044B2-A901-43C5-BFDF-5E4E71C7BC33}.Debug|Any CPU.Build.0 = Debug|Any CPU
862+
{063044B2-A901-43C5-BFDF-5E4E71C7BC33}.Publish|Any CPU.ActiveCfg = Publish|Any CPU
863+
{063044B2-A901-43C5-BFDF-5E4E71C7BC33}.Publish|Any CPU.Build.0 = Publish|Any CPU
864+
{063044B2-A901-43C5-BFDF-5E4E71C7BC33}.Release|Any CPU.ActiveCfg = Release|Any CPU
865+
{063044B2-A901-43C5-BFDF-5E4E71C7BC33}.Release|Any CPU.Build.0 = Release|Any CPU
866+
{E0D45DDB-6D32-40FC-AC79-E1F342C4F513}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
867+
{E0D45DDB-6D32-40FC-AC79-E1F342C4F513}.Debug|Any CPU.Build.0 = Debug|Any CPU
868+
{E0D45DDB-6D32-40FC-AC79-E1F342C4F513}.Publish|Any CPU.ActiveCfg = Debug|Any CPU
869+
{E0D45DDB-6D32-40FC-AC79-E1F342C4F513}.Publish|Any CPU.Build.0 = Debug|Any CPU
870+
{E0D45DDB-6D32-40FC-AC79-E1F342C4F513}.Release|Any CPU.ActiveCfg = Release|Any CPU
871+
{E0D45DDB-6D32-40FC-AC79-E1F342C4F513}.Release|Any CPU.Build.0 = Release|Any CPU
856872
{8972254B-B8F0-4119-953B-378E3BACA59A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
857873
{8972254B-B8F0-4119-953B-378E3BACA59A}.Debug|Any CPU.Build.0 = Debug|Any CPU
858874
{8972254B-B8F0-4119-953B-378E3BACA59A}.Publish|Any CPU.ActiveCfg = Debug|Any CPU
@@ -975,6 +991,8 @@ Global
975991
{36DDC119-C030-407E-AC51-A877E9E0F660} = {1B4CBDE0-10C2-4E7D-9CD0-FE7586C96ED1}
976992
{7AAD7388-307D-41FB-B80A-EF9E3A4E31F0} = {1B4CBDE0-10C2-4E7D-9CD0-FE7586C96ED1}
977993
{8CF06B22-50F3-4F71-A002-622DB49DF0F5} = {1B4CBDE0-10C2-4E7D-9CD0-FE7586C96ED1}
994+
{063044B2-A901-43C5-BFDF-5E4E71C7BC33} = {1B4CBDE0-10C2-4E7D-9CD0-FE7586C96ED1}
995+
{E0D45DDB-6D32-40FC-AC79-E1F342C4F513} = {1B4CBDE0-10C2-4E7D-9CD0-FE7586C96ED1}
978996
{8972254B-B8F0-4119-953B-378E3BACA59A} = {5D4C0700-BBB5-418F-A7B2-F392B9A18263}
979997
EndGlobalSection
980998
GlobalSection(ExtensibilityGlobals) = postSolution
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System.Text;
4+
using Microsoft.SemanticKernel;
5+
using Microsoft.SemanticKernel.ChatCompletion;
6+
using Microsoft.SemanticKernel.Connectors.AzureAIInference;
7+
8+
namespace ChatCompletion;
9+
10+
// The following example shows how to use Semantic Kernel with Azure AI Inference / Azure AI Studio
11+
public class AzureAIInference_ChatCompletion(ITestOutputHelper output) : BaseTest(output)
12+
{
13+
[Fact]
14+
public async Task ServicePromptAsync()
15+
{
16+
Console.WriteLine("======== Azure AI Inference - Chat Completion ========");
17+
18+
var chatService = new AzureAIInferenceChatCompletionService(
19+
endpoint: new Uri(TestConfiguration.AzureAIInference.Endpoint),
20+
apiKey: TestConfiguration.AzureAIInference.ApiKey);
21+
22+
Console.WriteLine("Chat content:");
23+
Console.WriteLine("------------------------");
24+
25+
var chatHistory = new ChatHistory("You are a librarian, expert about books");
26+
27+
// First user message
28+
chatHistory.AddUserMessage("Hi, I'm looking for book suggestions");
29+
OutputLastMessage(chatHistory);
30+
31+
// First assistant message
32+
var reply = await chatService.GetChatMessageContentAsync(chatHistory);
33+
chatHistory.Add(reply);
34+
OutputLastMessage(chatHistory);
35+
36+
// Second user message
37+
chatHistory.AddUserMessage("I love history and philosophy, I'd like to learn something new about Greece, any suggestion");
38+
OutputLastMessage(chatHistory);
39+
40+
// Second assistant message
41+
reply = await chatService.GetChatMessageContentAsync(chatHistory);
42+
chatHistory.Add(reply);
43+
OutputLastMessage(chatHistory);
44+
45+
/* Output:
46+
47+
Chat content:
48+
------------------------
49+
System: You are a librarian, expert about books
50+
------------------------
51+
User: Hi, I'm looking for book suggestions
52+
------------------------
53+
Assistant: Sure, I'd be happy to help! What kind of books are you interested in? Fiction or non-fiction? Any particular genre?
54+
------------------------
55+
User: I love history and philosophy, I'd like to learn something new about Greece, any suggestion?
56+
------------------------
57+
Assistant: Great! For history and philosophy books about Greece, here are a few suggestions:
58+
59+
1. "The Greeks" by H.D.F. Kitto - This is a classic book that provides an overview of ancient Greek history and culture, including their philosophy, literature, and art.
60+
61+
2. "The Republic" by Plato - This is one of the most famous works of philosophy in the Western world, and it explores the nature of justice and the ideal society.
62+
63+
3. "The Peloponnesian War" by Thucydides - This is a detailed account of the war between Athens and Sparta in the 5th century BCE, and it provides insight into the political and military strategies of the time.
64+
65+
4. "The Iliad" by Homer - This epic poem tells the story of the Trojan War and is considered one of the greatest works of literature in the Western canon.
66+
67+
5. "The Histories" by Herodotus - This is a comprehensive account of the Persian Wars and provides a wealth of information about ancient Greek culture and society.
68+
69+
I hope these suggestions are helpful!
70+
------------------------
71+
*/
72+
}
73+
74+
[Fact]
75+
public async Task ChatPromptAsync()
76+
{
77+
StringBuilder chatPrompt = new("""
78+
<message role="system">You are a librarian, expert about books</message>
79+
<message role="user">Hi, I'm looking for book suggestions</message>
80+
""");
81+
82+
var kernel = Kernel.CreateBuilder()
83+
.AddAzureAIInferenceChatCompletion(
84+
endpoint: new Uri(TestConfiguration.AzureAIInference.Endpoint),
85+
apiKey: TestConfiguration.AzureAIInference.ApiKey)
86+
.Build();
87+
88+
var reply = await kernel.InvokePromptAsync(chatPrompt.ToString());
89+
90+
chatPrompt.AppendLine($"<message role=\"assistant\"><![CDATA[{reply}]]></message>");
91+
chatPrompt.AppendLine("<message role=\"user\">I love history and philosophy, I'd like to learn something new about Greece, any suggestion</message>");
92+
93+
reply = await kernel.InvokePromptAsync(chatPrompt.ToString());
94+
95+
Console.WriteLine(reply);
96+
}
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System.Text;
4+
using Microsoft.SemanticKernel;
5+
using Microsoft.SemanticKernel.ChatCompletion;
6+
using Microsoft.SemanticKernel.Connectors.AzureAIInference;
7+
8+
namespace ChatCompletion;
9+
10+
/// <summary>
11+
/// These examples demonstrate the ways different content types are streamed by OpenAI LLM via the chat completion service.
12+
/// </summary>
13+
public class AzureAIInference_ChatCompletionStreaming(ITestOutputHelper output) : BaseTest(output)
14+
{
15+
/// <summary>
16+
/// This example demonstrates chat completion streaming using OpenAI.
17+
/// </summary>
18+
[Fact]
19+
public Task StreamChatAsync()
20+
{
21+
Console.WriteLine("======== Azure AI Inference - Chat Completion Streaming ========");
22+
23+
var chatService = new AzureAIInferenceChatCompletionService(
24+
endpoint: new Uri(TestConfiguration.AzureAIInference.Endpoint),
25+
apiKey: TestConfiguration.AzureAIInference.ApiKey);
26+
27+
return this.StartStreamingChatAsync(chatService);
28+
}
29+
30+
/// <summary>
31+
/// This example demonstrates chat completion streaming using OpenAI via the kernel.
32+
/// </summary>
33+
[Fact]
34+
public async Task StreamChatPromptAsync()
35+
{
36+
Console.WriteLine("======== Azure AI Inference - Chat Prompt Completion Streaming ========");
37+
38+
StringBuilder chatPrompt = new("""
39+
<message role="system">You are a librarian, expert about books</message>
40+
<message role="user">Hi, I'm looking for book suggestions</message>
41+
""");
42+
43+
var kernel = Kernel.CreateBuilder()
44+
.AddAzureAIInferenceChatCompletion(
45+
endpoint: new Uri(TestConfiguration.AzureAIInference.Endpoint),
46+
apiKey: TestConfiguration.AzureAIInference.ApiKey)
47+
.Build();
48+
49+
var reply = await StreamMessageOutputFromKernelAsync(kernel, chatPrompt.ToString());
50+
51+
chatPrompt.AppendLine($"<message role=\"assistant\"><![CDATA[{reply}]]></message>");
52+
chatPrompt.AppendLine("<message role=\"user\">I love history and philosophy, I'd like to learn something new about Greece, any suggestion</message>");
53+
54+
reply = await StreamMessageOutputFromKernelAsync(kernel, chatPrompt.ToString());
55+
56+
Console.WriteLine(reply);
57+
}
58+
59+
/// <summary>
60+
/// This example demonstrates how the chat completion service streams text content.
61+
/// It shows how to access the response update via StreamingChatMessageContent.Content property
62+
/// and alternatively via the StreamingChatMessageContent.Items property.
63+
/// </summary>
64+
[Fact]
65+
public async Task StreamTextFromChatAsync()
66+
{
67+
Console.WriteLine("======== Stream Text from Chat Content ========");
68+
69+
// Create chat completion service
70+
var chatService = new AzureAIInferenceChatCompletionService(
71+
endpoint: new Uri(TestConfiguration.AzureAIInference.Endpoint),
72+
apiKey: TestConfiguration.AzureAIInference.ApiKey);
73+
74+
// Create chat history with initial system and user messages
75+
ChatHistory chatHistory = new("You are a librarian, an expert on books.");
76+
chatHistory.AddUserMessage("Hi, I'm looking for book suggestions.");
77+
chatHistory.AddUserMessage("I love history and philosophy. I'd like to learn something new about Greece, any suggestion?");
78+
79+
// Start streaming chat based on the chat history
80+
await foreach (StreamingChatMessageContent chatUpdate in chatService.GetStreamingChatMessageContentsAsync(chatHistory))
81+
{
82+
// Access the response update via StreamingChatMessageContent.Content property
83+
Console.Write(chatUpdate.Content);
84+
85+
// Alternatively, the response update can be accessed via the StreamingChatMessageContent.Items property
86+
Console.Write(chatUpdate.Items.OfType<StreamingTextContent>().FirstOrDefault());
87+
}
88+
}
89+
90+
/// <summary>
91+
/// Starts streaming chat with the chat completion service.
92+
/// </summary>
93+
/// <param name="chatCompletionService">The chat completion service instance.</param>
94+
private async Task StartStreamingChatAsync(IChatCompletionService chatCompletionService)
95+
{
96+
Console.WriteLine("Chat content:");
97+
Console.WriteLine("------------------------");
98+
99+
var chatHistory = new ChatHistory("You are a librarian, expert about books");
100+
OutputLastMessage(chatHistory);
101+
102+
// First user message
103+
chatHistory.AddUserMessage("Hi, I'm looking for book suggestions");
104+
OutputLastMessage(chatHistory);
105+
106+
// First assistant message
107+
await StreamMessageOutputAsync(chatCompletionService, chatHistory, AuthorRole.Assistant);
108+
109+
// Second user message
110+
chatHistory.AddUserMessage("I love history and philosophy, I'd like to learn something new about Greece, any suggestion?");
111+
OutputLastMessage(chatHistory);
112+
113+
// Second assistant message
114+
await StreamMessageOutputAsync(chatCompletionService, chatHistory, AuthorRole.Assistant);
115+
}
116+
117+
/// <summary>
118+
/// Streams the message output from the chat completion service.
119+
/// </summary>
120+
/// <param name="chatCompletionService">The chat completion service instance.</param>
121+
/// <param name="chatHistory">The chat history instance.</param>
122+
/// <param name="authorRole">The author role.</param>
123+
private async Task StreamMessageOutputAsync(IChatCompletionService chatCompletionService, ChatHistory chatHistory, AuthorRole authorRole)
124+
{
125+
bool roleWritten = false;
126+
string fullMessage = string.Empty;
127+
128+
await foreach (var chatUpdate in chatCompletionService.GetStreamingChatMessageContentsAsync(chatHistory))
129+
{
130+
if (!roleWritten && chatUpdate.Role.HasValue)
131+
{
132+
Console.Write($"{chatUpdate.Role.Value}: {chatUpdate.Content}");
133+
roleWritten = true;
134+
}
135+
136+
if (chatUpdate.Content is { Length: > 0 })
137+
{
138+
fullMessage += chatUpdate.Content;
139+
Console.Write(chatUpdate.Content);
140+
}
141+
}
142+
143+
Console.WriteLine("\n------------------------");
144+
chatHistory.AddMessage(authorRole, fullMessage);
145+
}
146+
147+
/// <summary>
148+
/// Outputs the chat history by streaming the message output from the kernel.
149+
/// </summary>
150+
/// <param name="kernel">The kernel instance.</param>
151+
/// <param name="prompt">The prompt message.</param>
152+
/// <returns>The full message output from the kernel.</returns>
153+
private async Task<string> StreamMessageOutputFromKernelAsync(Kernel kernel, string prompt)
154+
{
155+
bool roleWritten = false;
156+
string fullMessage = string.Empty;
157+
158+
await foreach (var chatUpdate in kernel.InvokePromptStreamingAsync<StreamingChatMessageContent>(prompt))
159+
{
160+
if (!roleWritten && chatUpdate.Role.HasValue)
161+
{
162+
Console.Write($"{chatUpdate.Role.Value}: {chatUpdate.Content}");
163+
roleWritten = true;
164+
}
165+
166+
if (chatUpdate.Content is { Length: > 0 })
167+
{
168+
fullMessage += chatUpdate.Content;
169+
Console.Write(chatUpdate.Content);
170+
}
171+
}
172+
173+
Console.WriteLine("\n------------------------");
174+
return fullMessage;
175+
}
176+
}

dotnet/samples/Concepts/ChatCompletion/Google_GeminiChatCompletion.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private async Task SimpleChatAsync(Kernel kernel)
9696
chatHistory.AddUserMessage("Hi, I'm looking for new power tools, any suggestion?");
9797
await MessageOutputAsync(chatHistory);
9898

99-
// First bot assistant message
99+
// First assistant message
100100
var reply = await chat.GetChatMessageContentAsync(chatHistory);
101101
chatHistory.Add(reply);
102102
await MessageOutputAsync(chatHistory);
@@ -105,7 +105,7 @@ private async Task SimpleChatAsync(Kernel kernel)
105105
chatHistory.AddUserMessage("I'm looking for a drill, a screwdriver and a hammer.");
106106
await MessageOutputAsync(chatHistory);
107107

108-
// Second bot assistant message
108+
// Second assistant message
109109
reply = await chat.GetChatMessageContentAsync(chatHistory);
110110
chatHistory.Add(reply);
111111
await MessageOutputAsync(chatHistory);

0 commit comments

Comments
 (0)