From 0982aa9d6dddbeb0bba07c92b8b6406905658154 Mon Sep 17 00:00:00 2001 From: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com> Date: Wed, 20 Aug 2025 13:22:11 +0100 Subject: [PATCH] .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> --- .../Providers/AIAgent_With_OpenAIAssistant.cs | 10 +++---- .../Providers/AIAgent_With_OpenAIClient.cs | 2 +- .../AIAgent_With_OpenAIResponseClient.cs | 9 +----- .../ChatOptionsExtensions.cs | 28 +++++++++++++++++++ 4 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 dotnet/src/Microsoft.Extensions.AI.Agents.OpenAI/ChatOptionsExtensions.cs diff --git a/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIAssistant.cs b/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIAssistant.cs index 3dc69536f7..de658df825 100644 --- a/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIAssistant.cs +++ b/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIAssistant.cs @@ -26,12 +26,10 @@ public sealed class AIAgent_With_OpenAIAssistant(ITestOutputHelper output) : Age AIAgent agent = openAIClient .GetAssistantClient() .CreateAIAgent( - TestConfiguration.OpenAI.ChatModelId, - options: new() - { - Name = JokerName, - Instructions = JokerInstructions, - }); + model: TestConfiguration.OpenAI.ChatModelId, + name: JokerName, + instructions: JokerInstructions + ); // Start a new thread for the agent conversation. AgentThread thread = agent.GetNewThread(); diff --git a/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIClient.cs b/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIClient.cs index 86fd251701..dd01ed6382 100644 --- a/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIClient.cs +++ b/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIClient.cs @@ -18,7 +18,7 @@ public sealed class AIAgent_With_OpenAIClient(ITestOutputHelper output) : AgentS [Fact] public async Task RunWithChatCompletion() { - // Get the agent directly from OpenAIClient. + // Create the agent using the OpenAI ChatClient. AIAgent agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey) .GetChatClient(TestConfiguration.OpenAI.ChatModelId) .CreateAIAgent(JokerInstructions, JokerName); diff --git a/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIResponseClient.cs b/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIResponseClient.cs index f9d8ac3307..a8da098ef6 100644 --- a/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIResponseClient.cs +++ b/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIResponseClient.cs @@ -4,7 +4,6 @@ using Microsoft.Extensions.AI; using Microsoft.Extensions.AI.Agents; using Microsoft.Shared.Samples; using OpenAI; -using OpenAI.Responses; namespace Providers; @@ -60,13 +59,7 @@ public sealed class AIAgent_With_OpenAIResponseClient(ITestOutputHelper output) { Name = JokerName, Instructions = JokerInstructions, - ChatOptions = new ChatOptions - { - // We can use the RawRepresentationFactory to provide Response service specific - // options. Here we can indicate that we do not want the service to store the - // conversation in a service managed thread. - RawRepresentationFactory = (_) => new ResponseCreationOptions() { StoredOutputEnabled = false } - } + ChatOptions = new ChatOptions().WithResponseStoredOutputDisabled() }); // Start a new thread for the agent conversation based on the type. diff --git a/dotnet/src/Microsoft.Extensions.AI.Agents.OpenAI/ChatOptionsExtensions.cs b/dotnet/src/Microsoft.Extensions.AI.Agents.OpenAI/ChatOptionsExtensions.cs new file mode 100644 index 0000000000..f8052f9498 --- /dev/null +++ b/dotnet/src/Microsoft.Extensions.AI.Agents.OpenAI/ChatOptionsExtensions.cs @@ -0,0 +1,28 @@ +// Copyright (c) Microsoft. All rights reserved. +using Microsoft.Shared.Diagnostics; +using OpenAI.Responses; + +namespace Microsoft.Extensions.AI.Agents; + +/// +/// Extension methods for +/// +public static class ChatOptionsExtensions +{ + /// + /// Disables the storage of response output in the chat options. + /// + /// Instance of + /// + public static ChatOptions WithResponseStoredOutputDisabled(this ChatOptions options) + { + Throw.IfNull(options); + + // We can use the RawRepresentationFactory to provide Response service specific + // options. Here we can indicate that we do not want the service to store the + // conversation in a service managed thread. + options.RawRepresentationFactory = (_) => new ResponseCreationOptions() { StoredOutputEnabled = false }; + + return options; + } +}