LLM for Unity  v2.4.2
Create characters in Unity with LLMs!
Loading...
Searching...
No Matches
RAG.cs
Go to the documentation of this file.
1
3using System;
4using System.IO.Compression;
5using System.Threading.Tasks;
6using UnityEditor;
7using UnityEngine;
8
9namespace LLMUnity
10{
14 public enum SearchMethods
15 {
18 }
19
20 public class NoChunking {}
21
32
37 [Serializable]
38 public class RAG : Searchable
39 {
41 [Tooltip("Search method type to use for RAG. SimpleSearch is a simple brute-force search, while DBSearch is a fast Approximate Nearest Neighbor (ANN) method (recommended!).")]
42 public SearchMethods searchType = SearchMethods.SimpleSearch;
44 [Tooltip("Search method GameObject.")]
47 [Tooltip("Chunking method type to use for RAG for splitting the inputs into chunks. This is useful to have a more consistent meaning within each data part.")]
50 [Tooltip("Chunking method GameObject.")]
52
59 public void Init(SearchMethods searchMethod = SearchMethods.SimpleSearch, ChunkingMethods chunkingMethod = ChunkingMethods.NoChunking, LLM llm = null)
60 {
61 searchType = searchMethod;
62 chunkingType = chunkingMethod;
63 UpdateGameObjects();
64 search.SetLLM(llm);
65 }
66
71 public void ReturnChunks(bool returnChunks)
72 {
73 if (chunking != null) chunking.ReturnChunks(returnChunks);
74 }
75
77 protected void ConstructSearch()
78 {
79 search = ConstructComponent<SearchMethod>(Type.GetType("LLMUnity." + searchType.ToString()), (previous, current) => current.llmEmbedder.llm = previous.llmEmbedder.llm);
80 if (chunking != null) chunking.SetSearch(search);
81 }
82
83 protected void ConstructChunking()
84 {
85 Type type = null;
86 if (chunkingType != ChunkingMethods.NoChunking) type = Type.GetType("LLMUnity." + chunkingType.ToString());
87 chunking = ConstructComponent<Chunking>(type);
88 if (chunking != null) chunking.SetSearch(search);
89 }
90
91 public override void UpdateGameObjects()
92 {
93 if (this == null) return;
94 ConstructSearch();
95 ConstructChunking();
96 }
97
98 protected Searchable GetSearcher()
99 {
100 if (chunking != null) return chunking;
101 if (search != null) return search;
102 throw new Exception("The search GameObject is null");
103 }
104
105#if UNITY_EDITOR
106 private void OnValidateUpdate()
107 {
108 EditorApplication.delayCall -= OnValidateUpdate;
109 UpdateGameObjects();
110 }
111
112 public virtual void OnValidate()
113 {
114 if (!Application.isPlaying) EditorApplication.delayCall += OnValidateUpdate;
115 }
116
117#endif
118
119 public override string Get(int key) { return GetSearcher().Get(key); }
120 public override async Task<int> Add(string inputString, string group = "") { return await GetSearcher().Add(inputString, group); }
121 public override int Remove(string inputString, string group = "") { return GetSearcher().Remove(inputString, group); }
122 public override void Remove(int key) { GetSearcher().Remove(key); }
123 public override int Count() { return GetSearcher().Count(); }
124 public override int Count(string group) { return GetSearcher().Count(group); }
125 public override void Clear() { GetSearcher().Clear(); }
126 public override async Task<int> IncrementalSearch(string queryString, string group = "") { return await GetSearcher().IncrementalSearch(queryString, group);}
127 public override (string[], float[], bool) IncrementalFetch(int fetchKey, int k) { return GetSearcher().IncrementalFetch(fetchKey, k);}
128 public override (int[], float[], bool) IncrementalFetchKeys(int fetchKey, int k) { return GetSearcher().IncrementalFetchKeys(fetchKey, k);}
129 public override void IncrementalSearchComplete(int fetchKey) { GetSearcher().IncrementalSearchComplete(fetchKey);}
130 public override void Save(ZipArchive archive) { GetSearcher().Save(archive); }
131 public override void Load(ZipArchive archive) { GetSearcher().Load(archive); }
133 }
134}
Class implementing the chunking functionality.
Definition Chunking.cs:18
void ReturnChunks(bool returnChunks)
Set to true to return chunks or the direct input with the Search function.
Definition Chunking.cs:30
Class implementing a search with a vector database. The search results are retrieved with Approximate...
Definition DBSearch.cs:18
Class implementing the LLM server.
Definition LLM.cs:19
Class implementing a Retrieval Augmented Generation (RAG) system based on a search method and an opti...
Definition RAG.cs:39
SearchMethod search
Search method GameObject.
Definition RAG.cs:45
void Init(SearchMethods searchMethod=SearchMethods.SimpleSearch, ChunkingMethods chunkingMethod=ChunkingMethods.NoChunking, LLM llm=null)
Constructs the Retrieval Augmented Generation (RAG) system based on the provided search and chunking ...
Definition RAG.cs:59
SearchMethods searchType
Search method type to use for RAG. SimpleSearch is a simple brute-force search, while DBSearch is a f...
Definition RAG.cs:42
ChunkingMethods chunkingType
Chunking method type to use for RAG for splitting the inputs into chunks. This is useful to have a mo...
Definition RAG.cs:48
Chunking chunking
Chunking method GameObject.
Definition RAG.cs:51
void ReturnChunks(bool returnChunks)
Set to true to return chunks or the direct input with the Search function.
Definition RAG.cs:71
Class implementing the search method template.
Definition Search.cs:254
void SetLLM(LLM llm)
Sets the LLM for encoding the search entries.
Definition Search.cs:274
void SetSearch(SearchMethod search)
Sets the search method of the plugin.
Definition Search.cs:469
Class implementing the search template.
Definition Search.cs:22
int Remove(string inputString, string group="")
Removes a phrase from the search.
Task< int > Add(string inputString, string group="")
Adds a phrase to the search.
int Count()
Returns a count of the phrases.
void Clear()
Clears the search object.
ValueTuple< int[], float[], bool > IncrementalFetchKeys(int fetchKey, int k)
Retrieves the most similar search results in batches (incremental search). The phrase keys and distan...
virtual ValueTuple< string[], float[], bool > IncrementalFetch(int fetchKey, int k)
Retrieves the most similar search results in batches (incremental search). The most similar results a...
Definition Search.cs:137
void IncrementalSearchComplete(int fetchKey)
Completes the search and clears the cached results for an incremental search.
string Get(int key)
Retrieves the phrase with the specific id.
void Save(string filePath)
Saves the state of the search object.
Definition Search.cs:149
async Task< bool > Load(string filePath)
Loads the state of the search object.
Definition Search.cs:166
Task< int > IncrementalSearch(string queryString, string group="")
Allows to do search and retrieve results in batches (incremental search).
Class implementing a sentence-based splitter.
Class implementing a simple search that compares the enconding of the search query with all the searc...
Class implementing a token-based splitter.
Class implementing a word-based splitter.
SearchMethods
Search methods implemented in LLMUnity.
Definition RAG.cs:15
ChunkingMethods
Chunking methods implemented in LLMUnity.
Definition RAG.cs:26