.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
This commit is contained in:
Roger Barreto
2025-06-19 19:06:08 +01:00
committed by GitHub
Unverified
parent 47dcad6b49
commit 2cd77596c3
3 changed files with 97 additions and 5 deletions
+22 -3
View File
@@ -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();
}
@@ -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;
/// <summary>
/// End-to-end sample showing how to use <see cref="ChatClientAgent"/> with OpenAI Chat Completion.
/// </summary>
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);
}
}
}
@@ -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.