LaravelRAGVector Database

Membangun Sistem RAG di Laravel dengan Vector Search dan Model AI Open Source

Oleh Aditya Nursyahbani60 menit baca41
Bagikan:
Membangun Sistem RAG di Laravel dengan Vector Search dan Model AI Open Source

Artificial Intelligence applications are evolving rapidly, but traditional chatbots still struggle with one major problem:

They cannot access your private or domain-specific data.

This is where Retrieval-Augmented Generation (RAG) becomes extremely powerful.

A RAG system allows an AI model to search your own knowledge base before generating a response. Instead of relying only on pretrained knowledge, the AI can answer questions using your documents, PDFs, articles, database records, or company knowledge.

In this tutorial series, you will learn how to build a complete RAG pipeline in Laravel using:

  • Laravel 12
  • PostgreSQL + pgvector
  • Open source embedding models
  • Ollama
  • Llama 3
  • Vector similarity search
  • AI-powered chat API

By the end of this series, you will have a production-ready AI assistant capable of answering questions using your own data.


What is RAG?

RAG stands for Retrieval-Augmented Generation.

The workflow looks like this:

  1. User sends a question
  2. System converts the question into embeddings
  3. Vector database searches for similar documents
  4. Relevant context is injected into the prompt
  5. AI model generates the final answer

Without RAG:

text
Question β†’ AI Model β†’ Answer

With RAG:

text
Question β†’ Embedding β†’ Vector Search β†’ Context β†’ AI Model β†’ Answer

This dramatically improves:

  • Accuracy
  • Freshness of information
  • Domain-specific responses
  • Hallucination reduction

RAG Architecture Overview

Our Laravel RAG system architecture:

text
Frontend Chat UI ↓ Laravel API ↓ Embedding Service ↓ PostgreSQL + pgvector ↓ Context Retrieval ↓ Ollama + Llama 3 ↓ AI Response

Why Use Laravel for AI Applications?

Laravel is surprisingly excellent for AI applications because it already provides:

  • Queue system
  • API development
  • Database ORM
  • Authentication
  • Event system
  • Broadcasting
  • Background jobs
  • File storage
  • Scheduling

This makes Laravel ideal for:

  • AI chat applications
  • Knowledge base systems
  • AI agents
  • Document indexing
  • Semantic search
  • AI SaaS products

Choosing the Vector Database

There are several vector database options:

DatabaseProsCons
PineconeManagedPaid
WeaviatePowerfulMore complex
QdrantFastSeparate infrastructure
ElasticsearchMatureHeavy
PostgreSQL pgvectorSimple + powerfulLess specialized

For Laravel developers, PostgreSQL + pgvector is one of the best choices because:

  • Easy integration
  • SQL support
  • Open source
  • Cheap infrastructure
  • Great performance
  • Familiar ecosystem

Installing PostgreSQL with pgvector

Install PostgreSQL first.

Ubuntu:

bash
sudo apt update sudo apt install postgresql postgresql-contrib

Install pgvector:

bash
sudo apt install postgresql-16-pgvector

Enable extension:

sql
CREATE EXTENSION vector;

Verify installation:

sql
SELECT * FROM pg_extension;

Creating Laravel Project

Create new Laravel project:

bash
composer create-project laravel/laravel laravel-rag

Enter project:

bash
cd laravel-rag

Configure Database

Update .env:

env
DB_CONNECTION=pgsql DB_HOST=127.0.0.1 DB_PORT=5432 DB_DATABASE=laravel_rag DB_USERNAME=postgres DB_PASSWORD=secret

Creating the Documents Table

Create migration:

bash
php artisan make:migration create_documents_table

Migration:

php
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::create('documents', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->timestamps(); }); DB::statement('ALTER TABLE documents ADD COLUMN embedding vector(768)'); } public function down(): void { Schema::dropIfExists('documents'); } };

Why Embeddings Matter

Embeddings are numerical vector representations of text.

Example:

text
"Laravel is a PHP framework" ↓ [0.213, -0.881, 0.115, ...]

Similar texts produce similar vectors.

This allows semantic search instead of keyword search.

Example:

  • Query: "How to authenticate users?", "AI chatbot"
  • Semantic Match: "Laravel authentication tutorial", "Conversational assistant"

Installing Ollama

Ollama allows running open source AI models locally.

Install Ollama:

bash
curl -fsSL https://ollama.com/install.sh | sh

Run model:

bash
ollama run llama3

Pull embedding model:

bash
ollama pull nomic-embed-text

Creating Embedding Service in Laravel

Create service:

bash
php artisan make:service EmbeddingService

Create file manually if needed.

php
<?php namespace App\Services; use Illuminate\Support\Facades\Http; class EmbeddingService { public function embed(string $text): array { $response = Http::post('http://localhost:11434/api/embeddings', [ 'model' => 'nomic-embed-text', 'prompt' => $text, ]); return $response->json()['embedding']; } }

Storing Embeddings

Create model:

bash
php artisan make:model Document

Insert document with embedding:

php
<?php use App\Models\Document; use App\Services\EmbeddingService; $embedding = app(EmbeddingService::class) ->embed($content); Document::create([ 'title' => $title, 'content' => $content, 'embedding' => '[' . implode(',', $embedding) . ']', ]);

Next Part

In the next article, we will build:

  • Vector similarity search
  • Semantic retrieval
  • AI chat endpoint
  • Prompt engineering
  • Context injection
  • Streaming responses