PHP code example of displace / ext-turbovec

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

    

displace / ext-turbovec example snippets



use Displace\Vector\IdMapIndex;

$index = new IdMapIndex(dim: 1024, bitWidth: 4);

// Vectors enter as packed float32 strings — pack('g*', ...) output.
$index->addWithIds(
    pack('g*', ...$embeddingA) . pack('g*', ...$embeddingB),
    [101, 102],                       // your ids, e.g. SQL primary keys
);

$result = $index->search(pack('g*', ...$queryEmbedding), k: 10);
foreach ($result as $row) {
    printf("doc %d scored %.4f\n", $row['id'], $row['score']);
}

$index->write('corpus.tvim');         // ...and IdMapIndex::load() it back

$packed = pack('g*', ...$floats);             // one vector
$batch  = $packedA . $packedB . $packedC;     // batches are plain concatenation