PHP code example of thaolaptrinh / laravel-rag
1. Go to this page and download the library: Download thaolaptrinh/laravel-rag library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
thaolaptrinh / laravel-rag example snippets
'rag' => [
'driver' => 'pgsql',
'host' => env('RAG_DB_HOST', '127.0.0.1'),
'port' => env('RAG_DB_PORT', '5432'),
'database' => env('RAG_DB_DATABASE', 'laravel'),
'username' => env('RAG_DB_USERNAME', 'postgres'),
'password' => env('RAG_DB_PASSWORD', ''),
'prefix' => '',
'search_path' => env('RAG_DB_SCHEMA', 'public'),
],
use Thaolaptrinh\Rag\Data\Document;
use Thaolaptrinh\Rag\Rag;
// Single document
$result = Rag::ingest(Document::create('Your document content here', [
'source' => 'manual',
'file_id' => 123,
]));
// Multiple documents
$result = Rag::ingestMany([
Document::create('Content 1', ['source' => 'pdf']),
Document::create('Content 2', ['source' => 'pdf']),
]);
// Simple query
$answer = Rag::query('What is this document about?');
// With options
$answer = Rag::query('What is this about?', [
'top_k' => 10,
'filters' => ['source' => 'pdf'],
'system' => 'Custom system prompt',
]);
echo $answer->text;
echo $answer->traceId;
// Access sources
foreach ($answer->sources as $source) {
echo $source->content;
echo $source->score;
}
Rag::queryStream('Tell me about...', function (string $token): void {
echo $token;
});
// Delete single document
Rag::delete('document-id');
// Delete multiple
$count = Rag::deleteMany(['id-1', 'id-2']);
// Delete all
Rag::truncate();
// Queue single document
Rag::ingestQueued($document);
Rag::ingestQueued($document, 'rag-embeddings');
// Queue multiple (one job per document)
Rag::ingestManyQueued($documents);
use Thaolaptrinh\Rag\Rag;
use Thaolaptrinh\Rag\Data\Document;
beforeEach(function () {
Rag::fake();
});
it('ingests documents', function () {
$result = Rag::ingest(Document::create('test content'));
expect($result->ingested)->toBe(1);
Rag::assertIngested('document-id');
});
it('queries the store', function () {
$answer = Rag::query('What is Laravel?');
expect($answer->text)->toBe('Fake response');
Rag::assertQueried('What is Laravel?');
});
bash
php artisan rag:install
bash
php artisan vendor:publish --provider="Thaolaptrinh\Rag\RagServiceProvider" --tag="rag-config"
php artisan vendor:publish --provider="Thaolaptrinh\Rag\RagServiceProvider" --tag="rag-migrations"
php artisan migrate
bash
php artisan rag:ingest "Your document content" --id="doc-1"
php artisan rag:index