LLM for Unity  v2.5.0
Create characters in Unity with LLMs!
Loading...
Searching...
No Matches
DBSearch.cs
Go to the documentation of this file.
1
3using System;
4using System.Collections.Generic;
5using Cloud.Unum.USearch;
6using System.IO.Compression;
7using UnityEngine;
8
9namespace LLMUnity
10{
16 [DefaultExecutionOrder(-2)]
17 public class DBSearch : SearchMethod
18 {
19 protected USearchIndex index;
21 [Tooltip("show/hide advanced options in the GameObject")]
22 [HideInInspector] public bool advancedOptions = false;
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.")]
31 [ModelAdvanced] public ulong connectivity = 32;
33 [Tooltip("The expansion factor used for index construction when adding vectors.")]
34 [ModelAdvanced] public ulong expansionAdd = 40;
36 [Tooltip("The expansion factor used for index construction during search operations.")]
37 [ModelAdvanced] public ulong expansionSearch = 16;
38
39 private Dictionary<int, (float[], string, List<int>)> incrementalSearchCache = new Dictionary<int, (float[], string, List<int>)>();
40
42 public new void Awake()
43 {
44 base.Awake();
45 if (!enabled) return;
46 InitIndex();
47 }
48
49 public void InitIndex()
50 {
51 index = new USearchIndex(metricKind, quantization, (ulong)llmEmbedder.llm.embeddingLength, connectivity, expansionAdd, expansionSearch, false);
52 }
53
54 protected override void AddInternal(int key, float[] embedding)
55 {
56 index.Add((ulong)key, embedding);
57 }
58
59 protected override void RemoveInternal(int key)
60 {
61 index.Remove((ulong)key);
62 }
63
64 protected int[] UlongToInt(ulong[] keys)
65 {
66 int[] intKeys = new int[keys.Length];
67 for (int i = 0; i < keys.Length; i++) intKeys[i] = (int)keys[i];
68 return intKeys;
69 }
70
71 public override int IncrementalSearch(float[] embedding, string group = "")
72 {
73 int key = nextIncrementalSearchKey++;
74 incrementalSearchCache[key] = (embedding, group, new List<int>());
75 return key;
76 }
77
78 public override ValueTuple<int[], float[], bool> IncrementalFetchKeys(int fetchKey, int k)
79 {
80 if (!incrementalSearchCache.ContainsKey(fetchKey)) throw new Exception($"There is no IncrementalSearch cached with this key: {fetchKey}");
81
82 (float[] embedding, string group, List<int> seenKeys) = incrementalSearchCache[fetchKey];
83
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);
86
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);
91
92 bool completed = intKeys.Length < k || seenKeys.Count == Count(group);
93 if (completed) IncrementalSearchComplete(fetchKey);
94 return (intKeys, distances, completed);
95 }
96
97 public override void IncrementalSearchComplete(int fetchKey)
98 {
99 incrementalSearchCache.Remove(fetchKey);
100 }
101
102 protected override void SaveInternal(ZipArchive archive)
103 {
104 index.Save(archive);
105 }
106
107 protected override void LoadInternal(ZipArchive archive)
108 {
109 index.Load(archive);
110 }
111
112 protected override void ClearInternal()
113 {
114 index.Dispose();
115 InitIndex();
116 incrementalSearchCache.Clear();
117 }
118
120 }
121}
Class implementing a search with a vector database. The search results are retrieved with Approximate...
Definition DBSearch.cs:18
ScalarKind quantization
The quantisation type used for vector data during indexing.
Definition DBSearch.cs:25
MetricKind metricKind
The metric kind used for distance calculation between vectors.
Definition DBSearch.cs:28
ulong expansionSearch
The expansion factor used for index construction during search operations.
Definition DBSearch.cs:37
ulong connectivity
The connectivity parameter limits the connections-per-node in the graph.
Definition DBSearch.cs:31
ulong expansionAdd
The expansion factor used for index construction when adding vectors.
Definition DBSearch.cs:34
bool advancedOptions
show/hide advanced options in the GameObject
Definition DBSearch.cs:22
Class implementing the search method template.
Definition Search.cs:254
int Count()
Returns a count of the phrases.
ValueTuple< int[], float[], bool > IncrementalFetchKeys(int fetchKey, int k)
Retrieves the most similar search results in batches (incremental search). The phrase keys and distan...
void IncrementalSearchComplete(int fetchKey)
Completes the search and clears the cached results for an incremental search.
Task< int > IncrementalSearch(string queryString, string group="")
Allows to do search and retrieve results in batches (incremental search).