Overview
The Knowledge Base System is a comprehensive RAG (Retrieval-Augmented Generation) platform that enables agents to ground their responses in custom knowledge sources. It supports multiple document formats, API connectors, social media sources, and a knowledge graph for structured entity queries. Service: Knowledge Service (Port 4005)Architecture
Multi-Loader Strategy
Technology Stack
Unified Content Processing Pipeline
All KB source ingestion flows through a unified pipeline built on the ContentConnector pattern.ContentConnector Interface
Every connector implements:ConnectorRegistry Resolution
Registered Connectors
NormalizedContent Shape
Indexing Flow
1
Upload / Add Source
Document uploaded or URL source added. Frontend sends to Knowledge Service via Gateway.
2
Connector Resolution
ConnectorRegistry resolves input to the appropriate ContentConnector.3
Content Normalization
Connector fetches and returns
NormalizedContent[] with dedup keys and content hashes. For social media: one kb_document per post.4
Media Extraction
Vision text extraction (OpenRouter) for images/PDFs. Transcription for audio/video.
5
Chunking
Text chunking with RecursiveCharacterTextSplitter (1000 chars, 200 overlap).
6
Embedding
Gemini Embedding 2 generates multimodal embeddings (3072 dimensions).
7
Vector Storage
Vectors stored in ChromaDB (single vector-store port for all environments), with a sparse full-text index (
tsvector + GIN) maintained on the chunk text for hybrid retrieval.8
Graph Extraction
Async via BullMQ: entity and relationship extraction with entity seeding from existing KB entities.
9
Document Summary
Per-document summaries generated for KB map overview.
Retrieval Stack
Retrieval is a multi-stage pipeline. Everything below the fusion step is on by default and independently toggleable via config.1
Hybrid retrieval
Two channels run in parallel: dense (Gemini vector similarity via ChromaDB) and sparse (Postgres full-text
ts_rank_cd over the chunk tsvector). unified-search.service also folds in image and graph-informed discovery. Toggle: HYBRID_SEARCH_ENABLED.2
Reciprocal Rank Fusion
The ranked channels are fused by RRF (
score = Σ 1/(k + rank), k = 60), which combines by rank rather than raw score — robust to the dense-cosine vs. sparse-ts_rank scale mismatch. Lexical-only hits get a calibrated pass-through score so exact matches still clear the downstream relevance gate. Pure impl: rank-fusion.utils.3
Engagement prior (learning loop)
A precomputed per-
(org, document) boost from accumulated engagement (resource_engagement_prior) is applied to the fused scores — empirical-Bayes shrunk and floor-gated, so it only ever helps and never moves low-evidence resources. Global kill-switch LEARNING_PRIOR_ENABLED; per-KB switch knowledge_bases.learning_prior_enabled.4
Cross-encoder rerank (bot side)
The agent path re-scores fused candidates with an OpenRouter cross-encoder (
cohere/rerank-v3.5). Falls back to a lexical blend (60% vector + 25% text relevance + 15% importance weight) when RERANKER_ENABLED=false.5
Context assembly (MMR + token budget)
Post-rerank chunks are diversified with MMR (lexical Jaccard,
λ = 0.6) to drop near-duplicates, then capped by a token budget (CONTEXT_TOKEN_BUDGET, ~4000) so retrieval can’t crowd out the prompt/history. Pure impls: mmr.utils, token-budget.utils.Outcome Attribution & Learning Loop (Workstream H)
The moat: capture what an agent surfaced, what the customer did with it, and feed that back so retrieval compounds.- Signal capture —
retrieval_events(per-turn candidate set + injected chunks + final order),message_feedback(thumbs), andresource_interactions(impression / click / conversion, via a signed first-party redirect). All keyed by asignal_sourceenum whose values includeend_user_thumbs,click,conversion,hitl_correction, and the reservedcreator_training(future training mode). - Attribution —
analytics.servicejoinsretrieval_events(unnestinjected_chunk_ids) ⋈ feedback ⋈ interactions, scoped to a KB via chunk → document, to produce per-document performance, feedback-driven knowledge gaps, and the impression → click → conversion funnel (analyticssection=resource-performance|feedback-gaps|funnel). - Learning —
learning-loop.servicemines the signals into an engagement prior (refreshed by a repeatable BullMQ job) and a training-readiness score with weighted per-source authority (creator_training>hitl_correction> thumbs/conversion > click). Surfaced at analyticssection=learning. The reranker fine-tune / learned-weight replacement is a later phase that consumes this substrate once signal volume accrues.
Knowledge Graph Pipeline
Three-phase extraction:- Entity Extraction
- Entity Resolution
- Cross-Document Inference
Text, image, and video content analyzed by LLM to extract named entities (people, orgs, concepts, products) and relationships. Extraction prompts are seeded with existing KB entities so the LLM reuses canonical names.
POST /knowledge/kb/:id/enhance-graph triggers entity resolution + cross-document inference as queued jobs.
Database Schema
Citation UI System
The citation system provides interactive source attribution in chat:Backend
- Chat response includes
sourcesarray with numbered citations - Each source has:
number,kbName,source,relevanceScore,text - Citations are transient per message (not persisted to database)

