PHP code example of hamzi / nativerag

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

    

hamzi / nativerag example snippets


use Hamzi\NativeRag\Facades\NativeRag;

$response = NativeRag::chat([
    ['role' => 'system', 'content' => 'You are a senior Laravel engineer.'],
    ['role' => 'user',   'content' => 'Explain service containers briefly.'],
]);

echo $response->content;        // The generated text
echo $response->promptTokens;   // Input tokens used
echo $response->completionTokens; // Output tokens generated

use Hamzi\NativeRag\Facades\NativeRag;
use Illuminate\Support\Facades\Route;

Route::post('/api/ai/stream', function () {
    return NativeRag::stream([
        ['role' => 'user', 'content' => 'Write a comprehensive guide on Eloquent ORM.'],
    ]);
});

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Hamzi\NativeRag\Traits\Embeddable;

class Article extends Model
{
    use Embeddable;

    /**
     * Define the text payload the AI engine will index.
     */
    public function toEmbeddableString(): string
    {
        return "Title: {$this->title}\n\nContent: {$this->content}";
    }
}

use Hamzi\NativeRag\Services\VectorSearchEngine;
use Hamzi\NativeRag\Facades\NativeRag;

// 1. Embed the user's question
$queryVector = NativeRag::embedding()->embed('How does quantum physics relate to computing?');

// 2. Search native database for closest chunks
$engine = new VectorSearchEngine();
$results = $engine->search($queryVector, limit: 5, minScore: 0.50);

foreach ($results as $chunk) {
    echo $chunk->chunk_content; // Matching text passage
    echo $chunk->similarity;    // Score: 0.0 – 1.0
}

use Hamzi\NativeRag\Facades\NativeRag;

// Use LM Studio for this specific call
$response = NativeRag::driver('lmstudio')->chat([
    ['role' => 'user', 'content' => 'Summarize this document.'],
]);
bash
php artisan vendor:publish --tag="nativerag-config"
php artisan vendor:publish --tag="nativerag-migrations"
php artisan migrate