LLM for Unity  v2.3.0
Create characters in Unity with LLMs!
Loading...
Searching...
No Matches
SimpleSearch.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.IO.Compression;
5using UnityEngine;
6
7namespace LLMUnity
8{
13 [DefaultExecutionOrder(-2)]
15 {
17 protected SortedDictionary<int, float[]> embeddings = new SortedDictionary<int, float[]>();
18 protected Dictionary<int, List<(int, float)>> incrementalSearchCache = new Dictionary<int, List<(int, float)>>();
19
20 protected override void AddInternal(int key, float[] embedding)
21 {
22 embeddings[key] = embedding;
23 }
24
25 protected override void RemoveInternal(int key)
26 {
27 embeddings.Remove(key);
28 }
29
30 public override int IncrementalSearch(float[] embedding, string group = "")
31 {
32 int key = nextIncrementalSearchKey++;
33
34 List<(int, float)> sortedLists = new List<(int, float)>();
35 if (dataSplits.TryGetValue(group, out List<int> dataSplit))
36 {
37 if (dataSplit.Count >= 0)
38 {
39 float[][] embeddingsSplit = new float[dataSplit.Count][];
40 for (int i = 0; i < dataSplit.Count; i++) embeddingsSplit[i] = embeddings[dataSplit[i]];
41
42 float[] unsortedDistances = InverseDotProduct(embedding, embeddingsSplit);
43 sortedLists = dataSplit.Zip(unsortedDistances, (first, second) => (first, second))
44 .OrderBy(item => item.Item2)
45 .ToList();
46 }
47 }
48 incrementalSearchCache[key] = sortedLists;
49 return key;
50 }
51
52 public override ValueTuple<int[], float[], bool> IncrementalFetchKeys(int fetchKey, int k)
53 {
54 if (!incrementalSearchCache.ContainsKey(fetchKey)) throw new Exception($"There is no IncrementalSearch cached with this key: {fetchKey}");
55
56 bool completed;
57 List<(int, float)> sortedLists;
58 if (k == -1)
59 {
60 sortedLists = incrementalSearchCache[fetchKey];
61 completed = true;
62 }
63 else
64 {
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;
69 }
70 if (completed) IncrementalSearchComplete(fetchKey);
71
72 int[] results = new int[sortedLists.Count];
73 float[] distances = new float[sortedLists.Count];
74 for (int i = 0; i < sortedLists.Count; i++)
75 {
76 results[i] = sortedLists[i].Item1;
77 distances[i] = sortedLists[i].Item2;
78 }
79 return (results.ToArray(), distances.ToArray(), completed);
80 }
81
82 public override void IncrementalSearchComplete(int fetchKey)
83 {
84 incrementalSearchCache.Remove(fetchKey);
85 }
86
87 protected override void ClearInternal()
88 {
89 embeddings.Clear();
90 incrementalSearchCache.Clear();
91 }
92
93 protected override void SaveInternal(ZipArchive archive)
94 {
95 ArchiveSaver.Save(archive, embeddings, GetSavePath("embeddings"));
96 ArchiveSaver.Save(archive, incrementalSearchCache, GetSavePath("incrementalSearchCache"));
97 }
98
99 protected override void LoadInternal(ZipArchive archive)
100 {
101 embeddings = ArchiveSaver.Load<SortedDictionary<int, float[]>>(archive, GetSavePath("embeddings"));
102 incrementalSearchCache = ArchiveSaver.Load<Dictionary<int, List<(int, float)>>>(archive, GetSavePath("incrementalSearchCache"));
103 }
104
106 }
107}
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).
Class implementing a simple search that compares the enconding of the search query with all the searc...