Mate-Engine/Assets/LLMUnity/Runtime/RAG/DBSearch.cs
shinyflvre 39a7b41d7d 1.4.0 Pre Release
**Changelog 1.4.0 – THE LOCAL AI Update**

**Disclaimer:**
The AI runs *entirely locally* and is designed to be environmentally and ethically responsible.
- It does **not** harm nature or exploit any artists.
- It has only **1.5 billion parameters**, making it a **very small model** by modern standards.
- It consumes as much GPU energy as watching a **1440p 60fps YouTube video**.
- It was **not trained on artist data**; it uses personal input and public Wikipedia datasets.
- It has its own personality, and I ensured it adheres to **ethical standards**.

---

### New Features

- Added **Qwen 2.5 1.5B LLM** to MateEngine!
  - Press **Middle Mouse Button** while hovering over the pet to open the chat window.
  - Runs fully locally on your GPU — no internet connection required.
  - Only uses GPU while processing prompts.
  - Note: This is a **lightweight model** and won’t handle complex tasks or coding. It’s just a small, fun feature!

- Added **Resize Button** – Quickly adjust app size for 1080p, 1440p, and 4K monitors.

- Added **VRM Information Panel** – Displays polygon count, VRM version, and bone status:
  - **Perfect** – Properly exported model.
  - **Failure** – Model has export issues; animations may break.

- Added **Options Submenu**.
- Added **Volume Sliders** – Independently control pet, effects, and menu volume.
- Added **Graphics Settings**.
- Added **Dance Trails** – Glowing effects on the pet’s hands during dance. Toggle on/off as desired.
- Improved **Music Source Selector** – You can now whitelist or add custom audio sources for dancing.
- Added **MateEngine Version Info** – View the current app version from the menu.
- Menu can now be opened with **M** key or **Right-Click**.
- Added full support for **VRM 1.x** and **VRM 0.x** models.
- Introduced **VRM Validator Tool** in Unity Editor – Great for content creators.

---

### Improvements & Fixes

- Major **Performance Optimizations**.
- Fixed a **GC Error** that could appear after 6–12 hours of continuous use.
- Improved **Hand Tracking** – Reduced stuttering in some animations.
- Added **Discord Rich Presence** integration.
- Added **Glowing Halo** for Steam users as exclusive DLC!
2025-04-08 19:16:19 +02:00

121 lines
4.8 KiB
C#

/// @file
/// @brief File implementing the vector database search.
using System;
using System.Collections.Generic;
using Cloud.Unum.USearch;
using System.IO.Compression;
using UnityEngine;
namespace LLMUnity
{
/// @ingroup rag
/// <summary>
/// Class implementing a search with a vector database.
/// The search results are retrieved with Approximate Nearest Neighbor (ANN) which is much faster that SimpleSearch.
/// </summary>
[DefaultExecutionOrder(-2)]
public class DBSearch : SearchMethod
{
protected USearchIndex index;
/// <summary> show/hide advanced options in the GameObject </summary>
[Tooltip("show/hide advanced options in the GameObject")]
[HideInInspector] public bool advancedOptions = false;
/// <summary> The quantisation type used for vector data during indexing. </summary>
[Tooltip("The quantisation type used for vector data during indexing.")]
[ModelAdvanced] public ScalarKind quantization = ScalarKind.Float16;
/// <summary> The metric kind used for distance calculation between vectors. </summary>
[Tooltip("The metric kind used for distance calculation between vectors.")]
[ModelAdvanced] public MetricKind metricKind = MetricKind.Cos;
/// <summary> The connectivity parameter limits the connections-per-node in the graph. </summary>
[Tooltip("The connectivity parameter limits the connections-per-node in the graph.")]
[ModelAdvanced] public ulong connectivity = 32;
/// <summary> The expansion factor used for index construction when adding vectors. </summary>
[Tooltip("The expansion factor used for index construction when adding vectors.")]
[ModelAdvanced] public ulong expansionAdd = 40;
/// <summary> The expansion factor used for index construction during search operations. </summary>
[Tooltip("The expansion factor used for index construction during search operations.")]
[ModelAdvanced] public ulong expansionSearch = 16;
private Dictionary<int, (float[], string, List<int>)> incrementalSearchCache = new Dictionary<int, (float[], string, List<int>)>();
/// \cond HIDE
public new void Awake()
{
base.Awake();
if (!enabled) return;
InitIndex();
}
public void InitIndex()
{
index = new USearchIndex(metricKind, quantization, (ulong)llmEmbedder.llm.embeddingLength, connectivity, expansionAdd, expansionSearch, false);
}
protected override void AddInternal(int key, float[] embedding)
{
index.Add((ulong)key, embedding);
}
protected override void RemoveInternal(int key)
{
index.Remove((ulong)key);
}
protected int[] UlongToInt(ulong[] keys)
{
int[] intKeys = new int[keys.Length];
for (int i = 0; i < keys.Length; i++) intKeys[i] = (int)keys[i];
return intKeys;
}
public override int IncrementalSearch(float[] embedding, string group = "")
{
int key = nextIncrementalSearchKey++;
incrementalSearchCache[key] = (embedding, group, new List<int>());
return key;
}
public override ValueTuple<int[], float[], bool> IncrementalFetchKeys(int fetchKey, int k)
{
if (!incrementalSearchCache.ContainsKey(fetchKey)) throw new Exception($"There is no IncrementalSearch cached with this key: {fetchKey}");
(float[] embedding, string group, List<int> seenKeys) = incrementalSearchCache[fetchKey];
if (!dataSplits.TryGetValue(group, out List<int> dataSplit)) return (new int[0], new float[0], true);
if (dataSplit.Count == 0) return (new int[0], new float[0], true);
Func<int, int> filter = (int key) => !dataSplit.Contains(key) || seenKeys.Contains(key) ? 0 : 1;
index.Search(embedding, k, out ulong[] keys, out float[] distances, filter);
int[] intKeys = UlongToInt(keys);
incrementalSearchCache[fetchKey].Item3.AddRange(intKeys);
bool completed = intKeys.Length < k || seenKeys.Count == Count(group);
if (completed) IncrementalSearchComplete(fetchKey);
return (intKeys, distances, completed);
}
public override void IncrementalSearchComplete(int fetchKey)
{
incrementalSearchCache.Remove(fetchKey);
}
protected override void SaveInternal(ZipArchive archive)
{
index.Save(archive);
}
protected override void LoadInternal(ZipArchive archive)
{
index.Load(archive);
}
protected override void ClearInternal()
{
index.Dispose();
InitIndex();
incrementalSearchCache.Clear();
}
/// \endcond
}
}