LLM for Unity  v2.4.1
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 public bool started { get; protected set; } = false;
46 public bool failed { get; protected set; } = false;
48 public static bool modelSetupFailed { get; protected set; } = false;
50 public static bool modelSetupComplete { get; protected set; } = false;
51
54 [ModelAdvanced] public string model = "";
56 [ModelAdvanced] public string chatTemplate = ChatTemplate.DefaultTemplate;
59 [ModelAdvanced] public string lora = "";
61 [ModelAdvanced] public string loraWeights = "";
63 [ModelExtras] public bool flashAttention = false;
64
66 public string APIKey;
67 // SSL certificate
68 [SerializeField]
69 private string SSLCert = "";
70 public string SSLCertPath = "";
71 // SSL key
72 [SerializeField]
73 private string SSLKey = "";
74 public string SSLKeyPath = "";
75
77 public int minContextLength = 0;
78 public int maxContextLength = 0;
79
80 IntPtr LLMObject = IntPtr.Zero;
81 List<LLMCaller> clients = new List<LLMCaller>();
82 LLMLib llmlib;
83 StreamWrapper logStreamWrapper = null;
84 Thread llmThread = null;
85 List<StreamWrapper> streamWrappers = new List<StreamWrapper>();
86 public LLMManager llmManager = new LLMManager();
87 private readonly object startLock = new object();
88 static readonly object staticLock = new object();
89 public LoraManager loraManager = new LoraManager();
90 string loraPre = "";
91 string loraWeightsPre = "";
92 public bool embeddingsOnly = false;
93 public int embeddingLength = 0;
94
96
97 public LLM()
98 {
99 LLMManager.Register(this);
100 }
101
102 void OnValidate()
103 {
104 if (lora != loraPre || loraWeights != loraWeightsPre)
105 {
106 loraManager.FromStrings(lora, loraWeights);
107 (loraPre, loraWeightsPre) = (lora, loraWeights);
108 }
109 }
110
114 public async void Awake()
115 {
116 if (!enabled) return;
117#if !UNITY_EDITOR
119#endif
120 modelSetupComplete = true;
122 {
123 failed = true;
124 return;
125 }
126 string arguments = GetLlamaccpArguments();
127 if (arguments == null)
128 {
129 failed = true;
130 return;
131 }
132 await Task.Run(() => StartLLMServer(arguments));
133 if (!started) return;
134 if (dontDestroyOnLoad) DontDestroyOnLoad(transform.root.gameObject);
135 }
136
140 public async Task WaitUntilReady()
141 {
142 while (!started) await Task.Yield();
143 }
144
149 public static async Task<bool> WaitUntilModelSetup(Callback<float> downloadProgressCallback = null)
150 {
151 if (downloadProgressCallback != null) LLMManager.downloadProgressCallbacks.Add(downloadProgressCallback);
152 while (!modelSetupComplete) await Task.Yield();
153 return !modelSetupFailed;
154 }
155
157 public static string GetLLMManagerAsset(string path)
158 {
159#if UNITY_EDITOR
160 if (!EditorApplication.isPlaying) return GetLLMManagerAssetEditor(path);
161#endif
162 return GetLLMManagerAssetRuntime(path);
163 }
164
165 public static string GetLLMManagerAssetEditor(string path)
166 {
167 // empty
168 if (string.IsNullOrEmpty(path)) return path;
169 // LLMManager - return location the file will be stored in StreamingAssets
170 ModelEntry modelEntry = LLMManager.Get(path);
171 if (modelEntry != null) return modelEntry.filename;
172 // StreamingAssets - return relative location within StreamingAssets
173 string assetPath = LLMUnitySetup.GetAssetPath(path); // Note: this will return the full path if a full path is passed
174 string basePath = LLMUnitySetup.GetAssetPath();
175 if (File.Exists(assetPath))
176 {
177 if (LLMUnitySetup.IsSubPath(assetPath, basePath)) return LLMUnitySetup.RelativePath(assetPath, basePath);
178 }
179 // full path
180 if (!File.Exists(assetPath))
181 {
182 LLMUnitySetup.LogError($"Model {path} was not found.");
183 }
184 else
185 {
186 string errorMessage = $"The model {path} was loaded locally. You can include it in the build in one of these ways:";
187 errorMessage += $"\n-Copy the model inside the StreamingAssets folder and use its StreamingAssets path";
188 errorMessage += $"\n-Load the model with the model manager inside the LLM GameObject and use its filename";
189 LLMUnitySetup.LogWarning(errorMessage);
190 }
191 return path;
192 }
193
194 public static string GetLLMManagerAssetRuntime(string path)
195 {
196 // empty
197 if (string.IsNullOrEmpty(path)) return path;
198 // LLMManager
199 string managerPath = LLMManager.GetAssetPath(path);
200 if (!string.IsNullOrEmpty(managerPath) && File.Exists(managerPath)) return managerPath;
201 // StreamingAssets
202 string assetPath = LLMUnitySetup.GetAssetPath(path);
203 if (File.Exists(assetPath)) return assetPath;
204 // download path
205 assetPath = LLMUnitySetup.GetDownloadAssetPath(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: " + 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 if (weights.Length == 0) return;
714 for (int i = 0; i < weights.Length; i++)
715 {
716 loraWeightRequest.loraWeights.Add(new LoraWeightRequest() { id = i, scale = weights[i] });
717 }
718
719 string json = JsonUtility.ToJson(loraWeightRequest);
720 int startIndex = json.IndexOf("[");
721 int endIndex = json.LastIndexOf("]") + 1;
722 json = json.Substring(startIndex, endIndex - startIndex);
723
724 IntPtr stringWrapper = llmlib.StringWrapper_Construct();
725 llmlib.LLM_LoraWeight(LLMObject, json, stringWrapper);
726 llmlib.StringWrapper_Delete(stringWrapper);
727 }
728
733 public async Task<List<LoraWeightResult>> ListLoras()
734 {
735 AssertStarted();
736 LLMNoInputReplyCallback callback = (IntPtr LLMObject, IntPtr strWrapper) =>
737 {
738 llmlib.LLM_LoraList(LLMObject, strWrapper);
739 };
740 string json = await LLMNoInputReply(callback);
741 if (String.IsNullOrEmpty(json)) return null;
742 LoraWeightResultList loraRequest = JsonUtility.FromJson<LoraWeightResultList>("{\"loraWeights\": " + json + "}");
743 return loraRequest.loraWeights;
744 }
745
751 public async Task<string> Slot(string json)
752 {
753 AssertStarted();
754 LLMReplyCallback callback = (IntPtr LLMObject, string jsonData, IntPtr strWrapper) =>
755 {
756 llmlib.LLM_Slot(LLMObject, jsonData, strWrapper);
757 };
758 return await LLMReply(callback, json);
759 }
760
767 public async Task<string> Completion(string json, Callback<string> streamCallback = null)
768 {
769 AssertStarted();
770 if (streamCallback == null) streamCallback = (string s) => {};
771 StreamWrapper streamWrapper = ConstructStreamWrapper(streamCallback);
772 await Task.Run(() => llmlib.LLM_Completion(LLMObject, json, streamWrapper.GetStringWrapper()));
773 if (!started) return null;
774 streamWrapper.Update();
775 string result = streamWrapper.GetString();
776 DestroyStreamWrapper(streamWrapper);
777 CheckLLMStatus();
778 return result;
779 }
780
785 public void CancelRequest(int id_slot)
786 {
787 AssertStarted();
788 llmlib?.LLM_Cancel(LLMObject, id_slot);
789 CheckLLMStatus();
790 }
791
795 public void Destroy()
796 {
797 lock (staticLock)
798 lock (startLock)
799 {
800 try
801 {
802 if (llmlib != null)
803 {
804 if (LLMObject != IntPtr.Zero)
805 {
806 llmlib.LLM_Stop(LLMObject);
807 if (remote) llmlib.LLM_StopServer(LLMObject);
808 StopLogging();
809 llmThread?.Join();
810 llmlib.LLM_Delete(LLMObject);
811 LLMObject = IntPtr.Zero;
812 }
813 llmlib.Destroy();
814 llmlib = null;
815 }
816 started = false;
817 failed = false;
818 }
819 catch (Exception e)
820 {
821 LLMUnitySetup.LogError(e.Message);
822 }
823 }
824 }
825
830 public void OnDestroy()
831 {
832 Destroy();
834 }
835 }
836}
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:751
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:733
static async Task< bool > WaitUntilModelSetup(Callback< float > downloadProgressCallback=null)
Allows to wait until the LLM models are downloaded and ready.
Definition LLM.cs:149
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:785
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
async void Awake()
The Unity Awake function that starts the LLM server.
Definition LLM.cs:114
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:830
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:48
string lora
the paths of the LORA models being used (relative to the Assets/StreamingAssets folder)....
Definition LLM.cs:59
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:44
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:54
string APIKey
API key to use for the server (optional)
Definition LLM.cs:66
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:56
bool flashAttention
enable use of flash attention
Definition LLM.cs:63
async Task< string > Completion(string json, Callback< string > streamCallback=null)
Allows to use the chat and completion functionality of the LLM.
Definition LLM.cs:767
static bool modelSetupComplete
Boolean set to true if the server has started and is ready to receive requests, false otherwise.
Definition LLM.cs:50
async Task WaitUntilReady()
Allows to wait until the LLM is ready.
Definition LLM.cs:140
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:795
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:61
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:46
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