mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
1bf520a7c2
* add support for background responses * Update dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponseUpdate.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix broken link * fix xml comments and background responses properties override funcitonity * change ai model provider * use Run{Streaming}Async overloads that don't require messages * stop using m: prefix in cref attribute of <see/> element. * reject input messages provided with continuation token + don't extract messages from message store and context provide if continuation token is provided * use agent thread for background-responses sample * require agent thread for background responses * Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> * Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> * remove CA1200 * Update dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunOptions.cs Co-authored-by: westey <164392973+westey-m@users.noreply.github.com> * Update dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponse.cs Co-authored-by: westey <164392973+westey-m@users.noreply.github.com> * Update dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponse.cs Co-authored-by: westey <164392973+westey-m@users.noreply.github.com> * address pr review comments * Update dotnet/samples/GettingStarted/Agents/Agent_Step17_BackgroundResponses/Program.cs Co-authored-by: westey <164392973+westey-m@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> Co-authored-by: westey <164392973+westey-m@users.noreply.github.com>
71 lines
2.2 KiB
C#
71 lines
2.2 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
// This sample shows how to use background responses with ChatClientAgent and 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(instructions: "You are good at telling jokes.", name: "Joker");
|
|
|
|
// 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("Tell me a joke about a pirate.", 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("Tell me a joke about a pirate.", 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);
|
|
}
|