
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:
- User sends a question
- System converts the question into embeddings
- Vector database searches for similar documents
- Relevant context is injected into the prompt
- AI model generates the final answer
Without RAG:
textQuestion โ AI Model โ Answer
With RAG:
textQuestion โ 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:
textFrontend 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:
| Database | Pros | Cons |
|---|---|---|
| Pinecone | Managed | Paid |
| Weaviate | Powerful | More complex |
| Qdrant | Fast | Separate infrastructure |
| Elasticsearch | Mature | Heavy |
| PostgreSQL pgvector | Simple + powerful | Less 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:
bashsudo apt update sudo apt install postgresql postgresql-contrib
Install pgvector:
bashsudo apt install postgresql-16-pgvector
Enable extension:
sqlCREATE EXTENSION vector;
Verify installation:
sqlSELECT * FROM pg_extension;
Creating Laravel Project
Create new Laravel project:
bashcomposer create-project laravel/laravel laravel-rag
Enter project:
bashcd laravel-rag
Configure Database
Update .env:
envDB_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:
bashphp 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:
bashcurl -fsSL https://ollama.com/install.sh | sh
Run model:
bashollama run llama3
Pull embedding model:
bashollama pull nomic-embed-text
Creating Embedding Service in Laravel
Create service:
bashphp 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:
bashphp 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