FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pgvector-php/examples/loading/example.php at master · pgvector/pgvector-php · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
pgvector
/
pgvector-php
Public
Notifications
You must be signed in to change notification settings
Fork
14
Star
197
Code
Issues
0
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
pgvector-php
/
examples
/
loading
/
example.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
47 lines (37 loc) · 1.23 KB
Breadcrumbs
pgvector-php
/
examples
/
loading
/
example.php
Copy path
File metadata and controls
47 lines (37 loc) · 1.23 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
);
Back
|
FazBrowse Home
|
New Git URL