-
Notifications
You must be signed in to change notification settings - Fork 260
/
KernelMemoryExtensions.cs
75 lines (67 loc) · 2.77 KB
/
KernelMemoryExtensions.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
// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.KernelMemory;
/// <summary>
/// Kernel Memory API extensions
/// </summary>
public static class KernelMemoryExtensions
{
/// <summary>
/// Return a list of synthetic memories of the specified type
/// </summary>
/// <param name="memory">Memory instance</param>
/// <param name="syntheticType">Type of synthetic data to return</param>
/// <param name="index">Optional name of the index where to search</param>
/// <param name="filter">Filter to match</param>
/// <param name="filters">Filters to match (using inclusive OR logic). If 'filter' is provided too, the value is merged into this list.</param>
/// <param name="cancellationToken">Async task cancellation token</param>
/// <returns>List of search results</returns>
public static async Task<List<Citation>> SearchSyntheticsAsync(
this IKernelMemory memory,
string syntheticType,
string? index = null,
MemoryFilter? filter = null,
ICollection<MemoryFilter>? filters = null,
CancellationToken cancellationToken = default)
{
if (filters == null)
{
filters = new List<MemoryFilter>();
if (filter == null) { filters.Add(new MemoryFilter()); }
}
if (filter != null)
{
filters.Add(filter);
}
foreach (var x in filters)
{
x.ByTag(Constants.ReservedSyntheticTypeTag, syntheticType);
}
SearchResult searchResult = await memory.SearchAsync(
query: "",
index: index,
filters: filters,
cancellationToken: cancellationToken).ConfigureAwait(false);
return searchResult.Results;
}
/// <summary>
/// Return a list of summaries matching the given filters
/// </summary>
/// <param name="memory">Memory instance</param>
/// <param name="index">Optional name of the index where to search</param>
/// <param name="filter">Filter to match</param>
/// <param name="filters">Filters to match (using inclusive OR logic). If 'filter' is provided too, the value is merged into this list.</param>
/// <param name="cancellationToken">Async task cancellation token</param>
/// <returns>List of search results</returns>
public static Task<List<Citation>> SearchSummariesAsync(
this IKernelMemory memory,
string? index = null,
MemoryFilter? filter = null,
ICollection<MemoryFilter>? filters = null,
CancellationToken cancellationToken = default)
{
return SearchSyntheticsAsync(memory, Constants.TagsSyntheticSummary, index, filter, filters, cancellationToken);
}
}