Files
agent-framework/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIClient.cs
T
westeyandGitHub ef16774878 .NET: Switch minimal console and a few samples to use latest patterns (#348)
* Switch minimal console and a few samples to use latest patterns

* Use interpolation for sample output.
2025-08-06 10:01:03 +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()
{
// Get the agent directly from OpenAIClient.
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}
""");
}
}
}