forked from microsoft/Generative-AI-for-beginners-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
72 lines (54 loc) · 3.2 KB
/
Copy pathProgram.cs
File metadata and controls
72 lines (54 loc) · 3.2 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
using MAF_BackgroundResponses_01_Simple;
using Microsoft.Extensions.AI;
using OpenAI;
using System.Text.Json;
using System.Threading.Tasks;
// Persisting Conversation — Simple Sample
//1) Demonstrates creating an AgentThread, running a prompt against it, and persisting the thread state.
//2) Shows how to reload the persisted thread and continue the conversation with context preserved.
//3) Uses the same agent/thread model to illustrate stateful interactions across runs.
// More information: https://learn.microsoft.com/en-us/agent-framework/tutorials/agents/persisted-conversation?pivots=programming-language-csharp
string SavedThreadFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "agent_thread.json");
// create a simple agent
var client = ChatClientProvider.GetChatClient();
var agent = client.AsAIAgent(
name: "agent",
instructions: "You are a helpful assistant");
// ------------------------------------------------------------
// Step1, create new thread, ask a question and persist the thread
var thread = await agent.CreateSessionAsync();
var question = "My name is Bruno";
StreamConsoleHelper.PrintHeader("Step1, create new thread, ask a question and persist the thread", false);
StreamConsoleHelper.PrintSection("Start answering your question");
StreamConsoleHelper.PrintLabeled("Question", question);
var response = await agent.RunAsync(question, thread);
StreamConsoleHelper.PrintAccumulatedLine(response.Text);
// Serialize thread state and write to file
var threadRaw = (await agent.SerializeSessionAsync(thread, JsonSerializerOptions.Web)).GetRawText();
await File.WriteAllTextAsync(SavedThreadFilePath, threadRaw);
StreamConsoleHelper.PrintLabeled("Saved thread to", SavedThreadFilePath);
// ------------------------------------------------------------
// Step2, create new thread and ask the question "what is my name"
StreamConsoleHelper.PrintHeader("Step2, create new thread and ask the question \"what is my name\"", false);
thread = await agent.CreateSessionAsync();
question = "What is my name?";
StreamConsoleHelper.PrintSection("Start answering your question");
StreamConsoleHelper.PrintLabeled("Question", question);
response = await agent.RunAsync(question, thread);
StreamConsoleHelper.PrintAccumulatedLine(response.Text);
// ------------------------------------------------------------
// Step3, load persisted thread and ask the question "what is my name"
StreamConsoleHelper.PrintHeader("Step3, load persisted thread and ask the question \"what is my name\"", false);
var loadedJson = await File.ReadAllTextAsync(SavedThreadFilePath);
JsonElement reloaded = JsonSerializer.Deserialize<JsonElement>(loadedJson, JsonSerializerOptions.Web);
// Rehydrate the thread for this agent
thread = await agent.DeserializeSessionAsync(reloaded, JsonSerializerOptions.Web);
question = "What is my name?";
StreamConsoleHelper.PrintSection("Start answering your question");
StreamConsoleHelper.PrintLabeled("Question", question);
response = await agent.RunAsync(question, thread);
StreamConsoleHelper.PrintAccumulatedLine(response.Text);
// ------------------------------------------------------------
// Step4, end
StreamConsoleHelper.PrintFooter("End of the example, press any key to exit");
Console.ReadKey();