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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Fix RAG dense retrieval on older SQLite runtimes (#10861)
alkinun3d5b0ebf8abb
diff --git a/studio/backend/core/rag/store.py b/studio/backend/core/rag/store.pyindex d7cf5669509..6d75938e9e2 100644--- a/studio/backend/core/rag/store.py+++ b/studio/backend/core/rag/store.py@@ -673,9 +673,10 @@ def search_dense(widen: list[str] = []for s in pending:fetch = fetches[s]+ # SQLite < 3.41 does not pass LIMIT to vec0's KNN planner; bind k explicitly.rows = conn.execute("SELECT chunk_id, distance FROM chunks_vec "- "WHERE scope=? AND embedding MATCH ? ORDER BY distance LIMIT ?",+ "WHERE scope=? AND embedding MATCH ? AND k=? ORDER BY distance",(s, _f32(vector), fetch),).fetchall()kept[s] = _drop_incompatible(@@ -694,6 +695,7 @@ def search_dense(# Past this many nearest neighbours the scope is effectively another embedder's, and a re-upload is the answer.+# 4096 is also vec0's own ceiling, so raising this errors the query instead of widening it._MAX_DENSE_FETCH = 4096# One id per bound parameter, kept under the oldest SQLITE_MAX_VARIABLE_NUMBER._ID_BATCH = 900diff --git a/studio/backend/tests/test_rag_store.py b/studio/backend/tests/test_rag_store.pyindex 760c6eb91fc..98b1429fc82 100644--- a/studio/backend/tests/test_rag_store.py+++ b/studio/backend/tests/test_rag_store.py@@ -4,6 +4,7 @@"""Store tests: incremental writes, dedupe, delete, scope, dense + lexical."""import math+import reimport sqlite3import pytest@@ -73,6 +74,28 @@ def test_dense_ranks_by_cosine(rag_conn):assert ranked[0][0] == "d1:0" and ranked[0][1] > 0.99+def test_dense_knn_binds_k_rather_than_limit(rag_conn):+ """vec0's KNN bound must arrive as ``k = ?``, not a bare ``LIMIT ?``, which SQLite forwards+ to a virtual table's planner only from 3.41 on. CI's SQLite accepts both and returns the+ same rows, so nothing else here catches a revert and only the executed statement can say."""+ _add_doc(rag_conn, "kb_a", "d1", "f", "h1", ["alpha alpha"])+ seen = []+ rag_conn.set_trace_callback(seen.append)+ try:+ store.search_dense(rag_conn, "kb_a", embed("alpha"), 5)+ finally:+ rag_conn.set_trace_callback(None)++ knn = [sql for sql in seen if "chunks_vec" in sql and "MATCH" in sql.upper()]+ assert knn, f"search_dense issued no vec0 MATCH query; statements were {seen}"+ for sql in knn:+ flat = " ".join(sql.split())+ assert re.search(r"\bk\s*=", flat), f"vec0 KNN query has no k constraint: {flat}"+ assert not re.search(+ r"\bLIMIT\b", flat, re.IGNORECASE+ ), f"vec0 KNN query still leans on LIMIT, which pre-3.41 SQLite never forwards: {flat}"++def test_dense_empty_before_any_ingest(rag_conn):# No chunks_vec table yet -> [], no crash.assert store.search_dense(rag_conn, "kb_a", embed("alpha"), 10) == []