GraphRAG System Documentation: Laravel, MongoDB, Neo4j & n8n
This document outlines the architecture and implementation of a sophisticated Retrieval-Augmented Generation (RAG) system using a GraphRAG approach. The system is built on a modern stack designed for scalability, data sovereignty, and powerful contextual retrieval.English Version
1. Architectural Overview
The system is designed to ingest unstructured documents (e.g., PDF, DOCX) from a MinIO file store, process them, and build a rich, interconnected knowledge base in Neo4j. This knowledge base serves as the intelligent backend for a RAG application built with Laravel. MongoDB acts as the primary application database and a persistent “golden record” store for ingested documents.1.1 Core Components
- Laravel 12: The core web application framework, providing API endpoints and business logic.
- MongoDB: The main application database. In this RAG context, it stores metadata and the original text content of processed documents, acting as an archival source of truth.
- Neo4j: The heart of the RAG engine. It serves a dual purpose:
- Knowledge Graph: Stores entities and their relationships.
- Vector Database: Stores text chunks and their vector embeddings, with a native vector search index.
- MinIO (S3-Compatible): The primary object storage for raw, unstructured files (PDFs, DOCX, etc.).
- docling (API Service): An external service used for converting various document formats into clean, plain text.
- n8n: A workflow automation tool used for event-driven, real-time ingestion pipelines.
- Artisan Commands: Laravel’s command-line interface, used for batch processing, backfilling, and scheduled tasks.
1.2 Data Flow Diagram
Ingestion Workflow:2. Implementation Details
2.1 Neo4j Setup
Ensure you are using Neo4j 5.11 or newer. First, create the vector index.2.2 MongoDB Schema
Thedocuments collection will track the state of each file.
2.3 Laravel Artisan Command (Glue Logic)
This command is the core of your batch processing system. It finds documents in MongoDB that need processing and populates the Neo4j knowledge graph.php artisan make:command ProcessDocumentsForRag
app/Console/Commands/ProcessDocumentsForRag.php:
2.4 n8n Ingestion Workflow (Real-time)
For immediate processing of uploaded files.- Trigger:
MinIO Triggernode (listens fors3:ObjectCreated:*events) or aWebhooknode that MinIO can call. - HTTP Request (Docling): Call the docling API. Pass the pre-signed URL of the new MinIO object.
- MongoDB Node: Create a new document in the
documentscollection with the extracted text and astatusofpending. - Execute Command Node / HTTP Request (Laravel): Trigger the Artisan command for the specific new document ID (
php artisan rag:process-documents --force-id=...) or call a dedicated API endpoint in your Laravel app that kicks off the processing job.
2.5 Laravel RAG Query Logic
This is the code that runs when a user asks a question.app/Services/GraphService.php
app/Http/Controllers/RAGController.php
Versi Bahasa Indonesia
1. Gambaran Umum Arsitektur
Sistem ini dirancang untuk menyerap (ingest) dokumen tidak terstruktur (misalnya PDF, DOCX) dari penyimpanan file MinIO, memprosesnya, dan membangun basis pengetahuan (knowledge base) yang kaya dan saling terhubung di Neo4j. Basis pengetahuan ini berfungsi sebagai backend cerdas untuk aplikasi RAG yang dibangun dengan Laravel. MongoDB bertindak sebagai basis data aplikasi utama dan tempat penyimpanan “golden record” yang persisten untuk dokumen yang diserap.1.1 Komponen Inti
- Laravel 12: Kerangka kerja (framework) aplikasi web inti, menyediakan endpoint API dan logika bisnis.
- MongoDB: Basis data aplikasi utama. Dalam konteks RAG ini, MongoDB menyimpan metadata dan konten teks asli dari dokumen yang diproses, berfungsi sebagai sumber kebenaran (source of truth) untuk arsip.
- Neo4j: Jantung dari mesin RAG. Komponen ini memiliki dua fungsi utama:
- Knowledge Graph: Menyimpan entitas dan hubungan antar entitas tersebut.
- Vector Database: Menyimpan potongan teks (chunk) dan vector embedding-nya, dilengkapi dengan indeks pencarian vektor native.
- MinIO (S3-Compatible): Penyimpanan objek utama untuk file mentah yang tidak terstruktur (PDF, DOCX, dll.).
- docling (Layanan API): Layanan eksternal yang digunakan untuk mengubah berbagai format dokumen menjadi teks biasa (plain text) yang bersih.
- n8n: Alat otomatisasi alur kerja (workflow) yang digunakan untuk pipeline ingesti event-driven secara real-time.
- Perintah Artisan (Artisan Command): Antarmuka baris perintah (CLI) dari Laravel, digunakan untuk pemrosesan batch, pengisian data historis (backfilling), dan tugas terjadwal.
1.2 Diagram Alur Data
Alur Kerja Ingesti (Ingestion Workflow):2. Detail Implementasi
2.1 Pengaturan Neo4j
Pastikan Anda menggunakan Neo4j versi 5.11 atau yang lebih baru. Pertama, buat indeks vektor.2.2 Skema MongoDB
Koleksidocuments akan melacak status dari setiap file.
2.3 Perintah Artisan Laravel (Logika Perekat)
Perintah ini adalah inti dari sistem pemrosesan batch Anda. Perintah ini mencari dokumen di MongoDB yang perlu diproses dan mengisi knowledge graph di Neo4j.php artisan make:command ProcessDocumentsForRag
app/Console/Commands/ProcessDocumentsForRag.php:
2.4 Alur Kerja Ingesti n8n (Real-time)
Untuk pemrosesan file yang baru diunggah secara langsung.- Pemicu (Trigger): Node
MinIO Trigger(mendengarkan events3:ObjectCreated:*) atau nodeWebhookyang dapat dipanggil oleh MinIO. - HTTP Request (Docling): Panggil API docling. Kirim URL pre-signed dari objek baru di MinIO.
- Node MongoDB: Buat dokumen baru di koleksi
documentsdengan teks yang telah diekstrak danstatuspending. - Node Execute Command / HTTP Request (Laravel): Picu perintah Artisan untuk ID dokumen yang spesifik (
php artisan rag:process-documents --force-id=...) atau panggil endpoint API khusus di aplikasi Laravel Anda yang memulai prosesnya.
2.5 Logika Kueri RAG di Laravel
Ini adalah kode yang berjalan ketika pengguna mengajukan pertanyaan.app/Services/GraphService.php
app/Http/Controllers/RAGController.php
