19 protected USearchIndex index;
21 [Tooltip(
"show/hide advanced options in the GameObject")]
24 [Tooltip(
"The quantisation type used for vector data during indexing.")]
25 [ModelAdvanced]
public ScalarKind
quantization = ScalarKind.Float16;
27 [Tooltip(
"The metric kind used for distance calculation between vectors.")]
28 [ModelAdvanced]
public MetricKind
metricKind = MetricKind.Cos;
30 [Tooltip(
"The connectivity parameter limits the connections-per-node in the graph.")]
33 [Tooltip(
"The expansion factor used for index construction when adding vectors.")]
36 [Tooltip(
"The expansion factor used for index construction during search operations.")]
39 private Dictionary<int, (
float[], string, List<int>)> incrementalSearchCache =
new Dictionary<
int, (
float[],
string, List<int>)>();
42 public new void Awake()
49 public void InitIndex()
54 protected override void AddInternal(
int key,
float[] embedding)
56 index.Add((ulong)key, embedding);
59 protected override void RemoveInternal(
int key)
61 index.Remove((ulong)key);
64 protected int[] UlongToInt(ulong[] keys)
66 int[] intKeys =
new int[keys.Length];
67 for (
int i = 0; i < keys.Length; i++) intKeys[i] = (
int)keys[i];
73 int key = nextIncrementalSearchKey++;
74 incrementalSearchCache[key] = (embedding, group,
new List<int>());
80 if (!incrementalSearchCache.ContainsKey(fetchKey))
throw new Exception($
"There is no IncrementalSearch cached with this key: {fetchKey}");
82 (
float[] embedding,
string group, List<int> seenKeys) = incrementalSearchCache[fetchKey];
84 if (!dataSplits.TryGetValue(group, out List<int> dataSplit))
return (
new int[0],
new float[0],
true);
85 if (dataSplit.Count == 0)
return (
new int[0],
new float[0],
true);
87 Func<int, int> filter = (
int key) => !dataSplit.Contains(key) || seenKeys.Contains(key) ? 0 : 1;
88 index.Search(embedding, k, out ulong[] keys, out
float[] distances, filter);
89 int[] intKeys = UlongToInt(keys);
90 incrementalSearchCache[fetchKey].Item3.AddRange(intKeys);
92 bool completed = intKeys.Length < k || seenKeys.Count ==
Count(group);
94 return (intKeys, distances, completed);
99 incrementalSearchCache.Remove(fetchKey);
102 protected override void SaveInternal(ZipArchive archive)
107 protected override void LoadInternal(ZipArchive archive)
112 protected override void ClearInternal()
116 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...