Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: store float32 RAG vectors from their buffer and pack the query once (#15859)

Daniel Hanbdfffd7f582c
diff --git a/studio/backend/core/rag/store.py b/studio/backend/core/rag/store.py
index 70a8e59e951..429941bc02c 100644
--- a/studio/backend/core/rag/store.py
+++ b/studio/backend/core/rag/store.py
@@ -12,6 +12,7 @@ import os
import re
import sqlite3
import struct
+import sys
import threading
import uuid
from collections import OrderedDict
@@ -49,6 +50,17 @@ def _scopes(scope) -> list[str]:
def _f32(vector) -> bytes:
"""Pack a vector into float32 bytes for vec0."""
+ np = sys.modules.get("numpy")
+ if (
+ np is not None
+ and type(vector) is np.ndarray
+ and vector.ndim == 1
+ and vector.dtype == np.float32
+ and vector.dtype.isnative
+ and not np.isnan(vector).any()
+ ):
+ # float32 -> float -> 'f' is exact, so the buffer is the packed bytes; NaN keeps the struct path.
+ return vector.tobytes()
return struct.pack(f"{len(vector)}f", *(float(x) for x in vector))
@@ -815,6 +827,7 @@ def _search_dense(conn: sqlite3.Connection, scope, vector, k: int, embedding_mod
kept: dict[str, list[tuple[str, float]]] = {}
fetches = dict.fromkeys(scopes, max(k * 3, k + 10))
pending = list(scopes)
+ query = _f32(vector) if pending else b""
while pending:
widen: list[str] = []
for s in pending:
@@ -823,7 +836,7 @@ def _search_dense(conn: sqlite3.Connection, scope, vector, k: int, embedding_mod
rows = conn.execute(
"SELECT chunk_id, distance FROM chunks_vec "
"WHERE scope=? AND embedding MATCH ? AND k=? ORDER BY distance",
- (s, _f32(vector), fetch),
+ (s, query, fetch),
).fetchall()
kept[s] = _drop_incompatible(
conn,
diff --git a/studio/backend/tests/test_rag_store.py b/studio/backend/tests/test_rag_store.py
index 94e8219765d..bd65adf1e07 100644
--- a/studio/backend/tests/test_rag_store.py
+++ b/studio/backend/tests/test_rag_store.py
@@ -593,3 +593,28 @@ def test_a_legacy_archive_still_gets_two_different_ends(rag_home, rag_conn):
assert oldest and newest
assert oldest != newest, "both ends of the fetch returned the same rows"
assert not set(oldest) & set(newest), (oldest, newest)
+
+
+def test_dense_packs_float32_rows_without_struct_and_the_query_once(rag_conn, monkeypatch):
+ np = pytest.importorskip("numpy")
+ texts = ["alpha alpha", "hotel golf", "alpha bravo"]
+ for i, scope in enumerate(("kb_a", "kb_b", "kb_c")):
+ doc = f"d{i}"
+ store.create_document(rag_conn, scope = scope, filename = "f", sha256 = doc, document_id = doc)
+ rows = np.asarray([embed(t) for t in texts], dtype = np.float32)
+ store.add_chunks(rag_conn, scope, doc, [_chunk(t, j) for j, t in enumerate(texts)], rows)
+ stored = rag_conn.execute("SELECT embedding FROM chunks_vec ORDER BY rowid").fetchall()
+ expected = [
+ store.struct.pack(f"{len(VOCAB)}f", *np.asarray(embed(t), dtype = np.float32).tolist())
+ for t in texts
+ ] * 3
+ assert [bytes(r[0]) for r in stored] == expected
+
+ packs = []
+ real_pack = store.struct.pack
+ monkeypatch.setattr(store.struct, "pack", lambda *a: packs.append(a) or real_pack(*a))
+ query = np.asarray(embed("alpha"), dtype = np.float32)
+ ranked = store.search_dense(rag_conn, ["kb_a", "kb_b", "kb_c"], query, 9)
+ as_list = store.search_dense(rag_conn, ["kb_a", "kb_b", "kb_c"], query.tolist(), 9)
+ assert ranked == as_list and ranked[0][1] > 0.99
+ assert len(packs) == 1