4using System.Collections.Generic;
6using UndreamAI.LlamaLib;
17 public int ErrorCode {
get;
private set; }
19 public LLMException(
string message,
int errorCode) : base(message)
21 ErrorCode = errorCode;
37 public string assetPath;
38 public string fullPath;
41 public LoraAsset(
string path,
float weight = 1)
43 assetPath =
LLM.GetLLMManagerAsset(path);
44 fullPath = RuntimePath(path);
48 public static string RuntimePath(
string path)
60 List<LoraAsset> loras =
new List<LoraAsset>();
61 public string delimiter =
",";
78 string fullPath =
LoraAsset.RuntimePath(path);
79 for (
int i = 0; i < loras.Count; i++)
82 if (lora.assetPath == path || lora.fullPath == fullPath)
return i;
102 public void Add(
string path,
float weight = 1)
115 if (index != -1) loras.RemoveAt(index);
128 LLMUnitySetup.LogError($
"LoRA {path} not loaded with the LLM");
131 loras[index].weight = weight;
139 public void FromStrings(
string loraString,
string loraWeightsString)
141 if (
string.IsNullOrEmpty(loraString) &&
string.IsNullOrEmpty(loraWeightsString))
149 List<string> loraStringArr =
new List<string>(loraString.Split(delimiter));
150 List<string> loraWeightsStringArr =
new List<string>(loraWeightsString.Split(delimiter));
151 if (loraStringArr.Count != loraWeightsStringArr.Count)
LLMUnitySetup.LogError($
"LoRAs number ({loraString}) doesn't match the number of weights ({loraWeightsString})",
true);
153 List<LoraAsset> lorasNew =
new List<LoraAsset>();
154 for (
int i = 0; i < loraStringArr.Count; i++) lorasNew.Add(
new LoraAsset(loraStringArr[i].Trim(),
float.Parse(loraWeightsStringArr[i])));
169 string loraString =
"";
170 string loraWeightsString =
"";
171 for (
int i = 0; i < loras.Count; i++)
175 loraString += delimiter;
176 loraWeightsString += delimiter;
178 loraString += loras[i].assetPath;
179 loraWeightsString += loras[i].weight;
181 return (loraString, loraWeightsString);
190 float[] weights =
new float[loras.Count];
191 for (
int i = 0; i < loras.Count; i++) weights[i] = loras[i].weight;
201 string[] loraPaths =
new string[loras.Count];
202 for (
int i = 0; i < loras.Count; i++) loraPaths[i] = loras[i].assetPath;
210 public static Action<string> WrapActionForMainThread(
211 Action<string> callback, MonoBehaviour owner)
213 if (callback ==
null)
return null;
214 var context = SynchronizationContext.Current;
220 if (owner ==
null)
return;
227 if (owner !=
null) callback(msg);
242 LLMUnitySetup.LogError($
"Exception in callback wrapper: {e}");
249 public static LlamaLib.CharArrayCallback WrapCallbackForAsync(
250 Action<string> callback, MonoBehaviour owner)
252 if (callback ==
null)
return null;
253 Action<string> mainThreadCallback = WrapActionForMainThread(callback, owner);
254 return msg => mainThreadCallback(msg);
Class implementing a basic LLM Destroy Exception.
Class implementing a basic LLM Exception.
Class implementing helper functions for setup and process management.
Unity MonoBehaviour component that manages a local LLM server instance. Handles model loading,...
Class representing a LORA asset.
Class representing the LORA manager allowing to convert and retrieve LORA assets to string (for seria...
float[] GetWeights()
Gets the weights of the LORAs in the manager.
void Add(string path, float weight=1)
Adds a LORA with the defined weight.
void Remove(string path)
Removes a LORA based on its path.
int IndexOf(string path)
Searches for a LORA based on the path.
void SetWeight(string path, float weight)
Modifies the weight of a LORA.
void FromStrings(string loraString, string loraWeightsString)
Converts strings with the lora paths and weights to entries in the LORA manager.
string
Converts the entries of the LORA manager to strings with the lora paths and weights.
bool Contains(string path)
Checks if the provided LORA based on a path exists already in the LORA manager.
string[] GetLoras()
Gets the paths of the LORAs in the manager.
void Clear()
Clears the LORA assets.