From 2cd77596c3e8de58e4268df58eba74e752e824c9 Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com> Date: Thu, 19 Jun 2025 19:06:08 +0100 Subject: [PATCH] .Net: Add OpenAI Responses Agent Samples (#83) * Improved Merge logic * Add modularization for Model Samples and Azure OpenAI * Address PR feedback * Warning fix * Address PR comments * Adding OpenAI Response Samples * Clean up * Revert to provider specific chat options * Fix warnings * Revert slnx changes --- dotnet/samples/GettingStarted/AgentSample.cs | 25 +++++++- ...Agent_With_OpenAIResponseChatCompletion.cs | 60 +++++++++++++++++++ .../GettingStarted/Steps/Step01_Running.cs | 17 +++++- 3 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 dotnet/samples/GettingStarted/Providers/ChatClientAgent_With_OpenAIResponseChatCompletion.cs diff --git a/dotnet/samples/GettingStarted/AgentSample.cs b/dotnet/samples/GettingStarted/AgentSample.cs index 12cf9fa7af..62100ce344 100644 --- a/dotnet/samples/GettingStarted/AgentSample.cs +++ b/dotnet/samples/GettingStarted/AgentSample.cs @@ -6,6 +6,7 @@ using Azure.Identity; using Microsoft.Extensions.AI; using Microsoft.Shared.Samples; using OpenAI; +using OpenAI.Responses; namespace GettingStarted; @@ -18,17 +19,30 @@ public class AgentSample(ITestOutputHelper output) : BaseSample(output) { OpenAI, AzureOpenAI, + OpenAIResponses, + OpenAIResponses_InMemoryMessage, + OpenAIResponses_ConversationId } protected IChatClient GetChatClient(ChatClientProviders provider) - { - return provider switch + => provider switch { ChatClientProviders.OpenAI => GetOpenAIChatClient(), ChatClientProviders.AzureOpenAI => GetAzureOpenAIChatClient(), + ChatClientProviders.OpenAIResponses or + ChatClientProviders.OpenAIResponses_InMemoryMessage or + ChatClientProviders.OpenAIResponses_ConversationId + => GetOpenAIResponsesClient(), _ => throw new NotSupportedException($"Provider {provider} is not supported.") }; - } + + protected ChatOptions? GetChatOptions(ChatClientProviders? provider) + => provider switch + { + ChatClientProviders.OpenAIResponses_InMemoryMessage => new() { RawRepresentationFactory = static (_) => new ResponseCreationOptions() { StoredOutputEnabled = false } }, + ChatClientProviders.OpenAIResponses_ConversationId => new() { RawRepresentationFactory = static (_) => new ResponseCreationOptions() { StoredOutputEnabled = true } }, + _ => null + }; private IChatClient GetOpenAIChatClient() => new OpenAIClient(TestConfiguration.OpenAI.ApiKey) @@ -42,4 +56,9 @@ public class AgentSample(ITestOutputHelper output) : BaseSample(output) : new AzureOpenAIClient(TestConfiguration.AzureOpenAI.Endpoint, new ApiKeyCredential(TestConfiguration.AzureOpenAI.ApiKey))) .GetChatClient(TestConfiguration.AzureOpenAI.DeploymentName) .AsIChatClient(); + + private IChatClient GetOpenAIResponsesClient() + => new OpenAIClient(TestConfiguration.OpenAI.ApiKey) + .GetOpenAIResponseClient(TestConfiguration.OpenAI.ChatModelId) + .AsIChatClient(); } diff --git a/dotnet/samples/GettingStarted/Providers/ChatClientAgent_With_OpenAIResponseChatCompletion.cs b/dotnet/samples/GettingStarted/Providers/ChatClientAgent_With_OpenAIResponseChatCompletion.cs new file mode 100644 index 0000000000..9d7f323356 --- /dev/null +++ b/dotnet/samples/GettingStarted/Providers/ChatClientAgent_With_OpenAIResponseChatCompletion.cs @@ -0,0 +1,60 @@ +// Copyright (c) Microsoft. All rights reserved. + +using Microsoft.Agents; +using Microsoft.Extensions.AI; +using Microsoft.Shared.Samples; +using OpenAI; +using OpenAI.Responses; + +#pragma warning disable CS8524 // The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. + +namespace Providers; + +/// +/// End-to-end sample showing how to use with OpenAI Chat Completion. +/// +public sealed class ChatClientAgent_With_OpenAIResponsesChatCompletion(ITestOutputHelper output) : AgentSample(output) +{ + private const string JokerName = "Joker"; + private const string JokerInstructions = "You are good at telling jokes."; + + [Theory] + [InlineData(false)] // This will use in-memory messages to store the thread state. + [InlineData(true)] // This will use the conversation id to reference the thread state on the server side. + public async Task RunWithChatCompletion(bool useConversationIdThread) + { + // Get the chat client to use for the agent. + using var chatClient = new OpenAIClient(TestConfiguration.OpenAI.ApiKey) + .GetOpenAIResponseClient(TestConfiguration.OpenAI.ChatModelId) + .AsIChatClient(); + + // Define the agent + ChatClientAgent agent = + new(chatClient, new() + { + Name = JokerName, + Instructions = JokerInstructions, + ChatOptions = new ChatOptions + { + RawRepresentationFactory = (_) => new ResponseCreationOptions() { StoredOutputEnabled = useConversationIdThread } + } + }); + + // 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) + { + this.WriteUserMessage(input); + + var response = await agent.RunAsync(input, thread); + + this.WriteResponseOutput(response); + } + } +} diff --git a/dotnet/samples/GettingStarted/Steps/Step01_Running.cs b/dotnet/samples/GettingStarted/Steps/Step01_Running.cs index 9f5fca31db..baf4c8761d 100644 --- a/dotnet/samples/GettingStarted/Steps/Step01_Running.cs +++ b/dotnet/samples/GettingStarted/Steps/Step01_Running.cs @@ -25,6 +25,7 @@ public sealed class Step01_Running(ITestOutputHelper output) : AgentSample(outpu [Theory] [InlineData(ChatClientProviders.OpenAI)] [InlineData(ChatClientProviders.AzureOpenAI)] + [InlineData(ChatClientProviders.OpenAIResponses)] public async Task RunWithoutThread(ChatClientProviders provider) { // Get the chat client to use for the agent. @@ -59,17 +60,23 @@ public sealed class Step01_Running(ITestOutputHelper output) : AgentSample(outpu [Theory] [InlineData(ChatClientProviders.OpenAI)] [InlineData(ChatClientProviders.AzureOpenAI)] - public async Task RunWithConversationThread(ChatClientProviders provider) + [InlineData(ChatClientProviders.OpenAIResponses_InMemoryMessage)] + [InlineData(ChatClientProviders.OpenAIResponses_ConversationId)] + public async Task RunWithThread(ChatClientProviders provider) { // Get the chat client to use for the agent. using var chatClient = base.GetChatClient(provider); + // Get chat options based on the store type, if needed. + var chatOptions = base.GetChatOptions(provider); + // Define the agent ChatClientAgent agent = new(chatClient, new() { Name = JokerName, Instructions = JokerInstructions, + ChatOptions = chatOptions, }); // Start a new thread for the agent conversation. @@ -97,17 +104,23 @@ public sealed class Step01_Running(ITestOutputHelper output) : AgentSample(outpu [Theory] [InlineData(ChatClientProviders.OpenAI)] [InlineData(ChatClientProviders.AzureOpenAI)] - public async Task StreamingRunWithConversationThread(ChatClientProviders provider) + [InlineData(ChatClientProviders.OpenAIResponses_InMemoryMessage)] + [InlineData(ChatClientProviders.OpenAIResponses_ConversationId)] + public async Task RunStreamingWithThread(ChatClientProviders provider) { // Get the chat client to use for the agent. using var chatClient = base.GetChatClient(provider); + // Get chat options based on the store type, if needed. + var chatOptions = base.GetChatOptions(provider); + // Define the agent ChatClientAgent agent = new(chatClient, new() { Name = ParrotName, Instructions = ParrotInstructions, + ChatOptions = chatOptions, }); // Start a new thread for the agent conversation.