LLM for Unity  v2.3.0
Create characters in Unity with LLMs!
Loading...
Searching...
No Matches
LLMUtils.cs
Go to the documentation of this file.
1
3using System;
4using System.Collections.Generic;
5
6namespace LLMUnity
7{
12 public class LLMException : Exception
13 {
14 public int ErrorCode { get; private set; }
15
16 public LLMException(string message, int errorCode) : base(message)
17 {
18 ErrorCode = errorCode;
19 }
20 }
21
26 public class DestroyException : Exception {}
27
32 public class LoraAsset
33 {
34 public string assetPath;
35 public string fullPath;
36 public float weight;
37
38 public LoraAsset(string path, float weight = 1)
39 {
40 assetPath = LLM.GetLLMManagerAsset(path);
41 fullPath = RuntimePath(path);
42 this.weight = weight;
43 }
44
45 public static string RuntimePath(string path)
46 {
47 return LLMUnitySetup.GetFullPath(LLM.GetLLMManagerAssetRuntime(path));
48 }
49 }
50
55 public class LoraManager
56 {
57 List<LoraAsset> loras = new List<LoraAsset>();
58 public string delimiter = ",";
59
63 public void Clear()
64 {
65 loras.Clear();
66 }
67
73 public int IndexOf(string path)
74 {
75 string fullPath = LoraAsset.RuntimePath(path);
76 for (int i = 0; i < loras.Count; i++)
77 {
78 LoraAsset lora = loras[i];
79 if (lora.assetPath == path || lora.fullPath == fullPath) return i;
80 }
81 return -1;
82 }
83
89 public bool Contains(string path)
90 {
91 return IndexOf(path) != -1;
92 }
93
99 public void Add(string path, float weight = 1)
100 {
101 if (Contains(path)) return;
102 loras.Add(new LoraAsset(path, weight));
103 }
104
109 public void Remove(string path)
110 {
111 int index = IndexOf(path);
112 if (index != -1) loras.RemoveAt(index);
113 }
114
120 public void SetWeight(string path, float weight)
121 {
122 int index = IndexOf(path);
123 if (index == -1)
124 {
125 LLMUnitySetup.LogError($"LoRA {path} not loaded with the LLM");
126 return;
127 }
128 loras[index].weight = weight;
129 }
130
136 public void FromStrings(string loraString, string loraWeightsString)
137 {
138 if (string.IsNullOrEmpty(loraString) && string.IsNullOrEmpty(loraWeightsString))
139 {
140 Clear();
141 return;
142 }
143
144 try
145 {
146 List<string> loraStringArr = new List<string>(loraString.Split(delimiter));
147 List<string> loraWeightsStringArr = new List<string>(loraWeightsString.Split(delimiter));
148 if (loraStringArr.Count != loraWeightsStringArr.Count) throw new Exception($"LoRAs number ({loraString}) doesn't match the number of weights ({loraWeightsString})");
149
150 List<LoraAsset> lorasNew = new List<LoraAsset>();
151 for (int i = 0; i < loraStringArr.Count; i++) lorasNew.Add(new LoraAsset(loraStringArr[i].Trim(), float.Parse(loraWeightsStringArr[i])));
152 loras = lorasNew;
153 }
154 catch (Exception e)
155 {
156 LLMUnitySetup.LogError($"Loras not set: {e.Message}");
157 }
158 }
159
164 public (string, string) ToStrings()
165 {
166 string loraString = "";
167 string loraWeightsString = "";
168 for (int i = 0; i < loras.Count; i++)
169 {
170 if (i > 0)
171 {
172 loraString += delimiter;
173 loraWeightsString += delimiter;
174 }
175 loraString += loras[i].assetPath;
176 loraWeightsString += loras[i].weight;
177 }
178 return (loraString, loraWeightsString);
179 }
180
185 public float[] GetWeights()
186 {
187 float[] weights = new float[loras.Count];
188 for (int i = 0; i < loras.Count; i++) weights[i] = loras[i].weight;
189 return weights;
190 }
191
196 public string[] GetLoras()
197 {
198 string[] loraPaths = new string[loras.Count];
199 for (int i = 0; i < loras.Count; i++) loraPaths[i] = loras[i].assetPath;
200 return loraPaths;
201 }
202 }
204}
Class implementing a basic LLM Destroy Exception.
Definition LLMUtils.cs:26
Class implementing a basic LLM Exception.
Definition LLMUtils.cs:13
Class implementing helper functions for setup and process management.
Class implementing the LLM server.
Definition LLM.cs:19
Class representing a LORA asset.
Definition LLMUtils.cs:33
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
int IndexOf(string path)
Searches for a LORA based on the path.
Definition LLMUtils.cs:73
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
Converts the entries of the LORA manager to strings with the lora paths and weights.
Definition LLMUtils.cs:164
bool Contains(string path)
Checks if the provided LORA based on a path exists already in the LORA manager.
Definition LLMUtils.cs:89
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