Vector Search
Last updated
This page explains what ToriiDB needs for vector operations and how SetVector attaches an OpenAI embedding to an entry in the background.
Prerequisites
An embedder is created in store.New when an API key is found (see Configuration). Without it, ordinary storage still works and vector writes and searches return ErrNoEmbedder.
| Setting | Value |
|---|---|
| Model | text-embedding-3-small |
| Dimension | TORIIDB_EMBED_DIM, default 256 |
| Endpoint | https://api.openai.com/v1/embeddings |
| Response check | Vector length must equal the dimension |
Attaching a Vector
SET article:1 Redis-style embedded storage VECTOR
err := torii.SetVector(ctx, "article:1", "embedded JSON database", store.SetDefault, nil)
err = d.SetVector(ctx, 0, "article:1", "embedded JSON database", nil)
SetVector rejects blank text, runs a normal Set (so the value is readable immediately and any old vector is cleared), then starts a background job tracked by the store's wait group:
- Look up the embedding cache under the read lock.
- On a miss, call OpenAI with no lock held (35 s timeout) and store the result in the cache.
- Under the write lock, attach the vector only if the key still exists and its value is still the embedded text.
- Append a
SETrecord carrying the encoded vector.
A failed embedding is silently dropped; the text stays stored without a vector. Store.Close() waits for pending jobs.
Consistency Notes
- A write is searchable only after its background job finishes.
- Changing the value before the job finishes discards the stale embedding.
- Search compares only vectors whose length equals the query vector's length.
- Field-level
SETcannot carryVECTOR.