FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pgvector-php/examples/pgsql/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
/
pgsql
/
example.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
27 lines (19 loc) · 924 Bytes
Breadcrumbs
pgvector-php
/
examples
/
pgsql
/
example.php
Copy path
File metadata and controls
27 lines (19 loc) · 924 Bytes
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
<?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
);
Back
|
FazBrowse Home
|
New Git URL