LLM for Unity  v3.0.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.Linq;
7using System.Threading.Tasks;
8using UndreamAI.LlamaLib;
9using UnityEditor;
10using UnityEngine;
11
12namespace LLMUnity
13{
20 public class LLM : MonoBehaviour
21 {
22 #region Inspector Fields
24 [Tooltip("Show/hide advanced options in the inspector")]
25 [HideInInspector] public bool advancedOptions = false;
26
28 [Tooltip("Enable remote server functionality to allow external connections")]
29 [LocalRemote, SerializeField] private bool _remote = false;
30
32 [Tooltip("Port to use for the remote LLM server")]
33 [Remote, SerializeField] private int _port = 13333;
34
36 [Tooltip("API key required for server access (leave empty to disable authentication)")]
37 [SerializeField] private string _APIKey = "";
38
40 [Tooltip("SSL certificate for the remote LLM server")]
41 [SerializeField] private string _SSLCert = "";
42
44 [Tooltip("SSL key for the remote LLM server")]
45 [SerializeField] private string _SSLKey = "";
46
48 [Tooltip("Number of threads to use for processing (-1 = use all available threads)")]
49 [LLM, SerializeField] private int _numThreads = -1;
50
52 [Tooltip("Number of model layers to offload to GPU (0 = CPU only). Falls back to CPU if GPU unsupported")]
53 [LLM, SerializeField] private int _numGPULayers = 0;
54
56 [Tooltip("Number of prompts that can be processed in parallel (-1 = auto-detect from clients)")]
57 [LLM, SerializeField] private int _parallelPrompts = -1;
58
60 [Tooltip("Size of the prompt context in tokens (0 = use model's default context size)")]
61 [DynamicRange("minContextLength", "maxContextLength", false), Model, SerializeField] private int _contextSize = 8192;
62
64 [Tooltip("Batch size for prompt processing (larger = more memory, potentially faster)")]
65 [ModelAdvanced, SerializeField] private int _batchSize = 512;
66
68 [Tooltip("LLM model file path (.gguf format)")]
69 [ModelAdvanced, SerializeField] private string _model = "";
70
72 [Tooltip("Enable flash attention optimization (requires compatible model)")]
73 [ModelExtras, SerializeField] private bool _flashAttention = false;
74
76 [Tooltip("Enable LLM reasoning ('thinking' mode)")]
77 [ModelAdvanced, SerializeField] private bool _reasoning = false;
78
80 [Tooltip("LORA adapter model paths (.gguf format), separated by commas")]
81 [ModelAdvanced, SerializeField] private string _lora = "";
82
84 [Tooltip("Weights for LORA adapters, separated by commas (default: 1.0 for each)")]
85 [ModelAdvanced, SerializeField] private string _loraWeights = "";
86
88 [Tooltip("Persist this LLM GameObject across scene transitions")]
89 [LLM] public bool dontDestroyOnLoad = true;
90
92 [SerializeField]
93 [Tooltip("True if this model only supports embeddings (no text generation)")]
94 private bool _embeddingsOnly = false;
95
97 [SerializeField]
98 [Tooltip("Number of dimensions in embedding vectors (0 if not an embedding model)")]
99 private int _embeddingLength = 0;
100 #endregion
101
102 #region Public Properties with Validation
103
105 public int numThreads
106 {
107 get => _numThreads;
108 set
109 {
110 AssertNotStarted();
111 if (value < -1)
112 LLMUnitySetup.LogError("numThreads must be >= -1", true);
113 _numThreads = value;
114 }
115 }
116
118 public int numGPULayers
119 {
120 get => _numGPULayers;
121 set
122 {
123 AssertNotStarted();
124 if (value < 0)
125 LLMUnitySetup.LogError("numGPULayers must be >= 0", true);
126 _numGPULayers = value;
127 }
128 }
129
132 {
133 get => _parallelPrompts;
134 set
135 {
136 AssertNotStarted();
137 if (value < -1)
138 LLMUnitySetup.LogError("parallelPrompts must be >= -1", true);
139 _parallelPrompts = value;
140 }
141 }
142
144 public int contextSize
145 {
146 get => _contextSize;
147 set
148 {
149 AssertNotStarted();
150 if (value < 0)
151 LLMUnitySetup.LogError("contextSize must be >= 0", true);
152 _contextSize = value;
153 }
154 }
155
157 public int batchSize
158 {
159 get => _batchSize;
160 set
161 {
162 AssertNotStarted();
163 if (value <= 0)
164 LLMUnitySetup.LogError("batchSize must be > 0", true);
165 _batchSize = value;
166 }
167 }
168
170 public bool flashAttention
171 {
172 get => _flashAttention;
173 set
174 {
175 AssertNotStarted();
176 _flashAttention = value;
177 }
178 }
179
181 public string model
182 {
183 get => _model;
184 set => SetModel(value);
185 }
186
188 public bool reasoning
189 {
190 get => _reasoning;
191 set => SetReasoning(value);
192 }
193
195 public string lora
196 {
197 get => _lora;
198 set
199 {
200 if (value == _lora) return;
201 AssertNotStarted();
202 _lora = value;
203 UpdateLoraManagerFromStrings();
204 }
205 }
206
208 public string loraWeights
209 {
210 get => _loraWeights;
211 set
212 {
213 if (value == _loraWeights) return;
214 _loraWeights = value;
215 UpdateLoraManagerFromStrings();
216 ApplyLoras();
217 }
218 }
219
221 public bool remote
222 {
223 get => _remote;
224 set
225 {
226 if (value == _remote) return;
227 _remote = value;
228 RestartServer();
229 }
230 }
231
233 public int port
234 {
235 get => _port;
236 set
237 {
238 if (value == _port) return;
239 if (value < 0 || value > 65535)
240 LLMUnitySetup.LogError("port must be between 0 and 65535", true);
241 _port = value;
242 RestartServer();
243 }
244 }
245
247 public string APIKey
248 {
249 get => _APIKey;
250 set
251 {
252 if (value == _APIKey) return;
253 _APIKey = value;
254 RestartServer();
255 }
256 }
257
259 public string SSLCert
260 {
261 get => _SSLCert;
262 set
263 {
264 AssertNotStarted();
265 if (value == _SSLCert) return;
266 _SSLCert = value;
267 }
268 }
269
271 public string SSLKey
272 {
273 get => _SSLKey;
274 set
275 {
276 AssertNotStarted();
277 if (value == _SSLKey) return;
278 _SSLKey = value;
279 }
280 }
281
282 #endregion
283
284 #region Other Public Properties
286 public bool started { get; private set; } = false;
287
289 public bool failed { get; private set; } = false;
290
292 public static bool modelSetupFailed { get; private set; } = false;
293
295 public static bool modelSetupComplete { get; private set; } = false;
296
298 public LLMService llmService { get; private set; }
299
301 [Tooltip("Model architecture name (e.g., llama, mistral)")]
302 public string architecture => llmlib?.architecture;
303
305 [Tooltip("True if this model only supports embeddings (no text generation)")]
306 public bool embeddingsOnly => _embeddingsOnly;
307
309 [Tooltip("Number of dimensions in embedding vectors (0 if not an embedding model)")]
310 public int embeddingLength => _embeddingLength;
311 #endregion
312
313 #region Private Fields
315 public int minContextLength = 0;
316 public int maxContextLength = 0;
317
318 private LlamaLib llmlib = null;
319 // [Local, SerializeField]
320 protected LLMService _llmService;
321 private readonly List<LLMClient> clients = new List<LLMClient>();
322 public LLMManager llmManager = new LLMManager();
323 private static readonly object staticLock = new object();
324 public LoraManager loraManager = new LoraManager();
325 string loraPre = "";
326 string loraWeightsPre = "";
328 #endregion
329
330 #region Unity Lifecycle
331 public LLM()
332 {
333 LLMManager.Register(this);
334 }
335
336 void OnValidate()
337 {
338 if (lora != loraPre || loraWeights != loraWeightsPre)
339 {
340 loraManager.FromStrings(lora, loraWeights);
341 (loraPre, loraWeightsPre) = (lora, loraWeights);
342 }
343 }
344
349 public async void Awake()
350 {
351 if (!enabled) return;
352
353#if !UNITY_EDITOR
355#endif
356 modelSetupComplete = true;
358 {
359 failed = true;
360 return;
361 }
362
363 await StartServiceAsync();
364 if (!started) return;
365 if (dontDestroyOnLoad) DontDestroyOnLoad(transform.root.gameObject);
366 }
367
368 public void OnDestroy()
369 {
370 Destroy();
372 }
373
374 #endregion
375
376 #region Initialization
377 private void ValidateParameters()
378 {
379 if ((SSLCert != "" && SSLKey == "") || (SSLCert == "" && SSLKey != ""))
380 {
381 LLMUnitySetup.LogError("Both SSL certificate and key must be provided together!", true);
382 }
383 }
384
385 private string GetValidatedModelPath()
386 {
387 if (string.IsNullOrEmpty(model))
388 {
389 LLMUnitySetup.LogError("No model file provided!", true);
390 }
391
392 string modelPath = GetLLMManagerAssetRuntime(model);
393 if (!File.Exists(modelPath))
394 {
395 LLMUnitySetup.LogError($"Model file not found: {modelPath}", true);
396 }
397 return modelPath;
398 }
399
400 private List<string> GetValidatedLoraPaths()
401 {
402 loraManager.FromStrings(lora, loraWeights);
403 List<string> loraPaths = new List<string>();
404
405 foreach (string loraPath in loraManager.GetLoras())
406 {
407 string resolvedPath = GetLLMManagerAssetRuntime(loraPath);
408 if (!File.Exists(resolvedPath))
409 {
410 LLMUnitySetup.LogError($"LORA file not found: {resolvedPath}", true);
411 }
412 loraPaths.Add(resolvedPath);
413 }
414 return loraPaths;
415 }
416
417 private async Task StartServiceAsync()
418 {
419 started = false;
420 failed = false;
421
422 try
423 {
424 ValidateParameters();
425 string modelPath = GetValidatedModelPath();
426 List<string> loraPaths = GetValidatedLoraPaths();
427
428 CreateLib();
429 await CreateServiceAsync(modelPath, loraPaths);
430 }
431 catch (LLMUnityException ex)
432 {
433 LLMUnitySetup.LogError(ex.Message);
434 failed = true;
435 return;
436 }
437 catch (Exception ex)
438 {
439 LLMUnitySetup.LogError($"Failed to create LLM service: {ex.Message}");
440 Destroy();
441 failed = true;
442 return;
443 }
444
445 if (started)
446 {
447 LLMUnitySetup.Log($"LLM service created successfully, using {architecture}");
448 }
449 }
450
451 private void CreateLib()
452 {
453 if (LLMUnitySetup.DebugMode <= LLMUnitySetup.DebugModeType.All)
454 {
455 LlamaLib.Debug(LLMUnitySetup.DebugModeType.All - LLMUnitySetup.DebugMode + 1);
456#if ENABLE_IL2CPP
457 IL2CPP_Logging.LoggingCallback(LLMUnitySetup.Log);
458#else
459 LlamaLib.LoggingCallback(LLMUnitySetup.Log);
460#endif
461 }
462 bool useGPU = numGPULayers > 0;
463 llmlib = new LlamaLib(useGPU);
464 }
465
469 private void SetupServer()
470 {
471 if (!remote) return;
472
473 if (!string.IsNullOrEmpty(SSLCert) && !string.IsNullOrEmpty(SSLKey))
474 {
475 LLMUnitySetup.Log("Enabling SSL for server");
476 llmService.SetSSL(SSLCert, SSLKey);
477 }
478 llmService.StartServer("", port, APIKey);
479 }
480
484 private void RestartServer()
485 {
486 if (!started) return;
487 llmService.StopServer();
488 SetupServer();
489 }
490
491 private async Task CreateServiceAsync(string modelPath, List<string> loraPaths)
492 {
493 int numSlots = GetNumClients();
494 int effectiveThreads = numThreads;
495
496 if (Application.platform == RuntimePlatform.Android && numThreads <= 0)
497 {
498 effectiveThreads = LLMUnitySetup.AndroidGetNumBigCores();
499 }
500
501 string processorType = SystemInfo.processorType;
502 await Task.Run(() =>
503 {
504 lock (staticLock)
505 {
506 IntPtr llmPtr = LLMService.CreateLLM(
507 llmlib, modelPath, numSlots, effectiveThreads, numGPULayers,
508 flashAttention, contextSize, batchSize, embeddingsOnly, loraPaths.ToArray());
509
510 llmService = new LLMService(llmlib, llmPtr);
511
512 string serverString = "llamalib_**architecture**_server";
513 if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsServer)
514 serverString = "llamalib_win-x64_server.exe";
515 else if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXServer)
516 serverString = processorType.Contains("Intel") ? "llamalib_osx-x64_server" : "llamalib_osx-arm64_server";
517 else if (Application.platform == RuntimePlatform.LinuxEditor || Application.platform == RuntimePlatform.LinuxPlayer || Application.platform == RuntimePlatform.LinuxServer)
518 serverString = "llamalib_linux-x64_server";
519 LLMUnitySetup.Log($"Deploy server command: {serverString} {llmService.Command}");
520 SetupServer();
521 llmService.Start();
522
523 started = llmService.Started();
524 if (started)
525 {
526 ApplyLoras();
528 }
529 }
530 });
531 }
532
533 #endregion
534
535 #region Public Methods
540 public async Task WaitUntilReady()
541 {
542 while (!started && !failed)
543 {
544 await Task.Yield();
545 }
546
547 if (failed)
548 {
549 LLMUnitySetup.LogError("LLM failed to start", true);
550 }
551 }
552
558 public static async Task<bool> WaitUntilModelSetup(Action<float> downloadProgressCallback = null)
559 {
560 if (downloadProgressCallback != null)
561 {
562 LLMManager.downloadProgressCallbacks.Add(downloadProgressCallback);
563 }
564
565 while (!modelSetupComplete)
566 {
567 await Task.Yield();
568 }
569
570 return !modelSetupFailed;
571 }
572
577 public void SetModel(string path)
578 {
579 if (model == path) return;
580 AssertNotStarted();
581
582 _model = GetLLMManagerAsset(path);
583 if (string.IsNullOrEmpty(model)) return;
584
585 ModelEntry modelEntry = LLMManager.Get(model) ?? new ModelEntry(GetLLMManagerAssetRuntime(model));
586
587 maxContextLength = modelEntry.contextLength;
588 if (contextSize > maxContextLength)
589 {
590 contextSize = maxContextLength;
591 }
592
593 SetEmbeddings(modelEntry.embeddingLength, modelEntry.embeddingOnly);
594
595 if (contextSize == 0 && modelEntry.contextLength > 32768)
596 {
597 LLMUnitySetup.LogWarning($"Model {path} has large context size ({modelEntry.contextLength}). Consider setting contextSize to ≤32768 to avoid excessive memory usage.");
598 }
599
600#if UNITY_EDITOR
601 if (!EditorApplication.isPlaying) EditorUtility.SetDirty(this);
602#endif
603 }
604
609 public void SetReasoning(bool reasoning)
610 {
611 llmService.EnableReasoning(reasoning);
612 }
613
620 {
621 _embeddingsOnly = embeddingsOnly;
622 _embeddingLength = embeddingLength;
623
624#if UNITY_EDITOR
625 if (!EditorApplication.isPlaying) EditorUtility.SetDirty(this);
626#endif
627 }
628
634 public void Register(LLMClient llmClient)
635 {
636 if (llmClient == null)
637 {
638 LLMUnitySetup.LogError("llmClient is null", true);
639 }
640
641 clients.Add(llmClient);
642 }
643
648 public List<LoraIdScalePath> ListLoras()
649 {
650 AssertStarted();
651 return llmService.LoraList();
652 }
653
654 #endregion
655
656 #region LORA Management
662 public void SetLora(string path, float weight = 1f)
663 {
664 AssertNotStarted();
665 loraManager.Clear();
666 AddLora(path, weight);
667 }
668
674 public void AddLora(string path, float weight = 1f)
675 {
676 AssertNotStarted();
677 loraManager.Add(path, weight);
678 UpdateLoras();
679 }
680
685 public void RemoveLora(string path)
686 {
687 AssertNotStarted();
688 loraManager.Remove(path);
689 UpdateLoras();
690 }
691
695 public void RemoveLoras()
696 {
697 AssertNotStarted();
698 loraManager.Clear();
699 UpdateLoras();
700 }
701
707 public void SetLoraWeight(string path, float weight)
708 {
709 loraManager.SetWeight(path, weight);
710 UpdateLoras();
711 if (started) ApplyLoras();
712 }
713
718 public void SetLoraWeights(Dictionary<string, float> loraToWeight)
719 {
720 if (loraToWeight == null)
721 {
722 LLMUnitySetup.LogError("loraToWeight is null", true);
723 }
724
725 foreach (var entry in loraToWeight)
726 {
727 loraManager.SetWeight(entry.Key, entry.Value);
728 }
729 UpdateLoras();
730 if (started) ApplyLoras();
731 }
732
733 private void UpdateLoras()
734 {
735 (_lora, _loraWeights) = loraManager.ToStrings();
736#if UNITY_EDITOR
737 if (!EditorApplication.isPlaying) EditorUtility.SetDirty(this);
738#endif
739 }
740
741 private void UpdateLoraManagerFromStrings()
742 {
743 loraManager.FromStrings(_lora, _loraWeights);
744 }
745
746 private void ApplyLoras()
747 {
748 if (!started) return;
749 var loras = new List<LoraIdScale>();
750 float[] weights = loraManager.GetWeights();
751
752 for (int i = 0; i < weights.Length; i++)
753 {
754 loras.Add(new LoraIdScale(i, weights[i]));
755 }
756
757 if (loras.Count > 0)
758 {
759 llmService.LoraWeight(loras);
760 }
761 }
762
763 #endregion
764
765 #region SSL Configuration
770 public void SetSSLCertFromFile(string path)
771 {
772 SSLCert = ReadFileContents(path);
773 }
774
779 public void SetSSLKeyFromFile(string path)
780 {
781 SSLKey = ReadFileContents(path);
782 }
783
784 private string ReadFileContents(string path)
785 {
786 if (string.IsNullOrEmpty(path)) return "";
787
788 if (!File.Exists(path))
789 {
790 LLMUnitySetup.LogError($"File not found: {path}");
791 return "";
792 }
793
794 return File.ReadAllText(path);
795 }
796
797 #endregion
798
799 #region Helper Methods
800 private int GetNumClients()
801 {
802 return Math.Max(parallelPrompts == -1 ? clients.Count : parallelPrompts, 1);
803 }
804
805 private void AssertStarted()
806 {
807 string error = null;
808 if (failed) error = "LLM service couldn't be created";
809 else if (!started) error = "LLM service not started";
810 if (error != null) LLMUnitySetup.LogError(error, true);
811 }
812
813 private void AssertNotStarted()
814 {
815 if (started) LLMUnitySetup.LogError("This method can't be called when the LLM has started", true);
816 }
817
821 public void Destroy()
822 {
823 lock (staticLock)
824 {
825 try
826 {
827 llmService?.Dispose();
828 llmlib = null;
829 started = false;
830 failed = false;
831 }
832 catch (Exception ex)
833 {
834 LLMUnitySetup.LogError($"Error during LLM cleanup: {ex.Message}");
835 }
836 }
837 }
838
839 #endregion
840
841 #region Static Asset Management
843 public static string GetLLMManagerAsset(string path)
844 {
845#if UNITY_EDITOR
846 if (!EditorApplication.isPlaying) return GetLLMManagerAssetEditor(path);
847#endif
848 return GetLLMManagerAssetRuntime(path);
849 }
850
851 public static string GetLLMManagerAssetEditor(string path)
852 {
853 if (string.IsNullOrEmpty(path)) return path;
854
855 // Check LLMManager first
856 ModelEntry modelEntry = LLMManager.Get(path);
857 if (modelEntry != null) return modelEntry.filename;
858
859 // Check StreamingAssets
860 string assetPath = LLMUnitySetup.GetAssetPath(path);
861 string basePath = LLMUnitySetup.GetAssetPath();
862
863 if (File.Exists(assetPath) && LLMUnitySetup.IsSubPath(assetPath, basePath))
864 {
865 return LLMUnitySetup.RelativePath(assetPath, basePath);
866 }
867
868 // Warn about local files not in build
869 if (File.Exists(assetPath))
870 {
871 string errorMessage = $"The model {path} was loaded locally. You can include it in the build in one of these ways:";
872 errorMessage += $"\n-Copy the model inside the StreamingAssets folder and use its StreamingAssets path";
873 errorMessage += $"\n-Load the model with the model manager inside the LLM GameObject and use its filename";
874 LLMUnitySetup.LogWarning(errorMessage);
875 }
876 else
877 {
878 LLMUnitySetup.LogError($"Model file not found: {path}");
879 }
880
881 return path;
882 }
883
884 public static string GetLLMManagerAssetRuntime(string path)
885 {
886 if (string.IsNullOrEmpty(path)) return path;
887
888 // Try LLMManager path
889 string managerPath = LLMManager.GetAssetPath(path);
890 if (!string.IsNullOrEmpty(managerPath) && File.Exists(managerPath))
891 {
892 return managerPath;
893 }
894
895 // Try StreamingAssets
896 string assetPath = LLMUnitySetup.GetAssetPath(path);
897 if (File.Exists(assetPath)) return assetPath;
898
899 // Try download path
900 string downloadPath = LLMUnitySetup.GetDownloadAssetPath(path);
901 if (File.Exists(downloadPath)) return downloadPath;
902
903 return path;
904 }
905
907 #endregion
908 }
909}
Unity MonoBehaviour base class for LLM client functionality. Handles both local and remote LLM connec...
Definition LLMClient.cs:21
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.
Unity MonoBehaviour component that manages a local LLM server instance. Handles model loading,...
Definition LLM.cs:21
int numGPULayers
Number of model layers to offload to GPU (0 = CPU only)
Definition LLM.cs:119
void SetLoraWeights(Dictionary< string, float > loraToWeight)
Changes the weights of multiple LORA adapters.
Definition LLM.cs:718
static async Task< bool > WaitUntilModelSetup(Action< float > downloadProgressCallback=null)
Waits asynchronously until model setup is complete.
Definition LLM.cs:558
void SetLoraWeight(string path, float weight)
Changes the weight of a specific LORA adapter.
Definition LLM.cs:707
LLMService llmService
The underlying LLM service instance.
Definition LLM.cs:298
int parallelPrompts
Number of prompts that can be processed in parallel (-1 = auto-detect from clients)
Definition LLM.cs:132
string architecture
Model architecture name (e.g., llama, mistral)
Definition LLM.cs:302
async void Awake()
Unity Awake method that initializes the LLM server. Sets up the model, starts the service,...
Definition LLM.cs:349
void SetReasoning(bool reasoning)
Enable LLM reasoning ("thinking" mode)
Definition LLM.cs:609
bool reasoning
Enable LLM reasoning ('thinking' mode)
Definition LLM.cs:189
bool advancedOptions
Show/hide advanced options in the inspector.
Definition LLM.cs:25
void RemoveLora(string path)
Removes a specific LORA adapter.
Definition LLM.cs:685
bool embeddingsOnly
True if this model only supports embeddings (no text generation)
Definition LLM.cs:306
static bool modelSetupFailed
True if model setup failed during initialization.
Definition LLM.cs:292
string lora
LORA adapter model paths (.gguf format), separated by commas.
Definition LLM.cs:196
int contextSize
Size of the prompt context in tokens (0 = use model's default context size)
Definition LLM.cs:145
void SetSSLCertFromFile(string path)
Sets the SSL certificate for secure server connections.
Definition LLM.cs:770
int numThreads
Number of threads to use for processing (-1 = use all available threads)
Definition LLM.cs:106
bool started
True if the LLM server has started and is ready to receive requests.
Definition LLM.cs:286
void SetModel(string path)
Sets the model file to use. Automatically configures context size and embedding settings.
Definition LLM.cs:577
int port
Port to use for the remote LLM server.
Definition LLM.cs:234
bool remote
Enable remote server functionality to allow external connections.
Definition LLM.cs:222
void SetLora(string path, float weight=1f)
Sets a single LORA adapter, replacing any existing ones.
Definition LLM.cs:662
void RemoveLoras()
Removes all LORA adapters.
Definition LLM.cs:695
string SSLKey
SSL key for the remote LLM server.
Definition LLM.cs:272
bool dontDestroyOnLoad
Persist this LLM GameObject across scene transitions.
Definition LLM.cs:89
void SetEmbeddings(int embeddingLength, bool embeddingsOnly)
Configure the LLM for embedding generation.
Definition LLM.cs:619
string model
LLM model file path (.gguf format)
Definition LLM.cs:182
string APIKey
API key required for server access (leave empty to disable authentication)
Definition LLM.cs:248
int embeddingLength
Number of dimensions in embedding vectors (0 if not an embedding model)
Definition LLM.cs:310
void Register(LLMClient llmClient)
Registers an LLMClient for slot management.
Definition LLM.cs:634
int batchSize
Batch size for prompt processing (larger = more memory, potentially faster)
Definition LLM.cs:158
bool flashAttention
Enable flash attention optimization (requires compatible model)
Definition LLM.cs:171
static bool modelSetupComplete
True if model setup completed (successfully or not)
Definition LLM.cs:295
void AddLora(string path, float weight=1f)
Adds a LORA adapter to the existing set.
Definition LLM.cs:674
void SetSSLKeyFromFile(string path)
Sets the SSL private key for secure server connections.
Definition LLM.cs:779
async Task WaitUntilReady()
Waits asynchronously until the LLM is ready to accept requests.
Definition LLM.cs:540
void Destroy()
Stops and cleans up the LLM service.
Definition LLM.cs:821
string loraWeights
Weights for LORA adapters, separated by commas (default: 1.0 for each)
Definition LLM.cs:209
bool failed
True if the LLM server failed to start.
Definition LLM.cs:289
List< LoraIdScalePath > ListLoras()
Gets a list of loaded LORA adapters.
Definition LLM.cs:648
string SSLCert
SSL certificate for the remote LLM server.
Definition LLM.cs:260
Class representing the LORA manager allowing to convert and retrieve LORA assets to string (for seria...
Definition LLMUtils.cs:59
float[] GetWeights()
Gets the weights of the LORAs in the manager.
Definition LLMUtils.cs:188
void Add(string path, float weight=1)
Adds a LORA with the defined weight.
Definition LLMUtils.cs:102
void Remove(string path)
Removes a LORA based on its path.
Definition LLMUtils.cs:112
void SetWeight(string path, float weight)
Modifies the weight of a LORA.
Definition LLMUtils.cs:123
void FromStrings(string loraString, string loraWeightsString)
Converts strings with the lora paths and weights to entries in the LORA manager.
Definition LLMUtils.cs:139
string[] GetLoras()
Gets the paths of the LORAs in the manager.
Definition LLMUtils.cs:199
void Clear()
Clears the LORA assets.
Definition LLMUtils.cs:66
Class implementing a LLM model entry.
Definition LLMManager.cs:18