From 6ce8ae178e581477e39f06772791c100f2e0db71 Mon Sep 17 00:00:00 2001 From: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com> Date: Wed, 23 Jul 2025 20:16:48 +0100 Subject: [PATCH] Update Response Agent conversation state examples --- ...2_OpenAIResponseAgent_ConversationState.cs | 117 +++++++++++++++++- .../AgentUtilities/BaseResponsesAgentTest.cs | 2 +- 2 files changed, 115 insertions(+), 4 deletions(-) diff --git a/dotnet/samples/GettingStartedWithAgents/OpenAIResponse/Step02_OpenAIResponseAgent_ConversationState.cs b/dotnet/samples/GettingStartedWithAgents/OpenAIResponse/Step02_OpenAIResponseAgent_ConversationState.cs index e3c998af0185..3f5c9a31d7a1 100644 --- a/dotnet/samples/GettingStartedWithAgents/OpenAIResponse/Step02_OpenAIResponseAgent_ConversationState.cs +++ b/dotnet/samples/GettingStartedWithAgents/OpenAIResponse/Step02_OpenAIResponseAgent_ConversationState.cs @@ -10,6 +10,7 @@ namespace GettingStarted.OpenAIResponseAgents; /// /// This example demonstrates how to manage conversation state during a model interaction using . /// OpenAI provides a few ways to manage conversation state, which is important for preserving information across multiple messages or turns in a conversation. +/// See: https://platform.openai.com/docs/guides/conversation-state?api-mode=responses for more information. /// public class Step02_OpenAIResponseAgent_ConversationState(ITestOutputHelper output) : BaseResponsesAgentTest(output) { @@ -42,7 +43,36 @@ public async Task ManuallyConstructPastConversationAsync() } [Fact] - public async Task ManuallyManageConversationStateWithResponsesChatCompletionApiAsync() + public async Task ManuallyConstructPastConversationStreamingAsync() + { + // Define the agent + OpenAIResponseAgent agent = new(this.Client) + { + StoreEnabled = false, + }; + + ICollection messages = + [ + new ChatMessageContent(AuthorRole.User, "knock knock."), + new ChatMessageContent(AuthorRole.Assistant, "Who's there?"), + new ChatMessageContent(AuthorRole.User, "Orange.") + ]; + foreach (ChatMessageContent message in messages) + { + WriteAgentChatMessage(message); + } + + // Invoke the agent and output the response + var responseItems = agent.InvokeStreamingAsync(messages); + Console.Write("\n# assistant: "); + await foreach (StreamingChatMessageContent responseItem in responseItems) + { + Console.Write(responseItem.Content); + } + } + + [Fact] + public async Task ManageConversationStateWithResponseIdAsync() { // Define the agent OpenAIResponseAgent agent = new(this.Client) @@ -53,7 +83,7 @@ public async Task ManuallyManageConversationStateWithResponsesChatCompletionApiA string[] messages = [ "Tell me a joke?", - "Tell me another?", + "Explain why this is funny?", ]; // Invoke the agent and output the response @@ -73,7 +103,39 @@ public async Task ManuallyManageConversationStateWithResponsesChatCompletionApiA } [Fact] - public async Task ManageConversationStateWithResponseApiAsync() + public async Task ManageConversationStateWithResponseIdStreamingAsync() + { + // Define the agent + OpenAIResponseAgent agent = new(this.Client) + { + StoreEnabled = false, + }; + + string[] messages = + [ + "Tell me a joke?", + "Explain why this is funny?", + ]; + + // Invoke the agent and output the response + AgentThread? agentThread = null; + foreach (string message in messages) + { + var userMessage = new ChatMessageContent(AuthorRole.User, message); + WriteAgentChatMessage(userMessage); + + Console.Write("\n# assistant: "); + var responseItems = agent.InvokeStreamingAsync(userMessage, agentThread); + await foreach (AgentResponseItem responseItem in responseItems) + { + agentThread = responseItem.Thread; + Console.Write(responseItem.Message.Content); + } + } + } + + [Fact] + public async Task StoreConversationStateAsync() { // Define the agent OpenAIResponseAgent agent = new(this.Client) @@ -119,4 +181,53 @@ public async Task ManageConversationStateWithResponseApiAsync() await agentThread.DeleteAsync(); } } + + [Fact] + public async Task StoreConversationStateWithStreamingAsync() + { + // Define the agent + OpenAIResponseAgent agent = new(this.Client) + { + StoreEnabled = true, + }; + + string[] messages = + [ + "Tell me a joke?", + "Explain why this is funny.", + ]; + + // Invoke the agent and output the response + AgentThread? agentThread = null; + foreach (string message in messages) + { + var userMessage = new ChatMessageContent(AuthorRole.User, message); + WriteAgentChatMessage(userMessage); + + Console.Write("\n# assistant: "); + var responseItems = agent.InvokeStreamingAsync(userMessage, agentThread); + await foreach (AgentResponseItem responseItem in responseItems) + { + agentThread = responseItem.Thread; + Console.Write(responseItem.Message.Content); + } + } + + // Display the contents in the latest thread + if (agentThread is not null) + { + this.Output.WriteLine("\n\nResponse Thread Messages\n"); + var responseAgentThread = agentThread as OpenAIResponseAgentThread; + var threadMessages = responseAgentThread?.GetMessagesAsync(); + if (threadMessages is not null) + { + await foreach (var threadMessage in threadMessages) + { + WriteAgentChatMessage(threadMessage); + } + } + + await agentThread.DeleteAsync(); + } + } } diff --git a/dotnet/src/InternalUtilities/samples/AgentUtilities/BaseResponsesAgentTest.cs b/dotnet/src/InternalUtilities/samples/AgentUtilities/BaseResponsesAgentTest.cs index b7d56e732c62..611db3ca924d 100644 --- a/dotnet/src/InternalUtilities/samples/AgentUtilities/BaseResponsesAgentTest.cs +++ b/dotnet/src/InternalUtilities/samples/AgentUtilities/BaseResponsesAgentTest.cs @@ -36,7 +36,7 @@ protected BaseResponsesAgentTest(ITestOutputHelper output, string? model = null) protected VectorStoreClient VectorStoreClient { get; set; } - protected bool EnableLogging { get; set; } = true; + protected bool EnableLogging { get; set; } = false; /// protected override OpenAIResponseClient Client { get; }