Files
agent-framework/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIClient.cs
T
Mark Wallace 0982aa9d6d .NET: Fix bugbash issues (#448)
* 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>
2025-08-20 12:22:11 +00:00

111 lines
3.7 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Extensions.AI.Agents;
using Microsoft.Shared.Samples;
using OpenAI;
using OpenAI.Chat;
namespace Providers;
/// <summary>
/// End-to-end sample showing how to use <see cref="AIAgent"/> with OpenAI Chat Completion and Responses.
/// </summary>
public sealed class AIAgent_With_OpenAIClient(ITestOutputHelper output) : AgentSample(output)
{
private const string JokerName = "Joker";
private const string JokerInstructions = "You are good at telling jokes.";
[Fact]
public async Task RunWithChatCompletion()
{
// Create the agent using the OpenAI ChatClient.
AIAgent agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
.GetChatClient(TestConfiguration.OpenAI.ChatModelId)
.CreateAIAgent(JokerInstructions, JokerName);
// Start a new thread for the agent conversation.
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)}
""");
}
}
[Fact]
public async Task RunWithChatCompletionReturnChatCompletion()
{
// Get the agent directly from OpenAIClient.
var agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
.GetChatClient(TestConfiguration.OpenAI.ChatModelId)
.CreateAIAgent(JokerInstructions, JokerName);
// Start a new thread for the agent conversation.
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}");
var response = await agent.RunAsync(input, thread);
var chatCompletion = response.AsChatCompletion();
Console.WriteLine(
$"""
Assistant:
{chatCompletion.Content.Last().Text}
""");
}
}
[Fact]
public async Task RunWithChatCompletionWithOpenAIChatMessage()
{
// Get the agent directly from OpenAIClient.
var agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
.GetChatClient(TestConfiguration.OpenAI.ChatModelId)
.CreateAIAgent(JokerInstructions, JokerName);
// Start a new thread for the agent conversation.
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}");
// Use the OpenAI.Chat message types directly
var chatMessage = new UserChatMessage(input);
var chatCompletion = await agent.RunAsync(chatMessage, thread);
Console.WriteLine(
$"""
Assistant:
{chatCompletion.Content.Last().Text}
""");
}
}
}