-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathProgram.cs
More file actions
118 lines (94 loc) · 4.88 KB
/
Program.cs
File metadata and controls
118 lines (94 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using MAF_BackgroundResponses_01_Simple;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI;
using System.Text;
// Simple demo that shows how to use Agent Framework's Background Responses feature.
// Background responses allow the agent to continue generating responses in the background
// after you receive partial streaming updates. You can interrupt, capture the continuation
// token, and later resume the generation from that token to get the rest of the response.
// See: https://learn.microsoft.com/en-us/agent-framework/user-guide/agents/agent-background-responses
await BackgroundResponsesDemo.RunAsync();
internal static class BackgroundResponsesDemo
{
public static async Task RunAsync()
{
// get chat response client
var responseClient = ResponseClientProvider.GetResponseClient();
// create a simple agent with basic instructions
var agent = responseClient.AsAIAgent(
name: "agent",
instructions: "You are a helpful assistant");
// Enable background responses so the agent can keep generating after an interruption.
var options = new AgentRunOptions
{
AllowBackgroundResponses = true
};
//1) Create a thread for the conversation
var thread = await agent.CreateSessionAsync();
//2) The question we'll ask
var question = "Write a 2 lines story about .NET Conf";
StreamConsoleHelper.PrintHeader("Background Responses — streaming with interruption and continuation");
StreamConsoleHelper.PrintLabeled("Question", question);
// Streaming phase: start streaming and simulate an interruption when we receive some text
StreamConsoleHelper.PrintSection("Start streaming (background responses enabled)");
AgentResponseUpdate? latestReceivedUpdate = null;
await foreach (var update in agent.RunStreamingAsync(question, thread, options))
{
// Convert continuation token to string for display and storage
var tokenString = update.ContinuationToken?.ToString();
// Print each streaming update in a clean, easy-to-read format
StreamConsoleHelper.PrintUpdate(update.Text, tokenString);
latestReceivedUpdate = update;
// Simulate an interruption if we have received any non-empty text
if (!string.IsNullOrWhiteSpace(update.Text))
{
StreamConsoleHelper.PrintInfo(">> interruption simulated — capturing continuation token and stopping streaming");
break;
}
}
StreamConsoleHelper.PrintSection("Start simple questions");
question = "solve 2 + 2 ";
StreamConsoleHelper.PrintLabeled("Question 1", question);
StreamConsoleHelper.StartAccumulatedStream();
await foreach (var update in agent.RunStreamingAsync(question, thread, options))
{
StreamConsoleHelper.AccumulateAndPrint(update.Text);
}
StreamConsoleHelper.FlushAccumulated();
question = "Tell me the capital of Italy";
StreamConsoleHelper.PrintLabeled("Question 2", question);
StreamConsoleHelper.StartAccumulatedStream();
await foreach (var update in agent.RunStreamingAsync(question, thread, options))
{
StreamConsoleHelper.AccumulateAndPrint(update.Text);
}
StreamConsoleHelper.FlushAccumulated();
StreamConsoleHelper.PrintSection("End simple questions");
Console.WriteLine();
// Continuation phase: resume from the captured continuation token to receive the rest
StreamConsoleHelper.PrintSection("Start continuation (resume using continuation token)");
if (latestReceivedUpdate?.ContinuationToken is null)
{
StreamConsoleHelper.PrintError("No continuation token available to resume the response. Aborting continuation.");
}
else
{
options.ContinuationToken = latestReceivedUpdate!.ContinuationToken;
// Prepare accumulator in StreamConsoleHelper to join token fragments into sentences
StreamConsoleHelper.StartAccumulatedStream();
// When resuming we pass the thread and options; the agent will continue from the token
await foreach (var update in agent.RunStreamingAsync(thread, options))
{
var tokenString = update.ContinuationToken?.ToString();
// Accumulate tokens and print flushed sentences for more readable output
StreamConsoleHelper.AccumulateAndPrint(update.Text, tokenString);
}
// Flush any remaining buffered fragments so the last partial sentence is printed
StreamConsoleHelper.FlushAccumulated();
}
Console.WriteLine();
StreamConsoleHelper.PrintFooter("End of demo — press any key to exit");
Console.ReadKey();
}
}