LLM for Unity  v2.3.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 [HideInInspector] public bool advancedOptions = false;
23 [ModelAdvanced] public ScalarKind quantization = ScalarKind.Float16;
25 [ModelAdvanced] public MetricKind metricKind = MetricKind.Cos;
27 [ModelAdvanced] public ulong connectivity = 32;
29 [ModelAdvanced] public ulong expansionAdd = 40;
31 [ModelAdvanced] public ulong expansionSearch = 16;
32
33 private Dictionary<int, (float[], string, List<int>)> incrementalSearchCache = new Dictionary<int, (float[], string, List<int>)>();
34
36 public new void Awake()
37 {
38 base.Awake();
39 if (!enabled) return;
40 InitIndex();
41 }
42
43 public void InitIndex()
44 {
45 index = new USearchIndex(metricKind, quantization, (ulong)llmEmbedder.llm.embeddingLength, connectivity, expansionAdd, expansionSearch, false);
46 }
47
48 protected override void AddInternal(int key, float[] embedding)
49 {
50 index.Add((ulong)key, embedding);
51 }
52
53 protected override void RemoveInternal(int key)
54 {
55 index.Remove((ulong)key);
56 }
57
58 protected int[] UlongToInt(ulong[] keys)
59 {
60 int[] intKeys = new int[keys.Length];
61 for (int i = 0; i < keys.Length; i++) intKeys[i] = (int)keys[i];
62 return intKeys;
63 }
64
65 public override int IncrementalSearch(float[] embedding, string group = "")
66 {
67 int key = nextIncrementalSearchKey++;
68 incrementalSearchCache[key] = (embedding, group, new List<int>());
69 return key;
70 }
71
72 public override ValueTuple<int[], float[], bool> IncrementalFetchKeys(int fetchKey, int k)
73 {
74 if (!incrementalSearchCache.ContainsKey(fetchKey)) throw new Exception($"There is no IncrementalSearch cached with this key: {fetchKey}");
75
76 (float[] embedding, string group, List<int> seenKeys) = incrementalSearchCache[fetchKey];
77
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);
80
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);
85
86 bool completed = intKeys.Length < k || seenKeys.Count == Count(group);
87 if (completed) IncrementalSearchComplete(fetchKey);
88 return (intKeys, distances, completed);
89 }
90
91 public override void IncrementalSearchComplete(int fetchKey)
92 {
93 incrementalSearchCache.Remove(fetchKey);
94 }
95
96 protected override void SaveInternal(ZipArchive archive)
97 {
98 index.Save(archive);
99 }
100
101 protected override void LoadInternal(ZipArchive archive)
102 {
103 index.Load(archive);
104 }
105
106 protected override void ClearInternal()
107 {
108 index.Dispose();
109 InitIndex();
110 incrementalSearchCache.Clear();
111 }
112
114 }
115}
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:23
MetricKind metricKind
The metric kind used for distance calculation between vectors.
Definition DBSearch.cs:25
ulong expansionSearch
The expansion factor used for index construction during search operations.
Definition DBSearch.cs:31
ulong connectivity
The connectivity parameter limits the connections-per-node in the graph.
Definition DBSearch.cs:27
ulong expansionAdd
The expansion factor used for index construction when adding vectors.
Definition DBSearch.cs:29
bool advancedOptions
toggle to show/hide advanced options in the GameObject
Definition DBSearch.cs:21
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).