LLM for Unity  v2.3.0
Create characters in Unity with LLMs!
Loading...
Searching...
No Matches
LLM.cs
Go to the documentation of this file.
1
3using System;
4using System.Collections.Generic;
5using System.IO;
6using System.Threading;
7using System.Threading.Tasks;
8using UnityEditor;
9using UnityEngine;
10
11namespace LLMUnity
12{
13 [DefaultExecutionOrder(-1)]
18 public class LLM : MonoBehaviour
19 {
21 [HideInInspector] public bool advancedOptions = false;
23 [LocalRemote] public bool remote = false;
25 [Remote] public int port = 13333;
27 [LLM] public int numThreads = -1;
31 [LLM] public int numGPULayers = 0;
33 [LLM] public bool debug = false;
35 [LLMAdvanced] public int parallelPrompts = -1;
37 [LLMAdvanced] public bool dontDestroyOnLoad = true;
40 [DynamicRange("minContextLength", "maxContextLength", false), Model] public int contextSize = 8192;
42 [ModelAdvanced] public int batchSize = 512;
44 [TextArea(5, 10), ChatAdvanced] public string basePrompt = "";
46 public bool started { get; protected set; } = false;
48 public bool failed { get; protected set; } = false;
50 public static bool modelSetupFailed { get; protected set; } = false;
52 public static bool modelSetupComplete { get; protected set; } = false;
53
56 [ModelAdvanced] public string model = "";
58 [ModelAdvanced] public string chatTemplate = ChatTemplate.DefaultTemplate;
61 [ModelAdvanced] public string lora = "";
63 [ModelAdvanced] public string loraWeights = "";
65 [ModelExtras] public bool flashAttention = false;
66
68 public string APIKey;
69 // SSL certificate
70 [SerializeField]
71 private string SSLCert = "";
72 public string SSLCertPath = "";
73 // SSL key
74 [SerializeField]
75 private string SSLKey = "";
76 public string SSLKeyPath = "";
77
79 public int minContextLength = 0;
80 public int maxContextLength = 0;
81
82 IntPtr LLMObject = IntPtr.Zero;
83 List<LLMCaller> clients = new List<LLMCaller>();
84 LLMLib llmlib;
85 StreamWrapper logStreamWrapper = null;
86 Thread llmThread = null;
87 List<StreamWrapper> streamWrappers = new List<StreamWrapper>();
88 public LLMManager llmManager = new LLMManager();
89 private readonly object startLock = new object();
90 static readonly object staticLock = new object();
91 public LoraManager loraManager = new LoraManager();
92 string loraPre = "";
93 string loraWeightsPre = "";
94 public bool embeddingsOnly = false;
95 public int embeddingLength = 0;
96
98
99 public LLM()
100 {
101 LLMManager.Register(this);
102 }
103
104 void OnValidate()
105 {
106 if (lora != loraPre || loraWeights != loraWeightsPre)
107 {
108 loraManager.FromStrings(lora, loraWeights);
109 (loraPre, loraWeightsPre) = (lora, loraWeights);
110 }
111 }
112
116 public async void Awake()
117 {
118 if (!enabled) return;
119#if !UNITY_EDITOR
121#endif
122 modelSetupComplete = true;
124 {
125 failed = true;
126 return;
127 }
128 string arguments = GetLlamaccpArguments();
129 if (arguments == null)
130 {
131 failed = true;
132 return;
133 }
134 await Task.Run(() => StartLLMServer(arguments));
135 if (!started) return;
136 if (dontDestroyOnLoad) DontDestroyOnLoad(transform.root.gameObject);
137 if (basePrompt != "") await SetBasePrompt(basePrompt);
138 }
139
143 public async Task WaitUntilReady()
144 {
145 while (!started) await Task.Yield();
146 }
147
152 public static async Task<bool> WaitUntilModelSetup(Callback<float> downloadProgressCallback = null)
153 {
154 if (downloadProgressCallback != null) LLMManager.downloadProgressCallbacks.Add(downloadProgressCallback);
155 while (!modelSetupComplete) await Task.Yield();
156 return !modelSetupFailed;
157 }
158
160 public static string GetLLMManagerAsset(string path)
161 {
162#if UNITY_EDITOR
163 if (!EditorApplication.isPlaying) return GetLLMManagerAssetEditor(path);
164#endif
165 return GetLLMManagerAssetRuntime(path);
166 }
167
168 public static string GetLLMManagerAssetEditor(string path)
169 {
170 // empty
171 if (string.IsNullOrEmpty(path)) return path;
172 // LLMManager - return location the file will be stored in StreamingAssets
173 ModelEntry modelEntry = LLMManager.Get(path);
174 if (modelEntry != null) return modelEntry.filename;
175 // StreamingAssets - return relative location within StreamingAssets
176 string assetPath = LLMUnitySetup.GetAssetPath(path); // Note: this will return the full path if a full path is passed
177 string basePath = LLMUnitySetup.GetAssetPath();
178 if (File.Exists(assetPath))
179 {
180 if (LLMUnitySetup.IsSubPath(assetPath, basePath)) return LLMUnitySetup.RelativePath(assetPath, basePath);
181 }
182 // full path
183 if (!File.Exists(assetPath))
184 {
185 LLMUnitySetup.LogError($"Model {path} was not found.");
186 }
187 else
188 {
189 string errorMessage = $"The model {path} was loaded locally. You can include it in the build in one of these ways:";
190 errorMessage += $"\n-Copy the model inside the StreamingAssets folder and use its StreamingAssets path";
191 errorMessage += $"\n-Load the model with the model manager inside the LLM GameObject and use its filename";
192 LLMUnitySetup.LogWarning(errorMessage);
193 }
194 return path;
195 }
196
197 public static string GetLLMManagerAssetRuntime(string path)
198 {
199 // empty
200 if (string.IsNullOrEmpty(path)) return path;
201 // LLMManager
202 string managerPath = LLMManager.GetAssetPath(path);
203 if (!string.IsNullOrEmpty(managerPath) && File.Exists(managerPath)) return managerPath;
204 // StreamingAssets
205 string assetPath = LLMUnitySetup.GetAssetPath(path);
206 if (File.Exists(assetPath)) return assetPath;
207 // give up
208 return path;
209 }
210
212
219 public void SetModel(string path)
220 {
221 model = GetLLMManagerAsset(path);
222 if (!string.IsNullOrEmpty(model))
223 {
224 ModelEntry modelEntry = LLMManager.Get(model);
225 if (modelEntry == null) modelEntry = new ModelEntry(GetLLMManagerAssetRuntime(model));
226 SetTemplate(modelEntry.chatTemplate);
227
228 maxContextLength = modelEntry.contextLength;
229 if (contextSize > maxContextLength) contextSize = maxContextLength;
230 SetEmbeddings(modelEntry.embeddingLength, modelEntry.embeddingOnly);
231 if (contextSize == 0 && modelEntry.contextLength > 32768)
232 {
233 LLMUnitySetup.LogWarning($"The model {path} has very large context size ({modelEntry.contextLength}), consider setting it to a smaller value (<=32768) to avoid filling up the RAM");
234 }
235 }
236#if UNITY_EDITOR
237 if (!EditorApplication.isPlaying) EditorUtility.SetDirty(this);
238#endif
239 }
240
247 public void SetLora(string path, float weight = 1)
248 {
249 AssertNotStarted();
250 loraManager.Clear();
251 AddLora(path, weight);
252 }
253
260 public void AddLora(string path, float weight = 1)
261 {
262 AssertNotStarted();
263 loraManager.Add(path, weight);
264 UpdateLoras();
265 }
266
272 public void RemoveLora(string path)
273 {
274 AssertNotStarted();
275 loraManager.Remove(path);
276 UpdateLoras();
277 }
278
282 public void RemoveLoras()
283 {
284 AssertNotStarted();
285 loraManager.Clear();
286 UpdateLoras();
287 }
288
294 public void SetLoraWeight(string path, float weight)
295 {
296 loraManager.SetWeight(path, weight);
297 UpdateLoras();
298 if (started) ApplyLoras();
299 }
300
305 public void SetLoraWeights(Dictionary<string, float> loraToWeight)
306 {
307 foreach (KeyValuePair<string, float> entry in loraToWeight) loraManager.SetWeight(entry.Key, entry.Value);
308 UpdateLoras();
309 if (started) ApplyLoras();
310 }
311
312 public void UpdateLoras()
313 {
314 (lora, loraWeights) = loraManager.ToStrings();
315 (loraPre, loraWeightsPre) = (lora, loraWeights);
316#if UNITY_EDITOR
317 if (!EditorApplication.isPlaying) EditorUtility.SetDirty(this);
318#endif
319 }
320
325 public void SetTemplate(string templateName, bool setDirty = true)
326 {
327 chatTemplate = templateName;
328 if (started) llmlib?.LLM_SetTemplate(LLMObject, chatTemplate);
329#if UNITY_EDITOR
330 if (setDirty && !EditorApplication.isPlaying) EditorUtility.SetDirty(this);
331#endif
332 }
333
339 public void SetEmbeddings(int embeddingLength, bool embeddingsOnly)
340 {
341 this.embeddingsOnly = embeddingsOnly;
342 this.embeddingLength = embeddingLength;
343#if UNITY_EDITOR
344 if (!EditorApplication.isPlaying) EditorUtility.SetDirty(this);
345#endif
346 }
347
349
350 string ReadFileContents(string path)
351 {
352 if (String.IsNullOrEmpty(path)) return "";
353 else if (!File.Exists(path))
354 {
355 LLMUnitySetup.LogError($"File {path} not found!");
356 return "";
357 }
358 return File.ReadAllText(path);
359 }
360
362
367 public void SetSSLCert(string path)
368 {
369 SSLCertPath = path;
370 SSLCert = ReadFileContents(path);
371 }
372
377 public void SetSSLKey(string path)
378 {
379 SSLKeyPath = path;
380 SSLKey = ReadFileContents(path);
381 }
382
387 public string GetTemplate()
388 {
389 return chatTemplate;
390 }
391
392 protected virtual string GetLlamaccpArguments()
393 {
394 // Start the LLM server in a cross-platform way
395 if ((SSLCert != "" && SSLKey == "") || (SSLCert == "" && SSLKey != ""))
396 {
397 LLMUnitySetup.LogError($"Both SSL certificate and key need to be provided!");
398 return null;
399 }
400
401 if (model == "")
402 {
403 LLMUnitySetup.LogError("No model file provided!");
404 return null;
405 }
406 string modelPath = GetLLMManagerAssetRuntime(model);
407 if (!File.Exists(modelPath))
408 {
409 LLMUnitySetup.LogError($"File {modelPath} not found!");
410 return null;
411 }
412
413 loraManager.FromStrings(lora, loraWeights);
414 string loraArgument = "";
415 foreach (string lora in loraManager.GetLoras())
416 {
417 string loraPath = GetLLMManagerAssetRuntime(lora);
418 if (!File.Exists(loraPath))
419 {
420 LLMUnitySetup.LogError($"File {loraPath} not found!");
421 return null;
422 }
423 loraArgument += $" --lora \"{loraPath}\"";
424 }
425
426 int numThreadsToUse = numThreads;
427 if (Application.platform == RuntimePlatform.Android && numThreads <= 0) numThreadsToUse = LLMUnitySetup.AndroidGetNumBigCores();
428
429 int slots = GetNumClients();
430 string arguments = $"-m \"{modelPath}\" -c {contextSize} -b {batchSize} --log-disable -np {slots}";
431 if (embeddingsOnly) arguments += " --embedding";
432 if (numThreadsToUse > 0) arguments += $" -t {numThreadsToUse}";
433 arguments += loraArgument;
434 arguments += $" -ngl {numGPULayers}";
435 if (LLMUnitySetup.FullLlamaLib && flashAttention) arguments += $" --flash-attn";
436 if (remote)
437 {
438 arguments += $" --port {port} --host 0.0.0.0";
439 if (!String.IsNullOrEmpty(APIKey)) arguments += $" --api-key {APIKey}";
440 }
441
442 // the following is the equivalent for running from command line
443 string serverCommand;
444 if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer) serverCommand = "undreamai_server.exe";
445 else serverCommand = "./undreamai_server";
446 serverCommand += " " + arguments;
447 serverCommand += $" --template \"{chatTemplate}\"";
448 if (remote && SSLCert != "" && SSLKey != "") serverCommand += $" --ssl-cert-file {SSLCertPath} --ssl-key-file {SSLKeyPath}";
449 LLMUnitySetup.Log($"Deploy server command: {serverCommand}");
450 return arguments;
451 }
452
453 private void SetupLogging()
454 {
455 logStreamWrapper = ConstructStreamWrapper(LLMUnitySetup.LogWarning, true);
456 llmlib?.Logging(logStreamWrapper.GetStringWrapper());
457 }
458
459 private void StopLogging()
460 {
461 if (logStreamWrapper == null) return;
462 llmlib?.StopLogging();
463 DestroyStreamWrapper(logStreamWrapper);
464 }
465
466 private void StartLLMServer(string arguments)
467 {
468 started = false;
469 failed = false;
470 bool useGPU = numGPULayers > 0;
471
472 foreach (string arch in LLMLib.PossibleArchitectures(useGPU))
473 {
474 string error;
475 try
476 {
477 InitLib(arch);
478 InitService(arguments);
479 LLMUnitySetup.Log($"Using architecture: {arch}");
480 break;
481 }
482 catch (LLMException e)
483 {
484 error = e.Message;
485 Destroy();
486 }
487 catch (DestroyException)
488 {
489 break;
490 }
491 catch (Exception e)
492 {
493 error = $"{e.GetType()}: {e.Message}";
494 }
495 LLMUnitySetup.Log($"Tried architecture: {arch}, " + error);
496 }
497 if (llmlib == null)
498 {
499 LLMUnitySetup.LogError("LLM service couldn't be created");
500 failed = true;
501 return;
502 }
503 CallWithLock(StartService);
504 LLMUnitySetup.Log("LLM service created");
505 }
506
507 private void InitLib(string arch)
508 {
509 llmlib = new LLMLib(arch);
510 CheckLLMStatus(false);
511 }
512
513 void CallWithLock(EmptyCallback fn)
514 {
515 lock (startLock)
516 {
517 if (llmlib == null) throw new DestroyException();
518 fn();
519 }
520 }
521
522 private void InitService(string arguments)
523 {
524 lock (staticLock)
525 {
526 if (debug) CallWithLock(SetupLogging);
527 CallWithLock(() => { LLMObject = llmlib.LLM_Construct(arguments); });
528 CallWithLock(() => llmlib.LLM_SetTemplate(LLMObject, chatTemplate));
529 if (remote)
530 {
531 if (SSLCert != "" && SSLKey != "")
532 {
533 LLMUnitySetup.Log("Using SSL");
534 CallWithLock(() => llmlib.LLM_SetSSL(LLMObject, SSLCert, SSLKey));
535 }
536 CallWithLock(() => llmlib.LLM_StartServer(LLMObject));
537 }
538 CallWithLock(() => CheckLLMStatus(false));
539 }
540 }
541
542 private void StartService()
543 {
544 llmThread = new Thread(() => llmlib.LLM_Start(LLMObject));
545 llmThread.Start();
546 while (!llmlib.LLM_Started(LLMObject)) {}
547 ApplyLoras();
548 started = true;
549 }
550
557 public int Register(LLMCaller llmCaller)
558 {
559 clients.Add(llmCaller);
560 int index = clients.IndexOf(llmCaller);
561 if (parallelPrompts != -1) return index % parallelPrompts;
562 return index;
563 }
564
565 protected int GetNumClients()
566 {
567 return Math.Max(parallelPrompts == -1 ? clients.Count : parallelPrompts, 1);
568 }
569
571 public delegate void LLMStatusCallback(IntPtr LLMObject, IntPtr stringWrapper);
572 public delegate void LLMNoInputReplyCallback(IntPtr LLMObject, IntPtr stringWrapper);
573 public delegate void LLMReplyCallback(IntPtr LLMObject, string json_data, IntPtr stringWrapper);
575
576 StreamWrapper ConstructStreamWrapper(Callback<string> streamCallback = null, bool clearOnUpdate = false)
577 {
578 StreamWrapper streamWrapper = new StreamWrapper(llmlib, streamCallback, clearOnUpdate);
579 streamWrappers.Add(streamWrapper);
580 return streamWrapper;
581 }
582
583 void DestroyStreamWrapper(StreamWrapper streamWrapper)
584 {
585 streamWrappers.Remove(streamWrapper);
586 streamWrapper.Destroy();
587 }
588
591 public void Update()
592 {
593 foreach (StreamWrapper streamWrapper in streamWrappers) streamWrapper.Update();
594 }
595
596 void AssertStarted()
597 {
598 string error = null;
599 if (failed) error = "LLM service couldn't be created";
600 else if (!started) error = "LLM service not started";
601 if (error != null)
602 {
603 LLMUnitySetup.LogError(error);
604 throw new Exception(error);
605 }
606 }
607
608 void AssertNotStarted()
609 {
610 if (started)
611 {
612 string error = "This method can't be called when the LLM has started";
613 LLMUnitySetup.LogError(error);
614 throw new Exception(error);
615 }
616 }
617
618 void CheckLLMStatus(bool log = true)
619 {
620 if (llmlib == null) { return; }
621 IntPtr stringWrapper = llmlib.StringWrapper_Construct();
622 int status = llmlib.LLM_Status(LLMObject, stringWrapper);
623 string result = llmlib.GetStringWrapperResult(stringWrapper);
624 llmlib.StringWrapper_Delete(stringWrapper);
625 string message = $"LLM {status}: {result}";
626 if (status > 0)
627 {
628 if (log) LLMUnitySetup.LogError(message);
629 throw new LLMException(message, status);
630 }
631 else if (status < 0)
632 {
633 if (log) LLMUnitySetup.LogWarning(message);
634 }
635 }
636
637 async Task<string> LLMNoInputReply(LLMNoInputReplyCallback callback)
638 {
639 AssertStarted();
640 IntPtr stringWrapper = llmlib.StringWrapper_Construct();
641 await Task.Run(() => callback(LLMObject, stringWrapper));
642 string result = llmlib?.GetStringWrapperResult(stringWrapper);
643 llmlib?.StringWrapper_Delete(stringWrapper);
644 CheckLLMStatus();
645 return result;
646 }
647
648 async Task<string> LLMReply(LLMReplyCallback callback, string json)
649 {
650 AssertStarted();
651 IntPtr stringWrapper = llmlib.StringWrapper_Construct();
652 await Task.Run(() => callback(LLMObject, json, stringWrapper));
653 string result = llmlib?.GetStringWrapperResult(stringWrapper);
654 llmlib?.StringWrapper_Delete(stringWrapper);
655 CheckLLMStatus();
656 return result;
657 }
658
664 public async Task<string> Tokenize(string json)
665 {
666 AssertStarted();
667 LLMReplyCallback callback = (IntPtr LLMObject, string jsonData, IntPtr strWrapper) =>
668 {
669 llmlib.LLM_Tokenize(LLMObject, jsonData, strWrapper);
670 };
671 return await LLMReply(callback, json);
672 }
673
679 public async Task<string> Detokenize(string json)
680 {
681 AssertStarted();
682 LLMReplyCallback callback = (IntPtr LLMObject, string jsonData, IntPtr strWrapper) =>
683 {
684 llmlib.LLM_Detokenize(LLMObject, jsonData, strWrapper);
685 };
686 return await LLMReply(callback, json);
687 }
688
694 public async Task<string> Embeddings(string json)
695 {
696 AssertStarted();
697 LLMReplyCallback callback = (IntPtr LLMObject, string jsonData, IntPtr strWrapper) =>
698 {
699 llmlib.LLM_Embeddings(LLMObject, jsonData, strWrapper);
700 };
701 return await LLMReply(callback, json);
702 }
703
708 public void ApplyLoras()
709 {
710 LoraWeightRequestList loraWeightRequest = new LoraWeightRequestList();
711 loraWeightRequest.loraWeights = new List<LoraWeightRequest>();
712 float[] weights = loraManager.GetWeights();
713 for (int i = 0; i < weights.Length; i++)
714 {
715 loraWeightRequest.loraWeights.Add(new LoraWeightRequest() { id = i, scale = weights[i] });
716 }
717
718 string json = JsonUtility.ToJson(loraWeightRequest);
719 int startIndex = json.IndexOf("[");
720 int endIndex = json.LastIndexOf("]") + 1;
721 json = json.Substring(startIndex, endIndex - startIndex);
722
723 IntPtr stringWrapper = llmlib.StringWrapper_Construct();
724 llmlib.LLM_Lora_Weight(LLMObject, json, stringWrapper);
725 llmlib.StringWrapper_Delete(stringWrapper);
726 }
727
732 public async Task<List<LoraWeightResult>> ListLoras()
733 {
734 AssertStarted();
735 LLMNoInputReplyCallback callback = (IntPtr LLMObject, IntPtr strWrapper) =>
736 {
737 llmlib.LLM_LoraList(LLMObject, strWrapper);
738 };
739 string json = await LLMNoInputReply(callback);
740 if (String.IsNullOrEmpty(json)) return null;
741 LoraWeightResultList loraRequest = JsonUtility.FromJson<LoraWeightResultList>("{\"loraWeights\": " + json + "}");
742 return loraRequest.loraWeights;
743 }
744
750 public async Task<string> Slot(string json)
751 {
752 AssertStarted();
753 LLMReplyCallback callback = (IntPtr LLMObject, string jsonData, IntPtr strWrapper) =>
754 {
755 llmlib.LLM_Slot(LLMObject, jsonData, strWrapper);
756 };
757 return await LLMReply(callback, json);
758 }
759
766 public async Task<string> Completion(string json, Callback<string> streamCallback = null)
767 {
768 AssertStarted();
769 if (streamCallback == null) streamCallback = (string s) => {};
770 StreamWrapper streamWrapper = ConstructStreamWrapper(streamCallback);
771 await Task.Run(() => llmlib.LLM_Completion(LLMObject, json, streamWrapper.GetStringWrapper()));
772 if (!started) return null;
773 streamWrapper.Update();
774 string result = streamWrapper.GetString();
775 DestroyStreamWrapper(streamWrapper);
776 CheckLLMStatus();
777 return result;
778 }
779
780 public async Task SetBasePrompt(string base_prompt)
781 {
782 AssertStarted();
783 SystemPromptRequest request = new SystemPromptRequest() { system_prompt = base_prompt, prompt = " ", n_predict = 0 };
784 await Completion(JsonUtility.ToJson(request));
785 }
786
791 public void CancelRequest(int id_slot)
792 {
793 AssertStarted();
794 llmlib?.LLM_Cancel(LLMObject, id_slot);
795 CheckLLMStatus();
796 }
797
801 public void Destroy()
802 {
803 lock (staticLock)
804 lock (startLock)
805 {
806 try
807 {
808 if (llmlib != null)
809 {
810 if (LLMObject != IntPtr.Zero)
811 {
812 llmlib.LLM_Stop(LLMObject);
813 if (remote) llmlib.LLM_StopServer(LLMObject);
814 StopLogging();
815 llmThread?.Join();
816 llmlib.LLM_Delete(LLMObject);
817 LLMObject = IntPtr.Zero;
818 }
819 llmlib.Destroy();
820 llmlib = null;
821 }
822 started = false;
823 failed = false;
824 }
825 catch (Exception e)
826 {
827 LLMUnitySetup.LogError(e.Message);
828 }
829 }
830 }
831
836 public void OnDestroy()
837 {
838 Destroy();
840 }
841 }
842}
Class implementing the skeleton of a chat template.
static string DefaultTemplate
the default template used when it can't be determined ("chatml")
Class implementing calling of LLM functions (local and remote).
Definition LLMCaller.cs:17
Class implementing the LLM model manager.
static void Unregister(LLM llm)
Removes a LLM from the model manager.
static ModelEntry Get(string path)
Gets the model entry for a model path.
static Task< bool > Setup()
Setup of the models.
static void Register(LLM llm)
Registers a LLM to the model manager.
Class implementing helper functions for setup and process management.
Class implementing the LLM server.
Definition LLM.cs:19
int numGPULayers
number of model layers to offload to the GPU (0 = GPU not used). Use a large number i....
Definition LLM.cs:31
void ApplyLoras()
Sets the lora scale, only works after the LLM service has started.
Definition LLM.cs:708
async Task< string > Slot(string json)
Allows to save / restore the state of a slot.
Definition LLM.cs:750
void SetLoraWeights(Dictionary< string, float > loraToWeight)
Allows to change the weights (scale) of the LORA models in the LLM.
Definition LLM.cs:305
async Task< List< LoraWeightResult > > ListLoras()
Gets a list of the lora adapters.
Definition LLM.cs:732
static async Task< bool > WaitUntilModelSetup(Callback< float > downloadProgressCallback=null)
Allows to wait until the LLM models are downloaded and ready.
Definition LLM.cs:152
string GetTemplate()
Returns the chat template of the LLM.
Definition LLM.cs:387
void SetLoraWeight(string path, float weight)
Allows to change the weight (scale) of a LORA model in the LLM.
Definition LLM.cs:294
void CancelRequest(int id_slot)
Allows to cancel the requests in a specific slot of the LLM.
Definition LLM.cs:791
int parallelPrompts
number of prompts that can happen in parallel (-1 = number of LLMCaller objects)
Definition LLM.cs:35
bool debug
select to log the output of the LLM in the Unity Editor.
Definition LLM.cs:33
string basePrompt
a base prompt to use as a base for all LLMCaller objects
Definition LLM.cs:44
async void Awake()
The Unity Awake function that starts the LLM server.
Definition LLM.cs:116
async Task< string > Detokenize(string json)
Detokenises the provided query.
Definition LLM.cs:679
void OnDestroy()
The Unity OnDestroy function called when the onbject is destroyed. The function StopProcess is called...
Definition LLM.cs:836
void SetLora(string path, float weight=1)
Allows to set a LORA model to use in the LLM. The model provided is copied to the Assets/StreamingAss...
Definition LLM.cs:247
void AddLora(string path, float weight=1)
Allows to add a LORA model to use in the LLM. The model provided is copied to the Assets/StreamingAss...
Definition LLM.cs:260
bool advancedOptions
toggle to show/hide advanced options in the GameObject
Definition LLM.cs:21
void RemoveLora(string path)
Allows to remove a LORA model from the LLM. Models supported are in .gguf format.
Definition LLM.cs:272
static bool modelSetupFailed
Boolean set to true if the models were not downloaded successfully.
Definition LLM.cs:50
string lora
the paths of the LORA models being used (relative to the Assets/StreamingAssets folder)....
Definition LLM.cs:61
int contextSize
Size of the prompt context (0 = context size of the model). This is the number of tokens the model ca...
Definition LLM.cs:40
int numThreads
number of threads to use (-1 = all)
Definition LLM.cs:27
bool started
Boolean set to true if the server has started and is ready to receive requests, false otherwise.
Definition LLM.cs:46
void SetModel(string path)
Allows to set the model used by the LLM. The model provided is copied to the Assets/StreamingAssets f...
Definition LLM.cs:219
int port
port to use for the LLM server
Definition LLM.cs:25
bool remote
toggle to enable remote server functionality
Definition LLM.cs:23
void SetSSLCert(string path)
Use a SSL certificate for the LLM server.
Definition LLM.cs:367
void RemoveLoras()
Allows to remove all LORA models from the LLM.
Definition LLM.cs:282
bool dontDestroyOnLoad
select to not destroy the LLM GameObject when loading a new Scene.
Definition LLM.cs:37
void SetEmbeddings(int embeddingLength, bool embeddingsOnly)
Set LLM Embedding parameters.
Definition LLM.cs:339
string model
the LLM model to use. Models with .gguf format are allowed.
Definition LLM.cs:56
string APIKey
API key to use for the server (optional)
Definition LLM.cs:68
async Task< string > Tokenize(string json)
Tokenises the provided query.
Definition LLM.cs:664
int batchSize
Batch size for prompt processing.
Definition LLM.cs:42
void SetSSLKey(string path)
Use a SSL key for the LLM server.
Definition LLM.cs:377
string chatTemplate
Chat template used for the model.
Definition LLM.cs:58
bool flashAttention
enable use of flash attention
Definition LLM.cs:65
async Task< string > Completion(string json, Callback< string > streamCallback=null)
Allows to use the chat and completion functionality of the LLM.
Definition LLM.cs:766
static bool modelSetupComplete
Boolean set to true if the server has started and is ready to receive requests, false otherwise.
Definition LLM.cs:52
async Task WaitUntilReady()
Allows to wait until the LLM is ready.
Definition LLM.cs:143
void SetTemplate(string templateName, bool setDirty=true)
Set the chat template for the LLM.
Definition LLM.cs:325
void Destroy()
Stops and destroys the LLM.
Definition LLM.cs:801
void Update()
The Unity Update function. It is used to retrieve the LLM replies.
Definition LLM.cs:591
string loraWeights
the weights of the LORA models being used.
Definition LLM.cs:63
int Register(LLMCaller llmCaller)
Registers a local LLMCaller object. This allows to bind the LLMCaller "client" to a specific slot of ...
Definition LLM.cs:557
bool failed
Boolean set to true if the server has failed to start.
Definition LLM.cs:48
async Task< string > Embeddings(string json)
Computes the embeddings of the provided query.
Definition LLM.cs:694
Class representing the LORA manager allowing to convert and retrieve LORA assets to string (for seria...
Definition LLMUtils.cs:56
float[] GetWeights()
Gets the weights of the LORAs in the manager.
Definition LLMUtils.cs:185
void Add(string path, float weight=1)
Adds a LORA with the defined weight.
Definition LLMUtils.cs:99
void Remove(string path)
Removes a LORA based on its path.
Definition LLMUtils.cs:109
void SetWeight(string path, float weight)
Modifies the weight of a LORA.
Definition LLMUtils.cs:120
void FromStrings(string loraString, string loraWeightsString)
Converts strings with the lora paths and weights to entries in the LORA manager.
Definition LLMUtils.cs:136
string[] GetLoras()
Gets the paths of the LORAs in the manager.
Definition LLMUtils.cs:196
void Clear()
Clears the LORA assets.
Definition LLMUtils.cs:63
Class implementing a LLM model entry.
Definition LLMManager.cs:18