17 protected SortedDictionary<int, float[]> embeddings =
new SortedDictionary<int, float[]>();
18 protected Dictionary<int, List<(int, float)>> incrementalSearchCache =
new Dictionary<
int, List<(
int,
float)>>();
20 protected override void AddInternal(
int key,
float[] embedding)
22 embeddings[key] = embedding;
25 protected override void RemoveInternal(
int key)
27 embeddings.Remove(key);
32 int key = nextIncrementalSearchKey++;
34 List<(int, float)> sortedLists =
new List<(
int,
float)>();
35 if (dataSplits.TryGetValue(group, out List<int> dataSplit))
37 if (dataSplit.Count >= 0)
39 float[][] embeddingsSplit =
new float[dataSplit.Count][];
40 for (
int i = 0; i < dataSplit.Count; i++) embeddingsSplit[i] = embeddings[dataSplit[i]];
42 float[] unsortedDistances = InverseDotProduct(embedding, embeddingsSplit);
43 sortedLists = dataSplit.Zip(unsortedDistances, (first, second) => (first, second))
44 .OrderBy(item => item.Item2)
48 incrementalSearchCache[key] = sortedLists;
54 if (!incrementalSearchCache.ContainsKey(fetchKey))
throw new Exception($
"There is no IncrementalSearch cached with this key: {fetchKey}");
57 List<(int, float)> sortedLists;
60 sortedLists = incrementalSearchCache[fetchKey];
65 int getK = Math.Min(k, incrementalSearchCache[fetchKey].
Count);
66 sortedLists = incrementalSearchCache[fetchKey].GetRange(0, getK);
67 incrementalSearchCache[fetchKey].RemoveRange(0, getK);
68 completed = incrementalSearchCache[fetchKey].Count == 0;
72 int[] results =
new int[sortedLists.Count];
73 float[] distances =
new float[sortedLists.Count];
74 for (
int i = 0; i < sortedLists.Count; i++)
76 results[i] = sortedLists[i].Item1;
77 distances[i] = sortedLists[i].Item2;
79 return (results.ToArray(), distances.ToArray(), completed);
84 incrementalSearchCache.Remove(fetchKey);
87 protected override void ClearInternal()
90 incrementalSearchCache.Clear();
93 protected override void SaveInternal(ZipArchive archive)
95 ArchiveSaver.Save(archive, embeddings, GetSavePath(
"embeddings"));
96 ArchiveSaver.Save(archive, incrementalSearchCache, GetSavePath(
"incrementalSearchCache"));
99 protected override void LoadInternal(ZipArchive archive)
101 embeddings = ArchiveSaver.Load<SortedDictionary<int, float[]>>(archive, GetSavePath(
"embeddings"));
102 incrementalSearchCache = ArchiveSaver.Load<Dictionary<int, List<(int, float)>>>(archive, GetSavePath(
"incrementalSearchCache"));
ValueTuple< int[], float[], bool > IncrementalFetchKeys(int fetchKey, int k)
Retrieves the most similar search results in batches (incremental search). The phrase keys and distan...