mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
0982aa9d6d
* Fix bugbash issues * Update dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIResponseClient.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
85 lines
2.9 KiB
C#
85 lines
2.9 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using Microsoft.Extensions.AI;
|
|
using Microsoft.Extensions.AI.Agents;
|
|
using Microsoft.Shared.Samples;
|
|
using OpenAI;
|
|
|
|
namespace Providers;
|
|
|
|
/// <summary>
|
|
/// End-to-end sample showing how to use <see cref="ChatClientAgent"/> with OpenAI Chat Completion.
|
|
/// </summary>
|
|
public sealed class AIAgent_With_OpenAIResponseClient(ITestOutputHelper output) : AgentSample(output)
|
|
{
|
|
private const string JokerName = "Joker";
|
|
private const string JokerInstructions = "You are good at telling jokes.";
|
|
|
|
/// <summary>
|
|
/// This will use the conversation id to reference the thread state on the server side.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task RunWithResponses()
|
|
{
|
|
// Get the agent directly from OpenAIClient.
|
|
AIAgent agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
|
.GetOpenAIResponseClient(TestConfiguration.OpenAI.ChatModelId)
|
|
.CreateAIAgent(JokerInstructions, JokerName);
|
|
|
|
// Start a new thread for the agent conversation based on the type.
|
|
AgentThread thread = agent.GetNewThread();
|
|
|
|
// Respond to user input.
|
|
await RunAgentAsync("Tell me a joke about a pirate.");
|
|
await RunAgentAsync("Now add some emojis to the joke.");
|
|
|
|
// Local function to invoke agent and display the conversation messages for the thread.
|
|
async Task RunAgentAsync(string input)
|
|
{
|
|
Console.WriteLine(
|
|
$"""
|
|
User: {input}
|
|
Assistant:
|
|
{await agent.RunAsync(input, thread)}
|
|
|
|
""");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// This will use in-memory messages to store the thread state.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task RunWithResponsesAndStoreOutputDisabled()
|
|
{
|
|
// Get the agent directly from OpenAIClient.
|
|
AIAgent agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
|
.GetOpenAIResponseClient(TestConfiguration.OpenAI.ChatModelId)
|
|
.CreateAIAgent(options: new()
|
|
{
|
|
Name = JokerName,
|
|
Instructions = JokerInstructions,
|
|
ChatOptions = new ChatOptions().WithResponseStoredOutputDisabled()
|
|
});
|
|
|
|
// Start a new thread for the agent conversation based on the type.
|
|
AgentThread thread = agent.GetNewThread();
|
|
|
|
// Respond to user input.
|
|
await RunAgentAsync("Tell me a joke about a pirate.");
|
|
await RunAgentAsync("Now add some emojis to the joke.");
|
|
|
|
// Local function to invoke agent and display the conversation messages for the thread.
|
|
async Task RunAgentAsync(string input)
|
|
{
|
|
Console.WriteLine(
|
|
$"""
|
|
User: {input}
|
|
Assistant:
|
|
{await agent.RunAsync(input, thread)}
|
|
|
|
""");
|
|
}
|
|
}
|
|
}
|