Skip to content

Knowledge Base (RAG)

The knowledge base provides Retrieval-Augmented Generation (RAG) capabilities. Text is chunked, embedded, and stored in PostgreSQL with pgvector. The system supports hybrid search combining BM25 full-text search with vector cosine similarity, merged via Reciprocal Rank Fusion.

ComponentTechnologyPurpose
Embedding modelnomic-embed-text via Ollama/LiteLLMGenerate 768-dim embeddings
Vector storagePostgreSQL + pgvectorHNSW cosine similarity index
Full-text searchPostgreSQL tsvector + GINBM25 ranking via ts_rank
Hybrid searchReciprocal Rank Fusion (RRF)Merge BM25 and cosine results
Chunk size1000 charactersWith metadata (filePath, chunkIndex, language)

Runs both BM25 full-text and vector cosine searches independently, then merges the ranked result lists using Reciprocal Rank Fusion. Best overall recall.

Terminal window
curl -X POST http://localhost:3005/api/knowledge/search \
-H "Authorization: Bearer <token>" \
-d '{"query": "authentication flow", "mode": "hybrid"}'

Each knowledge entry stores three levels of detail to optimize context usage:

TierFieldDescriptionWhen Used
L0abstract2-3 sentence summarySearch result previews
L1overviewKey points overviewAgent context injection
L2contentFull text contentExplicit read via read_knowledge tool

Search results return L0/L1 by default. Agents use read_knowledge to load the full L2 content when needed.

Agent outputs are automatically indexed after completion when the output exceeds 100 characters. Controlled by the RAG_AUTO_INDEX environment variable (default: true). Indexed with source type agent_output.

Agents with the knowledge tool can:

  • search_knowledge — Search with query, limit, source type filter, and mode selection
  • read_knowledge — Load full L2 content for a specific entry
  • index_file — Index a file into the knowledge base
  • index_directory — Index all files in a directory (with optional glob patterns)

Uploaded documents are automatically indexed into the knowledge base after OCR text extraction and categorization. See the Document Management page.

External models (Claude Code, Gemini CLI) can search and index via MCP:

  • octipus_search_knowledge — Hybrid search
  • octipus_index_file — Index a file
EndpointMethodDescription
GET /api/knowledgeGETBrowse entries with sourceType, limit, offset filters
GET /api/knowledge/statsGETCounts by source type
GET /api/knowledge/:idGETFull entry content
POST /api/knowledge/searchPOSTSearch with query, mode, limit, sourceType
DELETE /api/knowledge/:idDELETEDelete single entry
POST /api/knowledge/indexPOSTIndex a file or directory
RoleAccessRationale
researchYesPrimary knowledge consumer/producer
codingYesLook up past solutions and patterns
reviewYesReference past decisions and standards
generalYesBroad access for general tasks
aiYesRAG system builder
writingYesReference existing documentation
dataYesLook up schemas and patterns
securityYesReference past audits and findings

The knowledge page provides:

  • Stats bar — Total entries, documents, code, and agent outputs
  • Search — Large search input with mode toggle (Hybrid / Semantic / Keyword) and source type filter
  • Browse — Paginated list of all entries when no search is active
  • Entry cards — Abstract preview, source type badge, file path, date
  • Detail modal — Full content, abstract, overview, metadata, delete and re-index buttons
  • Index dialog — Manual file/directory indexing with path, source type, and glob pattern inputs
CREATE TABLE embeddings (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
source_type TEXT NOT NULL, -- 'document', 'code', 'agent_output'
source_id TEXT NOT NULL, -- file path or agent ID
content TEXT NOT NULL, -- L2: full text chunk
abstract TEXT, -- L0: 2-3 sentence summary
overview TEXT, -- L1: key points overview
embedding vector(768) NOT NULL, -- nomic-embed-text dimension
content_tsv TSVECTOR, -- auto-populated, used for BM25
model TEXT NOT NULL,
metadata JSONB DEFAULT '{}',
created_at TIMESTAMP DEFAULT NOW()
);
CREATE INDEX ON embeddings USING hnsw (embedding vector_cosine_ops);
CREATE INDEX ON embeddings USING gin (content_tsv);
VariableDefaultDescription
RAG_AUTO_INDEXtrueAuto-index agent outputs on completion
  1. PostgreSQL with the pgvector extension installed
  2. Pull the embedding model: ollama pull nomic-embed-text
  3. Register the model in LiteLLM config with topic embedding
  4. Run migrations for the embeddings table and hybrid search columns