Skip to content

Studio: fast incremental RAG indexing (warm embedder + FTS5) - #5899

Closed
danielhanchen wants to merge 7 commits into
feature/ragfrom
studio-rag-fast-indexing
Closed

danielhanchen wants to merge 7 commits into
feature/ragfrom
studio-rag-fast-indexing

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Stacked on feature/rag (PR #5759). Keeps that PR's UX and retrieval accuracy while cutting
per-document indexing from about a minute to a few seconds. All changes sit behind a single
UNSLOTH_RAG_FAST=1 flag, so with the flag unset the behavior is identical to feature/rag.

This PR is a running progress log; it is updated as benchmark runs complete. Details and live
results are in studio/RAG_FAST_INDEXING.md.

Root cause of the slow path (measured on feature/rag)

  • The embedder is loaded inside a fresh spawn subprocess on every upload, so the cold model load
    recurs each time rather than once at startup.
  • BM25 rebuilds the entire scope index on every document (O(N^2) as a knowledge base fills).

Change

  • Warm the embedder at startup in a daemon thread.
  • Run ingestion in a thread in the warm main process so the embedder singleton is reused.
  • Reimplement BM25 on incremental SQLite FTS5 in the existing rag.db; insert only the new
    document's chunks. Dense (sqlite-vec) and RRF fusion are unchanged.

Validation

Two isolated Studios (baseline vs improved), same corpus and model, driven through the real UI with
Playwright. Authoritative corpus (arXiv Attention/BERT/RAG, RFC 9110). Local GGUF
unsloth/Qwen3.5-9B-GGUF for the chat tool path. Metrics: upload-to-ready latency, Recall@k/MRR,
scaling, and end-to-end UI with citations.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new documentation file, studio/RAG_FAST_INDEXING.md, which outlines the design, root causes of slow indexing, proposed optimizations (such as warming the embedder at startup, in-process ingestion, and incremental SQLite FTS5 BM25), and validation strategies for fast incremental indexing in Studio RAG. There are no review comments provided, and I have no additional feedback to offer.

@danielhanchen

Copy link
Copy Markdown
Member Author

Benchmark results (real Studio HTTP API, baseline vs UNSLOTH_RAG_FAST=1)

Same bge-small-en-v1.5 embedder, same corpus, same chunk settings, single GPU.

Indexing latency (upload to ready)

Document Baseline Improved Speedup
attention 1706.03762 23.4 s 7.4 s 3.2x
bert 1810.04805 24.6 s 8.0 s 3.1x
rag 2005.11401 23.2 s 6.9 s 3.4x
rfc9110.txt 19.4 s 1.2 s 16x
mean 22.7 s 5.9 s ~4x

Scaling (8 small docs into one KB, per-document): baseline 17.6 s/doc, improved 0.12 s/doc.

Retrieval accuracy (no regression)

Mode Baseline R@5 / MRR Improved R@5 / MRR
dense 1.000 / 0.875 1.000 / 0.875
hybrid 1.000 / 0.833 1.000 / 0.844

Indexing a paper drops from ~23 s to ~7 s and a small document from ~18 s to ~0.12 s, accuracy held.
Cause of the baseline cost: the embedder is reloaded inside a fresh spawn subprocess on every
upload, plus a full BM25 scope rebuild per document. UI walkthrough and local-GGUF chat path next.

@danielhanchen

Copy link
Copy Markdown
Member Author

End-to-end validation in the real Studio UI (Playwright + local GGUF)

Drove both Studios through the actual web UI with Playwright and the local
unsloth/Qwen3.5-9B-GGUF (Q4_K_M) loaded via llama-server: load model, enable the RAG toggle,
upload bert_1810.04805.pdf via the composer, ask "What are BERT's two pre-training objectives?".

  • RAG works identically on both: the model calls search_knowledge_base, retrieves from the
    uploaded PDF, and answers with source citations ([3][4] bert_1810.04805.pdf).
  • API-level tool-call test: 3/3 gold questions called the tool, retrieved the correct source
    paper, and answered correctly (BERT to MLM + NSP, Transformer to 8 heads, RAG to DPR).
  • Indexing speed in the UI for the same upload: baseline 34.9 s vs improved 14.0 s.

The fast path preserves the PR's full RAG behavior (document types, RAG toggle, tool-call retrieval
with citations) while indexing materially faster.

@danielhanchen

Copy link
Copy Markdown
Member Author

"Shorter" follow-up: trimmed RAG surface (branch studio-rag-trimmed)

Stacked on this branch, I trimmed the two genuinely optional, non-default subsystems while keeping
the full validated core (document types, RAG toggle, search_knowledge_base tool, KBs, per-thread
docs, hybrid search, source citations).

Removed (committed + tested):

Subsystem LoC removed Why it is safe
cross-encoder reranker ~375 (with test) enable_rerank=False everywhere; off by default, adds a second model download + a candidate-widening pass on every search
late chunking (Jina single-pass) ~360 (with test) chunking_strategy defaults to standard; the standard per-chunk encoder already hits Recall@5 = 1.0 on the gold set

Net git diff --shortstat: 10 files, 9 insertions, 777 deletions. RAG suite: 99 passed (the 2
multimodal failures are a pre-existing test-isolation issue on the base branch, reproduced there
unchanged, not introduced by the trim).

Assessed but intentionally kept (removable, but coupled to persisted schema + the frontend
citation/preview UX, so not ripped out unattended):

  • preview / chunk-locator (~4.5K incl. frontend): retrieval and the tool do not depend on it, but
    the preview routes back the citation -> source-preview UX this PR ships.
  • multimodal captioning + image embeddings (~0.5-0.9K): mode/kind/image_path are persisted
    columns the preview routes read, so removing it cascades into preview/locator and reduces the
    "many document types" coverage.

Net: the clean, fully-testable trim is ~768 lines; the larger preview/locator + multimodal
removals are documented as a coordinated frontend follow-up rather than an unattended change.

@danielhanchen

Copy link
Copy Markdown
Member Author

Superseded by #5910.

#5910 (clean-room compact RAG) already incorporates the fast-indexing
architecture this PR prototyped on feature/rag:

Since this PR is stacked on feature/rag (which #5910 replaces), it
cannot rebase onto main cleanly, and its core improvements are already
in #5910. Closing as superseded. The benchmark harness here
(studio/bench/*, RAG_FAST_INDEXING.md) remains available in the
branch history if we want to revive it as a standalone benchmarking PR
later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants