Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace GettingStarted.OpenAIResponseAgents;
/// <summary>
/// This example demonstrates how to manage conversation state during a model interaction using <see cref="OpenAIResponseAgent"/>.
/// 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.
/// </summary>
public class Step02_OpenAIResponseAgent_ConversationState(ITestOutputHelper output) : BaseResponsesAgentTest(output)
{
Expand Down Expand Up @@ -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<ChatMessageContent> 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)
Expand All @@ -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
Expand All @@ -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<StreamingChatMessageContent> responseItem in responseItems)
{
agentThread = responseItem.Thread;
Console.Write(responseItem.Message.Content);
}
}
}

[Fact]
public async Task StoreConversationStateAsync()
{
// Define the agent
OpenAIResponseAgent agent = new(this.Client)
Expand Down Expand Up @@ -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<StreamingChatMessageContent> 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <inheritdoc/>
protected override OpenAIResponseClient Client { get; }
Expand Down
Loading