-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexample.php
47 lines (37 loc) · 1.23 KB
/
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Pgvector\Vector;
ini_set('memory_limit', '512M');
// generate random data
$rows = 100000;
$dimensions = 128;
$embeddings = [];
for ($i = 0; $i < $rows; $i++) {
$embedding = [];
for ($j = 0; $j < $dimensions; $j++) {
$embedding[] = rand() / getrandmax();
}
$embeddings[] = $embedding;
}
// enable extension
$db = pg_connect('postgres://localhost/pgvector_example');
pg_query($db, 'CREATE EXTENSION IF NOT EXISTS vector');
// create table
pg_query($db, 'DROP TABLE IF EXISTS items');
pg_query($db, 'CREATE TABLE items (id bigserial, embedding vector(128))');
// load data
echo "Loading $rows rows\n";
$rows = array_map(fn ($e) => new Vector($e), $embeddings);
pg_copy_from($db, 'items (embedding)', $rows);
echo "Success!\n";
// create any indexes *after* loading initial data (skipping for this example)
$createIndex = false;
if ($createIndex) {
echo "Creating index\n";
pg_query($db, "SET maintenance_work_mem = '8GB'");
pg_query($db, 'SET max_parallel_maintenance_workers = 7');
pg_query($db, 'CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops)');
}
// update planner statistics for good measure
pg_query($db, 'ANALYZE items');
pg_close($db);