CtrlK
BlogDocsLog inGet started
Tessl Logo

g14wxz/retrieval-threshold-guard

Enforces minimum similarity thresholds on vector retrieval to prevent RAG hallucination from weak matches.

97

Quality

97%

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Passed

No known issues

Overview
Quality
Evals
Security
Files

index.mddocs/

Retrieval Threshold Guard

Enforces minimum similarity thresholds on vector retrieval to prevent RAG hallucination from weak matches.

Overview

Vector similarity search always returns results, even when those results are semantically irrelevant. Without a hard threshold, low-confidence matches flow into LLM context and produce hallucinated answers. This tile enforces a configurable but floor-limited similarity threshold on all retrieval RPCs, guaranteeing that empty results are returned when no strong matches exist.

Reference

RPC Signature

match_documents(
  query_embedding VECTOR(1536),
  match_threshold FLOAT DEFAULT 0.75,
  match_count INT DEFAULT 10
) RETURNS TABLE (id UUID, content TEXT, similarity FLOAT)

Threshold Enforcement

  • Default: 0.75 (configurable per deployment).
  • Hard floor: 0.5 — the function raises an exception if a value below 0.5 is passed.
  • Similarity formula: 1 - (embedding <=> query_embedding) (cosine distance).

Client Usage

const { data } = await supabase.rpc('match_documents', {
  query_embedding: embedding,
  match_threshold: 0.8,
  match_count: 5,
});
// data is [] when no matches meet the threshold

Dependencies

  • hybrid-search-rrf-pattern — provides the base retrieval pattern that this tile augments with threshold guards.

Composition Position

Runs after hybrid-search-rrf-pattern establishes the base search RPC. This tile wraps or modifies that RPC to add threshold enforcement. Downstream consumers (LLM chains, API endpoints) MUST handle empty result sets gracefully.

docs

index.md

tile.json