-
Notifications
You must be signed in to change notification settings - Fork 358
/
Copy pathKernelMemoryTests.cs
432 lines (355 loc) · 18.8 KB
/
KernelMemoryTests.cs
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Copyright (c) Microsoft. All rights reserved.
using System.Diagnostics.CodeAnalysis;
using Microsoft.KernelMemory;
using Microsoft.KernelMemory.MemoryDb.Elasticsearch.Internals;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.Elasticsearch.FunctionalTests.Additional;
public class KernelMemoryTests : MemoryDbFunctionalTest
{
private const string NoAnswer = "INFO NOT FOUND";
public KernelMemoryTests(IConfiguration cfg, ITestOutputHelper output)
: base(cfg, output)
{
this.KernelMemory = new KernelMemoryBuilder()
.With(new KernelMemoryConfig { DefaultIndexName = "default4tests" })
.WithSearchClientConfig(new SearchClientConfig { EmptyAnswer = NotFound })
.WithOpenAI(this.OpenAiConfig)
.WithElasticsearchMemoryDb(this.ElasticsearchConfig)
.Build<MemoryServerless>();
}
public IKernelMemory KernelMemory { get; }
[Fact]
[Trait("Category", "Elasticsearch")]
public async Task ItSupportsLimitsAndMinRelevanceScoreAsync()
{
string indexName = nameof(this.ItSupportsLimitsAndMinRelevanceScoreAsync);
this.Output.WriteLine($"Index name: {indexName}");
const string Id = "ItSupportsLimitsAndMinRelevance-file7-Silicon-Carbon.txt";
this.Output.WriteLine("Uploading document");
await this.KernelMemory.ImportDocumentAsync(
new Document(Id)
.AddFile(TestsHelper.SKWikipediaSiliconFileName),
index: indexName,
steps: Constants.PipelineWithoutSummary);
while (!await this.KernelMemory.IsDocumentReadyAsync(documentId: Id, index: indexName))
{
this.Output.WriteLine("Waiting for memory ingestion to complete...");
await Task.Delay(TimeSpan.FromSeconds(2));
}
//no minRelevance or limit:
var results = await this.KernelMemory.SearchAsync("What is Silicon?", index: indexName, null, null, minRelevance: 0, limit: -1);
Assert.True((results.Results.Count > 0));
var partitions = results.Results[0].Partitions;
var maxRelevance = partitions.Max(p => p.Relevance);
var minRelevance = partitions.Min(p => p.Relevance);
this.Output.WriteLine($"{partitions.Count}, Max Relevance: {maxRelevance}, Min Relevance: {minRelevance}");
Assert.True((partitions.Count > 5));
Assert.True((minRelevance < 0.82));
//test limit 5:
results = await this.KernelMemory.SearchAsync("What is Silicon?", index: indexName, null, null, minRelevance: 0, limit: 5);
partitions = results.Results[0].Partitions;
Assert.True((partitions.Count == 5));
//test min relevance 0.82:
results = await this.KernelMemory.SearchAsync("What is Silicon?", index: indexName, null, null, minRelevance: 0.82, limit: 10);
partitions = results.Results[0].Partitions;
minRelevance = partitions.Min(p => p.Relevance);
Assert.True((minRelevance >= 0.82));
await this.KernelMemory.DeleteDocumentAsync(Id, index: indexName);
this.Output.WriteLine("Deleting index");
await this.KernelMemory.DeleteIndexAsync(indexName);
}
[Fact]
[Trait("Category", "Elasticsearch")]
[SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "<Pending>")]
public async Task ItSupportsMultipleFiltersAsync()
{
// This is an adaptation of the same test in Elasticsearch.FunctionalTests
string indexName = nameof(this.ItSupportsMultipleFiltersAsync);
this.Output.WriteLine($"Index name: {indexName}");
const string Id = "ItSupportsMultipleFilters-file1-NASA-news.pdf";
const string Found = "spacecraft";
this.Output.WriteLine("Uploading document");
await this.KernelMemory.ImportDocumentAsync(
new Document(Id)
.AddFile(TestsHelper.NASANewsFileName)
.AddTag("type", "news")
.AddTag("user", "admin")
.AddTag("user", "owner"),
index: indexName,
steps: Constants.PipelineWithoutSummary);
while (!await this.KernelMemory.IsDocumentReadyAsync(documentId: Id, index: indexName))
{
this.Output.WriteLine("Waiting for memory ingestion to complete...");
await Task.Delay(TimeSpan.FromSeconds(2));
}
// Multiple filters: unknown users cannot see the memory
var answer = await this.KernelMemory.AskAsync("What is Orion?", filters:
[
MemoryFilters.ByTag("user", "someone1"),
MemoryFilters.ByTag("user", "someone2"),
], index: indexName);
this.Output.WriteLine(answer.Result);
Assert.Contains(NotFound, answer.Result, StringComparison.OrdinalIgnoreCase);
// Multiple filters: unknown users cannot see the memory even if the type is correct (testing AND logic)
answer = await this.KernelMemory.AskAsync("What is Orion?", filters:
[
MemoryFilters.ByTag("user", "someone1").ByTag("type", "news"),
MemoryFilters.ByTag("user", "someone2").ByTag("type", "news"),
], index: indexName);
this.Output.WriteLine(answer.Result);
Assert.Contains(NotFound, answer.Result, StringComparison.OrdinalIgnoreCase);
// Multiple filters: AND + OR logic works
answer = await this.KernelMemory.AskAsync("What is Orion?", filters:
[
MemoryFilters.ByTag("user", "someone1").ByTag("type", "news"),
MemoryFilters.ByTag("user", "admin").ByTag("type", "fact"),
], index: indexName);
this.Output.WriteLine(answer.Result);
Assert.Contains(NotFound, answer.Result, StringComparison.OrdinalIgnoreCase);
// Multiple filters: OR logic works
answer = await this.KernelMemory.AskAsync("What is Orion?", filters:
[
MemoryFilters.ByTag("user", "someone1"),
MemoryFilters.ByTag("user", "admin"),
], index: indexName);
this.Output.WriteLine(answer.Result);
Assert.Contains(Found, answer.Result, StringComparison.OrdinalIgnoreCase);
// Multiple filters: OR logic works
answer = await this.KernelMemory.AskAsync("What is Orion?", filters:
[
MemoryFilters.ByTag("user", "someone1").ByTag("type", "news"),
MemoryFilters.ByTag("user", "admin").ByTag("type", "news"),
], index: indexName);
this.Output.WriteLine(answer.Result);
Assert.Contains(Found, answer.Result, StringComparison.OrdinalIgnoreCase);
await this.KernelMemory.DeleteDocumentAsync(Id, index: indexName);
this.Output.WriteLine("Deleting index");
await this.KernelMemory.DeleteIndexAsync(indexName);
}
[Fact]
[Trait("Category", "Elasticsearch")]
public async Task ItSupportsTagsAsync()
{
// This is an adaptation of the same test in Elasticsearch.FunctionalTests
// Arrange
const string Id = "ItSupportTags-file1-NASA-news.pdf";
await this.KernelMemory.ImportDocumentAsync(
TestsHelper.NASANewsFileName,
documentId: Id,
tags: new TagCollection
{
{ "type", "news" },
{ "type", "test" },
{ "ext", "pdf" }
},
steps: Constants.PipelineWithoutSummary).ConfigureAwait(false);
while (!await this.KernelMemory.IsDocumentReadyAsync(documentId: Id).ConfigureAwait(false))
{
this.Output.WriteLine("Waiting for memory ingestion to complete...");
await Task.Delay(TimeSpan.FromSeconds(2)).ConfigureAwait(false);
}
// Act
var defaultRetries = 0; // withRetries ? 4 : 0;
var retries = defaultRetries;
var answer1 = await this.KernelMemory.AskAsync("What is Orion?", filter: MemoryFilters.ByTag("type", "news")).ConfigureAwait(false);
this.Output.WriteLine("answer1: " + answer1.Result);
while (retries-- > 0 && !answer1.Result.Contains("spacecraft", StringComparison.OrdinalIgnoreCase))
{
await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
answer1 = await this.KernelMemory.AskAsync("What is Orion?", filter: MemoryFilters.ByTag("type", "news")).ConfigureAwait(false);
this.Output.WriteLine("answer1: " + answer1.Result);
}
retries = defaultRetries;
var answer2 = await this.KernelMemory.AskAsync("What is Orion?", filter: MemoryFilters.ByTag("type", "test")).ConfigureAwait(false);
this.Output.WriteLine("answer2: " + answer2.Result);
while (retries-- > 0 && !answer2.Result.Contains("spacecraft", StringComparison.OrdinalIgnoreCase))
{
await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
answer2 = await this.KernelMemory.AskAsync("What is Orion?", filter: MemoryFilters.ByTag("type", "test")).ConfigureAwait(false);
this.Output.WriteLine("answer2: " + answer2.Result);
}
retries = defaultRetries;
var answer3 = await this.KernelMemory.AskAsync("What is Orion?", filter: MemoryFilters.ByTag("ext", "pdf")).ConfigureAwait(false);
this.Output.WriteLine("answer3: " + answer3.Result);
while (retries-- > 0 && !answer3.Result.Contains("spacecraft", StringComparison.OrdinalIgnoreCase))
{
await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
answer3 = await this.KernelMemory.AskAsync("What is Orion?", filter: MemoryFilters.ByTag("type", "test")).ConfigureAwait(false);
this.Output.WriteLine("answer3: " + answer3.Result);
}
var answer4 = await this.KernelMemory.AskAsync("What is Orion?", filter: MemoryFilters.ByTag("foo", "bar")).ConfigureAwait(false);
this.Output.WriteLine(answer4.Result);
// Assert
Assert.Contains("spacecraft", answer1.Result, StringComparison.OrdinalIgnoreCase);
Assert.Contains("spacecraft", answer2.Result, StringComparison.OrdinalIgnoreCase);
Assert.Contains("spacecraft", answer3.Result, StringComparison.OrdinalIgnoreCase);
Assert.Contains("NOT FOUND", answer4.Result, StringComparison.OrdinalIgnoreCase);
}
[Fact]
[Trait("Category", "Elasticsearch")]
public async Task ItSupportsASingleFilterAsync()
{
// This is an adaptation of the same test in Elasticsearch.FunctionalTests
string indexName = nameof(this.ItSupportsASingleFilterAsync);
const string Id = "ItSupportsASingleFilter-file1-NASA-news.pdf";
const string Found = "spacecraft";
this.Output.WriteLine("Uploading document");
await this.KernelMemory.ImportDocumentAsync(
new Document(Id)
.AddFile(TestsHelper.NASANewsFileName)
.AddTag("type", "news")
.AddTag("user", "admin")
.AddTag("user", "owner"),
index: indexName,
steps: Constants.PipelineWithoutSummary).ConfigureAwait(false);
while (!await this.KernelMemory.IsDocumentReadyAsync(documentId: Id, index: indexName).ConfigureAwait(false))
{
this.Output.WriteLine("Waiting for memory ingestion to complete...");
await Task.Delay(TimeSpan.FromSeconds(2)).ConfigureAwait(false);
}
//await Task.Delay(TimeSpan.FromSeconds(4)).ConfigureAwait(false);
MemoryAnswer answer;
// Simple filter: unknown user cannot see the memory
answer = await this.KernelMemory.AskAsync("What is Orion?", filter: MemoryFilters.ByTag("user", "someone"), index: indexName).ConfigureAwait(false);
this.Output.WriteLine(answer.Result);
Assert.Contains(NotFound, answer.Result, StringComparison.OrdinalIgnoreCase);
// Simple filter: test AND logic: valid type + invalid user
answer = await this.KernelMemory.AskAsync("What is Orion?", filter: MemoryFilters.ByTag("type", "news").ByTag("user", "someone"), index: indexName).ConfigureAwait(false);
this.Output.WriteLine(answer.Result);
Assert.Contains(NotFound, answer.Result, StringComparison.OrdinalIgnoreCase);
// Simple filter: test AND logic: invalid type + valid user
answer = await this.KernelMemory.AskAsync("What is Orion?", filter: MemoryFilters.ByTag("type", "fact").ByTag("user", "owner"), index: indexName).ConfigureAwait(false);
this.Output.WriteLine(answer.Result);
//Assert.Contains(Found, answer.Result, StringComparison.OrdinalIgnoreCase);
Assert.Contains(NotFound, answer.Result, StringComparison.OrdinalIgnoreCase);
// Simple filter: known user can see the memory
answer = await this.KernelMemory.AskAsync("What is Orion?", filter: MemoryFilters.ByTag("user", "admin"), index: indexName).ConfigureAwait(false);
this.Output.WriteLine(answer.Result);
Assert.Contains(Found, answer.Result, StringComparison.OrdinalIgnoreCase);
// Simple filter: known user can see the memory
answer = await this.KernelMemory.AskAsync("What is Orion?", filter: MemoryFilters.ByTag("user", "owner"), index: indexName).ConfigureAwait(false);
this.Output.WriteLine(answer.Result);
Assert.Contains(Found, answer.Result, StringComparison.OrdinalIgnoreCase);
// Simple filter: test AND logic with correct values
answer = await this.KernelMemory.AskAsync("What is Orion?", filter: MemoryFilters.ByTag("type", "news").ByTag("user", "owner"), index: indexName).ConfigureAwait(false);
this.Output.WriteLine(answer.Result);
Assert.Contains(Found, answer.Result, StringComparison.OrdinalIgnoreCase);
this.Output.WriteLine("Deleting memories extracted from the document");
await this.KernelMemory.DeleteDocumentAsync(Id, index: indexName).ConfigureAwait(false);
this.Output.WriteLine("Deleting index");
await this.KernelMemory.DeleteIndexAsync(indexName).ConfigureAwait(false);
}
[Fact]
[Trait("Category", "Elasticsearch")]
public async Task CanImportOneDocumentAndAskAsync()
{
var indexName = nameof(this.CanImportOneDocumentAndAskAsync);
// Imports a document into the index
var id = await this.KernelMemory.ImportDocumentAsync(
filePath: TestsHelper.WikipediaCarbonFileName,
documentId: "doc001",
tags: new TagCollection
{
{ "indexedOn", DateTimeOffset.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss.fffzzz") }
},
index: indexName)
.ConfigureAwait(false);
this.Output.WriteLine($"Indexed document with id '{id}'.");
// Waits for the documents to be saved
var actualIndexName = IndexNameHelper.Convert(indexName, base.ElasticsearchConfig);
//await this.Client.WaitForDocumentsAsync(actualIndexName, expectedDocuments: 2)
// .ConfigureAwait(false);
// Asks a question on the data we just inserted
MemoryAnswer? answer = await this.TryToGetTopAnswerAsync(indexName, "What can carbon bond to?")
.ConfigureAwait(false);
this.PrintAnswerOfDocument(answer, "doc001");
}
[Fact]
[Trait("Category", "Elasticsearch")]
public async Task CanImportTwoDocumentsAndAskAsync()
{
var indexName = nameof(this.CanImportTwoDocumentsAndAskAsync);
// Proceeds
var docId = await this.KernelMemory.ImportDocumentAsync(
TestsHelper.WikipediaCarbonFileName,
index: indexName,
documentId: "doc001").ConfigureAwait(false);
this.Output.WriteLine($"Indexed {docId}");
docId = await this.KernelMemory.ImportDocumentAsync(
new Document("doc002")
.AddFiles([
TestsHelper.WikipediaMoonFilename,
TestsHelper.LoremIpsumFileName,
TestsHelper.SKReadmeFileName
])
.AddTag("user", "Blake"),
index: indexName)
.ConfigureAwait(false);
this.Output.WriteLine($"Indexed {docId}");
docId = await this.KernelMemory.ImportDocumentAsync(new Document("doc003")
.AddFile(TestsHelper.NASANewsFileName)
.AddTag("user", "Taylor")
.AddTag("collection", "meetings")
.AddTag("collection", "NASA")
.AddTag("collection", "space")
.AddTag("type", "news"),
index: indexName)
.ConfigureAwait(false);
this.Output.WriteLine($"Indexed {docId}");
// Waits for the documents to be saved
var actualIndexName = IndexNameHelper.Convert(indexName, this.ElasticsearchConfig);
//await this.Client.WaitForDocumentsAsync(actualIndexName, expectedDocuments: 10)
// .ConfigureAwait(false);
// This should return a citation to doc001
var answer = await this.KernelMemory.AskAsync("What's E = m*c^2?", indexName)
.ConfigureAwait(false);
this.PrintAnswerOfDocument(answer, "doc001");
// This should return a citation to doc002
answer = await this.KernelMemory.AskAsync("What's Semantic Kernel?", indexName)
.ConfigureAwait(false);
this.PrintAnswerOfDocument(answer, "doc002");
}
private void PrintAnswerOfDocument(MemoryAnswer? answer, string expectedDocumentId)
{
ArgumentNullException.ThrowIfNull(answer);
this.Output.WriteLine($"Question: {answer.Question}");
this.Output.WriteLine($"Answer: {answer.Result}");
var foundDocumentReference = false;
foreach (var citation in answer.RelevantSources)
{
this.Output.WriteLine($" - {citation.SourceName} - {citation.Link} [{citation.Partitions.First().LastUpdate:D}]");
if (citation.DocumentId == expectedDocumentId)
{
foundDocumentReference = true;
}
}
if (!foundDocumentReference)
{
throw new InvalidOperationException($"It should have found a citation to document '{expectedDocumentId}'.");
}
}
private async Task<MemoryAnswer?> TryToGetTopAnswerAsync(string indexName, string question)
{
MemoryAnswer? answer = null;
// We need to wait a bit for the indexing to complete, so this is why we retry a few times with a delay.
// TODO: add Polly.
for (int i = 0; i < 3; i++)
{
answer = await this.KernelMemory.AskAsync(
question: question,
index: indexName,
filter: null,
filters: null,
minRelevance: 0)
.ConfigureAwait(false);
if (answer.Result != NoAnswer)
{
break;
}
await Task.Delay(500)
.ConfigureAwait(false);
}
return answer;
}
}