PHP code example of thakladd / vector-lite

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

    

thakladd / vector-lite example snippets


$movie = Movie::find(123);
$similarMovies = $movie->getBestVectorMatches(3);

'providers' => [
    // ...
    ThaKladd\VectorLite\VectorLiteServiceProvider::class,
],

return [
    'similarity_alias' => 'similarity',
    'vector_column' => 'vector',
    'embed_hash_column' => 'embed_hash',
    'openai' => [
        'api_key' => env('OPENAI_API_KEY'),
    ],
    'default_dimensions' => 1536,
    'use_clustering_dimensions' => true,
    'reduction_method' => \ThaKladd\VectorLite\Enums\ReduceBy::RPM,
    'clustering_dimensions' => 64,
    'clusters_size' => 500,
    'use_cached_cosim' => true,
    'cache_driver' => env('CACHE_DRIVER', false),
    'cache_time' => 60 * 60 * 24,
    'ffi' => [
        'enabled' => env('VECTOR_LITE_FFI_ENABLED', false),
        'library_path' => env('VECTOR_LITE_FFI_LIBRARY_PATH'),
    ],
    'exclude_self_by_default' => false,
];

class MyModel extends VectorModel {}

$modelQuery = YourModel::query();
$modelQuery->filterByClosestClusters(3);
$modelQuery->getBestClustersByVector($vector, 3);
$modelQuery->findBestClustersByVector($vector);
$modelQuery->selectSimilarity($vector);
$modelQuery->findBestByVector($vector);
$modelQuery->searchBestByVector($vector, 3);
$modelQuery->whereVector($vector, '>=', 0.8);
$modelQuery->whereVectorBetween($vector, 0.5, 0.9);
$modelQuery->havingSimilarity($vector, '>=', 0.8);
$modelQuery->orderBySimilarity($vector, 'desc');
$modelQuery->withoutModels([$vector, $vector2]);
$modelQuery->

$model = YourModel::find(1);
$model->getBestVectorMatches(3);
$model->findBestVectorMatch();
$model->getBestClusters(3);
$model->findBestCluster();

$all = YourModel::all();
$all->searchBestByVector($vector, 3);
$all->sortBySimilarityToVector($vector, 'asc');
$all->filterAboveSimilarityThreshold($vector, 0.5);
$all->pluckSimilarities($vector);
$all->withSimilarities($vector);
$all->findBestByVector($vector);

$model = YourModel::find(1);
$cluster = $model->cluster;
$models = $cluster->models;

use ThaKladd\VectorLite\VectorLite;

$table->vectorLite('embedding', dimensions: 1536);

use ThaKladd\VectorLite\Casts\AsVector;

protected $casts = [
    'embedding' => AsVector::class, // Laravel 11+: casts(): array { return ['embedding' => AsVector::class]; }
];

protected static ?string $vectorColumnName = 'embedding';

Document::query()->whereVectorSimilarTo('embedding', $queryEmbedding, minSimilarity: 0.4);
Document::query()->whereVectorSimilarTo('embedding', 'best wineries in Napa Valley'); // auto-embeds the string

Document::query()
    ->select('*') // selectVectorDistance() is additive, like the AI SDK's - select('*') first to keep other columns
    ->selectVectorDistance('embedding', $queryEmbedding, as: 'distance')
    ->whereVectorDistanceLessThan('embedding', $queryEmbedding, maxDistance: 0.3)
    ->orderByVectorDistance('embedding', $queryEmbedding);

use ThaKladd\VectorLite\Embeddings;
use Illuminate\Support\Str;

$response = Embeddings::for(['Napa Valley has great wine.'])->cache(seconds: 3600)->generate();
$response->embeddings; // [[0.123, 0.456, ...]]

$embedding = Str::of('Napa Valley has great wine.')->toEmbeddings(cache: true);

use ThaKladd\VectorLite\Reranking;

$response = Reranking::of([$textA, $textB])->rerank('PHP frameworks');
$response->first()->document;
$response->first()->score;
$response->first()->index;

// Or on any collection:
$posts->rerank('body', 'Laravel tutorials');

use ThaKladd\VectorLite\Tools\SimilaritySearch;

$tool = SimilaritySearch::usingModel(Document::class, 'embedding', minSimilarity: 0.7, limit: 10);
$tool('best wineries in Napa Valley');