Bandhu — RAG Reference

RAG components, and exactly where each one plugs in

Companion to docs/pipeline.html. That doc numbers 12 stages; this doc takes each RAG-relevant piece of infrastructure and says precisely which numbered stage it belongs to — not just "retrieval happens somewhere in the middle."

Three separate stores — don't conflate them

The easiest way to lose clarity on a RAG system is treating "the data" as one thing. Bandhu has three, and they never share a code path.

Vector database

Content Library (pgvector)

Vetted, short, human-reviewed entries — grounding techniques, reframes, thinking-trap psychoeducation. Static-ish, updated rarely, shared across every user.

Built from knowledge-base/vetted/. Embedded once, queried on every eligible check-in.
Structured store

Per-user Memory

Small, per-person facts: mood tag, theme, last suggestion + outcome, rolling help-offer count, and the Summarizer's rolling narrative. Updated every check-in.

Never embedded, never vector-searched — read/written directly by row/stage number.
Reference corpus

MITI Rubric

The tone-scoring rubric (not retrievable content) — used only by the sampled evaluator to grade a response after the fact. Never touches a live reply before it's sent.

From knowledge-base/sources/miti-manual/. Async only.

The pipeline, annotated by what touches RAG

Same 12 stages and numbering as docs/pipeline.html. Stages with a highlighted number touch one of the three stores above or a RAG-specific process; muted stages are pure logic and don't.

1
Ingest & normalize
No RAG component — language detection and format handling only.
2
Safety gate
Deliberately not RAG — rules-based keyword/pattern matching only, for auditability. No embeddings, no vector search, ever.
3
Classify

Produces the emotion·category·intensity tag that pre-filters stage 6's vector search. The special-case branch here also does a direct, non-vector lookup into the Content Library's fixed redirect templates.

feeds Retrieval's pre-filter special-case branch → fixed templates, no embedding involved
4
Memory read

Reads the per-user structured store directly, plus stage 11's rolling narrative.

Per-user Memory store
5
Eligibility gate

Reads the rolling help-offer count from the same structured store — no new component, same one as stage 4.

Per-user Memory store
6
Retrieval

The core RAG operation. Embeds the check-in text, checks the cache, queries pgvector filtered by stage 3's tag, returns top 2-3 chunks.

Embedding model Vector database Cache (Redis/Upstash)
7
Orchestrator
Consumes stage 6's output, adds no new component — except the Thinking Trap branch, which re-invokes Retrieval with a pattern-specific filter instead of the general tag.
8
Generate

The fast/cheap phrasing model — constrained to only use what stage 6 retrieved. This is the "generation" half of retrieval-augmented generation.

Generation model
9
Guardrail check
Pure rule engine — not a data component. Fixed-template content was already resolved upstream at stage 3's branch.
10
Memory write → response

Writes the new structured fact to the per-user store — the raw material stage 11 later synthesizes.

Per-user Memory store
11
Summarizer

Reads the structured store's accumulated facts, writes back a short rolling narrative — the thing stage 4 actually reads on the next check-in.

Per-user Memory store
12
Sampled evaluator

The only stage that touches the MITI rubric. Scores a sample of already-sent responses — never in the live path.

MITI Rubric corpus

Component reference

Same components as above, described in full, each tagged with exactly which stage(s) call it — the reverse lookup.

Vector store

Content Library

Short, 1-3 sentence, human-reviewed entries — never paragraphs pulled wholesale from a source document. Two distinct kinds live here: general retrievable entries (tagged, embedded, found by similarity) and fixed redirect templates (medical doubt, disorder questions, medication questions, medical-document handling — looked up directly by category, never embedded or searched).

Stage 3 — special-case lookupStage 6 — RetrievalThinking Trap re-entry
Compute

Embedding model

Turns text into a vector once, at corpus build time, for every Content Library entry (offline, one-time cost). Turns the live check-in text into a vector at query time — one small, cheap call per check-in.

Stage 6 — RetrievalThinking Trap re-entry
Vector store

Vector database (pgvector)

Holds the Content Library's embedded, tagged entries. Queried with metadata pre-filtering (emotion/category/intensity/language) before similarity search runs, so top_k can stay small — 2-3 results, never a long list.

Stage 6 — RetrievalThinking Trap re-entry
Compute

Cache (Redis/Upstash)

Stores embedding + retrieval results for common or near-duplicate check-ins, with a similarity threshold to reuse a cached result instead of re-embedding. Sits directly in front of the vector database — a cache hit skips both the embedding call and the pgvector query.

Stage 6 — Retrieval
Structured store

Per-user Memory

Not embedded, not vector-searched — a small structured record per person: current mood tag, recurring theme, last suggestion offered + whether it helped, and the rolling help-offer count the eligibility gate reads. Written every check-in, never grows into a raw transcript.

Stage 4 — readStage 5 — eligibility countStage 10 — writeStage 11 — Summarizer input
Compute · periodic

Summarizer

Not per-message. Runs on its own schedule (still undecided — see open items in pipeline.html) over the accumulated structured facts, and writes back a short rolling narrative. This is what lets a pattern from Monday actually inform Wednesday's response, and what "Let's look" / Looking Back actually shows.

Reads Stage 10's outputFeeds Stage 4Stage 11
Compute

Generation model

Fast, cheap model — its only job is phrasing. It composes the acknowledgment plus at most one optional line, using exclusively what Retrieval or the structured memory handed it. Never generates advice or content freely; the model's job is tone, not invention.

Stage 8 — GenerateThinking Trap re-entry
Reference corpus

MITI Rubric

The Motivational Interviewing Treatment Integrity coding manual — the scoring rubric, not something retrieved into a live reply. Feeds only the sampled evaluator, which grades 5-10% of already-sent responses for tone drift over time.

Stage 12 — Sampled evaluator, async only