PHP code example of illuma-law / laravel-vector-schema

1. Go to this page and download the library: Download illuma-law/laravel-vector-schema 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/ */

    

illuma-law / laravel-vector-schema example snippets


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use IllumaLaw\VectorSchema\VectorSchema;

return new class extends Migration {
    public function up(): void
    {
        // Ensures the pgvector extension is created on PostgreSQL databases
        VectorSchema::ensureExtension();

        Schema::create('documents', function (Blueprint $table) {
            $table->id();
            $table->text('content');
            
            // Define 'embedding' column with 768 dimensions
            $table->vectorColumn('embedding', 768)->nullable();
            
            $table->timestamps();
        });

        // Creates an HNSW index on pgsql (ignored on others)
        Schema::table('documents', function (Blueprint $table) {
            $table->hnswIndex('embedding');
        });
    }

    public function down(): void
    {
        Schema::table('documents', function (Blueprint $table) {
            $table->dropHnswIndex('embedding');
        });
        Schema::dropIfExists('documents');
    }
};

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use IllumaLaw\VectorSchema\Casts\VectorArray;

class Document extends Model
{
    protected $casts = [
        'embedding' => VectorArray::class,
    ];
}

$document = new Document();
$document->content = 'Hello world';
$document->embedding = [0.1, 0.5, -0.3, ...]; // Will be cast correctly on save
$document->save();

use IllumaLaw\VectorSchema\Support\VectorProcessor;

$processor = new VectorProcessor();

// Returns a list<float> of length 3
$normalized = $processor->normalizeVector([0.5, '1.5', 2], expectedDimensions: 3);

if ($normalized === []) {
    // Handle invalid input or dimension mismatch
}

$processor = new VectorProcessor();

$vectors = [
    [1.0, 1.0, 1.0],
    [3.0, 3.0, 3.0],
    [NAN, 0.0, 0.0], // Automatically filtered out
];

// Returns [2.0, 2.0, 2.0]
$centroid = $processor->averageVectors($vectors, expectedDimensions: 3);

// Assuming $queryEmbedding is an array of 768 floats from your Embedding model (e.g., OpenAI)
$queryEmbedding = [...]; 

$results = Document::query()
    ->whereHybridVectorSimilarTo(
        column: 'embedding', 
        vector: $queryEmbedding, 
        minSimilarity: 0.7, 
        order: true // Automatically order by the closest match
    )
    ->take(10)
    ->get();

$results = Document::query()
    ->select('id', 'content')
    ->selectHybridVectorDistance('embedding', $queryEmbedding, as: 'distance')
    ->orderBy('distance', 'asc') // Closest first
    ->get();

echo $results->first()->distance;

$results = Document::query()
    ->whereHybridVectorDistanceLessThan('embedding', $queryEmbedding, maxDistance: 0.3)
    ->get();

$results = Document::query()
    ->orderByHybridVectorDistance('embedding', $queryEmbedding, 'asc')
    ->get();