Skip to content

Commit

Permalink
Added test for binary embeddings
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed May 18, 2024
1 parent 53b1a26 commit f0b9dc4
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions tests/PhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,34 @@ public function testWorks()

pg_query($db, 'CREATE EXTENSION IF NOT EXISTS vector');
pg_query($db, 'DROP TABLE IF EXISTS items');
pg_query($db, 'CREATE TABLE items (id bigserial primary key, embedding vector(3), half_embedding halfvec(3), sparse_embedding sparsevec(3))');
pg_query($db, 'CREATE TABLE items (id bigserial primary key, embedding vector(3), half_embedding halfvec(3), binary_embedding bit(3), sparse_embedding sparsevec(3))');

$embedding1 = new Vector([1, 1, 1]);
$embedding2 = new Vector([2, 2, 2]);
$embedding3 = new Vector([1, 1, 2]);
$halfEmbedding1 = new HalfVector([1, 1, 1]);
$halfEmbedding2 = new HalfVector([2, 2, 2]);
$halfEmbedding3 = new HalfVector([1, 1, 2]);
$binaryEmbedding1 = '000';
$binaryEmbedding2 = '101';
$binaryEmbedding3 = '111';
$sparseEmbedding1 = SparseVector::fromDense([1, 1, 1]);
$sparseEmbedding2 = SparseVector::fromDense([2, 2, 2]);
$sparseEmbedding3 = SparseVector::fromDense([1, 1, 2]);
pg_query_params($db, 'INSERT INTO items (embedding, half_embedding, sparse_embedding) VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9)', [$embedding1, $halfEmbedding1, $sparseEmbedding1, $embedding2, $halfEmbedding2, $sparseEmbedding2, $embedding3, $halfEmbedding3, $sparseEmbedding3]);
pg_query_params($db, 'INSERT INTO items (embedding, half_embedding, binary_embedding, sparse_embedding) VALUES ($1, $2, $3, $4), ($5, $6, $7, $8), ($9, $10, $11, $12)', [$embedding1, $halfEmbedding1, $binaryEmbedding1, $sparseEmbedding1, $embedding2, $halfEmbedding2, $binaryEmbedding2, $sparseEmbedding2, $embedding3, $halfEmbedding3, $binaryEmbedding3, $sparseEmbedding3]);

$ids = [];
$embeddings = [];
$halfEmbeddings = [];
$binaryEmbeddings = [];
$sparseEmbeddings = [];
$embedding = new Vector([1, 1, 1]);
$result = pg_query_params($db, 'SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5', [$embedding]);
while ($row = pg_fetch_array($result)) {
$ids[] = $row['id'];
$embeddings[] = $row['embedding'];
$halfEmbeddings[] = $row['half_embedding'];
$binaryEmbeddings[] = $row['binary_embedding'];
$sparseEmbeddings[] = $row['sparse_embedding'];
}
pg_free_result($result);
Expand All @@ -45,6 +50,7 @@ public function testWorks()
$this->assertEquals(['[1,1,1]', '[1,1,2]', '[2,2,2]'], $embeddings);
$this->assertEquals([1, 1, 1], (new Vector($embeddings[0]))->toArray());
$this->assertEquals([1, 1, 1], (new HalfVector($halfEmbeddings[0]))->toArray());
$this->assertEquals('000', $binaryEmbeddings[0]);
$this->assertEquals([1, 1, 1], SparseVector::fromString($sparseEmbeddings[0])->toArray());

$rows = [$embedding1, $embedding2, $embedding3];
Expand Down

0 comments on commit f0b9dc4

Please sign in to comment.