LLM for Unity  v2.2.5
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{
9 public class LLMException : Exception
10 {
11 public int ErrorCode { get; private set; }
12
13 public LLMException(string message, int errorCode) : base(message)
14 {
15 ErrorCode = errorCode;
16 }
17 }
18
19 public class DestroyException : Exception {}
20
21 public class LoraAsset
22 {
23 public string assetPath;
24 public string fullPath;
25 public float weight;
26
27 public LoraAsset(string path, float weight = 1)
28 {
29 assetPath = LLM.GetLLMManagerAsset(path);
30 fullPath = RuntimePath(path);
31 this.weight = weight;
32 }
33
34 public static string RuntimePath(string path)
35 {
36 return LLMUnitySetup.GetFullPath(LLM.GetLLMManagerAssetRuntime(path));
37 }
38 }
39
40 public class LoraManager
41 {
42 List<LoraAsset> loras = new List<LoraAsset>();
43 public string delimiter = ",";
44
45 public void Clear()
46 {
47 loras.Clear();
48 }
49
50 public int IndexOf(string path)
51 {
52 string fullPath = LoraAsset.RuntimePath(path);
53 for (int i = 0; i < loras.Count; i++)
54 {
55 LoraAsset lora = loras[i];
56 if (lora.assetPath == path || lora.fullPath == fullPath) return i;
57 }
58 return -1;
59 }
60
61 public bool Contains(string path)
62 {
63 return IndexOf(path) != -1;
64 }
65
66 public void Add(string path, float weight = 1)
67 {
68 if (Contains(path)) return;
69 loras.Add(new LoraAsset(path, weight));
70 }
71
72 public void Remove(string path)
73 {
74 int index = IndexOf(path);
75 if (index != -1) loras.RemoveAt(index);
76 }
77
78 public void SetWeight(string path, float weight)
79 {
80 int index = IndexOf(path);
81 if (index == -1)
82 {
83 LLMUnitySetup.LogError($"LoRA {path} not loaded with the LLM");
84 return;
85 }
86 loras[index].weight = weight;
87 }
88
89 public void FromStrings(string loraString, string loraWeightsString)
90 {
91 if (string.IsNullOrEmpty(loraString) && string.IsNullOrEmpty(loraWeightsString))
92 {
93 Clear();
94 return;
95 }
96
97 try
98 {
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})");
102
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])));
105 loras = lorasNew;
106 }
107 catch (Exception e)
108 {
109 LLMUnitySetup.LogError($"Loras not set: {e.Message}");
110 }
111 }
112
113 public (string, string) ToStrings()
114 {
115 string loraString = "";
116 string loraWeightsString = "";
117 for (int i = 0; i < loras.Count; i++)
118 {
119 if (i > 0)
120 {
121 loraString += delimiter;
122 loraWeightsString += delimiter;
123 }
124 loraString += loras[i].assetPath;
125 loraWeightsString += loras[i].weight;
126 }
127 return (loraString, loraWeightsString);
128 }
129
130 public float[] GetWeights()
131 {
132 float[] weights = new float[loras.Count];
133 for (int i = 0; i < loras.Count; i++) weights[i] = loras[i].weight;
134 return weights;
135 }
136
137 public string[] GetLoras()
138 {
139 string[] loraPaths = new string[loras.Count];
140 for (int i = 0; i < loras.Count; i++) loraPaths[i] = loras[i].assetPath;
141 return loraPaths;
142 }
143 }
145}