๐Ÿ“– Build an OpenAI RAG system with document upload, semantic search and caching

โšก 778 views ยท ๐Ÿ“– Internal Wiki & Knowledge Base

Description

Overview

This workflow implements a complete Retrieval-Augmented Generation (RAG) system for document ingestion and intelligent querying.

It allows users to upload documents, convert them into vector embeddings, and query them using natural language. The system retrieves relevant document context and generates accurate AI responses while using caching to improve performance and reduce costs.

This workflow is ideal for building AI knowledge bases, document assistants, and internal search systems.


How It Works

1. Input & Configuration


Document Upload Flow

  1. Text Extraction

    • Extracts text from uploaded PDF documents
  2. Text Chunking

    • Splits text into overlapping chunks for better retrieval accuracy
  3. Document Structuring

    • Converts chunks into structured documents
  4. Embedding Generation

    • Generates vector embeddings using OpenAI
  5. Vector Storage

    • Stores embeddings in PGVector (Postgres)
  6. Upload Logging

    • Logs document metadata (user, filename, timestamp)
  7. Response

    • Returns success message via webhook

Query Flow

  1. Cache Check

    • Checks if query result exists in cache (last 1 hour)
  2. Cache Routing


Cache Hit Flow

  1. Format Cached Response
  1. Respond to User

Cache Miss Flow

  1. Vector Retrieval
  1. AI Answer Generation
  1. Cache Storage
  1. Response

Setup Instructions

  1. Webhook Setup

    • Configure endpoint (rag-system)
    • Send payload with:
      • action: upload / query
      • user_id
      • document or query
  2. OpenAI Setup

    • Add API credentials for:
      • Embeddings
      • Chat model
  3. Postgres + PGVector

    • Enable PGVector extension
    • Create tables:
      • documents
      • query_cache
      • upload_log
  4. Configure Parameters

    • Adjust:
      • Chunk size (e.g., 1000)
      • Overlap (e.g., 200)
      • TopK (e.g., 5)
  5. Optional Enhancements

    • Add authentication layer
    • Add multi-tenant filtering (user_id)

Use Cases


Requirements


Key Features


Summary

A complete RAG-based AI system that enables document ingestion, semantic search, and intelligent query answering. It combines vector databases, LLMs, and caching to deliver fast, accurate, and scalable AI-powered knowledge retrieval.

๐Ÿ”— Nodes Used

Postgres, Webhook, AI Agent, Embeddings OpenAI, OpenAI Chat Model, Recursive Character Text Splitter

๐Ÿ“ฅ Import

Download workflow.json and import into n8n: Workflow menu โ†’ Import from File

๐Ÿ“– Importing guide ยท ๐Ÿ”‘ Credential setup