-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexample.php
27 lines (19 loc) · 924 Bytes
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Pgvector\Vector;
$db = pg_connect('postgres://localhost/pgvector_example');
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))');
$embedding1 = new Vector([1, 1, 1]);
$embedding2 = new Vector([2, 2, 2]);
$embedding3 = new Vector([1, 1, 2]);
pg_query_params($db, 'INSERT INTO items (embedding) VALUES ($1), ($2), ($3)', [$embedding1, $embedding2, $embedding3]);
$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)) {
echo $row['id'] . ': ' . new Vector($row['embedding']) . "\n";
}
pg_free_result($result);
pg_query($db, 'CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)');
pg_close($db);