This repository contains a detailed tutorial for building an RAG (Retrieval-Augmented Generation) AI Agent in n8n, capable of answering questions based on documents stored in a vector database.
Create an AI agent that can answer questions based on stored documents using GPT-4o mini from OpenAI, PostgreSQL for memory, and Supabase Vector Store for vector storage.
- n8n - Automation platform
- OpenAI GPT-4o mini - AI model
- PostgreSQL - AI agent memory
- Supabase Vector Store - Vector storage
- Google Drive - Document upload
The AI agent workflow involves:
- Connecting OpenAI's API to PostgreSQL and Supabase Vector Store
- Configuring the AI agent's memory in PostgreSQL
- Storing documents in Supabase Vector Store
- Creating a Google Drive trigger to capture new documents
- Processing files and generating embeddings with OpenAI
- Using n8n to manage the data flow
The test documents contain fictional questions and answers for a virtual Customer Support assistant, generated by ChatGPT.
- Add a message trigger
- Use the node "Advanced AI / AI Agent"
- The AI agent will respond and decide when to consult the database
- Configure the "AI Agent"
- Chat Model: Create OpenAI credentials
- Memory: Choose PostgreSQL (using Supabase)
- Create a new project in Supabase (e.g.,
n8n AI Agent - Date - DEMO Z
) - Set a password for the database
- Ensure the project is active (Check Project Status)
- Go to Project Settings > Database > Connection Parameters
- Create a new PostgreSQL credential in n8n
- Configure the parameters:
- Host, Port, User, Password (obtained from Supabase)
- Add the vector storage tool
- Name and description
- Select Supabase Vector Store
- Connect to the Supabase account (Project Settings > API)
- Copy URL and secret key into n8n
- Create the storage table
- In Supabase SQL Editor, run the script from n8n documentation (Quickstart for setting up your vector store)
- Choose Table Name:
documents
- Add option:
match_documents
- Create a folder in Google Drive
- In n8n, add a trigger "Google Drive - On Changes on Specific Folder"
- Select event: When a document is created
- Upload documents to the Google Drive folder
- Run "Fetch Test Event" in n8n to verify document detection
- Convert the document to a processable format
- Google File Conversion: TXT to CSV
- Download the document
- Google Drive - Download File by ID
- Transform the file
- Data Transformation: Extract from text file
- Ensure no duplicates in Supabase
- Delete previous records in Supabase Vector Store
- Add the condition
operation: delete / filters (string):
metadata->>file_id=like.*{{$json.id}}*
- Add OpenAI embeddings
- Define chunk size and overlap for document splitting
- Use the "Recursive Character Test" model
- Save settings and test agent queries
Your AI agent is now ready to answer questions based on stored documents! 🚀
It can retrieve information from the vector database and generate intelligent responses using the GPT-4o mini model.
If you need assistance or want to contribute, feel free to open an issue or submit a pull request!