forked from microsoft/kernel-memory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
196 lines (162 loc) · 6.06 KB
/
Program.cs
File metadata and controls
196 lines (162 loc) · 6.06 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) Microsoft. All rights reserved.
#pragma warning disable CS8602 // memory is initialized before usage
#pragma warning disable CS0162 // unreachable code is managed via boolean settings
using System.Security.Cryptography;
using System.Text;
using Microsoft.KernelMemory;
public static class Program
{
private static MemoryWebClient? s_memory;
private static readonly List<string> s_toDelete = [];
public static async Task Main()
{
s_memory = new MemoryWebClient("http://127.0.0.1:9001/");
// =======================
// === INGESTION =========
// =======================
await StoreIntent();
// Wait for remote ingestion pipelines to complete
foreach (var docId in s_toDelete)
{
while (!await s_memory.IsDocumentReadyAsync(documentId: docId))
{
Console.WriteLine("Waiting for memory ingestion to complete...");
await Task.Delay(TimeSpan.FromSeconds(2));
}
}
// =======================
// === Intent detection =========
// =======================
await AskForIntent("can I talk with someone?");
await AskForIntent("visa payment");
await AskForIntent("branch hours");
await AskForIntent("I want to reset access");
await AskForIntent("Check latest transactions");
// =======================
// === PURGE =============
// =======================
await DeleteMemories();
}
// =======================
// === INGESTION =========
// =======================
// Adding intent as a tag with samples to memory.
private static async Task StoreIntent()
{
var intentSamples = new Dictionary<string, List<string>>
{
{
"FAQ", new List<string>
{
"How do I check the balance on my bank Visa Debit Card?",
"Can I transfer money from my account to an account at another financial institution?",
"Can I join the bank online?",
"How do I find my tax statements?",
"Can I access my tax forms online?",
"Bank information",
"If I lose my debit or credit card, what should I do?",
"My card was lost/stolen, what should I do?",
"How do I activate my credit card?"
}
},
{
"account-balance", new List<string>
{
"Tell me my account balance.",
"I'd like to know my account balance"
}
},
{
"transfer-funds", new List<string>
{
"How to transfer fund to my checking account?",
"I need to pay my credit card",
"Can I pay my bill?",
}
},
{
"transaction-history", new List<string>
{
"Can I get transaction records?",
}
},
{
"change-PIN", new List<string>
{
"Update my password",
"How to change the PIN for my account?",
"Can you reset my PIN?"
}
},
{
"transfer-to-agent", new List<string>
{
"Connect me to an agent",
"Transfer to an agent",
"Can I talk to a human?"
}
},
};
foreach (KeyValuePair<string, List<string>> intentSample in intentSamples)
{
foreach (string intentRequest in intentSample.Value)
{
var docId = HashThis(intentRequest);
if (await s_memory.IsDocumentReadyAsync(docId))
{
continue;
}
Console.WriteLine($"Uploading intent {intentSample.Key} with question: {intentRequest}");
await s_memory.ImportTextAsync(intentRequest, tags: new TagCollection() { { "intent", intentSample.Key } }, documentId: docId);
Console.WriteLine($"- Document Id: {docId}");
s_toDelete.Add(docId);
}
}
Console.WriteLine("\n====================================\n");
}
// =======================
// === RETRIEVAL =========
// =======================
// Helper function to retrieve a tag value from a SearchResult
private static string? GetTagValue(SearchResult answer, string tagName, string? defaultValue = null)
{
if (answer.Results.Count == 0)
{
return defaultValue;
}
Citation citation = answer.Results[0];
if (citation == null)
{
return defaultValue;
}
if (citation.Partitions[0].Tags.ContainsKey(tagName))
{
return citation.Partitions[0].Tags[tagName][0];
}
return defaultValue;
}
private static async Task AskForIntent(string request)
{
Console.WriteLine($"Question: {request}");
// we ask for one chank of data with a minimum relevance of 0.75
SearchResult answer = await s_memory.SearchAsync(request, minRelevance: 0.75, limit: 1);
string? intent = GetTagValue(answer, "intent", "none");
Console.WriteLine($"Intent: {intent}");
Console.WriteLine("\n====================================\n");
}
private static string HashThis(string value)
{
return Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(value))).ToUpperInvariant();
}
// =======================
// === PURGE =============
// =======================
private static async Task DeleteMemories()
{
foreach (var docId in s_toDelete)
{
Console.WriteLine($"Deleting memories derived from {docId}");
await s_memory.DeleteDocumentAsync(docId);
}
}
}