Files
wehub-resource-sync b957a53def
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:21:23 +08:00

89 lines
2.9 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;
using OpenAI.Assistants;
namespace Agents;
/// <summary>
/// Demonstrate the use of <see cref="AgentChat.ResetAsync"/>.
/// </summary>
public class MixedChat_Reset(ITestOutputHelper output) : BaseAssistantTest(output)
{
private const string AgentInstructions =
"""
The user may either provide information or query on information previously provided.
If the query does not correspond with information provided, inform the user that their query cannot be answered.
""";
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task ResetChat(bool useChatClient)
{
// Define the assistant
Assistant assistant =
await this.AssistantClient.CreateAssistantAsync(
this.Model,
instructions: AgentInstructions,
metadata: SampleMetadata);
// Create the agent
OpenAIAssistantAgent assistantAgent = new(assistant, this.AssistantClient);
ChatCompletionAgent chatAgent =
new()
{
Name = nameof(ChatCompletionAgent),
Instructions = AgentInstructions,
Kernel = this.CreateKernelWithChatCompletion(useChatClient, out var chatClient),
};
// Create a chat for agent interaction.
AgentGroupChat chat = new();
// Respond to user input
try
{
await InvokeAgentAsync(assistantAgent, "What is my favorite color?");
await InvokeAgentAsync(chatAgent);
await InvokeAgentAsync(assistantAgent, "I like green.");
await InvokeAgentAsync(chatAgent);
await InvokeAgentAsync(assistantAgent, "What is my favorite color?");
await InvokeAgentAsync(chatAgent);
await chat.ResetAsync();
await InvokeAgentAsync(assistantAgent, "What is my favorite color?");
await InvokeAgentAsync(chatAgent);
}
finally
{
await chat.ResetAsync();
await this.AssistantClient.DeleteAssistantAsync(assistantAgent.Id);
}
chatClient?.Dispose();
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(Agent agent, string? input = null)
{
if (!string.IsNullOrWhiteSpace(input))
{
ChatMessageContent message = new(AuthorRole.User, input);
chat.AddChatMessage(message);
this.WriteAgentChatMessage(message);
}
await foreach (ChatMessageContent response in chat.InvokeAsync(agent))
{
this.WriteAgentChatMessage(response);
}
}
}
}