Skip to main content

Build an OpenAI RAG system with document upload, semantic search and caching

Workflow preview

Workflow preview
100%
Build an OpenAI RAG system with document upload, semantic search and caching preview
Open on n8n.io

1. Workflow Overview

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 vect...

Best for

  • Internal Wiki automation workflows
  • AI RAG automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

n8n-nodes-base.webhook, n8n-nodes-base.set, n8n-nodes-base.switch, n8n-nodes-base.extractfromfile, @n8n/n8n-nodes-langchain.textsplitterrecursivecharactertextsplitter, @n8n/n8n-nodes-langchain.documentdefaultdataloader, @n8n/n8n-nodes-langchain.embeddingsopenai, @n8n/n8n-nodes-langchain.vectorstorepgvector

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by ResilNext.

Original n8n.io source

1.1 Workflow description

Title
Build an OpenAI RAG system with document upload, semantic search and caching
Workflow name
Build an OpenAI RAG system with document upload, semantic search and caching

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

  • Receives requests via webhook (rag-system)
  • Supports two actions:
  • upload → process documents
  • query → answer questions
  • Defines:
  • Chunk size & overlap
  • TopK retrieval count
  • Database table names

Document Upload Flow

  1. Text Extraction
  • Extracts text from uploaded PDF documents
  1. Text Chunking
  • Splits text into overlapping chunks for better retrieval accuracy
  1. Document Structuring
  • Converts chunks into structured documents
  1. Embedding Generation
  • Generates vector embeddings using OpenAI
  1. Vector Storage
  • Stores embeddings in PGVector (Postgres)
  1. Upload Logging
  • Logs document metadata (user, filename, timestamp)
  1. Response
  • Returns success message via webhook

Query Flow

  1. Cache Check
  • Checks if query result exists in cache (last 1 hour)
  1. Cache Routing
  • If cached → return cached response
  • If not → proceed to retrieval

Cache Hit Flow

  1. Format Cached Response
  • Standardizes cached output format
  1. Respond to User
  • Returns cached answer with cached: true

Cache Miss Flow

  1. Vector Retrieval
  • Retrieves top relevant document chunks from PGVector
  1. AI Answer Generation
  • Uses LLM with retrieved context
  • Generates accurate, context-based answer
  1. Cache Storage
  • Saves query + response in database for reuse
  1. Response
  • Returns generated answer with cached: false

Setup Instructions

  1. Webhook Setup
  • Configure endpoint (rag-system)
  • Send payload with:
  • action: upload / query
  • user_id
  • document or query
  1. OpenAI Setup
  • Add API credentials for:
  • Embeddings
  • Chat model
  1. Postgres + PGVector
  • Enable PGVector extension
  • Create tables:
  • documents
  • query_cache
  • upload_log
  1. Configure Parameters
  • Adjust:
  • Chunk size (e.g., 1000)
  • Overlap (e.g., 200)
  • TopK (e.g., 5)
  1. Optional Enhancements
  • Add authentication layer
  • Add multi-tenant filtering (user_id)

Use Cases

  • AI document search systems
  • Internal knowledge base assistants
  • Customer support knowledge retrieval
  • Legal or compliance document analysis
  • SaaS AI chat with custom data

Requirements

  • OpenAI API key
  • Postgres database with PGVector
  • n8n instance (cloud or self-hosted)

Key Features

  • Full RAG architecture (upload + query)
  • PDF document ingestion pipeline
  • Semantic search with vector embeddings
  • Context-aware AI responses
  • Query caching for performance optimization
  • Multi-user support via metadata filtering
  • Scalable and modular design

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.

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - Webhook Trigger

Type / Role
n8n-nodes-base.webhook - webhook
Config choices
Version 2.1

Block 2 - Workflow Configuration

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 3 - Route by Action

Type / Role
n8n-nodes-base.switch - switch
Config choices
Version 3.4

Block 4 - Extract Text from Document

Type / Role
n8n-nodes-base.extractFromFile - extractFromFile
Config choices
Version 1.1

Block 5 - Text Splitter

Type / Role
@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter - textSplitterRecursiveCharacterTextSplitter
Config choices
Version 1

Block 6 - Document Loader

Type / Role
@n8n/n8n-nodes-langchain.documentDefaultDataLoader - documentDefaultDataLoader
Config choices
Version 1.1

Block 7 - OpenAI Embeddings

Type / Role
@n8n/n8n-nodes-langchain.embeddingsOpenAi - embeddingsOpenAi
Config choices
Version 1.2

Block 8 - Store Embeddings in PGVector

Type / Role
@n8n/n8n-nodes-langchain.vectorStorePGVector - vectorStorePGVector
Config choices
Version 1.3

Block 9 - Log Upload to Cache

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 10 - Respond Upload Success

Type / Role
n8n-nodes-base.respondToWebhook - respondToWebhook
Config choices
Version 1.5

Block 11 - Check Query Cache

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 12 - Cache Hit or Miss

Type / Role
n8n-nodes-base.switch - switch
Config choices
Version 3.4

Block 13 - Retrieve Relevant Chunks

Type / Role
@n8n/n8n-nodes-langchain.vectorStorePGVector - vectorStorePGVector
Config choices
Version 1.3

Block 14 - OpenAI Chat Model

Type / Role
@n8n/n8n-nodes-langchain.lmChatOpenAi - lmChatOpenAi
Config choices
Version 1.3

Block 15 - Answer Query with Context

Type / Role
@n8n/n8n-nodes-langchain.agent - agent
Config choices
Version 3

Block 16 - Save to Query Cache

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 17 - Respond with Answer

Type / Role
n8n-nodes-base.respondToWebhook - respondToWebhook
Config choices
Version 1.5

Block 18 - Format Cached Response

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 19 - Respond with Cached Answer

Type / Role
n8n-nodes-base.respondToWebhook - respondToWebhook
Config choices
Version 1.5

Block 20 - Sticky Note1

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 21 - Sticky Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 22 - Answer questions with a vector store

Type / Role
@n8n/n8n-nodes-langchain.toolVectorStore - toolVectorStore
Config choices
Version 1.1

Block 23 - Sticky Note2

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 24 - Sticky Note4

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Showing the first 24 of 33 workflow blocks. Download the JSON for the full node graph.

3. Summary Table

Workflow Build an OpenAI RAG system with document upload, semantic search and caching
Complexity advanced
Nodes 33
Categories Internal Wiki, AI RAG
Author ResilNext
Published 06 Apr 2026

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/14827/14827.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Build an OpenAI RAG system with document upload, semantic search and caching do?

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 vect...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Internal Wiki, AI RAG use case.