Document Q&A System with Contextual Embeddings and Vector Search - n8n Workflow

Build a powerful RAG system using this n8n workflow. Ingest research papers, generate Voyage-Context-3 contextual embeddings, store vectors in MongoDB Atlas, and run an interactive Q&A agent powered by OpenAI. This n8n templates example showcases advanced AI flows.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

AI/ML Engineers developing advanced Retrieval-Augmented Generation (RAG) knowledge bases.
Researchers or analysts who need to efficiently process and query large PDF documents.
Developers seeking robust n8n templates for handling large data volumes using subworkflows.
Users interested in implementing a multi-turn chat experience using the Respond to Chat n8n node.

Overview

This comprehensive n8n workflow addresses the challenges of processing large documents (like research papers) for highly accurate Q&A. Traditional embedding methods often struggle with document context, leading to lower retrieval accuracy. This n8n workflow solves this by utilizing Voyage-Context-3, a contextual chunk embedding model that encodes context from aggregated chunks, resulting in superior vector matching.

The overall system is split into two phases: Document Ingestion and Interactive Q&A. The ingestion process, stabilized by using an n8n subworkflow for batch processing, downloads a PDF, chunks the content, generates the embeddings, and persists both the vectors and the raw text in a MongoDB Atlas vector store. The subsequent Q&A agent uses an interactive chat n8n trigger, asking clarifying questions before performing vector search and synthesizing an answer via an OpenAI RAG model.

How it Works

This complex n8n workflow operates in two main logical paths: ingestion (run manually) and Q&A (run by the chat n8n trigger).

Document Ingestion Flow (Manual n8n Trigger)


  1. Start and Setup: The process begins with the When clicking ‘Execute workflow’ n8n trigger. The Set Variables n8n node defines the URL of the research paper. Old entries are removed from the MongoDB collection by the Clear Collection n8n node.

  2. Extraction: The Import Research Paper n8n node (HTTP Request) downloads the PDF, and the Extract from File n8n node converts it into separate pages of text.

  3. Batching & Stability: Pages are batched into groups of 10 (Batch 10) and passed to the subworkflow via the Call Embeddings Subworkflow n8n node. This approach, using an n8n subworkflow, ensures stability when processing large documents.

  4. Subworkflow Execution: Inside the subworkflow, text is broken into 1000-character chunks using a Chunk Page Text Code n8n node. These chunks are then batched (in groups of 3) and sent to the Voyage-Context-3 Embeddings API in a single bulk request, utilizing its contextual embedding capability.

  5. Storage: The returned vectors, along with the chunked text and metadata (page number, URL), are stored in the MongoDB Atlas vector store using the Insert Documents Vectors n8n node.

Interactive Q&A Flow (Chat n8n Trigger)


  1. User Input: The When chat message received n8n trigger initiates the Q&A process when a user submits a query.

  2. Clarifying Questions: An OpenAI LLM (Generate Clarifying Questions) is used to generate follow-up questions to better contextualize the user's intent. The system uses the Wait for Answer feature of the Langchain Chat n8n node in a loop to collect the user’s answers.

  3. Query Vectorization: The original query, combined with the clarifying answers, is vectorized using the Voyage-Context-3 Embeddings1 n8n node (configured for query input type).

  4. Retrieval: The query vector is used by the Perform Similarity Search MongoDB n8n node, executing a $vectorSearch aggregation query to retrieve the 10 most relevant document chunks from MongoDB Atlas.

  5. Synthesis: The retrieved documents are passed as context to the RAG Agent (OpenAI GPT-4.1-Mini), which synthesizes the final, accurate answer. The final response is delivered back to the user via the Respond to User n8n node.

Installation Guide

To use this powerful n8n workflow, follow these steps:


  1. Import the n8n Workflow: Copy the provided JSON and import it into your n8n instance via the 'New' menu > 'Import from JSON'.

  2. Voyage.ai Credentials: You need API credentials for Voyage.ai. Create an HTTP Header Auth credential named 'Voyage.ai' that sends the API Key in the header Authorization: Bearer YOURAPIKEY.

  3. MongoDB Credentials: Set up a MongoDB Atlas account (with a configured Vector Search index) and create a MongoDB credential in n8n.

  4. OpenAI Credentials: Set up an OpenAI API Key credential for the RAG agent and question generator.

  5. Ingestion Setup (Part 1): In the Set Variables n8n node, define the url of the PDF document you wish to ingest. Run the workflow manually by clicking the 'Execute workflow' n8n trigger to populate your vector store.

  6. Q&A Agent Setup (Part 2): To use the interactive chat feature, you must publish the n8n workflow to enable the public chat interface for the When chat message received n8n trigger.

Node Details

This n8n workflow relies on several specialized n8n node types for its functionality:

When chat message received (n8n trigger): The primary entry point for the Q&A system. It listens for user messages in the public chat interface.
Set Variables (n8n node): Defines the source URL for the document ingestion phase (e.g., https://arxiv.org/pdf/2402.06196).
Extract from File (n8n node): Key for RAG document processing, this node converts the imported PDF file into structured data, splitting the content by page.
Execute Workflow (n8n node): Crucial for handling large documents stably. It calls the main workflow as a subworkflow to process document pages in isolation and batches.
Code (n8n node): The Chunk Page Text code node is used for specialized text preprocessing, chunking pages into 1000-character segments with zero overlap, as recommended for contextual embeddings.
HTTP Request (Voyage-Context-3 Embeddings n8n node): Communicates with the Voyage.ai API to generate contextual embeddings. It is configured to send grouped inputs using the document input type during ingestion and the query input type during retrieval.
MongoDB (n8n node): Used extensively for both storage and retrieval. The Insert Documents Vectors n8n node persists the text chunks and their corresponding embeddings. The Perform Similarity Search n8n node executes the native MongoDB $vectorSearch aggregation query.
Information Extractor (Langchain n8n node): The Generate Clarifying Questions n8n node utilizes a structured output LLM call to generate a list of questions to improve the search context.


  • RAG Agent (OpenAI n8n node): The core intelligence, using GPT-4.1-Mini, receives the user query and the retrieved document context to formulate the final answer.

Related n8n Workflows

Free

Nodes: 21 Nodes
Updated: December 26 2025
View all
Created by

Freelance AI Automation Engineer based in London, UK. Since 2024, my n8n templates have documented my journey into applied AI and have helped hundreds of businesses and organisations get up to speed with AI automation. Today, I continue to explore use-cases as AI evolves and occasionally upload templates which I find novel and interesting. Subscribe to the RSS Feed: https://cdn.subworkflow.ai/n8n-templates/rss.xml

Featured*