103 public static string LlamaLibReleaseURL = $
"https://github.com/undreamai/LlamaLib/releases/download/{LlamaLibVersion}";
109 public static string LlamaLibURL = $
"{LlamaLibReleaseURL}/{libraryName}.zip";
111 public static string LLMUnityStore = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"LLMUnity");
119 public static readonly Dictionary<string, (string, string, string)[]>
modelOptions =
new Dictionary<
string, (
string,
string,
string)[]>()
121 {
"Large models (more than 10B)",
new(string, string, string)[]
123 (
"Gemma 3 12B",
"https://huggingface.co/lmstudio-community/gemma-3-12b-it-GGUF/resolve/main/gemma-3-12b-it-Q4_K_M.gguf",
"https://ai.google.dev/gemma/terms"),
124 (
"Phi 4 14B",
"https://huggingface.co/bartowski/phi-4-GGUF/resolve/main/phi-4-Q4_K_M.gguf",
null),
125 (
"Qwen 3 14B",
"https://huggingface.co/unsloth/Qwen3-14B-GGUF/resolve/main/Qwen3-14B-Q4_K_M.gguf",
null),
126 (
"DeepSeek R1 Distill Qwen 14B",
"https://huggingface.co/lmstudio-community/DeepSeek-R1-Distill-Qwen-14B-GGUF/resolve/main/DeepSeek-R1-Distill-Qwen-14B-Q4_K_M.gguf",
null),
128 {
"Medium models (up to 10B)",
new(string, string, string)[]
130 (
"Llama 3.1 8B",
"https://huggingface.co/bartowski/Meta-Llama-3.1-8B-Instruct-GGUF/resolve/main/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf",
"https://huggingface.co/meta-llama/Meta-Llama-3.1-8B/blob/main/LICENSE"),
131 (
"Qwen 3 8B",
"https://huggingface.co/unsloth/Qwen3-8B-GGUF/resolve/main/Qwen3-8B-Q4_K_M.gguf",
null),
132 (
"DeepSeek R1 Distill Llama 8B",
"https://huggingface.co/lmstudio-community/DeepSeek-R1-Distill-Llama-8B-GGUF/resolve/main/DeepSeek-R1-Distill-Llama-8B-Q4_K_M.gguf",
null),
133 (
"DeepSeek R1 Distill Qwen 7B",
"https://huggingface.co/lmstudio-community/DeepSeek-R1-Distill-Qwen-7B-GGUF/resolve/main/DeepSeek-R1-Distill-Qwen-7B-Q4_K_M.gguf",
null),
134 (
"Gemma 2 9B it",
"https://huggingface.co/bartowski/gemma-2-9b-it-GGUF/resolve/main/gemma-2-9b-it-Q4_K_M.gguf",
"https://ai.google.dev/gemma/terms"),
135 (
"Mistral 7B Instruct v0.2",
"https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.2-GGUF/resolve/main/mistral-7b-instruct-v0.2.Q4_K_M.gguf",
null),
136 (
"OpenHermes 2.5 7B",
"https://huggingface.co/TheBloke/OpenHermes-2.5-Mistral-7B-GGUF/resolve/main/openhermes-2.5-mistral-7b.Q4_K_M.gguf",
null),
138 {
"Small models (up to 5B)",
new(string, string, string)[]
140 (
"Llama 3.2 3B",
"https://huggingface.co/hugging-quants/Llama-3.2-3B-Instruct-Q4_K_M-GGUF/resolve/main/llama-3.2-3b-instruct-q4_k_m.gguf",
"https://huggingface.co/meta-llama/Llama-3.2-1B/blob/main/LICENSE.txt"),
141 (
"Gemma 3 4B",
"https://huggingface.co/lmstudio-community/gemma-3-4b-it-GGUF/resolve/main/gemma-3-4b-it-Q4_K_M.gguf",
"https://ai.google.dev/gemma/terms"),
142 (
"Phi 4 4B",
"https://huggingface.co/bartowski/microsoft_Phi-4-mini-instruct-GGUF/resolve/main/microsoft_Phi-4-mini-instruct-Q4_K_M.gguf",
null),
143 (
"Qwen 3 4B",
"https://huggingface.co/unsloth/Qwen3-4B-GGUF/resolve/main/Qwen3-4B-Q4_K_M.gguf",
null),
145 {
"Tiny models (up to 2B)",
new(string, string, string)[]
147 (
"Llama 3.2 1B",
"https://huggingface.co/hugging-quants/Llama-3.2-1B-Instruct-Q4_K_M-GGUF/resolve/main/llama-3.2-1b-instruct-q4_k_m.gguf",
"https://huggingface.co/meta-llama/Llama-3.2-1B/blob/main/LICENSE.txt"),
148 (
"Gemma 3 1B",
"https://huggingface.co/lmstudio-community/gemma-3-1b-it-GGUF/resolve/main/gemma-3-1b-it-Q4_K_M.gguf",
"https://ai.google.dev/gemma/terms"),
149 (
"Qwen 3 1.7B",
"https://huggingface.co/unsloth/Qwen3-1.7B-GGUF/resolve/main/Qwen3-1.7B-Q4_K_M.gguf",
null),
150 (
"Qwen 3 0.6B",
"https://huggingface.co/unsloth/Qwen3-0.6B-GGUF/resolve/main/Qwen3-0.6B-Q4_K_M.gguf",
null),
151 (
"DeepSeek R1 Distill Qwen 1.5B",
"https://huggingface.co/lmstudio-community/DeepSeek-R1-Distill-Qwen-1.5B-GGUF/resolve/main/DeepSeek-R1-Distill-Qwen-1.5B-Q4_K_M.gguf",
null),
153 {
"RAG models",
new(string, string, string)[]
155 (
"All MiniLM L12 v2",
"https://huggingface.co/leliuga/all-MiniLM-L12-v2-GGUF/resolve/main/all-MiniLM-L12-v2.Q4_K_M.gguf",
null),
156 (
"BGE large en v1.5",
"https://huggingface.co/CompendiumLabs/bge-large-en-v1.5-gguf/resolve/main/bge-large-en-v1.5-q4_k_m.gguf",
null),
157 (
"BGE base en v1.5",
"https://huggingface.co/CompendiumLabs/bge-base-en-v1.5-gguf/resolve/main/bge-base-en-v1.5-q4_k_m.gguf",
null),
158 (
"BGE small en v1.5",
"https://huggingface.co/CompendiumLabs/bge-small-en-v1.5-gguf/resolve/main/bge-small-en-v1.5-q4_k_m.gguf",
null),
163 [
LLMUnity]
public static DebugModeType DebugMode = DebugModeType.All;
164 static string DebugModeKey =
"DebugMode";
165 public static bool CUBLAS =
false;
166 static string CUBLASKey =
"CUBLAS";
167 static List<Action<string>> errorCallbacks =
new List<Action<string>>();
168 static readonly
object lockObject =
new object();
169 static Dictionary<string, Task> androidExtractTasks =
new Dictionary<string, Task>();
171 public enum DebugModeType
180 public static void Log(
string message)
182 if ((
int)DebugMode > (
int)DebugModeType.All)
return;
186 public static void LogWarning(
string message)
188 if ((
int)DebugMode > (
int)DebugModeType.Warning)
return;
189 Debug.LogWarning(message);
192 public static void LogError(
string message,
bool throwException =
false)
194 if ((
int)DebugMode > (
int)DebugModeType.Error)
return;
195 Debug.LogError(message);
196 foreach (Action<string> errorCallback
in errorCallbacks) errorCallback(message);
197 if (throwException)
throw new LLMUnityException(message);
200 static void LoadPlayerPrefs()
202 DebugMode = (DebugModeType)PlayerPrefs.GetInt(DebugModeKey, (
int)DebugModeType.All);
203 CUBLAS = PlayerPrefs.GetInt(CUBLASKey, 0) == 1;
206 public static void SetDebugMode(DebugModeType newDebugMode)
208 if (DebugMode == newDebugMode)
return;
209 DebugMode = newDebugMode;
210 PlayerPrefs.SetInt(DebugModeKey, (
int)DebugMode);
215 public static void SetCUBLAS(
bool value)
217 if (CUBLAS == value)
return;
219 PlayerPrefs.SetInt(CUBLASKey, value ? 1 : 0);
225 public static string GetAssetPath(
string relPath =
"")
227 string assetsDir = Application.platform == RuntimePlatform.Android ? Application.persistentDataPath : Application.streamingAssetsPath;
228 return Path.Combine(assetsDir, relPath).Replace(
'\\',
'/');
231 public static string GetDownloadAssetPath(
string relPath =
"")
233 string assetsDir = Application.streamingAssetsPath;
235 bool isVisionOS =
false;
236#if UNITY_2022_3_OR_NEWER
237 isVisionOS = Application.platform == RuntimePlatform.VisionOS;
239 if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer || isVisionOS)
241 assetsDir = Application.persistentDataPath;
243 return Path.Combine(assetsDir, relPath).Replace(
'\\',
'/');
246 static void InitializeOnLoadCommon()
248#if UNITY_EDITOR || !((UNITY_ANDROID || UNITY_IOS || UNITY_VISIONOS))
249 LlamaLib.baseLibraryPath = Path.Combine(
libraryPath, LlamaLib.GetPlatform(),
"native");
254 [InitializeOnLoadMethod]
255 static async Task InitializeOnLoad()
258 LlamaLib.libraryExclusion =
new List<string>(){CUBLAS ?
"tinyblas" :
"cublas"};
259 InitializeOnLoadCommon();
260 await DownloadLibrary();
264 [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
265 static void InitializeOnLoad()
267 InitializeOnLoadCommon();
272 static Dictionary<string, ResumingWebClient> downloadClients =
new Dictionary<string, ResumingWebClient>();
274 public static void CancelDownload(
string savePath)
276 if (!downloadClients.ContainsKey(savePath))
return;
277 downloadClients[savePath].CancelDownloadAsync();
278 downloadClients.Remove(savePath);
281 public static async Task DownloadFile(
282 string fileUrl,
string savePath,
bool overwrite =
false,
283 Action<string> callback =
null, Action<float> progressCallback =
null
286 if (File.Exists(savePath) && !overwrite)
288 Log($
"File already exists at: {savePath}");
292 Log($
"Downloading {fileUrl} to {savePath}...");
293 string tmpPath = Path.Combine(Application.temporaryCachePath, Path.GetFileName(savePath));
295 ResumingWebClient client =
new ResumingWebClient();
296 downloadClients[savePath] = client;
297 await client.DownloadFileTaskAsyncResume(
new Uri(fileUrl), tmpPath, !overwrite, progressCallback);
298 downloadClients.Remove(savePath);
300 AssetDatabase.StartAssetEditing();
302 Directory.CreateDirectory(Path.GetDirectoryName(savePath));
303 File.Move(tmpPath, savePath);
305 AssetDatabase.StopAssetEditing();
307 Log($
"Download complete!");
310 progressCallback?.Invoke(1f);
311 callback?.Invoke(savePath);
314 public static async Task AndroidExtractFile(
string assetName,
bool overwrite =
false,
bool log =
true,
int chunkSize = 1024 * 1024)
319 if (!androidExtractTasks.TryGetValue(assetName, out extractionTask))
321#if !UNITY_EDITOR && UNITY_ANDROID
322 extractionTask = AndroidExtractFileOnce(assetName, overwrite, log, chunkSize);
324 extractionTask = Task.CompletedTask;
326 androidExtractTasks[assetName] = extractionTask;
329 await extractionTask;
332 public static async Task AndroidExtractFileOnce(
string assetName,
bool overwrite =
false,
bool log =
true,
int chunkSize = 1024 * 1024)
334 string source =
"jar:file://" + Application.dataPath +
"!/assets/" + assetName;
335 string target = GetAssetPath(assetName);
336 if (!overwrite && File.Exists(target))
338 if (log) Log($
"File {target} already exists");
342 Log($
"Extracting {source} to {target}");
345 UnityWebRequest www = UnityWebRequest.Get(source);
347 var operation = www.SendWebRequest();
349 while (!operation.isDone) await Task.Delay(1);
350 if (www.result != UnityWebRequest.Result.Success)
352 LogError(
"Failed to load file from StreamingAssets: " + www.error);
356 byte[] buffer =
new byte[chunkSize];
357 using (Stream responseStream =
new MemoryStream(www.downloadHandler.data))
358 using (FileStream fileStream =
new FileStream(target, FileMode.Create, FileAccess.Write))
361 while ((bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
363 await fileStream.WriteAsync(buffer, 0, bytesRead);
369 public static async Task AndroidExtractAsset(
string path,
bool overwrite =
false)
371 if (Application.platform != RuntimePlatform.Android)
return;
372 await AndroidExtractFile(Path.GetFileName(path), overwrite);
375 public static string GetFullPath(
string path)
377 return Path.GetFullPath(path).Replace(
'\\',
'/');
380 public static bool IsSubPath(
string childPath,
string parentPath)
382 return GetFullPath(childPath).StartsWith(GetFullPath(parentPath), StringComparison.OrdinalIgnoreCase);
385 public static string RelativePath(
string fullPath,
string basePath)
388 string fullParentPath = GetFullPath(basePath).TrimEnd(
'/');
389 string fullChildPath = GetFullPath(fullPath);
391 string relativePath = fullChildPath;
392 if (fullChildPath.StartsWith(fullParentPath, StringComparison.OrdinalIgnoreCase))
394 relativePath = fullChildPath.Substring(fullParentPath.Length);
395 while (relativePath.StartsWith(
"/")) relativePath = relativePath.Substring(1);
400 public static string SearchDirectory(
string directory,
string targetFileName)
402 string[] files = Directory.GetFiles(directory, targetFileName);
403 if (files.Length > 0)
return files[0];
404 string[] subdirectories = Directory.GetDirectories(directory);
405 foreach (var subdirectory
in subdirectories)
407 string result = SearchDirectory(subdirectory, targetFileName);
408 if (result !=
null)
return result;
415 [HideInInspector]
public static float libraryProgress = 1;
417 public static void CreateEmptyFile(
string path)
419 File.Create(path).Dispose();
422 static void ExtractInsideDirectory(
string zipPath,
string extractPath,
string prefix =
"",
bool overwrite =
true)
424 using (ZipArchive archive = ZipFile.OpenRead(zipPath))
426 foreach (ZipArchiveEntry entry
in archive.Entries)
428 if (
string.IsNullOrEmpty(entry.Name))
431 string destinationPath;
432 if (!String.IsNullOrEmpty(prefix))
434 string normalizedPath = entry.FullName.Replace(
'\\',
'/');
435 if (!normalizedPath.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
437 destinationPath = Path.Combine(extractPath, normalizedPath.Substring(prefix.Length));
441 destinationPath = Path.Combine(extractPath, entry.FullName);
444 Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
445 entry.ExtractToFile(destinationPath, overwrite);
450 static async Task DownloadAndExtractInsideDirectory(
string url,
string path,
string setupDir)
452 string urlName = Path.GetFileName(url);
453 string setupFile = Path.Combine(setupDir, urlName +
".complete");
454 if (File.Exists(setupFile))
return;
456 string zipPath = Path.Combine(Application.temporaryCachePath, urlName);
457 await DownloadFile(url, zipPath,
true,
null, SetLibraryProgress);
459 AssetDatabase.StartAssetEditing();
460 ExtractInsideDirectory(zipPath, path, $
"{libraryName}/runtimes/");
461 CreateEmptyFile(setupFile);
462 AssetDatabase.StopAssetEditing();
464 File.Delete(zipPath);
467 static void DeleteEarlierVersions()
469 List<string> assetPathSubDirs =
new List<string>();
470 foreach (
string dir
in new string[] { GetAssetPath(), Path.Combine(Application.dataPath,
"Plugins",
"Android") })
472 if (Directory.Exists(dir)) assetPathSubDirs.AddRange(Directory.GetDirectories(dir));
475 List<Regex> versionRegexes =
new List<Regex> {
new Regex(
"undreamai-(.+)-llamacpp"),
new Regex(
"LlamaLib-(.+)") };
476 foreach (
string assetPathSubDir
in assetPathSubDirs)
478 foreach (Regex regex
in versionRegexes)
480 Match match = regex.Match(Path.GetFileName(assetPathSubDir));
483 string version = match.Groups[1].Value;
486 Debug.Log($
"Deleting other LLMUnity version folder: {assetPathSubDir}");
487 Directory.Delete(assetPathSubDir,
true);
488 if (File.Exists(assetPathSubDir +
".meta")) File.Delete(assetPathSubDir +
".meta");
495 static async Task DownloadLibrary()
497 if (libraryProgress < 1)
return;
502 DeleteEarlierVersions();
504 string setupDir = Path.Combine(
libraryPath,
"setup");
505 Directory.CreateDirectory(setupDir);
518 private static void SetLibraryProgress(
float progress)
520 libraryProgress = Math.Min(0.99f, progress);
523 public static string AddAsset(
string assetPath)
525 if (!File.Exists(assetPath))
527 LogError($
"{assetPath} does not exist!");
530 string assetDir = GetAssetPath();
531 if (IsSubPath(assetPath, assetDir))
return RelativePath(assetPath, assetDir);
533 string filename = Path.GetFileName(assetPath);
534 string fullPath = GetAssetPath(filename);
535 AssetDatabase.StartAssetEditing();
536 foreach (
string path
in new string[] { fullPath, fullPath +
".meta" })
538 if (File.Exists(path)) File.Delete(path);
540 File.Copy(assetPath, fullPath);
541 AssetDatabase.StopAssetEditing();
551 errorCallbacks.Add(callback);
557 errorCallbacks.Remove(callback);
563 errorCallbacks.Clear();
566 public static int GetMaxFreqKHz(
int cpuId)
568 string[] paths =
new string[]
570 $
"/sys/devices/system/cpu/cpufreq/stats/cpu{cpuId}/time_in_state",
571 $
"/sys/devices/system/cpu/cpu{cpuId}/cpufreq/stats/time_in_state",
572 $
"/sys/devices/system/cpu/cpu{cpuId}/cpufreq/cpuinfo_max_freq"
575 foreach (var path
in paths)
577 if (!File.Exists(path))
continue;
580 using (StreamReader sr =
new StreamReader(path))
583 while ((line = sr.ReadLine()) !=
null)
585 string[] parts = line.Split(
' ');
586 if (parts.Length > 0 &&
int.TryParse(parts[0], out
int freqKHz))
588 if (freqKHz > maxFreqKHz)
590 maxFreqKHz = freqKHz;
595 if (maxFreqKHz != 0)
return maxFreqKHz;
600 public static bool IsSmtCpu(
int cpuId)
602 string[] paths =
new string[]
604 $
"/sys/devices/system/cpu/cpu{cpuId}/topology/core_cpus_list",
605 $
"/sys/devices/system/cpu/cpu{cpuId}/topology/thread_siblings_list"
608 foreach (var path
in paths)
610 if (!File.Exists(path))
continue;
611 using (StreamReader sr =
new StreamReader(path))
614 while ((line = sr.ReadLine()) !=
null)
616 if (line.Contains(
",") || line.Contains(
"-"))
632 int maxFreqKHzMin =
int.MaxValue;
633 int maxFreqKHzMax = 0;
634 List<int> cpuMaxFreqKHz =
new List<int>();
635 List<bool> cpuIsSmtCpu =
new List<bool>();
639 string cpuPath =
"/sys/devices/system/cpu/";
641 if (Directory.Exists(cpuPath))
643 foreach (
string cpuDir
in Directory.GetDirectories(cpuPath))
645 string dirName = Path.GetFileName(cpuDir);
646 if (!dirName.StartsWith(
"cpu"))
continue;
647 if (!
int.TryParse(dirName.Substring(3), out coreIndex))
continue;
649 int maxFreqKHz = GetMaxFreqKHz(coreIndex);
650 cpuMaxFreqKHz.Add(maxFreqKHz);
651 if (maxFreqKHz > maxFreqKHzMax) maxFreqKHzMax = maxFreqKHz;
652 if (maxFreqKHz < maxFreqKHzMin) maxFreqKHzMin = maxFreqKHz;
653 cpuIsSmtCpu.Add(IsSmtCpu(coreIndex));
663 int numCores = SystemInfo.processorCount;
664 int maxFreqKHzMedium = (maxFreqKHzMin + maxFreqKHzMax) / 2;
665 if (maxFreqKHzMedium == maxFreqKHzMax) numBigCores = numCores;
668 for (
int i = 0; i < cpuMaxFreqKHz.Count; i++)
670 if (cpuIsSmtCpu[i] || cpuMaxFreqKHz[i] >= maxFreqKHzMedium) numBigCores++;
674 if (numBigCores == 0) numBigCores = SystemInfo.processorCount / 2;
675 else numBigCores = Math.Min(numBigCores, SystemInfo.processorCount);
686 List<int> capacities =
new List<int>();
687 int minCapacity =
int.MaxValue;
690 string cpuPath =
"/sys/devices/system/cpu/";
692 if (Directory.Exists(cpuPath))
694 foreach (
string cpuDir
in Directory.GetDirectories(cpuPath))
696 string dirName = Path.GetFileName(cpuDir);
697 if (!dirName.StartsWith(
"cpu"))
continue;
698 if (!
int.TryParse(dirName.Substring(3), out coreIndex))
continue;
700 string capacityPath = Path.Combine(cpuDir,
"cpu_capacity");
701 if (!File.Exists(capacityPath))
break;
703 int capacity =
int.Parse(File.ReadAllText(capacityPath).Trim());
704 capacities.Add(capacity);
705 if (minCapacity > capacity) minCapacity = capacity;
715 foreach (
int capacity
in capacities)
717 if (capacity >= 2 * minCapacity) numBigCores++;
720 if (numBigCores == 0 || numBigCores > SystemInfo.processorCount) numBigCores = SystemInfo.processorCount;