4using System.Collections.Generic;
9 public class LLMException : Exception
11 public int ErrorCode {
get;
private set; }
13 public LLMException(
string message,
int errorCode) : base(message)
15 ErrorCode = errorCode;
19 public class DestroyException : Exception {}
21 public class LoraAsset
23 public string assetPath;
24 public string fullPath;
27 public LoraAsset(
string path,
float weight = 1)
29 assetPath = LLM.GetLLMManagerAsset(path);
30 fullPath = RuntimePath(path);
34 public static string RuntimePath(
string path)
36 return LLMUnitySetup.GetFullPath(LLM.GetLLMManagerAssetRuntime(path));
40 public class LoraManager
42 List<LoraAsset> loras =
new List<LoraAsset>();
43 public string delimiter =
",";
50 public int IndexOf(
string path)
52 string fullPath = LoraAsset.RuntimePath(path);
53 for (
int i = 0; i < loras.Count; i++)
55 LoraAsset lora = loras[i];
56 if (lora.assetPath == path || lora.fullPath == fullPath)
return i;
61 public bool Contains(
string path)
63 return IndexOf(path) != -1;
66 public void Add(
string path,
float weight = 1)
68 if (Contains(path))
return;
69 loras.Add(
new LoraAsset(path, weight));
72 public void Remove(
string path)
74 int index = IndexOf(path);
75 if (index != -1) loras.RemoveAt(index);
78 public void SetWeight(
string path,
float weight)
80 int index = IndexOf(path);
83 LLMUnitySetup.LogError($
"LoRA {path} not loaded with the LLM");
86 loras[index].weight = weight;
89 public void FromStrings(
string loraString,
string loraWeightsString)
91 if (
string.IsNullOrEmpty(loraString) &&
string.IsNullOrEmpty(loraWeightsString))
99 List<string> loraStringArr =
new List<string>(loraString.Split(delimiter));
100 List<string> loraWeightsStringArr =
new List<string>(loraWeightsString.Split(delimiter));
101 if (loraStringArr.Count != loraWeightsStringArr.Count)
throw new Exception($
"LoRAs number ({loraString}) doesn't match the number of weights ({loraWeightsString})");
103 List<LoraAsset> lorasNew =
new List<LoraAsset>();
104 for (
int i = 0; i < loraStringArr.Count; i++) lorasNew.Add(
new LoraAsset(loraStringArr[i].Trim(),
float.Parse(loraWeightsStringArr[i])));
109 LLMUnitySetup.LogError($
"Loras not set: {e.Message}");
113 public (string, string) ToStrings()
115 string loraString =
"";
116 string loraWeightsString =
"";
117 for (
int i = 0; i < loras.Count; i++)
121 loraString += delimiter;
122 loraWeightsString += delimiter;
124 loraString += loras[i].assetPath;
125 loraWeightsString += loras[i].weight;
127 return (loraString, loraWeightsString);
130 public float[] GetWeights()
132 float[] weights =
new float[loras.Count];
133 for (
int i = 0; i < loras.Count; i++) weights[i] = loras[i].weight;
137 public string[] GetLoras()
139 string[] loraPaths =
new string[loras.Count];
140 for (
int i = 0; i < loras.Count; i++) loraPaths[i] = loras[i].assetPath;