Laravel 13 is more than a php web framework. It is a complete platform for building intelligent applications. Retrieval-Augmented Generation (RAG) is the gold standard for connecting LLMs to your private data. By combining the Laravel AI SDK with pgvector, you can turn static PDFs into a living knowledge base.
This guide covers the full pipeline. You will learn to ingest documents, generate embeddings, and query them using an AI agent.
The Foundation: PostgreSQL and pgvector
RAG requires a vector database. PostgreSQL with the pgvector extension is the best choice for Laravel developers. It integrates directly with Eloquent.
First, install the Laravel AI SDK:
composer require laravel/ai
Next, enable the vector extension in your database migration. This ensures your php developer tools are ready for high-dimensional data.
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::ensureVectorExtensionExists();
}
};
Create a document_chunks table to store your text and its mathematical representation.
Schema::create('document_chunks', function (Blueprint $table) {
$table->id();
$table->string('source_file');
$table->text('content');
$table->vector('embedding', dimensions: 1536);
$table->timestamps();
$table->index('embedding');
});
The 1536 dimension matches standard OpenAI models. If you use a different provider, adjust this number accordingly.
Ingestion: From PDF to Vector

Raw PDFs are hard for LLMs to read. You must extract the text, break it into chunks, and convert those chunks into embeddings. This process is called ingestion.
Create an Artisan command to handle the heavy lifting. You can use any PDF parser library to extract the text.
// app/Console/Commands/IngestDocuments.php
public function handle()
{
$text = $this->extractPdfText('manual.pdf');
$chunks = $this->splitIntoChunks($text, 1000);
foreach ($chunks as $chunk) {
// Convert text to a vector using the AI SDK
$embedding = Str::of($chunk)->toEmbeddings();
DocumentChunk::create([
'source_file' => 'manual.pdf',
'content' => $chunk,
'embedding' => $embedding,
]);
}
$this->info('Knowledge base updated.');
}
The toEmbeddings() method is a powerful addition to the Laravel AI SDK. It handles the API call and returns the vector array automatically.
Retrieval: Semantic Search with Eloquent

Standard keyword search fails with complex questions. Vector search finds meaning. When a user asks a question, you embed that question and find the closest matching chunks in PostgreSQL.
You can build rest api with php that leverages this similarity search.
use App\Models\DocumentChunk;
$question = "How do I reset the industrial sensors?";
$chunks = DocumentChunk::whereVectorSimilarTo(
'embedding',
$question,
0.3, // Minimum similarity threshold
true // Order by similarity
)
->limit(5)
->get();
The whereVectorSimilarTo method abstracts the complex math. It converts the user's string into a vector and performs a cosine similarity search in one step.
The Agent: Putting It All Together

The final step is the AI agent. The agent uses the retrieved chunks as context to answer the user's question accurately. The Laravel AI SDK provides a SimilaritySearch tool for this purpose.
use App\Models\DocumentChunk;
use Laravel\Ai\Tools\SimilaritySearch;
use Laravel\Ai\Agent;
class SupportAgent extends Agent
{
public function tools(): iterable
{
return [
SimilaritySearch::usingModel(DocumentChunk::class, 'embedding'),
];
}
public function instructions(): string
{
return 'You are a technical support assistant. Use the retrieved documents to answer questions. If you do not know, say so.';
}
}
By providing the DocumentChunk model, the agent knows where to look. When a user asks a question, the agent triggers the SimilaritySearch tool, reads the chunks, and generates a grounded response.
Rapid Shipping with Laravel
Laravel's ecosystem is designed for speed. Use Laravel Forge for deployment and Laravel Cloud for scalable infrastructure. Monitoring your RAG pipeline is simple with Nightwatch.
This RAG architecture prevents the LLM from hallucinating. It ensures your application provides real answers from real data.
The next wave of developers is building with AI. Your story belongs here. We would love to hear what you build with the Laravel AI SDK.