Skip to content

Commit 57bac49

Browse files
rogerbarretoN-E-W-T-O-NSergeyMenshykhrogerio-barretodependabot[bot]
authored
.Net: SK integration with MEAI Abstractions (#10754)
## Microsoft Extensions AI Integration This PR brings native integration to majority of `Microsoft.Extensions.AI` abstractions into Semantic Kernel components and API's. - Resolves #10081 Including: - #11628 - #11619 - #10727 - #10729 - #10730 - #12132 --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: NEWTON MALLICK <[email protected]> Co-authored-by: SergeyMenshykh <[email protected]> Co-authored-by: Roger Barreto <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Eduard van Valkenburg <[email protected]> Co-authored-by: westey <[email protected]> Co-authored-by: Phil Jirsa <[email protected]> Co-authored-by: DavidJFowler <[email protected]> Co-authored-by: Evan Mattson <[email protected]> Co-authored-by: Tao Chen <[email protected]> Co-authored-by: Tommaso Stocchi <[email protected]> Co-authored-by: Chris <[email protected]> Co-authored-by: Mark Wallace <[email protected]> Co-authored-by: Dade Cook <[email protected]> Co-authored-by: Dade Cook <[email protected]>
1 parent 16ccfba commit 57bac49

File tree

141 files changed

+10365
-1267
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+10365
-1267
lines changed

dotnet/SK-dotnet.sln

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OllamaFunctionCalling", "sa
433433
EndProject
434434
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAIRealtime", "samples\Demos\OpenAIRealtime\OpenAIRealtime.csproj", "{6154129E-7A35-44A5-998E-B7001B5EDE14}"
435435
EndProject
436-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CreateChatGpt", "CreateChatGpt", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
437-
EndProject
438436
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "VectorDataIntegrationTests", "VectorDataIntegrationTests", "{4F381919-F1BE-47D8-8558-3187ED04A84F}"
439437
EndProject
440438
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QdrantIntegrationTests", "src\VectorDataIntegrationTests\QdrantIntegrationTests\QdrantIntegrationTests.csproj", "{27D33AB3-4DFF-48BC-8D76-FB2CDF90B707}"
@@ -1681,7 +1679,6 @@ Global
16811679
{B35B1DEB-04DF-4141-9163-01031B22C5D1} = {0D8C6358-5DAA-4EA6-A924-C268A9A21BC9}
16821680
{481A680F-476A-4627-83DE-2F56C484525E} = {5D4C0700-BBB5-418F-A7B2-F392B9A18263}
16831681
{6154129E-7A35-44A5-998E-B7001B5EDE14} = {5D4C0700-BBB5-418F-A7B2-F392B9A18263}
1684-
{02EA681E-C7D8-13C7-8484-4AC65E1B71E8} = {5D4C0700-BBB5-418F-A7B2-F392B9A18263}
16851682
{4F381919-F1BE-47D8-8558-3187ED04A84F} = {831DDCA2-7D2C-4C31-80DB-6BDB3E1F7AE0}
16861683
{27D33AB3-4DFF-48BC-8D76-FB2CDF90B707} = {4F381919-F1BE-47D8-8558-3187ED04A84F}
16871684
{B29A972F-A774-4140-AECF-6B577C476627} = {4F381919-F1BE-47D8-8558-3187ED04A84F}

dotnet/samples/Concepts/Agents/ChatCompletion_FunctionTermination.cs

Lines changed: 117 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ namespace Agents;
1313
/// </summary>
1414
public class ChatCompletion_FunctionTermination(ITestOutputHelper output) : BaseAgentsTest(output)
1515
{
16-
[Fact]
17-
public async Task UseAutoFunctionInvocationFilterWithAgentInvocationAsync()
16+
[Theory]
17+
[InlineData(true)]
18+
[InlineData(false)]
19+
public async Task UseAutoFunctionInvocationFilterWithAgentInvocation(bool useChatClient)
1820
{
1921
// Define the agent
2022
ChatCompletionAgent agent =
2123
new()
2224
{
2325
Instructions = "Answer questions about the menu.",
24-
Kernel = CreateKernelWithFilter(),
26+
Kernel = CreateKernelWithFilter(useChatClient),
2527
Arguments = new KernelArguments(new PromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }),
2628
};
2729

@@ -53,15 +55,60 @@ async Task InvokeAgentAsync(string input)
5355
}
5456
}
5557

56-
[Fact]
57-
public async Task UseAutoFunctionInvocationFilterWithStreamingAgentInvocationAsync()
58+
[Theory]
59+
[InlineData(true)]
60+
[InlineData(false)]
61+
public async Task UseAutoFunctionInvocationFilterWithAgentChat(bool useChatClient)
5862
{
5963
// Define the agent
6064
ChatCompletionAgent agent =
6165
new()
6266
{
6367
Instructions = "Answer questions about the menu.",
64-
Kernel = CreateKernelWithFilter(),
68+
Kernel = CreateKernelWithFilter(useChatClient),
69+
Arguments = new KernelArguments(new PromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }),
70+
};
71+
72+
KernelPlugin plugin = KernelPluginFactory.CreateFromType<MenuPlugin>();
73+
agent.Kernel.Plugins.Add(plugin);
74+
75+
// Create a chat for agent interaction.
76+
AgentGroupChat chat = new();
77+
78+
// Respond to user input, invoking functions where appropriate.
79+
await InvokeAgentAsync("Hello");
80+
await InvokeAgentAsync("What is the special soup?");
81+
await InvokeAgentAsync("What is the special drink?");
82+
await InvokeAgentAsync("Thank you");
83+
84+
// Display the entire chat history.
85+
WriteChatHistory(await chat.GetChatMessagesAsync().ToArrayAsync());
86+
87+
// Local function to invoke agent and display the conversation messages.
88+
async Task InvokeAgentAsync(string input)
89+
{
90+
ChatMessageContent message = new(AuthorRole.User, input);
91+
chat.AddChatMessage(message);
92+
this.WriteAgentChatMessage(message);
93+
94+
await foreach (ChatMessageContent response in chat.InvokeAsync(agent))
95+
{
96+
this.WriteAgentChatMessage(response);
97+
}
98+
}
99+
}
100+
101+
[Theory]
102+
[InlineData(true)]
103+
[InlineData(false)]
104+
public async Task UseAutoFunctionInvocationFilterWithStreamingAgentInvocation(bool useChatClient)
105+
{
106+
// Define the agent
107+
ChatCompletionAgent agent =
108+
new()
109+
{
110+
Instructions = "Answer questions about the menu.",
111+
Kernel = CreateKernelWithFilter(useChatClient),
65112
Arguments = new KernelArguments(new PromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }),
66113
};
67114

@@ -115,6 +162,61 @@ async Task InvokeAgentAsync(string input)
115162
}
116163
}
117164

165+
[Theory]
166+
[InlineData(true)]
167+
[InlineData(false)]
168+
public async Task UseAutoFunctionInvocationFilterWithStreamingAgentChat(bool useChatClient)
169+
{
170+
// Define the agent
171+
ChatCompletionAgent agent =
172+
new()
173+
{
174+
Instructions = "Answer questions about the menu.",
175+
Kernel = CreateKernelWithFilter(useChatClient),
176+
Arguments = new KernelArguments(new PromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }),
177+
};
178+
179+
KernelPlugin plugin = KernelPluginFactory.CreateFromType<MenuPlugin>();
180+
agent.Kernel.Plugins.Add(plugin);
181+
182+
// Create a chat for agent interaction.
183+
AgentGroupChat chat = new();
184+
185+
// Respond to user input, invoking functions where appropriate.
186+
await InvokeAgentAsync("Hello");
187+
await InvokeAgentAsync("What is the special soup?");
188+
await InvokeAgentAsync("What is the special drink?");
189+
await InvokeAgentAsync("Thank you");
190+
191+
// Display the entire chat history.
192+
WriteChatHistory(await chat.GetChatMessagesAsync().ToArrayAsync());
193+
194+
// Local function to invoke agent and display the conversation messages.
195+
async Task InvokeAgentAsync(string input)
196+
{
197+
ChatMessageContent message = new(AuthorRole.User, input);
198+
chat.AddChatMessage(message);
199+
this.WriteAgentChatMessage(message);
200+
201+
bool isFirst = false;
202+
await foreach (StreamingChatMessageContent response in chat.InvokeStreamingAsync(agent))
203+
{
204+
if (string.IsNullOrEmpty(response.Content))
205+
{
206+
continue;
207+
}
208+
209+
if (!isFirst)
210+
{
211+
Console.WriteLine($"\n# {response.Role} - {response.AuthorName ?? "*"}:");
212+
isFirst = true;
213+
}
214+
215+
Console.WriteLine($"\t > streamed: '{response.Content}'");
216+
}
217+
}
218+
}
219+
118220
private void WriteChatHistory(IEnumerable<ChatMessageContent> chat)
119221
{
120222
Console.WriteLine("================================");
@@ -126,11 +228,18 @@ private void WriteChatHistory(IEnumerable<ChatMessageContent> chat)
126228
}
127229
}
128230

129-
private Kernel CreateKernelWithFilter()
231+
private Kernel CreateKernelWithFilter(bool useChatClient)
130232
{
131233
IKernelBuilder builder = Kernel.CreateBuilder();
132234

133-
base.AddChatCompletionToKernel(builder);
235+
if (useChatClient)
236+
{
237+
base.AddChatClientToKernel(builder);
238+
}
239+
else
240+
{
241+
base.AddChatCompletionToKernel(builder);
242+
}
134243

135244
builder.Services.AddSingleton<IAutoFunctionInvocationFilter>(new AutoInvocationFilter());
136245

dotnet/samples/Concepts/Agents/ChatCompletion_HistoryReducer.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3+
using Microsoft.Extensions.AI;
34
using Microsoft.SemanticKernel;
45
using Microsoft.SemanticKernel.Agents;
56
using Microsoft.SemanticKernel.ChatCompletion;
@@ -19,26 +20,34 @@ public class ChatCompletion_HistoryReducer(ITestOutputHelper output) : BaseTest(
1920
/// Demonstrate the use of <see cref="ChatHistoryTruncationReducer"/> when directly
2021
/// invoking a <see cref="ChatCompletionAgent"/>.
2122
/// </summary>
22-
[Fact]
23-
public async Task TruncatedAgentReductionAsync()
23+
[Theory]
24+
[InlineData(true)]
25+
[InlineData(false)]
26+
public async Task TruncatedAgentReduction(bool useChatClient)
2427
{
2528
// Define the agent
26-
ChatCompletionAgent agent = CreateTruncatingAgent(10, 10);
29+
ChatCompletionAgent agent = CreateTruncatingAgent(10, 10, useChatClient, out var chatClient);
2730

2831
await InvokeAgentAsync(agent, 50);
32+
33+
chatClient?.Dispose();
2934
}
3035

3136
/// <summary>
3237
/// Demonstrate the use of <see cref="ChatHistorySummarizationReducer"/> when directly
3338
/// invoking a <see cref="ChatCompletionAgent"/>.
3439
/// </summary>
35-
[Fact]
36-
public async Task SummarizedAgentReductionAsync()
40+
[Theory]
41+
[InlineData(true)]
42+
[InlineData(false)]
43+
public async Task SummarizedAgentReduction(bool useChatClient)
3744
{
3845
// Define the agent
39-
ChatCompletionAgent agent = CreateSummarizingAgent(10, 10);
46+
ChatCompletionAgent agent = CreateSummarizingAgent(10, 10, useChatClient, out var chatClient);
4047

4148
await InvokeAgentAsync(agent, 50);
49+
50+
chatClient?.Dispose();
4251
}
4352

4453
// Proceed with dialog by directly invoking the agent and explicitly managing the history.
@@ -79,25 +88,30 @@ private async Task InvokeAgentAsync(ChatCompletionAgent agent, int messageCount)
7988
}
8089
}
8190

82-
private ChatCompletionAgent CreateSummarizingAgent(int reducerMessageCount, int reducerThresholdCount)
91+
private ChatCompletionAgent CreateSummarizingAgent(int reducerMessageCount, int reducerThresholdCount, bool useChatClient, out IChatClient? chatClient)
8392
{
84-
Kernel kernel = this.CreateKernelWithChatCompletion();
93+
Kernel kernel = this.CreateKernelWithChatCompletion(useChatClient, out chatClient);
94+
95+
var service = useChatClient
96+
? kernel.GetRequiredService<IChatClient>().AsChatCompletionService()
97+
: kernel.GetRequiredService<IChatCompletionService>();
98+
8599
return
86100
new()
87101
{
88102
Name = TranslatorName,
89103
Instructions = TranslatorInstructions,
90104
Kernel = kernel,
91-
HistoryReducer = new ChatHistorySummarizationReducer(kernel.GetRequiredService<IChatCompletionService>(), reducerMessageCount, reducerThresholdCount),
105+
HistoryReducer = new ChatHistorySummarizationReducer(service, reducerMessageCount, reducerThresholdCount),
92106
};
93107
}
94108

95-
private ChatCompletionAgent CreateTruncatingAgent(int reducerMessageCount, int reducerThresholdCount) =>
109+
private ChatCompletionAgent CreateTruncatingAgent(int reducerMessageCount, int reducerThresholdCount, bool useChatClient, out IChatClient? chatClient) =>
96110
new()
97111
{
98112
Name = TranslatorName,
99113
Instructions = TranslatorInstructions,
100-
Kernel = this.CreateKernelWithChatCompletion(),
114+
Kernel = this.CreateKernelWithChatCompletion(useChatClient, out chatClient),
101115
HistoryReducer = new ChatHistoryTruncationReducer(reducerMessageCount, reducerThresholdCount),
102116
};
103117
}

dotnet/samples/Concepts/Agents/ChatCompletion_Serialization.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ public class ChatCompletion_Serialization(ITestOutputHelper output) : BaseAgents
1313
private const string HostName = "Host";
1414
private const string HostInstructions = "Answer questions about the menu.";
1515

16-
[Fact]
17-
public async Task SerializeAndRestoreAgentGroupChatAsync()
16+
[Theory]
17+
[InlineData(true)]
18+
[InlineData(false)]
19+
public async Task SerializeAndRestoreAgentGroupChat(bool useChatClient)
1820
{
1921
// Define the agent
2022
ChatCompletionAgent agent =
2123
new()
2224
{
2325
Instructions = HostInstructions,
2426
Name = HostName,
25-
Kernel = this.CreateKernelWithChatCompletion(),
27+
Kernel = this.CreateKernelWithChatCompletion(useChatClient, out var chatClient),
2628
Arguments = new KernelArguments(new PromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }),
2729
};
2830

@@ -51,6 +53,8 @@ public async Task SerializeAndRestoreAgentGroupChatAsync()
5153
this.WriteAgentChatMessage(content);
5254
}
5355

56+
chatClient?.Dispose();
57+
5458
// Local function to invoke agent and display the conversation messages.
5559
async Task InvokeAgentAsync(AgentGroupChat chat, string input)
5660
{

0 commit comments

Comments
 (0)