Skip to content

Commit 0239be6

Browse files
authored
Update examples (#474)
1 parent bde65d7 commit 0239be6

File tree

3 files changed

+73
-26
lines changed

3 files changed

+73
-26
lines changed

examples/001-dotnet-WebClient/Program.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,17 @@ private static async Task StoreText()
8686
"in a system's rest frame, where the two quantities differ only by a multiplicative " +
8787
"constant and the units of measurement. The principle is described by the physicist " +
8888
"Albert Einstein's formula: E = m*c^2");
89+
Console.WriteLine($"- Document Id: {docId}");
8990
s_toDelete.Add(docId);
9091
}
9192

9293
// Simple file upload, with document ID
9394
private static async Task StoreFile()
9495
{
9596
Console.WriteLine("Uploading article file about Carbon");
96-
await s_memory.ImportDocumentAsync("file1-Wikipedia-Carbon.txt", documentId: "doc001");
97-
s_toDelete.Add("doc001");
97+
var docId = await s_memory.ImportDocumentAsync("file1-Wikipedia-Carbon.txt", documentId: "doc001");
98+
s_toDelete.Add(docId);
99+
Console.WriteLine($"- Document Id: {docId}");
98100
}
99101

100102
// Extract memory from images (OCR required)
@@ -103,8 +105,9 @@ private static async Task StoreImage()
103105
if (!ImageSupportDemoEnabled) { return; }
104106

105107
Console.WriteLine("Uploading Image file with a news about a conference sponsored by Microsoft");
106-
await s_memory.ImportDocumentAsync(new Document("img001").AddFiles(new[] { "file6-ANWC-image.jpg" }));
107-
s_toDelete.Add("img001");
108+
var docId = await s_memory.ImportDocumentAsync(new Document("img001").AddFiles(new[] { "file6-ANWC-image.jpg" }));
109+
s_toDelete.Add(docId);
110+
Console.WriteLine($"- Document Id: {docId}");
108111
}
109112

110113
// Uploading multiple files and adding a user tag, checking if the document already exists
@@ -113,9 +116,11 @@ private static async Task StoreMultipleFiles()
113116
if (!await s_memory.IsDocumentReadyAsync(documentId: "doc002"))
114117
{
115118
Console.WriteLine("Uploading a text file, a Word doc, and a PDF about Kernel Memory");
116-
await s_memory.ImportDocumentAsync(new Document("doc002")
119+
var docId = await s_memory.ImportDocumentAsync(new Document("doc002")
117120
.AddFiles(new[] { "file2-Wikipedia-Moon.txt", "file3-lorem-ipsum.docx", "file4-KM-Readme.pdf" })
118121
.AddTag("user", "Blake"));
122+
s_toDelete.Add(docId);
123+
Console.WriteLine($"- Document Id: {docId}");
119124
}
120125
else
121126
{
@@ -131,13 +136,15 @@ private static async Task StoreFileWithMultipleTags()
131136
if (!await s_memory.IsDocumentReadyAsync(documentId: "doc003"))
132137
{
133138
Console.WriteLine("Uploading a PDF with a news about NASA and Orion");
134-
await s_memory.ImportDocumentAsync(new Document("doc003")
139+
var docId = await s_memory.ImportDocumentAsync(new Document("doc003")
135140
.AddFile("file5-NASA-news.pdf")
136141
.AddTag("user", "Taylor")
137142
.AddTag("collection", "meetings")
138143
.AddTag("collection", "NASA")
139144
.AddTag("collection", "space")
140145
.AddTag("type", "news"));
146+
s_toDelete.Add(docId);
147+
Console.WriteLine($"- Document Id: {docId}");
141148
}
142149
else
143150
{
@@ -153,7 +160,9 @@ private static async Task StoreWebPage()
153160
if (!await s_memory.IsDocumentReadyAsync("webPage1"))
154161
{
155162
Console.WriteLine("Uploading https://raw.githubusercontent.com/microsoft/kernel-memory/main/README.md");
156-
await s_memory.ImportWebPageAsync("https://raw.githubusercontent.com/microsoft/kernel-memory/main/README.md", documentId: "webPage1");
163+
var docId = await s_memory.ImportWebPageAsync("https://raw.githubusercontent.com/microsoft/kernel-memory/main/README.md", documentId: "webPage1");
164+
s_toDelete.Add(docId);
165+
Console.WriteLine($"- Document Id: {docId}");
157166
}
158167
else
159168
{
@@ -169,7 +178,9 @@ private static async Task StoreHTMLFile()
169178
if (!await s_memory.IsDocumentReadyAsync(documentId: "htmlDoc001"))
170179
{
171180
Console.WriteLine("Uploading a HTML file about Apache Submarine project");
172-
await s_memory.ImportDocumentAsync(new Document("htmlDoc001").AddFile("file7-submarine.html").AddTag("user", "Ela"));
181+
var docId = await s_memory.ImportDocumentAsync(new Document("htmlDoc001").AddFile("file7-submarine.html").AddTag("user", "Ela"));
182+
s_toDelete.Add(docId);
183+
Console.WriteLine($"- Document Id: {docId}");
173184
}
174185
else
175186
{
@@ -185,9 +196,11 @@ private static async Task StoreWithCustomPipeline()
185196
if (!await s_memory.IsDocumentReadyAsync("webPage2"))
186197
{
187198
Console.WriteLine("Uploading https://raw.githubusercontent.com/microsoft/kernel-memory/main/docs/security/security-filters.md");
188-
await s_memory.ImportWebPageAsync("https://raw.githubusercontent.com/microsoft/kernel-memory/main/docs/security/security-filters.md",
199+
var docId = await s_memory.ImportWebPageAsync("https://raw.githubusercontent.com/microsoft/kernel-memory/main/docs/security/security-filters.md",
189200
documentId: "webPage2",
190201
steps: Constants.PipelineWithoutSummary);
202+
s_toDelete.Add(docId);
203+
Console.WriteLine($"- Document Id: {docId}");
191204
}
192205
else
193206
{
@@ -203,7 +216,9 @@ private static async Task StoreExcel()
203216
if (!await s_memory.IsDocumentReadyAsync(documentId: "xls01"))
204217
{
205218
Console.WriteLine("Uploading Excel file with some empty cells");
206-
await s_memory.ImportDocumentAsync(new Document("xls01").AddFiles(new[] { "file8-data.xlsx" }));
219+
var docId = await s_memory.ImportDocumentAsync(new Document("xls01").AddFiles(new[] { "file8-data.xlsx" }));
220+
s_toDelete.Add(docId);
221+
Console.WriteLine($"- Document Id: {docId}");
207222
}
208223
else
209224
{
@@ -219,7 +234,9 @@ private static async Task StoreJson()
219234
if (!await s_memory.IsDocumentReadyAsync(documentId: "json01"))
220235
{
221236
Console.WriteLine("Uploading JSON file");
222-
await s_memory.ImportDocumentAsync(new Document("json01").AddFiles(new[] { "file9-settings.json" }));
237+
var docId = await s_memory.ImportDocumentAsync(new Document("json01").AddFiles(new[] { "file9-settings.json" }));
238+
s_toDelete.Add(docId);
239+
Console.WriteLine($"- Document Id: {docId}");
223240
}
224241
else
225242
{

examples/002-dotnet-Serverless/Program.cs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ public static class Program
2222
private static readonly List<string> s_toDelete = new();
2323

2424
// Change this to True and configure Azure Document Intelligence to test OCR and support for images
25-
private const bool imageSupportDemoEnabled = false;
25+
private const bool ImageSupportDemoEnabled = true;
2626

2727
public static async Task Main()
2828
{
29+
var memoryConfiguration = new KernelMemoryConfig();
2930
var openAIConfig = new OpenAIConfig();
3031
var azureOpenAITextConfig = new AzureOpenAIConfig();
3132
var azureOpenAIEmbeddingConfig = new AzureOpenAIConfig();
@@ -39,6 +40,7 @@ public static async Task Main()
3940
.AddJsonFile("appsettings.json")
4041
.AddJsonFile("appsettings.Development.json", optional: true)
4142
.Build()
43+
.BindSection("KernelMemory", memoryConfiguration)
4244
.BindSection("KernelMemory:Services:OpenAI", openAIConfig)
4345
.BindSection("KernelMemory:Services:AzureOpenAIText", azureOpenAITextConfig)
4446
.BindSection("KernelMemory:Services:AzureOpenAIEmbedding", azureOpenAIEmbeddingConfig)
@@ -49,6 +51,7 @@ public static async Task Main()
4951
.BindSection("KernelMemory:Retrieval:SearchClient", searchClientConfig);
5052

5153
s_memory = new KernelMemoryBuilder()
54+
.AddSingleton(memoryConfiguration)
5255
// .WithOpenAIDefaults(Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
5356
// .WithOpenAI(openAIConfig)
5457
.WithAzureOpenAITextGeneration(azureOpenAITextConfig, new DefaultGPTTokenizer())
@@ -97,6 +100,8 @@ public static async Task Main()
97100
// =======================
98101

99102
await DeleteMemories();
103+
104+
Console.WriteLine("\n# DONE");
100105
}
101106

102107
// =======================
@@ -111,25 +116,28 @@ private static async Task StoreText()
111116
"in a system's rest frame, where the two quantities differ only by a multiplicative " +
112117
"constant and the units of measurement. The principle is described by the physicist " +
113118
"Albert Einstein's formula: E = m*c^2");
119+
Console.WriteLine($"- Document Id: {docId}");
114120
s_toDelete.Add(docId);
115121
}
116122

117123
// Simple file upload, with document ID
118124
private static async Task StoreFile()
119125
{
120126
Console.WriteLine("Uploading article file about Carbon");
121-
await s_memory.ImportDocumentAsync("file1-Wikipedia-Carbon.txt", documentId: "doc001");
122-
s_toDelete.Add("doc001");
127+
var docId = await s_memory.ImportDocumentAsync("file1-Wikipedia-Carbon.txt", documentId: "doc001");
128+
s_toDelete.Add(docId);
129+
Console.WriteLine($"- Document Id: {docId}");
123130
}
124131

125132
// Extract memory from images (OCR required)
126133
private static async Task StoreImage()
127134
{
128-
if (!imageSupportDemoEnabled) { return; }
135+
if (!ImageSupportDemoEnabled) { return; }
129136

130137
Console.WriteLine("Uploading Image file with a news about a conference sponsored by Microsoft");
131-
await s_memory.ImportDocumentAsync(new Document("img001").AddFiles(new[] { "file6-ANWC-image.jpg" }));
132-
s_toDelete.Add("img001");
138+
var docId = await s_memory.ImportDocumentAsync(new Document("img001").AddFiles(new[] { "file6-ANWC-image.jpg" }));
139+
s_toDelete.Add(docId);
140+
Console.WriteLine($"- Document Id: {docId}");
133141
}
134142

135143
// Uploading multiple files and adding a user tag, checking if the document already exists
@@ -138,9 +146,11 @@ private static async Task StoreMultipleFiles()
138146
if (!await s_memory.IsDocumentReadyAsync(documentId: "doc002"))
139147
{
140148
Console.WriteLine("Uploading a text file, a Word doc, and a PDF about Kernel Memory");
141-
await s_memory.ImportDocumentAsync(new Document("doc002")
149+
var docId = await s_memory.ImportDocumentAsync(new Document("doc002")
142150
.AddFiles(new[] { "file2-Wikipedia-Moon.txt", "file3-lorem-ipsum.docx", "file4-KM-Readme.pdf" })
143151
.AddTag("user", "Blake"));
152+
s_toDelete.Add(docId);
153+
Console.WriteLine($"- Document Id: {docId}");
144154
}
145155
else
146156
{
@@ -156,13 +166,15 @@ private static async Task StoreFileWithMultipleTags()
156166
if (!await s_memory.IsDocumentReadyAsync(documentId: "doc003"))
157167
{
158168
Console.WriteLine("Uploading a PDF with a news about NASA and Orion");
159-
await s_memory.ImportDocumentAsync(new Document("doc003")
169+
var docId = await s_memory.ImportDocumentAsync(new Document("doc003")
160170
.AddFile("file5-NASA-news.pdf")
161171
.AddTag("user", "Taylor")
162172
.AddTag("collection", "meetings")
163173
.AddTag("collection", "NASA")
164174
.AddTag("collection", "space")
165175
.AddTag("type", "news"));
176+
s_toDelete.Add(docId);
177+
Console.WriteLine($"- Document Id: {docId}");
166178
}
167179
else
168180
{
@@ -178,7 +190,9 @@ private static async Task StoreWebPage()
178190
if (!await s_memory.IsDocumentReadyAsync("webPage1"))
179191
{
180192
Console.WriteLine("Uploading https://raw.githubusercontent.com/microsoft/kernel-memory/main/README.md");
181-
await s_memory.ImportWebPageAsync("https://raw.githubusercontent.com/microsoft/kernel-memory/main/README.md", documentId: "webPage1");
193+
var docId = await s_memory.ImportWebPageAsync("https://raw.githubusercontent.com/microsoft/kernel-memory/main/README.md", documentId: "webPage1");
194+
s_toDelete.Add(docId);
195+
Console.WriteLine($"- Document Id: {docId}");
182196
}
183197
else
184198
{
@@ -194,7 +208,9 @@ private static async Task StoreHTMLFile()
194208
if (!await s_memory.IsDocumentReadyAsync(documentId: "htmlDoc001"))
195209
{
196210
Console.WriteLine("Uploading a HTML file about Apache Submarine project");
197-
await s_memory.ImportDocumentAsync(new Document("htmlDoc001").AddFile("file7-submarine.html").AddTag("user", "Ela"));
211+
var docId = await s_memory.ImportDocumentAsync(new Document("htmlDoc001").AddFile("file7-submarine.html").AddTag("user", "Ela"));
212+
s_toDelete.Add(docId);
213+
Console.WriteLine($"- Document Id: {docId}");
198214
}
199215
else
200216
{
@@ -210,9 +226,11 @@ private static async Task StoreWithCustomPipeline()
210226
if (!await s_memory.IsDocumentReadyAsync("webPage2"))
211227
{
212228
Console.WriteLine("Uploading https://raw.githubusercontent.com/microsoft/kernel-memory/main/docs/security/security-filters.md");
213-
await s_memory.ImportWebPageAsync("https://raw.githubusercontent.com/microsoft/kernel-memory/main/docs/security/security-filters.md",
229+
var docId = await s_memory.ImportWebPageAsync("https://raw.githubusercontent.com/microsoft/kernel-memory/main/docs/security/security-filters.md",
214230
documentId: "webPage2",
215231
steps: Constants.PipelineWithoutSummary);
232+
s_toDelete.Add(docId);
233+
Console.WriteLine($"- Document Id: {docId}");
216234
}
217235
else
218236
{
@@ -228,7 +246,9 @@ private static async Task StoreExcel()
228246
if (!await s_memory.IsDocumentReadyAsync(documentId: "xls01"))
229247
{
230248
Console.WriteLine("Uploading Excel file with some empty cells");
231-
await s_memory.ImportDocumentAsync(new Document("xls01").AddFiles(new[] { "file8-data.xlsx" }));
249+
var docId = await s_memory.ImportDocumentAsync(new Document("xls01").AddFiles(new[] { "file8-data.xlsx" }));
250+
s_toDelete.Add(docId);
251+
Console.WriteLine($"- Document Id: {docId}");
232252
}
233253
else
234254
{
@@ -244,7 +264,9 @@ private static async Task StoreJson()
244264
if (!await s_memory.IsDocumentReadyAsync(documentId: "json01"))
245265
{
246266
Console.WriteLine("Uploading JSON file");
247-
await s_memory.ImportDocumentAsync(new Document("json01").AddFiles(new[] { "file9-settings.json" }));
267+
var docId = await s_memory.ImportDocumentAsync(new Document("json01").AddFiles(new[] { "file9-settings.json" }));
268+
s_toDelete.Add(docId);
269+
Console.WriteLine($"- Document Id: {docId}");
248270
}
249271
else
250272
{
@@ -339,7 +361,7 @@ private static async Task AskQuestionAboutImageContent()
339361

340362
var answer = await s_memory.AskAsync(question, minRelevance: 0.76);
341363

342-
Console.WriteLine(imageSupportDemoEnabled
364+
Console.WriteLine(ImageSupportDemoEnabled
343365
? $"\nAnswer: {answer.Result}\n\n Sources:\n"
344366
: $"\nAnswer (none expected): {answer.Result}\n\n Sources:\n");
345367

examples/002-dotnet-Serverless/appsettings.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@
5656
// using the env vars AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET.
5757
"Auth": "AzureIdentity",
5858
"Endpoint": "https://<...>",
59-
"APIKey": ""
59+
"APIKey": "",
60+
// Hybrid search is not enabled by default. Note that when using hybrid search
61+
// relevance scores are different, usually lower, than when using just vector search
62+
"UseHybridSearch": false
6063
},
6164
"OpenAI": {
6265
// Name of the model used to generate text (text completion or chat completion)
@@ -91,6 +94,11 @@
9194
// "Seed": 1337,
9295
}
9396
},
97+
"DataIngestion": {
98+
// How many memory DB records to insert at once when extracting memories from
99+
// uploaded documents (used only if the Memory Db supports batching).
100+
"MemoryDbUpsertBatchSize": 100
101+
},
94102
"Retrieval": {
95103
"SearchClient": {
96104
// Maximum number of tokens accepted by the LLM used to generate answers.

0 commit comments

Comments
 (0)