Files
SergeyMenshykh a5db641ce3 .NET: Background responses sample (#1831)
* add samples that demonstrate background responses with function calling and agent state persistence

* update readme

* update sample description

* rename sample project

* remove unnecessary namespace and ysed task delay instead thread sleep generated by ai

* change names

* Update dotnet/samples/GettingStarted/Agents/Agent_Step20_BackgroundResponsesWithToolsAndPersistence/AgentStateStore.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* simplify sample and address pr review comments

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-31 17:13:43 +00:00

71 lines
2.2 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
// This sample shows how to use background responses with ChatClientAgent and Azure OpenAI Responses.
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using OpenAI;
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
AIAgent agent = new AzureOpenAIClient(
new Uri(endpoint),
new AzureCliCredential())
.GetOpenAIResponseClient(deploymentName)
.CreateAIAgent();
// Enable background responses (only supported by OpenAI Responses at this time).
AgentRunOptions options = new() { AllowBackgroundResponses = true };
AgentThread thread = agent.GetNewThread();
// Start the initial run.
AgentRunResponse response = await agent.RunAsync("Write a very long novel about otters in space.", thread, options);
// Poll until the response is complete.
while (response.ContinuationToken is { } token)
{
// Wait before polling again.
await Task.Delay(TimeSpan.FromSeconds(2));
// Continue with the token.
options.ContinuationToken = token;
response = await agent.RunAsync(thread, options);
}
// Display the result.
Console.WriteLine(response.Text);
// Reset options and thread for streaming.
options = new() { AllowBackgroundResponses = true };
thread = agent.GetNewThread();
AgentRunResponseUpdate? lastReceivedUpdate = null;
// Start streaming.
await foreach (AgentRunResponseUpdate update in agent.RunStreamingAsync("Write a very long novel about otters in space.", thread, options))
{
// Output each update.
Console.Write(update.Text);
// Track last update.
lastReceivedUpdate = update;
// Simulate connection loss after first piece of content received.
if (update.Text.Length > 0)
{
break;
}
}
// Resume from interruption point.
options.ContinuationToken = lastReceivedUpdate?.ContinuationToken;
await foreach (AgentRunResponseUpdate update in agent.RunStreamingAsync(thread, options))
{
// Output each update.
Console.Write(update.Text);
}