PHP code example of ayaashraf / laravel-rag

1. Go to this page and download the library: Download ayaashraf/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/ */

    

ayaashraf / laravel-rag example snippets


   'pgsql_vector' => [
            'driver' => 'pgsql',
            'host' => env('VECTOR_DB_HOST', 'localhost'),
            'port' => env('VECTOR_DB_PORT', 5432),
            'database' => env('VECTOR_DB_DATABASE', 'rag_vectors'),
            'username' => env('VECTOR_DB_USERNAME', 'postgres'),
            'password' => env('VECTOR_DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
        ],

use AyaAshraf\LaravelRag\Agents\DocumentSearchAgent;
use AyaAshraf\LaravelRag\Models\Document;

// Query documents with streaming
$stream = DocumentSearchAgent::make()
    ->stream('What is this about?', provider: 'gemini');

foreach ($stream as $chunk) {
    echo $chunk->text;
}

// Access documents directly
$docs = Document::where('status', 'processed')->get();
$chunks = $docs->first()->chunks;

use AyaAshraf\LaravelRag\Agents\DocumentSearchAgent;

$stream = DocumentSearchAgent::make()
    ->onChunksFound(function ($chunks, $metadata) {
        dump($metadata); // Vector search metrics
        dump($chunks);   // Retrieved document chunks
    })
    ->stream('Your question here', provider: 'gemini');

foreach ($stream as $event) {
    // Handle streaming events
}

use AyaAshraf\LaravelRag\Services\EmbeddingGenerator;

class CustomEmbeddingGenerator implements EmbeddingGenerator
{
    public function embed(array $texts): array
    {
        // Your embedding logic
        return $vectors;
    }

    public function dimensions(): int
    {
        return 768;
    }
}

// Register in AppServiceProvider
$this->app->bind(EmbeddingGenerator::class, CustomEmbeddingGenerator::class);

use AyaAshraf\LaravelRag\Models\Document;
use AyaAshraf\LaravelRag\Services\DocumentTextExtractor;
use AyaAshraf\LaravelRag\Services\DocumentEmbeddingIndexer;

$extractor = app(DocumentTextExtractor::class);
$indexer = app(DocumentEmbeddingIndexer::class);

$text = $extractor->extract('/path/to/file.pdf');
$chunksCreated = $indexer->index($document, $text);

   'caching' => [
       'embeddings' => [
           'cache' => true,
           'store' => 'redis',
       ],
   ]
   

'chunk_batch_size' => 8  // Process fewer chunks at once
bash
php artisan vendor:publish --tag=rag-config
php artisan vendor:publish --tag=rag-migrations
bash
php artisan migrate
bash
   php artisan queue:listen
   
bash
   php artisan rag:embed-documents