StackBlueprint

EP03 ARCHITECTURE · PGVECTOR HNSW + BM25 RRF SUPPORT AI

Self-Hosted n8n + pgvector Hybrid RAG Support AI Guide

Replace $852/mo Intercom Fin AI + Copilot ($10,224/yr, $0.99/resolution) by swapping postgres:17-alpine for pgvector/pgvector:pg17. Run HNSW + BM25 Reciprocal Rank Fusion, deterministic citation gates, and Slack HITL at $9.38–$15.58/mo ($0.000587/ticket).

Intercom Fin AI + Copilot $852.00/mo $10,224/yr · 4 seats + 400 Fin resolutions ($0.99 ea)
Self-Hosted n8n + pgvector $9.38–$15.58/mo 1,000 tickets/mo · $0.000587/ticket (1,687× cheaper)
Annual Savings & Payback $10,111.48/yr Kit v3 ($49) pays back in 1.7 days

1. Hybrid HNSW + BM25 Reciprocal Rank Fusion SQL (EP03 Kit)

A separate vector database adds 1–2 GB of RAM overhead and splits backup consistency. AI Support & pgvector RAG Kit v3 keeps dense vector(1536) HNSW embeddings and generated tsvector GIN indexes inside the same PostgreSQL 17 database, fused by search_kb_hybrid_rrf():

CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE IF NOT EXISTS kb_chunks (
  id BIGSERIAL PRIMARY KEY,
  document_id BIGINT NOT NULL REFERENCES kb_documents(id) ON DELETE CASCADE,
  chunk_index INTEGER NOT NULL,
  chunk_text TEXT NOT NULL,
  token_count INTEGER NOT NULL CHECK (token_count > 0),
  embedding vector(1536) NOT NULL,
  tsv tsvector GENERATED ALWAYS AS (to_tsvector('english', chunk_text)) STORED,
  UNIQUE (document_id, chunk_index)
);

CREATE INDEX IF NOT EXISTS idx_kb_chunks_embedding_hnsw
  ON kb_chunks USING hnsw (embedding vector_cosine_ops)
  WITH (m = 16, ef_construction = 64);
CREATE INDEX IF NOT EXISTS idx_kb_chunks_tsv_gin
  ON kb_chunks USING gin (tsv);

CREATE OR REPLACE FUNCTION search_kb_hybrid_rrf(
  query_text TEXT,
  query_embedding vector(1536),
  match_count INTEGER DEFAULT 5,
  min_similarity DOUBLE PRECISION DEFAULT 0.72,
  rrf_k INTEGER DEFAULT 60
)
RETURNS TABLE (
  chunk_id BIGINT, source_url TEXT, title TEXT, chunk_text TEXT,
  cosine_similarity DOUBLE PRECISION, lexical_rank DOUBLE PRECISION, rrf_score DOUBLE PRECISION
) LANGUAGE sql STABLE AS $$
  WITH dense_candidates AS (
    SELECT c.id AS chunk_id,
      (1 - (c.embedding <=> query_embedding))::double precision AS cosine_similarity,
      ROW_NUMBER() OVER (ORDER BY c.embedding <=> query_embedding ASC) AS dense_rank
    FROM kb_chunks c JOIN kb_documents d ON d.id = c.document_id
    WHERE d.is_active = TRUE AND (1 - (c.embedding <=> query_embedding)) >= min_similarity
    ORDER BY c.embedding <=> query_embedding ASC LIMIT GREATEST(match_count * 4, 20)
  ),
  lexical_candidates AS (
    SELECT c.id AS chunk_id,
      ts_rank_cd(c.tsv, websearch_to_tsquery('english', query_text))::double precision AS lexical_rank,
      ROW_NUMBER() OVER (ORDER BY ts_rank_cd(c.tsv, websearch_to_tsquery('english', query_text)) DESC) AS lex_rank
    FROM kb_chunks c JOIN kb_documents d ON d.id = c.document_id
    WHERE d.is_active = TRUE AND c.tsv @@ websearch_to_tsquery('english', query_text)
    ORDER BY lexical_rank DESC LIMIT GREATEST(match_count * 4, 20)
  ),
  fused AS (
    SELECT COALESCE(dc.chunk_id, lc.chunk_id) AS chunk_id,
      COALESCE(dc.cosine_similarity, 0.0) AS cosine_similarity,
      COALESCE(lc.lexical_rank, 0.0) AS lexical_rank,
      (COALESCE(1.0 / (rrf_k + dc.dense_rank), 0.0) +
       COALESCE(1.0 / (rrf_k + lc.lex_rank), 0.0))::double precision AS rrf_score
    FROM dense_candidates dc
    FULL OUTER JOIN lexical_candidates lc ON lc.chunk_id = dc.chunk_id
  )
  SELECT c.id, d.source_url, d.title, c.chunk_text, f.cosine_similarity, f.lexical_rank, f.rrf_score
  FROM fused f JOIN kb_chunks c ON c.id = f.chunk_id JOIN kb_documents d ON d.id = c.document_id
  ORDER BY f.rrf_score DESC, f.cosine_similarity DESC LIMIT match_count;
$$;

2. Loopback-Only pgvector Compose Overlay & Safety Gates

Kit v3 overlays onto your EP01 host with zero 0.0.0.0 bindings (all local host ports restricted to 127.0.0.1) and an optional MIT-licensed Chatwoot CE inbox profile ($0/seat):

# Stack Kit v3 on top of EP01 with zero public DB ports (127.0.0.1 only):
docker compose --env-file support-rag.env \
  -f docker-compose.yml -f docker-compose.rag.override.yml up -d

# Key service invariants in docker-compose.rag.override.yml:
# postgres:     image: pgvector/pgvector:pg17  | ports: ["127.0.0.1:5432:5432"] | mem_limit: 1536m
# chatwoot-web: image: chatwoot/chatwoot:v4.1.0 | ports: ["127.0.0.1:3000:3000"] | profiles: ["chatwoot"]

Affiliate disclosure: Hostinger and Apify links are affiliate links; we may earn a commission at no extra cost to you. See Affiliate Disclosure. Published by Oriancy.

Frequently Asked Questions

Why combine pgvector HNSW cosine similarity with GIN tsvector BM25 via Reciprocal Rank Fusion?
Pure vector search captures semantic phrasing but drops exact error codes, SKUs, and plan names. Fusing HNSW cosine similarity with PostgreSQL ts_rank_cd via RRF (k=60) in one SQL function improves both recall and precision without deploying a second vector database.
What is the unit cost per ticket compared to Intercom Fin AI?
For 1,000 tickets/month, OpenAI text-embedding-3-small query embeddings ($0.0020) plus gpt-4o-mini input ($0.3750) and output ($0.2100) cost $0.5870/mo ($0.000587/ticket) — 1,687× cheaper than Intercom Fin’s $0.99/resolution fee.
How do you stop an AI support bot from hallucinating refund promises or falling for prompt injection?
Fence customer input in <untrusted_customer_message> with zero tool execution, enforce deterministic cited_chunk_ids membership checks, hard-escalate refund/legal/outage intents, and keep AUTO_REPLY_ENABLED=false in Slack HITL mode until nightly golden evals pass 95% citation accuracy.