v0.7.0

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.
SettingValue
Modeltext-embedding-3-small
DimensionTORIIDB_EMBED_DIM, default 256
Endpointhttps://api.openai.com/v1/embeddings
Response checkVector length must equal the dimension

Attaching a Vector


  1. SET article:1 Redis-style embedded storage VECTOR

  1. err := torii.SetVector(ctx, "article:1", "embedded JSON database", store.SetDefault, nil)
  2. 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:
  1. Look up the embedding cache under the read lock.
  2. On a miss, call OpenAI with no lock held (the background job has a 35 s deadline; the embedding HTTP request itself times out after 30 s) and store the result in the cache.
  3. Under the write lock, attach the vector only if the key still exists and its value is still the embedded text.
  4. Append a SET record 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 SET cannot carry VECTOR.


中文