.NET: Update Migration Samples for SK OpenAIResponsesAgent Step 1 & 2 (#1010)

* Addressing a SK bug where threads can't be reused + threads should be capture after the request, not created before

* Using storedenabled to align with AF default behavior

* Address copilot comment

* Address Azure + OpenAI remaining sample improvements
This commit is contained in:
Roger Barreto
2025-09-30 16:49:16 +01:00
committed by GitHub
Unverified
parent 4866936041
commit ff8b97cc72
4 changed files with 23 additions and 17 deletions
@@ -28,20 +28,21 @@ async Task SKAgentAsync()
{
Name = "Joker",
Instructions = "You are good at telling jokes.",
StoreEnabled = true
};
var agentOptions = new OpenAIResponseAgentInvokeOptions() { ResponseCreationOptions = new() { MaxOutputTokenCount = 1000 } };
Microsoft.SemanticKernel.Agents.AgentThread? thread = new OpenAIResponseAgentThread(responseClient);
Microsoft.SemanticKernel.Agents.AgentThread? thread = null;
await foreach (var item in agent.InvokeAsync(userInput, thread, agentOptions))
{
thread = item.Thread;
Console.WriteLine(item.Message);
}
Console.WriteLine("---");
await foreach (var item in agent.InvokeStreamingAsync(userInput, thread, agentOptions))
{
// Thread need to be updated for subsequent calls
thread = item.Thread;
Console.Write(item.Message);
}
@@ -53,8 +53,9 @@ async Task SKAgentAsync()
.GetOpenAIResponseClient(deploymentName);
OpenAIResponseAgent agent = new(responseClient)
{
Name = "Joker",
Instructions = "You are good at telling jokes.",
Name = "Thinker",
Instructions = "You are good at thinking hard before answering.",
StoreEnabled = true
};
var agentOptions = new OpenAIResponseAgentInvokeOptions()
@@ -70,13 +71,12 @@ async Task SKAgentAsync()
}
};
Microsoft.SemanticKernel.Agents.AgentThread? thread = new OpenAIResponseAgentThread(responseClient);
Microsoft.SemanticKernel.Agents.AgentThread? thread = null;
await foreach (var item in agent.InvokeAsync(userInput, thread, agentOptions))
{
thread = item.Thread;
foreach (var content in item.Message.Items)
{
// Currently SK Responses Agent doesn't distinguish thinking from non-thinking content in non-streaming mode.
// SK Bugfix WIP: https://github.com/microsoft/semantic-kernel/issues/13046
if (content is ReasoningContent thinking)
{
Console.Write($"Thinking: \n{thinking}\n---\n");
@@ -97,7 +97,8 @@ async Task SKAgentAsync()
foreach (var content in item.Message.Items)
{
// Currently SK Agent doesn't output thinking in streaming mode.
// SK Bugfix WIP: https://github.com/microsoft/semantic-kernel/issues/13046
// SK Issue: https://github.com/microsoft/semantic-kernel/issues/13046
// OpenAI SDK Issue: https://github.com/openai/openai-dotnet/issues/643
if (content is StreamingReasoningContent thinking)
{
Console.WriteLine($"Thinking: [{thinking}]");
@@ -118,7 +119,7 @@ async Task AFAgentAsync()
var agent = new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential())
.GetOpenAIResponseClient(deploymentName)
.CreateAIAgent(name: "Joker", instructions: "You are good at telling jokes.");
.CreateAIAgent(name: "Thinker", instructions: "You are good at thinking hard before answering.");
var thread = agent.GetNewThread();
var agentOptions = new ChatClientAgentRunOptions(new()
@@ -25,14 +25,16 @@ async Task SKAgentAsync()
{
Name = "Joker",
Instructions = "You are good at telling jokes.",
StoreEnabled = true
};
var agentOptions = new OpenAIResponseAgentInvokeOptions() { ResponseCreationOptions = new() { MaxOutputTokenCount = 1000 } };
Microsoft.SemanticKernel.Agents.AgentThread? thread = new OpenAIResponseAgentThread(responseClient);
Microsoft.SemanticKernel.Agents.AgentThread? thread = null;
await foreach (var item in agent.InvokeAsync(userInput, thread, agentOptions))
{
Console.WriteLine(item.Message);
thread = item.Thread;
}
Console.WriteLine("---");
@@ -50,8 +50,9 @@ async Task SKAgentAsync()
var responseClient = new OpenAIClient(apiKey).GetOpenAIResponseClient(modelId);
OpenAIResponseAgent agent = new(responseClient)
{
Name = "Joker",
Instructions = "You are good at telling jokes.",
Name = "Thinker",
Instructions = "You are good at thinking hard before answering.",
StoreEnabled = true
};
var agentOptions = new OpenAIResponseAgentInvokeOptions()
@@ -67,13 +68,12 @@ async Task SKAgentAsync()
}
};
Microsoft.SemanticKernel.Agents.AgentThread? thread = new OpenAIResponseAgentThread(responseClient);
Microsoft.SemanticKernel.Agents.AgentThread? thread = null;
await foreach (var item in agent.InvokeAsync(userInput, thread, agentOptions))
{
thread = item.Thread;
foreach (var content in item.Message.Items)
{
// Currently SK Responses Agent doesn't distinguish thinking from non-thinking content in non-streaming mode.
// SK Bugfix WIP: https://github.com/microsoft/semantic-kernel/issues/13046
if (content is ReasoningContent thinking)
{
Console.Write($"Thinking: \n{thinking}\n---\n");
@@ -88,13 +88,15 @@ async Task SKAgentAsync()
Console.WriteLine("---");
var userMessage = new ChatMessageContent(AuthorRole.User, userInput);
thread = null;
await foreach (var item in agent.InvokeStreamingAsync(userMessage, thread, agentOptions))
{
thread = item.Thread;
foreach (var content in item.Message.Items)
{
// Currently SK Agent doesn't output thinking in streaming mode.
// SK Bugfix WIP: https://github.com/microsoft/semantic-kernel/issues/13046
// SK Issue: https://github.com/microsoft/semantic-kernel/issues/13046
// OpenAI SDK Issue: https://github.com/openai/openai-dotnet/issues/643
if (content is StreamingReasoningContent thinking)
{
Console.WriteLine($"Thinking: [{thinking}]");
@@ -114,7 +116,7 @@ async Task AFAgentAsync()
Console.WriteLine("\n=== AF Agent ===\n");
var agent = new OpenAIClient(apiKey).GetOpenAIResponseClient(modelId)
.CreateAIAgent(name: "Joker", instructions: "You are good at telling jokes.");
.CreateAIAgent(name: "Thinker", instructions: "You are at thinking hard before answering.");
var thread = agent.GetNewThread();
var agentOptions = new ChatClientAgentRunOptions(new()