Skip to content

.Net: Removing groupchat from samples that have a single agent and aren't demonstrating a groupchat concept #11261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
using Azure.AI.Projects;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.AzureAI;
using Microsoft.SemanticKernel.ChatCompletion;
using Resources;
Expand Down Expand Up @@ -33,9 +32,7 @@ public async Task AnalyzeCSVFileUsingAzureAIAgentAsync()
}
});
AzureAIAgent agent = new(definition, this.AgentsClient);

// Create a chat for agent interaction.
AgentGroupChat chat = new();
AzureAIAgentThread thread = new(this.AgentsClient);

// Respond to user input
try
Expand All @@ -46,19 +43,18 @@ public async Task AnalyzeCSVFileUsingAzureAIAgentAsync()
}
finally
{
await thread.DeleteAsync();
await this.AgentsClient.DeleteAgentAsync(agent.Id);
await this.AgentsClient.DeleteFileAsync(fileInfo.Id);
await chat.ResetAsync();
}

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

await foreach (ChatMessageContent response in chat.InvokeAsync(agent))
await foreach (ChatMessageContent response in agent.InvokeAsync(message, thread))
{
this.WriteAgentChatMessage(response);
await this.DownloadContentAsync(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,47 +53,6 @@ async Task InvokeAgentAsync(string input)
}
}

[Fact]
public async Task UseAutoFunctionInvocationFilterWithAgentChatAsync()
{
// Define the agent
ChatCompletionAgent agent =
new()
{
Instructions = "Answer questions about the menu.",
Kernel = CreateKernelWithFilter(),
Arguments = new KernelArguments(new PromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }),
};

KernelPlugin plugin = KernelPluginFactory.CreateFromType<MenuPlugin>();
agent.Kernel.Plugins.Add(plugin);

// Create a chat for agent interaction.
AgentGroupChat chat = new();

// Respond to user input, invoking functions where appropriate.
await InvokeAgentAsync("Hello");
await InvokeAgentAsync("What is the special soup?");
await InvokeAgentAsync("What is the special drink?");
await InvokeAgentAsync("Thank you");

// Display the entire chat history.
WriteChatHistory(await chat.GetChatMessagesAsync().ToArrayAsync());

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

await foreach (ChatMessageContent response in chat.InvokeAsync(agent))
{
this.WriteAgentChatMessage(response);
}
}
}

[Fact]
public async Task UseAutoFunctionInvocationFilterWithStreamingAgentInvocationAsync()
{
Expand Down Expand Up @@ -156,59 +115,6 @@ async Task InvokeAgentAsync(string input)
}
}

[Fact]
public async Task UseAutoFunctionInvocationFilterWithStreamingAgentChatAsync()
{
// Define the agent
ChatCompletionAgent agent =
new()
{
Instructions = "Answer questions about the menu.",
Kernel = CreateKernelWithFilter(),
Arguments = new KernelArguments(new PromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }),
};

KernelPlugin plugin = KernelPluginFactory.CreateFromType<MenuPlugin>();
agent.Kernel.Plugins.Add(plugin);

// Create a chat for agent interaction.
AgentGroupChat chat = new();

// Respond to user input, invoking functions where appropriate.
await InvokeAgentAsync("Hello");
await InvokeAgentAsync("What is the special soup?");
await InvokeAgentAsync("What is the special drink?");
await InvokeAgentAsync("Thank you");

// Display the entire chat history.
WriteChatHistory(await chat.GetChatMessagesAsync().ToArrayAsync());

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

bool isFirst = false;
await foreach (StreamingChatMessageContent response in chat.InvokeStreamingAsync(agent))
{
if (string.IsNullOrEmpty(response.Content))
{
continue;
}

if (!isFirst)
{
Console.WriteLine($"\n# {response.Role} - {response.AuthorName ?? "*"}:");
isFirst = true;
}

Console.WriteLine($"\t > streamed: '{response.Content}'");
}
}
}

private void WriteChatHistory(IEnumerable<ChatMessageContent> chat)
{
Console.WriteLine("================================");
Expand Down
68 changes: 0 additions & 68 deletions dotnet/samples/Concepts/Agents/ChatCompletion_HistoryReducer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,6 @@ public async Task SummarizedAgentReductionAsync()
await InvokeAgentAsync(agent, 50);
}

/// <summary>
/// Demonstrate the use of <see cref="ChatHistoryTruncationReducer"/> when using
/// <see cref="AgentGroupChat"/> to invoke a <see cref="ChatCompletionAgent"/>.
/// </summary>
[Fact]
public async Task TruncatedChatReductionAsync()
{
// Define the agent
ChatCompletionAgent agent = CreateTruncatingAgent(10, 10);

await InvokeChatAsync(agent, 50);
}

/// <summary>
/// Demonstrate the use of <see cref="ChatHistorySummarizationReducer"/> when using
/// <see cref="AgentGroupChat"/> to invoke a <see cref="ChatCompletionAgent"/>.
/// </summary>
[Fact]
public async Task SummarizedChatReductionAsync()
{
// Define the agent
ChatCompletionAgent agent = CreateSummarizingAgent(10, 10);

await InvokeChatAsync(agent, 50);
}

// Proceed with dialog by directly invoking the agent and explicitly managing the history.
private async Task InvokeAgentAsync(ChatCompletionAgent agent, int messageCount)
{
Expand Down Expand Up @@ -105,48 +79,6 @@ private async Task InvokeAgentAsync(ChatCompletionAgent agent, int messageCount)
}
}

// Proceed with dialog with AgentGroupChat.
private async Task InvokeChatAsync(ChatCompletionAgent agent, int messageCount)
{
AgentGroupChat chat = new();

int lastHistoryCount = 0;

int index = 1;
while (index <= messageCount)
{
// Provide user input
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, $"{index}"));
Console.WriteLine($"# {AuthorRole.User}: '{index}'");

// Invoke and display assistant response
await foreach (ChatMessageContent message in chat.InvokeAsync(agent))
{
Console.WriteLine($"# {message.Role} - {message.AuthorName ?? "*"}: '{message.Content}'");
}

index += 2;

// Display the message count of the chat-history for visibility into reduction
// Note: Messages provided in descending order (newest first)
ChatMessageContent[] history = await chat.GetChatMessagesAsync(agent).ToArrayAsync();
Console.WriteLine($"@ Message Count: {history.Length}\n");

// Display summary messages (if present) if reduction has occurred
if (history.Length < lastHistoryCount)
{
int summaryIndex = history.Length - 1;
while (history[summaryIndex].Metadata?.ContainsKey(ChatHistorySummarizationReducer.SummaryMetadataKey) ?? false)
{
Console.WriteLine($"\tSummary: {history[summaryIndex].Content}");
--summaryIndex;
}
}

lastHistoryCount = history.Length;
}
}

private ChatCompletionAgent CreateSummarizingAgent(int reducerMessageCount, int reducerThresholdCount)
{
Kernel kernel = this.CreateKernelWithChatCompletion();
Expand Down
14 changes: 9 additions & 5 deletions dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ await this.AssistantClient.CreateAssistantAsync(

// Create the agent
OpenAIAssistantAgent agent = new(assistant, this.AssistantClient);

// Create a chat for agent interaction.
AgentGroupChat chat = new();
AgentThread? agentThread = null;

// Respond to user input
try
Expand All @@ -50,20 +48,26 @@ Sum 426 1622 856 2904
}
finally
{
if (agentThread is not null)
{
await agentThread.DeleteAsync();
}

await this.AssistantClient.DeleteAssistantAsync(agent.Id);
}

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

await foreach (ChatMessageContent response in chat.InvokeAsync(agent))
await foreach (AgentResponseItem<ChatMessageContent> response in agent.InvokeAsync(message))
{
this.WriteAgentChatMessage(response);
await this.DownloadResponseImageAsync(response);

agentThread = response.Thread;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ await this.AssistantClient.CreateAssistantAsync(

// Create the agent
OpenAIAssistantAgent agent = new(assistant, this.AssistantClient);

// Create a chat for agent interaction.
AgentGroupChat chat = new();
AgentThread? agentThread = null;

// Respond to user input
try
Expand All @@ -42,6 +40,11 @@ await this.AssistantClient.CreateAssistantAsync(
}
finally
{
if (agentThread is not null)
{
await agentThread.DeleteAsync();
}

await this.AssistantClient.DeleteAssistantAsync(agent.Id);
await this.Client.DeleteFileAsync(fileId);
}
Expand All @@ -50,13 +53,14 @@ await this.AssistantClient.CreateAssistantAsync(
async Task InvokeAgentAsync(string input)
{
ChatMessageContent message = new(AuthorRole.User, input);
chat.AddChatMessage(new(AuthorRole.User, input));
this.WriteAgentChatMessage(message);

await foreach (ChatMessageContent response in chat.InvokeAsync(agent))
await foreach (AgentResponseItem<ChatMessageContent> response in agent.InvokeAsync(message))
{
this.WriteAgentChatMessage(response);
await this.DownloadResponseContentAsync(response);

agentThread = response.Thread;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,46 +62,42 @@ public async Task UseAutoFunctionInvocationFilterWithStreamingAgentInvocationAsy

private async Task InvokeAssistantAsync(OpenAIAssistantAgent agent)
{
// Create a thread for the agent conversation.
AgentGroupChat chat = new();
OpenAIAssistantAgentThread agentThread = new(this.AssistantClient);

try
{
// Respond to user input, invoking functions where appropriate.
ChatMessageContent message = new(AuthorRole.User, "What is the special soup?");
chat.AddChatMessage(message);
await chat.InvokeAsync(agent).ToArrayAsync();
await agent.InvokeAsync(message, agentThread).ToArrayAsync();

// Display the entire chat history.
ChatMessageContent[] history = await chat.GetChatMessagesAsync().Reverse().ToArrayAsync();
ChatMessageContent[] history = await agentThread.GetMessagesAsync(MessageCollectionOrder.Ascending).ToArrayAsync();
this.WriteChatHistory(history);
}
finally
{
await chat.ResetAsync();
await agentThread.DeleteAsync();
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
}
}

private async Task InvokeAssistantStreamingAsync(OpenAIAssistantAgent agent)
{
// Create a thread for the agent conversation.
AgentGroupChat chat = new();
OpenAIAssistantAgentThread agentThread = new(this.AssistantClient);

try
{
// Respond to user input, invoking functions where appropriate.
ChatMessageContent message = new(AuthorRole.User, "What is the special soup?");
chat.AddChatMessage(message);
await chat.InvokeStreamingAsync(agent).ToArrayAsync();
await agent.InvokeStreamingAsync(message, agentThread).ToArrayAsync();

// Display the entire chat history.
ChatMessageContent[] history = await chat.GetChatMessagesAsync().Reverse().ToArrayAsync();
ChatMessageContent[] history = await agentThread.GetMessagesAsync(MessageCollectionOrder.Ascending).ToArrayAsync();
this.WriteChatHistory(history);
}
finally
{
await chat.ResetAsync();
await agentThread.DeleteAsync();
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
}
}
Expand Down
Loading