-
Notifications
You must be signed in to change notification settings - Fork 321
/
SearchClient.cs
408 lines (346 loc) · 15.6 KB
/
SearchClient.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
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.KernelMemory.AI;
using Microsoft.KernelMemory.Context;
using Microsoft.KernelMemory.Diagnostics;
using Microsoft.KernelMemory.MemoryStorage;
using Microsoft.KernelMemory.Prompts;
namespace Microsoft.KernelMemory.Search;
public sealed class SearchClient : ISearchClient
{
private readonly IMemoryDb _memoryDb;
private readonly ITextGenerator _textGenerator;
private readonly SearchClientConfig _config;
private readonly ILogger<SearchClient> _log;
private readonly string _answerPrompt;
public SearchClient(
IMemoryDb memoryDb,
ITextGenerator textGenerator,
SearchClientConfig? config = null,
IPromptProvider? promptProvider = null,
ILoggerFactory? loggerFactory = null)
{
this._memoryDb = memoryDb;
this._textGenerator = textGenerator;
this._config = config ?? new SearchClientConfig();
this._config.Validate();
promptProvider ??= new EmbeddedPromptProvider();
this._answerPrompt = promptProvider.ReadPrompt(Constants.PromptNamesAnswerWithFacts);
this._log = (loggerFactory ?? DefaultLogger.Factory).CreateLogger<SearchClient>();
if (this._memoryDb == null)
{
throw new KernelMemoryException("Search memory DB not configured");
}
if (this._textGenerator == null)
{
throw new KernelMemoryException("Text generator not configured");
}
}
/// <inheritdoc />
public Task<IEnumerable<string>> ListIndexesAsync(CancellationToken cancellationToken = default)
{
return this._memoryDb.GetIndexesAsync(cancellationToken);
}
/// <inheritdoc />
public async Task<SearchResult> SearchAsync(
string index,
string query,
ICollection<MemoryFilter>? filters = null,
double minRelevance = 0,
int limit = -1,
IContext? context = null,
CancellationToken cancellationToken = default)
{
if (limit <= 0) { limit = this._config.MaxMatchesCount; }
var result = new SearchResult
{
Query = query,
Results = new List<Citation>()
};
if (string.IsNullOrWhiteSpace(query) && (filters == null || filters.Count == 0))
{
this._log.LogWarning("No query or filters provided");
return result;
}
var list = new List<(MemoryRecord memory, double relevance)>();
if (!string.IsNullOrEmpty(query))
{
this._log.LogTrace("Fetching relevant memories by similarity, min relevance {0}", minRelevance);
IAsyncEnumerable<(MemoryRecord, double)> matches = this._memoryDb.GetSimilarListAsync(
index: index,
text: query,
filters: filters,
minRelevance: minRelevance,
limit: limit,
withEmbeddings: false,
cancellationToken: cancellationToken);
// Memories are sorted by relevance, starting from the most relevant
await foreach ((MemoryRecord memory, double relevance) in matches.ConfigureAwait(false))
{
list.Add((memory, relevance));
}
}
else
{
this._log.LogTrace("Fetching relevant memories by filtering");
IAsyncEnumerable<MemoryRecord> matches = this._memoryDb.GetListAsync(
index: index,
filters: filters,
limit: limit,
withEmbeddings: false,
cancellationToken: cancellationToken);
await foreach (MemoryRecord memory in matches.ConfigureAwait(false))
{
list.Add((memory, float.MinValue));
}
}
// Memories are sorted by relevance, starting from the most relevant
foreach ((MemoryRecord memory, double relevance) in list)
{
// Note: a document can be composed by multiple files
string documentId = memory.GetDocumentId(this._log);
// Identify the file in case there are multiple files
string fileId = memory.GetFileId(this._log);
// Note: this is not a URL and perhaps could be dropped. For now it acts as a unique identifier. See also SourceUrl.
string linkToFile = $"{index}/{documentId}/{fileId}";
var partitionText = memory.GetPartitionText(this._log).Trim();
if (string.IsNullOrEmpty(partitionText))
{
this._log.LogError("The document partition is empty, doc: {0}", memory.Id);
continue;
}
// Relevance is `float.MinValue` when search uses only filters and no embeddings (see code above)
if (relevance > float.MinValue) { this._log.LogTrace("Adding result with relevance {0}", relevance); }
// If the file is already in the list of citations, only add the partition
var citation = result.Results.FirstOrDefault(x => x.Link == linkToFile);
if (citation == null)
{
citation = new Citation();
result.Results.Add(citation);
}
// Add the partition to the list of citations
citation.Index = index;
citation.DocumentId = documentId;
citation.FileId = fileId;
citation.Link = linkToFile;
citation.SourceContentType = memory.GetFileContentType(this._log);
citation.SourceName = memory.GetFileName(this._log);
citation.SourceUrl = memory.GetWebPageUrl(index);
citation.Partitions.Add(new Citation.Partition
{
Text = partitionText,
Relevance = (float)relevance,
PartitionNumber = memory.GetPartitionNumber(this._log),
SectionNumber = memory.GetSectionNumber(),
LastUpdate = memory.GetLastUpdate(),
Tags = memory.Tags,
});
// In cases where a buggy storage connector is returning too many records
if (result.Results.Count >= this._config.MaxMatchesCount)
{
break;
}
}
if (result.Results.Count == 0)
{
this._log.LogDebug("No memories found");
}
return result;
}
/// <inheritdoc />
public async Task<MemoryAnswer> AskAsync(
string index,
string question,
ICollection<MemoryFilter>? filters = null,
double minRelevance = 0,
IContext? context = null,
CancellationToken cancellationToken = default)
{
string emptyAnswer = context.GetCustomEmptyAnswerTextOrDefault(this._config.EmptyAnswer);
string answerPrompt = context.GetCustomRagPromptOrDefault(this._answerPrompt);
string factTemplate = context.GetCustomRagFactTemplateOrDefault(this._config.FactTemplate);
if (!factTemplate.EndsWith('\n')) { factTemplate += "\n"; }
var noAnswerFound = new MemoryAnswer
{
Question = question,
NoResult = true,
Result = emptyAnswer,
};
if (string.IsNullOrEmpty(question))
{
this._log.LogWarning("No question provided");
noAnswerFound.NoResultReason = "No question provided";
return noAnswerFound;
}
var facts = new StringBuilder();
var maxTokens = this._config.MaxAskPromptSize > 0
? this._config.MaxAskPromptSize
: this._textGenerator.MaxTokenTotal;
var tokensAvailable = maxTokens
- this._textGenerator.CountTokens(answerPrompt)
- this._textGenerator.CountTokens(question)
- this._config.AnswerTokens;
var factsUsedCount = 0;
var factsAvailableCount = 0;
var answer = noAnswerFound;
this._log.LogTrace("Fetching relevant memories");
IAsyncEnumerable<(MemoryRecord, double)> matches = this._memoryDb.GetSimilarListAsync(
index: index,
text: question,
filters: filters,
minRelevance: minRelevance,
limit: this._config.MaxMatchesCount,
withEmbeddings: false,
cancellationToken: cancellationToken);
// Memories are sorted by relevance, starting from the most relevant
await foreach ((MemoryRecord memory, double relevance) in matches.ConfigureAwait(false))
{
// Note: a document can be composed by multiple files
string documentId = memory.GetDocumentId(this._log);
// Identify the file in case there are multiple files
string fileId = memory.GetFileId(this._log);
// Note: this is not a URL and perhaps could be dropped. For now it acts as a unique identifier. See also SourceUrl.
string linkToFile = $"{index}/{documentId}/{fileId}";
string fileName = memory.GetFileName(this._log);
string webPageUrl = memory.GetWebPageUrl(index);
var partitionText = memory.GetPartitionText(this._log).Trim();
if (string.IsNullOrEmpty(partitionText))
{
this._log.LogError("The document partition is empty, doc: {0}", memory.Id);
continue;
}
factsAvailableCount++;
var fact = PromptUtils.RenderFactTemplate(
template: factTemplate,
factContent: partitionText,
source: (fileName == "content.url" ? webPageUrl : fileName),
relevance: relevance.ToString("P1", CultureInfo.CurrentCulture),
recordId: memory.Id,
tags: memory.Tags,
metadata: memory.Payload);
// Use the partition/chunk only if there's room for it
var size = this._textGenerator.CountTokens(fact);
if (size >= tokensAvailable)
{
// Stop after reaching the max number of tokens
break;
}
factsUsedCount++;
this._log.LogTrace("Adding text {0} with relevance {1}", factsUsedCount, relevance);
facts.Append(fact);
tokensAvailable -= size;
// If the file is already in the list of citations, only add the partition
var citation = answer.RelevantSources.FirstOrDefault(x => x.Link == linkToFile);
if (citation == null)
{
citation = new Citation();
answer.RelevantSources.Add(citation);
}
// Add the partition to the list of citations
citation.Index = index;
citation.DocumentId = documentId;
citation.FileId = fileId;
citation.Link = linkToFile;
citation.SourceContentType = memory.GetFileContentType(this._log);
citation.SourceName = fileName;
citation.SourceUrl = memory.GetWebPageUrl(index);
citation.Partitions.Add(new Citation.Partition
{
Text = partitionText,
Relevance = (float)relevance,
PartitionNumber = memory.GetPartitionNumber(this._log),
SectionNumber = memory.GetSectionNumber(),
LastUpdate = memory.GetLastUpdate(),
Tags = memory.Tags,
});
// In cases where a buggy storage connector is returning too many records
if (factsUsedCount >= this._config.MaxMatchesCount)
{
break;
}
}
if (factsAvailableCount > 0 && factsUsedCount == 0)
{
this._log.LogError("Unable to inject memories in the prompt, not enough tokens available");
noAnswerFound.NoResultReason = "Unable to use memories";
return noAnswerFound;
}
if (factsUsedCount == 0)
{
this._log.LogWarning("No memories available (min relevance: {0})", minRelevance);
noAnswerFound.NoResultReason = "No memories available";
return noAnswerFound;
}
var text = new StringBuilder();
var charsGenerated = 0;
var watch = new Stopwatch();
watch.Restart();
await foreach (var x in this.GenerateAnswer(question, facts.ToString(), context, cancellationToken).ConfigureAwait(false))
{
text.Append(x);
if (this._log.IsEnabled(LogLevel.Trace) && text.Length - charsGenerated >= 30)
{
charsGenerated = text.Length;
this._log.LogTrace("{0} chars generated", charsGenerated);
}
}
watch.Stop();
answer.Result = text.ToString();
this._log.LogSensitive("Answer: {0}", answer.Result);
answer.NoResult = ValueIsEquivalentTo(answer.Result, this._config.EmptyAnswer);
if (answer.NoResult)
{
answer.NoResultReason = "No relevant memories found";
this._log.LogTrace("Answer generated in {0} msecs. No relevant memories found", watch.ElapsedMilliseconds);
}
else
{
this._log.LogTrace("Answer generated in {0} msecs", watch.ElapsedMilliseconds);
}
return answer;
}
private IAsyncEnumerable<string> GenerateAnswer(string question, string facts, IContext? context, CancellationToken token)
{
string prompt = context.GetCustomRagPromptOrDefault(this._answerPrompt);
int maxTokens = context.GetCustomRagMaxTokensOrDefault(this._config.AnswerTokens);
double temperature = context.GetCustomRagTemperatureOrDefault(this._config.Temperature);
double nucleusSampling = context.GetCustomRagNucleusSamplingOrDefault(this._config.TopP);
prompt = prompt.Replace("{{$facts}}", facts.Trim(), StringComparison.OrdinalIgnoreCase);
question = question.Trim();
question = question.EndsWith('?') ? question : $"{question}?";
prompt = prompt.Replace("{{$input}}", question, StringComparison.OrdinalIgnoreCase);
prompt = prompt.Replace("{{$notFound}}", this._config.EmptyAnswer, StringComparison.OrdinalIgnoreCase);
var options = new TextGenerationOptions
{
MaxTokens = maxTokens,
Temperature = temperature,
NucleusSampling = nucleusSampling,
PresencePenalty = this._config.PresencePenalty,
FrequencyPenalty = this._config.FrequencyPenalty,
StopSequences = this._config.StopSequences,
TokenSelectionBiases = this._config.TokenSelectionBiases,
};
if (this._log.IsEnabled(LogLevel.Debug))
{
this._log.LogDebug("Running RAG prompt, size: {0} tokens, requesting max {1} tokens",
this._textGenerator.CountTokens(prompt),
this._config.AnswerTokens);
this._log.LogSensitive("Prompt: {0}", prompt);
}
return this._textGenerator.GenerateTextAsync(prompt, options, token);
}
private static bool ValueIsEquivalentTo(string value, string target)
{
value = value.Trim().Trim('.', '"', '\'', '`', '~', '!', '?', '@', '#', '$', '%', '^', '+', '*', '_', '-', '=', '|', '\\', '/', '(', ')', '[', ']', '{', '}', '<', '>');
target = target.Trim().Trim('.', '"', '\'', '`', '~', '!', '?', '@', '#', '$', '%', '^', '+', '*', '_', '-', '=', '|', '\\', '/', '(', ')', '[', ']', '{', '}', '<', '>');
return string.Equals(value, target, StringComparison.OrdinalIgnoreCase);
}
}