LLM for Unity  v2.3.0
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 {
40 public SearchMethods searchType = SearchMethods.SimpleSearch;
41 public SearchMethod search;
42 public ChunkingMethods chunkingType = ChunkingMethods.NoChunking;
43 public Chunking chunking;
44
51 public void Init(SearchMethods searchMethod = SearchMethods.SimpleSearch, ChunkingMethods chunkingMethod = ChunkingMethods.NoChunking, LLM llm = null)
52 {
53 searchType = searchMethod;
54 chunkingType = chunkingMethod;
55 UpdateGameObjects();
56 search.SetLLM(llm);
57 }
58
63 public void ReturnChunks(bool returnChunks)
64 {
65 if (chunking != null) chunking.ReturnChunks(returnChunks);
66 }
67
69 protected void ConstructSearch()
70 {
71 search = ConstructComponent<SearchMethod>(Type.GetType("LLMUnity." + searchType.ToString()), (previous, current) => current.llmEmbedder.llm = previous.llmEmbedder.llm);
72 if (chunking != null) chunking.SetSearch(search);
73 }
74
75 protected void ConstructChunking()
76 {
77 Type type = null;
78 if (chunkingType != ChunkingMethods.NoChunking) type = Type.GetType("LLMUnity." + chunkingType.ToString());
79 chunking = ConstructComponent<Chunking>(type);
80 if (chunking != null) chunking.SetSearch(search);
81 }
82
83 public override void UpdateGameObjects()
84 {
85 if (this == null) return;
86 ConstructSearch();
87 ConstructChunking();
88 }
89
90 protected Searchable GetSearcher()
91 {
92 if (chunking != null) return chunking;
93 if (search != null) return search;
94 throw new Exception("The search GameObject is null");
95 }
96
97#if UNITY_EDITOR
98 private void OnValidateUpdate()
99 {
100 EditorApplication.delayCall -= OnValidateUpdate;
101 UpdateGameObjects();
102 }
103
104 public virtual void OnValidate()
105 {
106 if (!Application.isPlaying) EditorApplication.delayCall += OnValidateUpdate;
107 }
108
109#endif
110
111 public override string Get(int key) { return GetSearcher().Get(key); }
112 public override async Task<int> Add(string inputString, string group = "") { return await GetSearcher().Add(inputString, group); }
113 public override int Remove(string inputString, string group = "") { return GetSearcher().Remove(inputString, group); }
114 public override void Remove(int key) { GetSearcher().Remove(key); }
115 public override int Count() { return GetSearcher().Count(); }
116 public override int Count(string group) { return GetSearcher().Count(group); }
117 public override void Clear() { GetSearcher().Clear(); }
118 public override async Task<int> IncrementalSearch(string queryString, string group = "") { return await GetSearcher().IncrementalSearch(queryString, group);}
119 public override (string[], float[], bool) IncrementalFetch(int fetchKey, int k) { return GetSearcher().IncrementalFetch(fetchKey, k);}
120 public override (int[], float[], bool) IncrementalFetchKeys(int fetchKey, int k) { return GetSearcher().IncrementalFetchKeys(fetchKey, k);}
121 public override void IncrementalSearchComplete(int fetchKey) { GetSearcher().IncrementalSearchComplete(fetchKey);}
122 public override void Save(ZipArchive archive) { GetSearcher().Save(archive); }
123 public override void Load(ZipArchive archive) { GetSearcher().Load(archive); }
125 }
126}
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
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:51
void ReturnChunks(bool returnChunks)
Set to true to return chunks or the direct input with the Search function.
Definition RAG.cs:63
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