Back to Recipes

RAG Over Company Documents

Ingest company documents, chunk and embed them, then let users ask questions against your knowledge base with cited answers.

Best for: Companies with internal documentation, knowledge bases, or policy manuals

What You Get

  • -Document ingestion pipeline
  • -Smart chunking with overlap strategy
  • -Vector embedding and indexing
  • -Semantic search with hybrid retrieval
  • -Cited answer generation

Step by Step

1. Set up PostgreSQL with pgvector

Install pgvector extension. Create tables: documents (id, title, file_type, created_at), chunks (id, document_id, content, chunk_index, embedding vector(1536)), and set up a HNSW index on the embedding column for fast similarity search.

2. Build the document ingestion pipeline

Accept uploads of PDF, DOCX, TXT, and Markdown files. Extract text using appropriate parsers (pdf-parse for PDF, mammoth for DOCX). Generate a unique document ID and store metadata.

3. Implement smart chunking

Use recursive character text splitting: chunk size of 1000 characters with 200-character overlap. For each chunk, store the document_id, chunk_index, and content. Handle edge cases: tables, code blocks, headers.

4. Generate and store embeddings

For each chunk, generate an embedding using text-embedding-3-small (1536 dimensions). Batch process chunks (20 at a time) to respect API rate limits. Store embeddings in pgvector.

5. Build the search API

Create a query endpoint that: generates an embedding for the query, performs cosine similarity search via pgvector, optionally adds keyword BM25 fallback, and returns top 5 chunks with document source and relevance scores.

6. Implement answer generation

Use OpenAI to generate answers from retrieved chunks. Prompt includes: the question, the retrieved chunks with source citations, and instructions to cite sources. Return answer with references.

7. Build the chat UI and admin panel

Create a chat interface with message history, source references displayed as collapsible citations, and an admin panel to upload/manage documents and view ingestion status.

Stack

PostgreSQL + pgvectorOpenAI embeddingsOpenAINext.jsLangChain or custom pipeline

Build This

Copy this prompt and paste it into Claude Code, OpenCode, Codex, or Cursor to build this recipe.

Build a RAG system over company documents using PostgreSQL with pgvector. ROLE: You are a retrieval-augmented generation system that ingests, chunks, embeds, and retrieves company documents to answer user questions with cited answers. CONSTRAINTS: - Maximum document size: 50MB per file; maximum 10,000 documents per instance - Chunk size: 1000 characters with 200-character overlap (recursive character splitting) - Embedding model: text-embedding-3-small (1536 dimensions) - Retrieval: top 5 chunks with minimum similarity score of 0.7 - All answers must cite at least one source chunk; refuse to answer if no relevant chunks found - Ingestion pipeline must handle PDF, DOCX, TXT, and Markdown formats TOOL CALLING: - Use function calling for: upload_document(file_path, file_type), search_chunks(query, top_k?, min_score?), get_document(doc_id), list_documents(page?, filter?), delete_document(doc_id) - Each tool returns structured JSON with document metadata and chunk content STRUCTURED OUTPUT: - Search results must return JSON: { chunks: [{ content: string, document_id: string, document_title: string, chunk_index: number, score: number }], total_results: number, query_time_ms: number } - Answer must return JSON: { answer: string, sources: [{ document_title: string, chunk_index: number, excerpt: string, relevance: number }], confidence: number } - Ingestion status must return JSON: { document_id: string, status: 'processing' | 'completed' | 'failed', chunks_created: number, embedding_time_ms: number, error?: string } CHAIN OF THOUGHT: - Query analysis: decompose the question into search terms, consider synonyms, identify required context - Retrieval: compare semantic similarity AND keyword overlap; boost chunks that match both - Answer generation: synthesize from multiple chunks, resolve conflicts between sources, cite each claim FEW-SHOT EXAMPLES: Query: 'What is our refund policy?' Retrieved chunks: [ { content: 'Refund Policy: Full refund within 30 days of purchase...', document_title: 'Policies.docx', score: 0.92 }, { content: 'Customer complaints about refunds should be escalated...', document_title: 'Support Playbook.md', score: 0.78 } ] Answer: 'Our refund policy allows a full refund within 30 days of purchase [Source: Policies.docx, Section 3]. For customer complaints about refunds, escalate to the support lead [Source: Support Playbook.md].' EVALUATION CRITERIA: - Retrieval precision: percentage of retrieved chunks that are relevant to the query - Answer accuracy: does the answer correctly address the question using the retrieved chunks? - Citation completeness: every claim in the answer has at least one source citation - Ingestion reliability: percentage of documents successfully chunked and embedded The system should: 1) Accept document uploads (PDF, DOCX, TXT, Markdown) via API or admin UI, 2) Extract text, chunk with overlap, generate embeddings, and store in pgvector, 3) Perform hybrid search (semantic + keyword) on user queries, 4) Generate cited answers using OpenAI with source references, 5) Provide a chat UI with collapsible source citations, 6) Include an admin panel for document management and ingestion monitoring.

Common Failure Modes

  • !Poor chunking strategy for different document types
  • !Embedding costs at scale
  • !Retrieving irrelevant chunks
  • !Hallucination from weak retrieval

Implementation Notes

Start with 10-20 documents to tune chunking. Monitor embedding API costs. Test retrieval quality before building the UI.

Related skill: rag document ingestion

Ship rag over company documents in production with 4M Labs

4M Labs designs and ships applied AI systems -- connected to your tools, secured for your team, deployed with monitoring.

  • Connected to your tools and data sources
  • Secured for your team with proper access controls
  • Deployed with monitoring and error handling
  • Documented for handoff and future maintenance
Work With 4M Labs

Frequently Asked Questions

Can I use this recipe in production?
Yes. Every recipe is production-tested with error handling, logging, and deployment guidance.
Which LLM providers are supported?
Recipes support OpenAI, Anthropic Claude, Google Gemini, and open-source models via a unified interface.
How do I customize these recipes?
Each recipe includes a configuration section. Override model selection, API keys, and parameters without changing core logic.