// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Agents;
namespace Steps;
///
/// Provides test methods to demonstrate the usage of chat agents with different interaction models.
///
/// This class contains examples of using to showcase scenarios with and without conversation history.
/// Each test method demonstrates how to configure and interact with the agents, including handling user input and displaying responses.
///
public sealed class Step01_Running(ITestOutputHelper output) : AgentSample(output)
{
private const string ParrotName = "Parrot";
private const string ParrotInstructions = "Repeat the user message in the voice of a pirate and then end with a parrot sound.";
private const string JokerName = "Joker";
private const string JokerInstructions = "You are good at telling jokes.";
///
/// Demonstrate the usage of where each invocation is
/// a unique interaction with no conversation history between them.
///
[Fact]
public async Task RunWithoutThread()
{
// Get the chat client to use for the agent.
using var chatClient = base.GetOpenAIChatClient();
// Define the agent
ChatClientAgent agent =
new(chatClient, new()
{
Name = ParrotName,
Instructions = ParrotInstructions,
});
// Respond to user input
await InvokeAgentAsync("Fortune favors the bold.");
await InvokeAgentAsync("I came, I saw, I conquered.");
await InvokeAgentAsync("Practice makes perfect.");
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
{
this.WriteUserMessage(input);
var response = await agent.RunAsync(input);
this.WriteResponseOutput(response);
}
}
///
/// Demonstrate the usage of where a conversation history is maintained.
///
[Fact]
public async Task RunWithConversationThread()
{
// Get the chat client to use for the agent.
using var chatClient = base.GetOpenAIChatClient();
// Define the agent
ChatClientAgent agent =
new(chatClient, new()
{
Name = JokerName,
Instructions = JokerInstructions,
});
// Start a new thread for the agent conversation.
AgentThread thread = agent.GetNewThread();
// Respond to user input
await InvokeAgentAsync("Tell me a joke about a pirate.");
await InvokeAgentAsync("Now add some emojis to the joke.");
// Local function to invoke agent and display the conversation messages for the thread.
async Task InvokeAgentAsync(string input)
{
this.WriteUserMessage(input);
var response = await agent.RunAsync(input, thread);
this.WriteResponseOutput(response);
}
}
///
/// Demonstrate the usage of in streaming mode,
/// where a conversation is maintained by the .
///
[Fact]
public async Task StreamingRunWithConversationThread()
{
// Get the chat client to use for the agent.
using var chatClient = base.GetOpenAIChatClient();
// Define the agent
ChatClientAgent agent =
new(chatClient, new()
{
Name = ParrotName,
Instructions = ParrotInstructions,
});
// Start a new thread for the agent conversation.
AgentThread thread = agent.GetNewThread();
// Respond to user input
await InvokeAgentAsync("Tell me a joke about a pirate.");
await InvokeAgentAsync("Now add some emojis to the joke.");
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
{
this.WriteUserMessage(input);
await foreach (var update in agent.RunStreamingAsync(input, thread))
{
this.WriteAgentOutput(update);
}
}
}
}