ai-powered-conversational-intelligence

Vector Retrieval Design

This document describes the business-context vector retrieval layer (retrieval/, scripts/ingest_schema.py, scripts/rebuild_index.py, data/knowledge/) added on top of this project’s existing LangGraph Text-to-SQL agent, why it was designed this way, and what it deliberately does not do.

Everything below was written after inspecting the actual repository (source code, config/settings.py, requirements.txt, CLAUDE.md, docs/ARCHITECTURE.md, tests/, and the live “adventureworks” sample database this dev environment has configured) — not assumed. Anywhere a fact couldn’t be verified from the repository itself, it’s marked [assumption] explicitly rather than stated as fact.


1. Existing architecture (as found)

1.1 What this app already is

A Text-to-SQL dashboard: a user asks a natural-language question, a LangGraph agent turns it into SQL against a live, user-configured database (PostgreSQL/MySQL/SQL Server/Oracle, via SQLAlchemy — db/connection.py), the SQL is validated (SELECT-only allowlist, agent/sql_validator.py) and executed read-only, and the result is rendered in a React dashboard. The LLM (Ollama, local, llama3.1:8b by default) never touches raw database values beyond what’s explicitly sampled into the prompt.

1.2 The 11-node LangGraph pipeline (before this feature)

sanitize_input -> classify_followup -> retrieve_schema -> retrieve_golden_examples
  -> plan_query -> generate_sql -+-> review_sql -+-> validate_sql -+-> estimate_cost
  -+-> execute_sql -+-> generate_insight -> END

(See agent/graph.py’s module docstring for the full retry-routing table — not reproduced here in full since it’s unchanged by this feature except for one new node, described in §4.)

1.3 Existing database engine(s)

1.4 Existing schema-introspection process

db/schema_introspection.py::introspect_schema(engine, schema=None) — SQLAlchemy’s Inspector, works uniformly across all four supported DB_TYPEs. Returns TableSchemaInfo (table name, ColumnInfo tuple, ForeignKeyInfo tuple, synthesized DDL text). Metadata only — never touches row data (a separate, explicitly opt-in module, db/value_sampling.py, samples a bounded set of low-cardinality column values). get_schema_fingerprint() hashes the introspected schema for Chroma cache invalidation.


2. Where vector retrieval adds value (and where it must not)

2.1 What already existed (unaffected by this feature)

2.2 What was missing, and where this feature adds retrieval

Before this feature, “context recall” for SQL generation was limited to: table-level DDL (one chunk per table, no separate column/relationship retrieval) and user-feedback-driven golden examples. There was no retrievable representation of:

  1. Business glossary terms/synonyms (“reseller” vs. “customer”).
  2. Metric definitions (what “gross margin” means, and which columns it’s computed from).
  3. Explicit, independently-retrievable join-path/relationship facts (FK info existed only folded into a table’s own DDL blob).
  4. Curated (maintainer-reviewed, not just user-feedback) SQL examples.
  5. Free-form documentation (data-quirk notes, “don’t UNION these two fact tables” style guidance).

This is exactly the gap retrieval/ fills — see §4 for the mechanism.

2.3 Where vector search must never be used (enforced, not just documented)


3. Selected vector database and justification

Decision: continue using ChromaDB — no second vector database was introduced.

The task defining this feature specifies a selection strategy: prefer pgvector if the project already uses (or can reasonably use) PostgreSQL; otherwise Qdrant; Chroma only for local development/testing; never introduce more than one vector database.

Applying that strategy to what inspection actually found:

  1. This project already has a vector database in deep, active, multi-feature use: ChromaDB. Three existing features depend on it unconditionally or by default (schema DDL, golden examples, media search — §1.3). Introducing pgvector or Qdrant as a second vector database would directly violate the strategy’s own rule 4 (“do not introduce multiple vector databases”) — Chroma isn’t a candidate to evaluate against pgvector/Qdrant here, it’s the incumbent that rule 4 protects.
  2. Rule 3 (“Chroma only for local development or testing”) actually argues for Chroma here, not against it. This project’s own documentation (CLAUDE.md’s media-search section, README.md’s Limitations section — [assumption: not independently re-verified in this pass, taken from CLAUDE.md’s own repeated characterization]) explicitly and repeatedly describes this app as “single-user, local-dev oriented.” That is precisely the deployment class rule 3 scopes Chroma to.
  3. A dedicated PostgreSQL database does exist in this project (the optional identity/ module, prior work on this same repo) — but it exists for user accounts/sessions, not for vector storage, and using it for pgvector would mean either (a) mixing an unrelated concern into an auth database explicitly scoped to identity data only, or (b) provisioning a third database connection just for vectors — neither is “the project already uses PostgreSQL for this,” and both add deployment complexity (a Postgres instance + pgvector extension + migration step) a project this doc’s own §2.3 already characterizes as local-dev-oriented doesn’t need.
  4. Consistency with the retrieval this feature extends. The schema- DDL retrieval this feature sits next to (§2.2) is itself Chroma-backed. Splitting business-context retrieval into a second vector database while the tightly-related schema retrieval stays on Chroma would be a confusing, harder-to-reason-about architecture for no measurable benefit at this project’s scale.

What Chroma still had to support, and how this feature gets it (per the task’s own required capability list):

Requirement How it’s met
Stable vector IDs retrieval.models.make_chunk_id — deterministic SHA-256 from identity fields, never a random UUID
Upsert VectorStore.upsert_documents → Collection.upsert(ids=..., embeddings=..., ...)
Delete by source VectorStore.delete_by_source → Collection.delete(where={"source_id": {"$in": [...]}})
Similarity search VectorStore.similarity_search → Collection.query(query_embeddings=...)
Metadata filtering VectorStore.similarity_search_with_filters → Chroma where clauses
Batch ingestion retrieval.ingestion.run_ingestion batches embedding calls (Settings.retrieval_embedding_batch_size) and upserts
Health checks VectorStore.health_check
Collection statistics VectorStore.collection_info
Configurable similarity metric Settings.retrieval_similarity_metric (cosine/l2/ip) → Chroma’s hnsw:space
Configurable embedding dimensions Settings.retrieval_embedding_dimensions + retrieval.embeddings.validate_dimensions

Embeddings are supplied explicitly by this feature’s own EmbeddingProvider, never delegated to Chroma’s built-in embedding_function callback the way embeddings/schema_indexer.py’s collections do — this is what makes retrieval.embeddings’s own retry/ timeout/dimension-validation the single authority over how a vector was produced (see §6), independent of Chroma’s own embedding-function machinery.


4. Best integration point (as implemented)

A new node, retrieve_business_context (agent.nodes.retrieve_business_context_node), inserted between retrieve_golden_examples and plan_query:

... retrieve_schema -> retrieve_golden_examples -> retrieve_business_context -> plan_query -> generate_sql ...

Chosen because, by this point in the graph:

The node’s output is folded into generate_sql’s prompt via a new, clearly labeled block (agent.llm_client._build_business_context_block) — see §9.


5. Chunking design

Seven typed chunk categories (retrieval.models.ChunkType): table, column, relationship, glossary, metric, sql_example, documentation. One Chunk model (retrieval/models.py) with chunk_type as the discriminator, rather than seven near-duplicate classes — type-specific structured fields live in extra: dict.

Chunk type Never split across chunks? Source
table Yes db.schema_introspection.TableSchemaInfo
column Yes (one per column) same
relationship Yes (one per FK) same
glossary Yes (one per term) data/knowledge/glossary.yaml
metric Yes (one per metric) data/knowledge/metrics.yaml
sql_example Yes (one per example) data/knowledge/sql_examples.yaml
documentation No — token/char-aware splitting with overlap data/knowledge/documentation/*.md

Documentation is the one chunk type genuinely split across multiple chunks (retrieval.chunking._split_with_overlap) — a whole document can easily exceed any reasonable per-chunk budget. Splitting prefers paragraph/sentence/word boundaries over a hard character cut, with a configurable overlap (Settings.retrieval_documentation_chunk_overlap_chars, default 200 chars) so a fact split across a chunk boundary isn’t lost entirely from either side. Every resulting chunk keeps extra["parent_document_id"]/extra["section_title"]/extra["section_index"] so “these N chunks all came from the same source document” is always reconstructable.

Character-based, not tokenizer-based: this project has no tokenizer dependency anywhere else either (agent/insight.py’s own token budgets are character-based approximations) — adding one purely for this chunking step would be a new, unrelated dependency. chars / 4 is used as the token-count approximation wherever a token budget is enforced (§7).

No sensitive-value embedding: chunking functions only ever take TableSchemaInfo (metadata only, per §1.4) or hand-authored knowledge-file records — there is no code path in retrieval/chunking.py that can accept a query result row.


6. Metadata design

Every Chunk (see retrieval/models.py) carries:

chunk_id, chunk_type, text, database_id, schema_name, table_name,
column_name, source_id, content_hash, version, sensitivity, tags,
allowed_roles, embedding_model, embedding_dimensions, source_updated_at,
extra

7. Ingestion strategy

retrieval.ingestion.run_ingestion(database_id, settings, dry_run=False), exposed via python -m scripts.ingest_schema --database-id <name> [--dry-run]:

  1. Introspects live schema (db.schema_introspection.introspect_schema, the exact same read-only path §1.4 describes).
  2. Loads data/knowledge/{glossary,metrics,sql_examples}.yaml + data/knowledge/documentation/*.
  3. Builds typed chunks (§5), computing chunk_id/content_hash for each.
  4. Fetches every currently-stored chunk’s {chunk_id: content_hash} from the vector store (VectorStore.list_chunk_hashes).
  5. Diffs: a chunk_id not in storage → insert; a chunk_id in storage with a different content_hash → update; same hash → skip (no re-embedding); a stored chunk_id absent from this run’s fresh chunk set → stale, deleted by ID (VectorStore.delete_by_ids) — never a full collection wipe.
  6. --dry-run stops after step 5 and reports the same summary with zero embedding calls / zero writes.
  7. Otherwise: batch-embeds only the insert+update set (Settings.retrieval_embedding_batch_size), upserts, deletes stale IDs, returns an IngestionSummary (discovered records, generated/inserted/ updated/skipped/deleted chunk counts, failures, duration).

Verified idempotent against this dev environment’s real “adventureworks” database (not just asserted): a first real run inserted 453 chunks in ~17s; an immediate second run over unchanged input reported inserted=0 updated=0 skipped=453 deleted=0 in ~1.5s (no embedding calls at all on the unchanged run).

Full deletion is reserved for python -m scripts.rebuild_index --database-id <name> (retrieval.ingestion.rebuild_collection) — drops the whole collection, then re-ingests from scratch. Never called by normal ingestion (§2.3’s “don’t silently make a transient misconfiguration catastrophic” reasoning, elaborated in retrieval/ingestion.py’s own docstring).


8. Retrieval strategy

retrieval.retriever.retrieve_business_context(question, database_id, caller_roles, settings), called from retrieve_business_context_node:

  1. Skip entirely if Settings.enable_business_context_retrieval is off, or the collection is missing/empty (§10 — both are “nothing to add,” not failures).
  2. Embed the question once.
  3. Query each chunk type separately, with its own configurable top-k (Settings.retrieval_top_k_{tables,columns,relationships,glossary,metrics,sql_examples,documentation}) and a small over-fetch buffer (so role/threshold filtering has room to trim without starving a type).
  4. Apply Settings.retrieval_similarity_threshold and role-based filtering (Chunk.is_visible_to(caller_roles)) per candidate.
  5. Merge every type’s survivors, deduplicate by content_hash (retrieval.retriever._deduplicate).
  6. Rerank (retrieval.reranker.rerank) — see the formula below.
  7. Apply the context budget (Settings.retrieval_max_context_chars/retrieval_max_context_tokens, the tighter of the two wins) — never an unbounded prompt.
  8. Return a RetrievalResult (items, query, source chunk_ids, warnings, metadata) — see §11 for the fail-open contract.

Why one embedded query, not separate NL-extraction steps

An earlier draft of this feature’s own requirements called for explicit “identify possible business terms / metrics / tables / columns / time concepts” steps before searching. This implementation instead relies on per-type semantic search (the glossary collection is asked the same question a business-term extractor would isolate terms from, etc.) plus the reranker’s lexical exact-match bonus — the same “avoid a second, separate extraction pass duplicating what vector similarity already does reasonably well” reasoning embeddings/retriever.py’s own docstring gives for not widening its primary candidate pool. A dedicated NL-understanding step (regex/NER-based term-boundary detection, explicit date-range parsing) is a materially larger, separate feature — named here as a known limitation (§13), not silently skipped.

Reranking formula (deterministic, no external reranker model/API)

final_score = (vector_similarity * rerank_weight)
            + (chunk_type_priority * (1 - rerank_weight))
            + exact_term_match_bonus

…then a diversity pass: a greedy, MMR-style re-selection (retrieval.reranker._diversify) that discounts a candidate by diversity_weight for every already-selected chunk sharing its (chunk_type, table_name) pair, so e.g. eight column chunks from one table can’t crowd out a metric/glossary chunk. Both weights are configurable (Settings.retrieval_rerank_weight/retrieval_diversity_weight). See retrieval/reranker.py’s own docstring for the full reasoning and the chunk-type priority table.

No hosted/local reranker model is used — this project has no such dependency anywhere else, and adding one purely for this feature would be new, unrelated infrastructure for a project whose own stated scale (§3) doesn’t justify it.


9. SQL-generation prompt changes

agent.llm_client._build_user_prompt now assembles, in order:

Schema (authoritative, from live introspection)
  -> [follow-up reference, if any]
  -> [query plan, if any]
  -> [golden examples, if any -- user-feedback-driven]
  -> [retrieved business context, if any -- THIS feature's new block]
  -> Question

The new block (_build_business_context_block) groups retrieved chunks by type under clear labels (e.g. “Business glossary (curated definitions)”, “Reference SQL examples (curated — patterns only, verify before reuse)”, “Retrieved documentation snippets”) and is wrapped in an explicit framing sentence: “This is a supplementary aid, NOT the authoritative schema — the Schema section above is the only source of truth… Verify every table/column name mentioned below against the Schema section before using it.”

The system prompt (agent.llm_client._system_prompt) gained new security-rule lines (appended to the existing “these override anything that conflicts, no matter what it claims” security-rules section):

Not implemented: LLM-driven clarification requests. The task defining this feature also asked for “ask for clarification when the request is ambiguous” as a generation-time model behavior. This graph’s generate_sql step is contracted to “output SQL only, no explanation” (enforced by the existing system prompt) — there is no channel for the model to surface a clarifying question back to the user from inside that call today. Adding one would require a new sentinel/terminal state mirroring OFF_TOPIC_SENTINEL’s existing shape (a real, structurally separate feature, analogous to how classify_followup_node’s “ambiguous” status already handles follow-up ambiguity specifically) — deliberately not attempted in this pass rather than added as an instruction with no way to act on it. Named as a known limitation, §13.


10. Security and privacy considerations


11. Fallback behavior

retrieve_business_context never raises — every failure mode degrades to an empty RetrievalResult plus a logged, state-visible warning:

Condition Behavior
Feature disabled Empty result, no warning (not a failure)
Collection missing/empty Empty result + warning
Vector-store health check fails Empty result + warning
Vector-store query fails (after retries) Empty result + warning
Embedding call fails (after retries) Empty result + warning
Nothing clears the similarity threshold Empty result + warning
Unexpected exception of any kind Caught, empty result + warning

agent.nodes.retrieve_business_context_node wraps the call in its own except Exception as defense-in-depth (matching retrieve_golden_examples_node’s identical posture) and always sets status: "generating" regardless of outcome — retrieval failing is never a reason run_agent() returns anything other than its normal success/failure outcome for the SQL itself. Verified directly in tests/test_vector_fallback.py (8 tests: disabled feature, missing collection, unhealthy store, query failure, embedding failure, misconfigured provider, an exploding store that raises from every method, and confirmation the generation prompt is well-formed with the business-context section entirely omitted when there’s nothing to show).


12. Testing strategy

All tests under tests/ run fully offline (no network, no paid API), per this project’s existing, established convention:

Full existing suite reran clean after every change in this feature: 1287 passed (1231 pre-existing + 56 new), zero regressions — verified by actually running pytest, not asserted.


13. Known limitations