Skip to content

Commit 2f1974d

Browse files
westey-mmarkwallace-microsoft
authored andcommitted
.Net: Removing groupchat from samples that have a single agent and aren't demonstrating a groupchat concept (microsoft#11261)
### Motivation and Context Addressing Feedback from bugbash ### Description Removing groupchat from samples that have a single agent and aren't demonstrating a groupchat concept ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄 Co-authored-by: Mark Wallace <[email protected]>
1 parent 2c17e4e commit 2f1974d

6 files changed

+29
-191
lines changed

dotnet/samples/Concepts/Agents/AzureAIAgent_FileManipulation.cs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
using Azure.AI.Projects;
33
using Microsoft.SemanticKernel;
4-
using Microsoft.SemanticKernel.Agents;
54
using Microsoft.SemanticKernel.Agents.AzureAI;
65
using Microsoft.SemanticKernel.ChatCompletion;
76
using Resources;
@@ -33,9 +32,7 @@ public async Task AnalyzeCSVFileUsingAzureAIAgentAsync()
3332
}
3433
});
3534
AzureAIAgent agent = new(definition, this.AgentsClient);
36-
37-
// Create a chat for agent interaction.
38-
AgentGroupChat chat = new();
35+
AzureAIAgentThread thread = new(this.AgentsClient);
3936

4037
// Respond to user input
4138
try
@@ -46,19 +43,18 @@ public async Task AnalyzeCSVFileUsingAzureAIAgentAsync()
4643
}
4744
finally
4845
{
46+
await thread.DeleteAsync();
4947
await this.AgentsClient.DeleteAgentAsync(agent.Id);
5048
await this.AgentsClient.DeleteFileAsync(fileInfo.Id);
51-
await chat.ResetAsync();
5249
}
5350

5451
// Local function to invoke agent and display the conversation messages.
5552
async Task InvokeAgentAsync(string input)
5653
{
5754
ChatMessageContent message = new(AuthorRole.User, input);
58-
chat.AddChatMessage(new(AuthorRole.User, input));
5955
this.WriteAgentChatMessage(message);
6056

61-
await foreach (ChatMessageContent response in chat.InvokeAsync(agent))
57+
await foreach (ChatMessageContent response in agent.InvokeAsync(message, thread))
6258
{
6359
this.WriteAgentChatMessage(response);
6460
await this.DownloadContentAsync(response);

dotnet/samples/Concepts/Agents/ChatCompletion_FunctionTermination.cs

-94
Original file line numberDiff line numberDiff line change
@@ -53,47 +53,6 @@ async Task InvokeAgentAsync(string input)
5353
}
5454
}
5555

56-
[Fact]
57-
public async Task UseAutoFunctionInvocationFilterWithAgentChatAsync()
58-
{
59-
// Define the agent
60-
ChatCompletionAgent agent =
61-
new()
62-
{
63-
Instructions = "Answer questions about the menu.",
64-
Kernel = CreateKernelWithFilter(),
65-
Arguments = new KernelArguments(new PromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }),
66-
};
67-
68-
KernelPlugin plugin = KernelPluginFactory.CreateFromType<MenuPlugin>();
69-
agent.Kernel.Plugins.Add(plugin);
70-
71-
// Create a chat for agent interaction.
72-
AgentGroupChat chat = new();
73-
74-
// Respond to user input, invoking functions where appropriate.
75-
await InvokeAgentAsync("Hello");
76-
await InvokeAgentAsync("What is the special soup?");
77-
await InvokeAgentAsync("What is the special drink?");
78-
await InvokeAgentAsync("Thank you");
79-
80-
// Display the entire chat history.
81-
WriteChatHistory(await chat.GetChatMessagesAsync().ToArrayAsync());
82-
83-
// Local function to invoke agent and display the conversation messages.
84-
async Task InvokeAgentAsync(string input)
85-
{
86-
ChatMessageContent message = new(AuthorRole.User, input);
87-
chat.AddChatMessage(message);
88-
this.WriteAgentChatMessage(message);
89-
90-
await foreach (ChatMessageContent response in chat.InvokeAsync(agent))
91-
{
92-
this.WriteAgentChatMessage(response);
93-
}
94-
}
95-
}
96-
9756
[Fact]
9857
public async Task UseAutoFunctionInvocationFilterWithStreamingAgentInvocationAsync()
9958
{
@@ -156,59 +115,6 @@ async Task InvokeAgentAsync(string input)
156115
}
157116
}
158117

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

dotnet/samples/Concepts/Agents/ChatCompletion_HistoryReducer.cs

-68
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,6 @@ public async Task SummarizedAgentReductionAsync()
4141
await InvokeAgentAsync(agent, 50);
4242
}
4343

44-
/// <summary>
45-
/// Demonstrate the use of <see cref="ChatHistoryTruncationReducer"/> when using
46-
/// <see cref="AgentGroupChat"/> to invoke a <see cref="ChatCompletionAgent"/>.
47-
/// </summary>
48-
[Fact]
49-
public async Task TruncatedChatReductionAsync()
50-
{
51-
// Define the agent
52-
ChatCompletionAgent agent = CreateTruncatingAgent(10, 10);
53-
54-
await InvokeChatAsync(agent, 50);
55-
}
56-
57-
/// <summary>
58-
/// Demonstrate the use of <see cref="ChatHistorySummarizationReducer"/> when using
59-
/// <see cref="AgentGroupChat"/> to invoke a <see cref="ChatCompletionAgent"/>.
60-
/// </summary>
61-
[Fact]
62-
public async Task SummarizedChatReductionAsync()
63-
{
64-
// Define the agent
65-
ChatCompletionAgent agent = CreateSummarizingAgent(10, 10);
66-
67-
await InvokeChatAsync(agent, 50);
68-
}
69-
7044
// Proceed with dialog by directly invoking the agent and explicitly managing the history.
7145
private async Task InvokeAgentAsync(ChatCompletionAgent agent, int messageCount)
7246
{
@@ -105,48 +79,6 @@ private async Task InvokeAgentAsync(ChatCompletionAgent agent, int messageCount)
10579
}
10680
}
10781

108-
// Proceed with dialog with AgentGroupChat.
109-
private async Task InvokeChatAsync(ChatCompletionAgent agent, int messageCount)
110-
{
111-
AgentGroupChat chat = new();
112-
113-
int lastHistoryCount = 0;
114-
115-
int index = 1;
116-
while (index <= messageCount)
117-
{
118-
// Provide user input
119-
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, $"{index}"));
120-
Console.WriteLine($"# {AuthorRole.User}: '{index}'");
121-
122-
// Invoke and display assistant response
123-
await foreach (ChatMessageContent message in chat.InvokeAsync(agent))
124-
{
125-
Console.WriteLine($"# {message.Role} - {message.AuthorName ?? "*"}: '{message.Content}'");
126-
}
127-
128-
index += 2;
129-
130-
// Display the message count of the chat-history for visibility into reduction
131-
// Note: Messages provided in descending order (newest first)
132-
ChatMessageContent[] history = await chat.GetChatMessagesAsync(agent).ToArrayAsync();
133-
Console.WriteLine($"@ Message Count: {history.Length}\n");
134-
135-
// Display summary messages (if present) if reduction has occurred
136-
if (history.Length < lastHistoryCount)
137-
{
138-
int summaryIndex = history.Length - 1;
139-
while (history[summaryIndex].Metadata?.ContainsKey(ChatHistorySummarizationReducer.SummaryMetadataKey) ?? false)
140-
{
141-
Console.WriteLine($"\tSummary: {history[summaryIndex].Content}");
142-
--summaryIndex;
143-
}
144-
}
145-
146-
lastHistoryCount = history.Length;
147-
}
148-
}
149-
15082
private ChatCompletionAgent CreateSummarizingAgent(int reducerMessageCount, int reducerThresholdCount)
15183
{
15284
Kernel kernel = this.CreateKernelWithChatCompletion();

dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ await this.AssistantClient.CreateAssistantAsync(
2727

2828
// Create the agent
2929
OpenAIAssistantAgent agent = new(assistant, this.AssistantClient);
30-
31-
// Create a chat for agent interaction.
32-
AgentGroupChat chat = new();
30+
AgentThread? agentThread = null;
3331

3432
// Respond to user input
3533
try
@@ -50,20 +48,26 @@ Sum 426 1622 856 2904
5048
}
5149
finally
5250
{
51+
if (agentThread is not null)
52+
{
53+
await agentThread.DeleteAsync();
54+
}
55+
5356
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
5457
}
5558

5659
// Local function to invoke agent and display the conversation messages.
5760
async Task InvokeAgentAsync(string input)
5861
{
5962
ChatMessageContent message = new(AuthorRole.User, input);
60-
chat.AddChatMessage(message);
6163
this.WriteAgentChatMessage(message);
6264

63-
await foreach (ChatMessageContent response in chat.InvokeAsync(agent))
65+
await foreach (AgentResponseItem<ChatMessageContent> response in agent.InvokeAsync(message))
6466
{
6567
this.WriteAgentChatMessage(response);
6668
await this.DownloadResponseImageAsync(response);
69+
70+
agentThread = response.Thread;
6771
}
6872
}
6973
}

dotnet/samples/Concepts/Agents/OpenAIAssistant_FileManipulation.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ await this.AssistantClient.CreateAssistantAsync(
2929

3030
// Create the agent
3131
OpenAIAssistantAgent agent = new(assistant, this.AssistantClient);
32-
33-
// Create a chat for agent interaction.
34-
AgentGroupChat chat = new();
32+
AgentThread? agentThread = null;
3533

3634
// Respond to user input
3735
try
@@ -42,6 +40,11 @@ await this.AssistantClient.CreateAssistantAsync(
4240
}
4341
finally
4442
{
43+
if (agentThread is not null)
44+
{
45+
await agentThread.DeleteAsync();
46+
}
47+
4548
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
4649
await this.Client.DeleteFileAsync(fileId);
4750
}
@@ -50,13 +53,14 @@ await this.AssistantClient.CreateAssistantAsync(
5053
async Task InvokeAgentAsync(string input)
5154
{
5255
ChatMessageContent message = new(AuthorRole.User, input);
53-
chat.AddChatMessage(new(AuthorRole.User, input));
5456
this.WriteAgentChatMessage(message);
5557

56-
await foreach (ChatMessageContent response in chat.InvokeAsync(agent))
58+
await foreach (AgentResponseItem<ChatMessageContent> response in agent.InvokeAsync(message))
5759
{
5860
this.WriteAgentChatMessage(response);
5961
await this.DownloadResponseContentAsync(response);
62+
63+
agentThread = response.Thread;
6064
}
6165
}
6266
}

dotnet/samples/Concepts/Agents/OpenAIAssistant_FunctionFilters.cs

+8-12
Original file line numberDiff line numberDiff line change
@@ -62,46 +62,42 @@ public async Task UseAutoFunctionInvocationFilterWithStreamingAgentInvocationAsy
6262

6363
private async Task InvokeAssistantAsync(OpenAIAssistantAgent agent)
6464
{
65-
// Create a thread for the agent conversation.
66-
AgentGroupChat chat = new();
65+
OpenAIAssistantAgentThread agentThread = new(this.AssistantClient);
6766

6867
try
6968
{
7069
// Respond to user input, invoking functions where appropriate.
7170
ChatMessageContent message = new(AuthorRole.User, "What is the special soup?");
72-
chat.AddChatMessage(message);
73-
await chat.InvokeAsync(agent).ToArrayAsync();
71+
await agent.InvokeAsync(message, agentThread).ToArrayAsync();
7472

7573
// Display the entire chat history.
76-
ChatMessageContent[] history = await chat.GetChatMessagesAsync().Reverse().ToArrayAsync();
74+
ChatMessageContent[] history = await agentThread.GetMessagesAsync(MessageCollectionOrder.Ascending).ToArrayAsync();
7775
this.WriteChatHistory(history);
7876
}
7977
finally
8078
{
81-
await chat.ResetAsync();
79+
await agentThread.DeleteAsync();
8280
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
8381
}
8482
}
8583

8684
private async Task InvokeAssistantStreamingAsync(OpenAIAssistantAgent agent)
8785
{
88-
// Create a thread for the agent conversation.
89-
AgentGroupChat chat = new();
86+
OpenAIAssistantAgentThread agentThread = new(this.AssistantClient);
9087

9188
try
9289
{
9390
// Respond to user input, invoking functions where appropriate.
9491
ChatMessageContent message = new(AuthorRole.User, "What is the special soup?");
95-
chat.AddChatMessage(message);
96-
await chat.InvokeStreamingAsync(agent).ToArrayAsync();
92+
await agent.InvokeStreamingAsync(message, agentThread).ToArrayAsync();
9793

9894
// Display the entire chat history.
99-
ChatMessageContent[] history = await chat.GetChatMessagesAsync().Reverse().ToArrayAsync();
95+
ChatMessageContent[] history = await agentThread.GetMessagesAsync(MessageCollectionOrder.Ascending).ToArrayAsync();
10096
this.WriteChatHistory(history);
10197
}
10298
finally
10399
{
104-
await chat.ResetAsync();
100+
await agentThread.DeleteAsync();
105101
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
106102
}
107103
}

0 commit comments

Comments
 (0)