Block 1 - Database retrieval1
- Type / Role
- @n8n/n8n-nodes-langchain.toolCode - toolCode
- Config choices
- Version 1.1
This workflow is provided as-is. Please review and test before using in production.
Video Guide I prepared a comprehensive guide demonstrating how to build a multi level retrieval AI agent in n8n that smartly narrows down search results first by file descriptions, then retrieves d...
@n8n/n8n-nodes-langchain.toolcode, n8n-nodes-base.stickynote, @n8n/n8n-nodes-langchain.chattrigger, @n8n/n8n-nodes-langchain.openai, n8n-nodes-base.supabase, n8n-nodes-base.httprequest, n8n-nodes-base.manualtrigger, n8n-nodes-base.code
This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Mark Shcherbakov.
Original n8n.io sourceI prepared a comprehensive guide demonstrating how to build a multi-level retrieval AI agent in n8n that smartly narrows down search results first by file descriptions, then retrieves detailed vector data for improved relevance and answer quality.
This workflow suits developers, AI enthusiasts, and data engineers working with vector stores and large document collections who want to enhance the precision of AI retrieval by leveraging metadata-based filtering before deep content search. It helps users managing many files or documents and aiming to reduce noise and input size limits in AI queries.
Performing vector searches directly on large numbers of document chunks can degrade AI input quality and introduce noise. This workflow implements a two-stage retrieval process that first searches file descriptions to filter relevant files, then runs vector searches only within those files to fetch precise results. This reduces irrelevant data, improves answer accuracy, and optimizes performance when dealing with dozens or hundreds of files split into multiple pieces.
This n8n workflow connects to a Supabase vector store to perform:
Multi-level Retrieval:
OpenAI Integration:
The filtered document chunks and associated metadata (like file names and URLs) are passed to an OpenAI message node that includes system instructions to guide the AI in leveraging the knowledge base and linked resources for comprehensive responses.
Custom Code Functions:
Two code nodes interact with Supabase stored procedures match_files and match_documents to perform the semantic searches with multiline metadata filtering unavailable in default vector filters.
Helper Flows and SQL Setup:
Templates and SQL scripts prepare database tables and functions, with additional flows to generate embeddings from file description summaries using OpenAI.
Preparation:
match_files and match_documents) using provided SQL scripts. Main Workflow Logic:
match_files RPC to retrieve file IDs by searching file descriptions with vector similarity thresholds and file limits. match_documents RPC to fetch vector document chunks only from those files with additional similarity filtering and count limits. This multi-tiered retrieval process improves search relevance and AI contextual understanding by smartly limiting vector search scope first to relevant files, then to specific document chunks, refining user query results.
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.
| Workflow | Two-stage document retrieval chatbot with OpenAI and Supabase vector search |
|---|---|
| Complexity | advanced |
| Nodes | 24 |
| Categories | Internal Wiki, AI RAG |
| Author | Mark Shcherbakov |
| Published | 28 Jun 2025 |
Use the JSON export at /data/workflows/5436/5436.json as the source template for this automation.
Open n8n, import the downloaded JSON, and review each node before activating the workflow.
Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.
Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.
Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.
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.
Video Guide I prepared a comprehensive guide demonstrating how to build a multi level retrieval AI agent in n8n that smartly narrows down search results first by file descriptions, then retrieves d...
Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.
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.