Unity: Embeddings & Search
Embeddings, vector search, and hybrid search in Unity. For the concepts behind each step see the guide pages on Embedding Models, Vector Search, and Hybrid & Lexical Search.
KV-store entries and queries are JSON strings; the engine takes vectors you
produce with Embeddings, not raw text. embed_dim must match the model — 384
for MiniLM/bge-small, 768 for bge-base.
Embed text
const string MODEL = "in-memory::sentence-transformers/all-MiniLM-L6-v2";
string resp = engine.Embeddings.Embed(JsonSerializer.Serialize(new {
model = MODEL,
input = new[] { "The guard saw a thief enter the keep at midnight." },
}));
// Parse resp -> data[0].embedding into a float[].
// Or a direct cosine similarity between two strings:
float sim = engine.Embeddings.Similarity(MODEL, "a knight's blade", "a warrior's sword");
Create a store, insert, query
// 1. Create
ulong storeId = engine.KvStore.Create(JsonSerializer.Serialize(new {
store_id = "npc-memory",
db_path = "./data/npc-memory",
embed_dim = 384,
}));
// 2. Insert — key_embedding is the float[] from Embeddings.Embed
engine.KvStore.Insert("npc-memory", JsonSerializer.Serialize(new[] { new {
id = "mem-001",
key_text = "The guard saw a thief enter the keep at midnight.",
key_embedding = vector, // float[]
facets = new { npc_id = "guard-01", location = "keep" },
priority = 0.8f,
}}));
// 3. Query — embed the question first, pass the vector
string results = engine.KvStore.Query("npc-memory", JsonSerializer.Serialize(new {
query_embedding = queryVector, // float[]
facet_filters = new { npc_id = "guard-01" },
limit = 5,
}));
// results: JSON array of {id, key_text, similarity, priority, combined_score}
ulong n = engine.KvStore.Count("npc-memory", "");
Hybrid (vector + keyword) search
engine.KvStore.CreateFtsIndex("npc-memory", "key_text");
string response = engine.KvStore.HybridQuery("npc-memory", JsonSerializer.Serialize(new {
embeddings = queryVector,
text = "thief keep",
fts_column = "key_text",
limit = 5,
vector_weight = 0.6f,
lexical_weight = 0.4f,
}));
See Hybrid & Lexical Search for the scoring details
and the lexical-only (KvStore.LexicalQuery) variant.