LLM for Unity  v3.0.3
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 int debugLevel = 1;
456 if (LLMUnitySetup.DebugMode == LLMUnitySetup.DebugModeType.Debug) debugLevel = 5;
457 LlamaLib.Debug(debugLevel);
458#if ENABLE_IL2CPP
459 IL2CPP_Logging.LoggingCallback(LLMUnitySetup.Log);
460#else
461 LlamaLib.LoggingCallback(LLMUnitySetup.Log);
462#endif
463 }
464 bool useGPU = numGPULayers > 0;
465 llmlib = new LlamaLib(useGPU);
466 }
467
471 private void SetupServer()
472 {
473 if (!remote) return;
474
475 if (!string.IsNullOrEmpty(SSLCert) && !string.IsNullOrEmpty(SSLKey))
476 {
477 LLMUnitySetup.Log("Enabling SSL for server");
478 llmService.SetSSL(SSLCert, SSLKey);
479 }
480 llmService.StartServer("", port, APIKey);
481 }
482
486 private void RestartServer()
487 {
488 if (!started) return;
489 llmService.StopServer();
490 SetupServer();
491 }
492
493 private async Task CreateServiceAsync(string modelPath, List<string> loraPaths)
494 {
495 int numSlots = GetNumClients();
496 int effectiveThreads = numThreads;
497
498 if (Application.platform == RuntimePlatform.Android && numThreads <= 0)
499 {
500 effectiveThreads = LLMUnitySetup.AndroidGetNumBigCores();
501 }
502
503 string processorType = SystemInfo.processorType;
504 await Task.Run(() =>
505 {
506 lock (staticLock)
507 {
508 IntPtr llmPtr = LLMService.CreateLLM(
509 llmlib, modelPath, numSlots, effectiveThreads, numGPULayers,
510 flashAttention, contextSize, batchSize, embeddingsOnly, loraPaths.ToArray());
511
512 llmService = new LLMService(llmlib, llmPtr);
513
514 string serverString = "llamalib_**architecture**_server";
515 if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsServer)
516 serverString = "llamalib_win-x64_server.exe";
517 else if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXServer)
518 serverString = processorType.Contains("Intel") ? "llamalib_osx-x64_server" : "llamalib_osx-arm64_server";
519 else if (Application.platform == RuntimePlatform.LinuxEditor || Application.platform == RuntimePlatform.LinuxPlayer || Application.platform == RuntimePlatform.LinuxServer)
520 serverString = "llamalib_linux-x64_server";
521 LLMUnitySetup.Log($"Deploy server command: {serverString} {llmService.Command}");
522 SetupServer();
523 llmService.Start();
524
525 started = llmService.Started();
526 if (started)
527 {
528 ApplyLoras();
530 }
531 }
532 });
533 }
534
535 #endregion
536
537 #region Public Methods
542 public async Task WaitUntilReady()
543 {
544 while (!started && !failed)
545 {
546 await Task.Yield();
547 }
548
549 if (failed)
550 {
551 LLMUnitySetup.LogError("LLM failed to start", true);
552 }
553 }
554
560 public static async Task<bool> WaitUntilModelSetup(Action<float> downloadProgressCallback = null)
561 {
562 if (downloadProgressCallback != null)
563 {
564 LLMManager.downloadProgressCallbacks.Add(downloadProgressCallback);
565 }
566
567 while (!modelSetupComplete)
568 {
569 await Task.Yield();
570 }
571
572 return !modelSetupFailed;
573 }
574
579 public void SetModel(string path)
580 {
581 if (model == path) return;
582 AssertNotStarted();
583
584 _model = GetLLMManagerAsset(path);
585 if (string.IsNullOrEmpty(model)) return;
586
587 ModelEntry modelEntry = LLMManager.Get(model) ?? new ModelEntry(GetLLMManagerAssetRuntime(model));
588
589 maxContextLength = modelEntry.contextLength;
590 if (contextSize > maxContextLength)
591 {
592 contextSize = maxContextLength;
593 }
594
595 SetEmbeddings(modelEntry.embeddingLength, modelEntry.embeddingOnly);
596
597 if (contextSize == 0 && modelEntry.contextLength > 32768)
598 {
599 LLMUnitySetup.LogWarning($"Model {path} has large context size ({modelEntry.contextLength}). Consider setting contextSize to ≤32768 to avoid excessive memory usage.");
600 }
601
602#if UNITY_EDITOR
603 if (!EditorApplication.isPlaying) EditorUtility.SetDirty(this);
604#endif
605 }
606
611 public void SetReasoning(bool reasoning)
612 {
613 llmService.EnableReasoning(reasoning);
614 }
615
622 {
623 _embeddingsOnly = embeddingsOnly;
624 _embeddingLength = embeddingLength;
625
626#if UNITY_EDITOR
627 if (!EditorApplication.isPlaying) EditorUtility.SetDirty(this);
628#endif
629 }
630
636 public void Register(LLMClient llmClient)
637 {
638 if (llmClient == null)
639 {
640 LLMUnitySetup.LogError("llmClient is null", true);
641 }
642
643 clients.Add(llmClient);
644 }
645
650 public List<LoraIdScalePath> ListLoras()
651 {
652 AssertStarted();
653 return llmService.LoraList();
654 }
655
656 #endregion
657
658 #region LORA Management
664 public void SetLora(string path, float weight = 1f)
665 {
666 AssertNotStarted();
667 loraManager.Clear();
668 AddLora(path, weight);
669 }
670
676 public void AddLora(string path, float weight = 1f)
677 {
678 AssertNotStarted();
679 loraManager.Add(path, weight);
680 UpdateLoras();
681 }
682
687 public void RemoveLora(string path)
688 {
689 AssertNotStarted();
690 loraManager.Remove(path);
691 UpdateLoras();
692 }
693
697 public void RemoveLoras()
698 {
699 AssertNotStarted();
700 loraManager.Clear();
701 UpdateLoras();
702 }
703
709 public void SetLoraWeight(string path, float weight)
710 {
711 loraManager.SetWeight(path, weight);
712 UpdateLoras();
713 if (started) ApplyLoras();
714 }
715
720 public void SetLoraWeights(Dictionary<string, float> loraToWeight)
721 {
722 if (loraToWeight == null)
723 {
724 LLMUnitySetup.LogError("loraToWeight is null", true);
725 }
726
727 foreach (var entry in loraToWeight)
728 {
729 loraManager.SetWeight(entry.Key, entry.Value);
730 }
731 UpdateLoras();
732 if (started) ApplyLoras();
733 }
734
735 private void UpdateLoras()
736 {
737 (_lora, _loraWeights) = loraManager.ToStrings();
738#if UNITY_EDITOR
739 if (!EditorApplication.isPlaying) EditorUtility.SetDirty(this);
740#endif
741 }
742
743 private void UpdateLoraManagerFromStrings()
744 {
745 loraManager.FromStrings(_lora, _loraWeights);
746 }
747
748 private void ApplyLoras()
749 {
750 if (!started) return;
751 var loras = new List<LoraIdScale>();
752 float[] weights = loraManager.GetWeights();
753
754 for (int i = 0; i < weights.Length; i++)
755 {
756 loras.Add(new LoraIdScale(i, weights[i]));
757 }
758
759 if (loras.Count > 0)
760 {
761 llmService.LoraWeight(loras);
762 }
763 }
764
765 #endregion
766
767 #region SSL Configuration
772 public void SetSSLCertFromFile(string path)
773 {
774 SSLCert = ReadFileContents(path);
775 }
776
781 public void SetSSLKeyFromFile(string path)
782 {
783 SSLKey = ReadFileContents(path);
784 }
785
786 private string ReadFileContents(string path)
787 {
788 if (string.IsNullOrEmpty(path)) return "";
789
790 if (!File.Exists(path))
791 {
792 LLMUnitySetup.LogError($"File not found: {path}");
793 return "";
794 }
795
796 return File.ReadAllText(path);
797 }
798
799 #endregion
800
801 #region Helper Methods
802 private int GetNumClients()
803 {
804 return Math.Max(parallelPrompts == -1 ? clients.Count : parallelPrompts, 1);
805 }
806
807 private void AssertStarted()
808 {
809 string error = null;
810 if (failed) error = "LLM service couldn't be created";
811 else if (!started) error = "LLM service not started";
812 if (error != null) LLMUnitySetup.LogError(error, true);
813 }
814
815 private void AssertNotStarted()
816 {
817 if (started) LLMUnitySetup.LogError("This method can't be called when the LLM has started", true);
818 }
819
823 public void Destroy()
824 {
825 lock (staticLock)
826 {
827 try
828 {
829 llmService?.Dispose();
830 llmlib = null;
831 started = false;
832 failed = false;
833 }
834 catch (Exception ex)
835 {
836 LLMUnitySetup.LogError($"Error during LLM cleanup: {ex.Message}");
837 }
838 }
839 }
840
841 #endregion
842
843 #region Static Asset Management
845 public static string GetLLMManagerAsset(string path)
846 {
847#if UNITY_EDITOR
848 if (!EditorApplication.isPlaying) return GetLLMManagerAssetEditor(path);
849#endif
850 return GetLLMManagerAssetRuntime(path);
851 }
852
853 public static string GetLLMManagerAssetEditor(string path)
854 {
855 if (string.IsNullOrEmpty(path)) return path;
856
857 // Check LLMManager first
858 ModelEntry modelEntry = LLMManager.Get(path);
859 if (modelEntry != null) return modelEntry.filename;
860
861 // Check StreamingAssets
862 string assetPath = LLMUnitySetup.GetAssetPath(path);
863 string basePath = LLMUnitySetup.GetAssetPath();
864
865 if (File.Exists(assetPath) && LLMUnitySetup.IsSubPath(assetPath, basePath))
866 {
867 return LLMUnitySetup.RelativePath(assetPath, basePath);
868 }
869
870 // Warn about local files not in build
871 if (File.Exists(assetPath))
872 {
873 string errorMessage = $"The model {path} was loaded locally. You can include it in the build in one of these ways:";
874 errorMessage += $"\n-Copy the model inside the StreamingAssets folder and use its StreamingAssets path";
875 errorMessage += $"\n-Load the model with the model manager inside the LLM GameObject and use its filename";
876 LLMUnitySetup.LogWarning(errorMessage);
877 }
878 else
879 {
880 LLMUnitySetup.LogError($"Model file not found: {path}");
881 }
882
883 return path;
884 }
885
886 public static string GetLLMManagerAssetRuntime(string path)
887 {
888 if (string.IsNullOrEmpty(path)) return path;
889
890 // Try LLMManager path
891 string managerPath = LLMManager.GetAssetPath(path);
892 if (!string.IsNullOrEmpty(managerPath) && File.Exists(managerPath))
893 {
894 return managerPath;
895 }
896
897 // Try StreamingAssets
898 string assetPath = LLMUnitySetup.GetAssetPath(path);
899 if (File.Exists(assetPath)) return assetPath;
900
901 // Try download path
902 string downloadPath = LLMUnitySetup.GetDownloadAssetPath(path);
903 if (File.Exists(downloadPath)) return downloadPath;
904
905 return path;
906 }
907
909 #endregion
910 }
911}
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:720
static async Task< bool > WaitUntilModelSetup(Action< float > downloadProgressCallback=null)
Waits asynchronously until model setup is complete.
Definition LLM.cs:560
void SetLoraWeight(string path, float weight)
Changes the weight of a specific LORA adapter.
Definition LLM.cs:709
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:611
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:687
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:772
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:579
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:664
void RemoveLoras()
Removes all LORA adapters.
Definition LLM.cs:697
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:621
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:636
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:676
void SetSSLKeyFromFile(string path)
Sets the SSL private key for secure server connections.
Definition LLM.cs:781
async Task WaitUntilReady()
Waits asynchronously until the LLM is ready to accept requests.
Definition LLM.cs:542
void Destroy()
Stops and cleans up the LLM service.
Definition LLM.cs:823
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:650
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