PHPackages                             rubyat/laravel-rag - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. rubyat/laravel-rag

ActiveLibrary[Search &amp; Filtering](/categories/search)

rubyat/laravel-rag
==================

Add semantic search and Retrieval-Augmented Generation (RAG) to any Laravel app with PostgreSQL + pgvector.

v0.1.1(1mo ago)10MITPHPPHP ^8.3

Since Jun 2Pushed 1mo agoCompare

[ Source](https://github.com/rubyat/laravel-rag)[ Packagist](https://packagist.org/packages/rubyat/laravel-rag)[ RSS](/packages/rubyat-laravel-rag/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (3)Versions (4)Used By (0)

rubyat/laravel-rag
==================

[](#rubyatlaravel-rag)

[![Latest Version](https://camo.githubusercontent.com/8fdee0d5c09e4f371ffd89b50c215dda983e3934aaecc0a5688216706f70ca60/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7275627961742f6c61726176656c2d7261672e737667)](https://packagist.org/packages/rubyat/laravel-rag)[![Total Downloads](https://camo.githubusercontent.com/b9aae5536f50ecf0bd533a1237b3e9b47d1a6a1564b143032866db4716f51ba4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7275627961742f6c61726176656c2d7261672e737667)](https://packagist.org/packages/rubyat/laravel-rag)[![License](https://camo.githubusercontent.com/43dcb0b520657f180404af5633e9d33a8eff8a3e8c8a3d2ffc9382eeae6579d1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7275627961742f6c61726176656c2d7261672e737667)](LICENSE)

A production-ready Retrieval-Augmented Generation (RAG) toolkit for Laravel with PostgreSQL + pgvector. Ingest documents, store embeddings, and answer questions grounded **only** in your own content — with source citations.

Features
--------

[](#features)

- 🧩 **Document ingestion** — text is chunked (configurable size/overlap), embedded, and stored.
- 🔎 **pgvector vector search** — cosine similarity over an **HNSW** index for high recall at any scale.
- 💬 **RAG pipeline** — retrieves the most relevant chunks and asks an LLM to answer from them.
- 📎 **Source citations** — every answer returns the chunks it used, with similarity scores.
- 🔌 **Driver-based** — swap embedding/chat providers; OpenAI built-in, plus deterministic `fake` drivers for offline dev and tests (no API key, no network).
- ⚙️ **Sync or queued ingestion** — inline by default, or dispatch a job for large documents.
- 🌐 **HTTP API** — ready-to-use `POST /api/rag/ingest` and `POST /api/rag/ask` endpoints.
- ✅ **Tested** — ships with a Pest suite that runs against pgvector using the fake drivers.

Requirements
------------

[](#requirements)

- PHP **8.3+**
- Laravel **11, 12, or 13**
- PostgreSQL with the [`pgvector`](https://github.com/pgvector/pgvector) extension (**0.5+** for HNSW)

Installation
------------

[](#installation)

```
composer require rubyat/laravel-rag
```

Publish the config and run the migration:

```
php artisan vendor:publish --tag=rag-config
php artisan migrate
```

> The migration runs `CREATE EXTENSION IF NOT EXISTS vector`, which needs a database role allowed to create extensions. The [pgvector Docker image](https://hub.docker.com/r/pgvector/pgvector) (`pgvector/pgvector:pg16`) is the quickest way to get it locally.

Configure a provider (or keep the offline `fake` drivers — no network needed):

```
OPENAI_API_KEY=sk-...
RAG_EMBEDDING_DRIVER=openai
RAG_CHAT_DRIVER=openai
```

Quick Start
-----------

[](#quick-start)

### In code

[](#in-code)

```
use Rubyat\LaravelRag\Ingestion\DocumentIngestor;
use Rubyat\LaravelRag\Rag\RagPipeline;

// Ingest a document (chunk -> embed -> store in pgvector)
app(DocumentIngestor::class)->ingest('handbook.md', $longText);

// Ask a question grounded in the ingested chunks
$result = app(RagPipeline::class)->ask('How does pgvector search work?');

$result['answer'];     // string — grounded in your documents
$result['citations'];  // array — [{ source, chunk_index, content, score }, ...]
```

### HTTP API

[](#http-api)

Ingest:

```
curl -X POST http://localhost:8000/api/rag/ingest \
  -H "Content-Type: application/json" \
  -d '{"source": "handbook.md", "content": "Postgres pgvector stores embeddings and supports cosine similarity search..."}'
# => 201 { "message": "Document ingested.", "source": "handbook.md", "chunks": 1 }
```

Ask:

```
curl -X POST http://localhost:8000/api/rag/ask \
  -H "Content-Type: application/json" \
  -d '{"question": "How does pgvector search work?", "top_k": 4}'
# => 200
# {
#   "answer": "pgvector stores embeddings and supports cosine similarity search...",
#   "citations": [
#     { "source": "handbook.md", "chunk_index": 0, "content": "...", "score": 0.83 }
#   ]
# }
```

Configuration
-------------

[](#configuration)

Published to `config/rag.php`. Every option is environment-driven:

OptionEnvDefaultDescription`embedding_driver``RAG_EMBEDDING_DRIVER``openai``openai` or `fake``chat_driver``RAG_CHAT_DRIVER``openai``openai` or `fake``dimensions``RAG_DIMENSIONS``1536`Vector size; must match the embedding model`chunk_size``RAG_CHUNK_SIZE``1000`Chunk length in characters`chunk_overlap``RAG_CHUNK_OVERLAP``200`Overlap between chunks`top_k``RAG_TOP_K``4`Chunks retrieved per question`queue_ingestion``RAG_QUEUE_INGESTION``false`Dispatch ingestion to the queue (needs a worker)`register_routes``RAG_REGISTER_ROUTES``true`Auto-register the HTTP routes`route_prefix``RAG_ROUTE_PREFIX``api/rag`Prefix for the package routesThe `fake` drivers are deterministic and require no network access — they power the test suite and let you try the full flow without API keys.

Swappable drivers
-----------------

[](#swappable-drivers)

Embedding and chat providers are resolved from config and implement small contracts, so you can plug in your own:

```
use Rubyat\LaravelRag\Contracts\EmbeddingDriver;
use Rubyat\LaravelRag\Contracts\ChatDriver;
```

Built in: `OpenAiEmbeddingDriver` / `OpenAiChatDriver` (HTTP) and `FakeEmbeddingDriver` / `FakeChatDriver` (offline, deterministic).

How it works
------------

[](#how-it-works)

```
            ingest                                   ask
              │                                        │
              ▼                                        ▼
      DocumentChunker                          VectorRetriever
              │ chunks                                 │ query embedding
              ▼                                         ▼
      EmbeddingDriver ─── embeddings ──►  pgvector documents (HNSW, cosine)
                                                        │ top-k chunks
                                                        ▼
                                                  RagPipeline
                                                  (context + LLM)
                                                        ▼
                                              answer + citations

```

Documents are stored one row per chunk in the `documents` table (`source`, `chunk_index`, `content`, `metadata`, `embedding vector(n)`), indexed with HNSW (`vector_cosine_ops`). Retrieval orders by cosine distance (`embedding  :query`) and returns a similarity score in `[0, 1]`.

Testing
-------

[](#testing)

The suite runs against PostgreSQL + pgvector using the deterministic `fake` drivers, so no API keys or network are required:

```
./vendor/bin/pest
```

License
-------

[](#license)

The MIT License (MIT). See [LICENSE](LICENSE).

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance90

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

53d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/14076245?v=4)[MD Rubyat Islam](/maintainers/rubyat)[@rubyat](https://github.com/rubyat)

---

Top Contributors

[![rubyat](https://avatars.githubusercontent.com/u/14076245?v=4)](https://github.com/rubyat "rubyat (4 commits)")

---

Tags

embeddingslaravelopenaipgvectorphpragsemantic-searchvector-searchlaravelopenaiembeddingsragpgvectorvector-searchsemantic-search

### Embed Badge

![Health badge](/badges/rubyat-laravel-rag/health.svg)

```
[![Health](https://phpackages.com/badges/rubyat-laravel-rag/health.svg)](https://phpackages.com/packages/rubyat-laravel-rag)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3345.3M347](/packages/psalm-plugin-laravel)[laravel/ai

The official AI SDK for Laravel.

1.0k3.2M246](/packages/laravel-ai)[api-platform/laravel

API Platform support for Laravel

58174.6k17](/packages/api-platform-laravel)[simplestats-io/laravel-client

Server-side analytics for Laravel that follows the full funnel from visit to registration to payment, attributed to the channel that drove it. Revenue, MRR, churn and ad-spend profit (ROAS/CAC) per channel. GDPR compliant, ad-blocker proof.

5022.6k](/packages/simplestats-io-laravel-client)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

255.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
