19 protected USearchIndex index;
23 [ModelAdvanced]
public ScalarKind
quantization = ScalarKind.Float16;
25 [ModelAdvanced]
public MetricKind
metricKind = MetricKind.Cos;
33 private Dictionary<int, (
float[], string, List<int>)> incrementalSearchCache =
new Dictionary<
int, (
float[],
string, List<int>)>();
36 public new void Awake()
43 public void InitIndex()
48 protected override void AddInternal(
int key,
float[] embedding)
50 index.Add((ulong)key, embedding);
53 protected override void RemoveInternal(
int key)
55 index.Remove((ulong)key);
58 protected int[] UlongToInt(ulong[] keys)
60 int[] intKeys =
new int[keys.Length];
61 for (
int i = 0; i < keys.Length; i++) intKeys[i] = (
int)keys[i];
67 int key = nextIncrementalSearchKey++;
68 incrementalSearchCache[key] = (embedding, group,
new List<int>());
74 if (!incrementalSearchCache.ContainsKey(fetchKey))
throw new Exception($
"There is no IncrementalSearch cached with this key: {fetchKey}");
76 (
float[] embedding,
string group, List<int> seenKeys) = incrementalSearchCache[fetchKey];
78 if (!dataSplits.TryGetValue(group, out List<int> dataSplit))
return (
new int[0],
new float[0],
true);
79 if (dataSplit.Count == 0)
return (
new int[0],
new float[0],
true);
81 Func<int, int> filter = (
int key) => !dataSplit.Contains(key) || seenKeys.Contains(key) ? 0 : 1;
82 index.Search(embedding, k, out ulong[] keys, out
float[] distances, filter);
83 int[] intKeys = UlongToInt(keys);
84 incrementalSearchCache[fetchKey].Item3.AddRange(intKeys);
86 bool completed = intKeys.Length < k || seenKeys.Count ==
Count(group);
88 return (intKeys, distances, completed);
93 incrementalSearchCache.Remove(fetchKey);
96 protected override void SaveInternal(ZipArchive archive)
101 protected override void LoadInternal(ZipArchive archive)
106 protected override void ClearInternal()
110 incrementalSearchCache.Clear();
ValueTuple< int[], float[], bool > IncrementalFetchKeys(int fetchKey, int k)
Retrieves the most similar search results in batches (incremental search). The phrase keys and distan...