-
Notifications
You must be signed in to change notification settings - Fork 7
/
example.php
52 lines (45 loc) · 1.75 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
48
49
50
51
52
<?php
require_once __DIR__ . '/vendor/autoload.php';
$db = pg_connect('postgres://localhost/pgvector_example');
pg_query($db, 'CREATE EXTENSION IF NOT EXISTS vector');
pg_query($db, 'DROP TABLE IF EXISTS documents');
pg_query($db, 'CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding bit(1024))');
// https://docs.cohere.com/reference/embed
function fetchEmbeddings($texts, $inputType)
{
$apiKey = getenv('CO_API_KEY') or die("Set CO_API_KEY\n");
$url = 'https://api.cohere.com/v1/embed';
$data = [
'texts' => $texts,
'model' => 'embed-english-v3.0',
'input_type' => $inputType,
'embedding_types' => ['ubinary']
];
$opts = [
'http' => [
'method' => 'POST',
'header' => "Authorization: Bearer $apiKey\r\nContent-Type: application/json\r\n",
'content' => json_encode($data)
]
];
$context = stream_context_create($opts);
$response = file_get_contents($url, false, $context);
return array_map(fn ($e) => implode(array_map(fn ($v) => str_pad(decbin($v), 8, '0', STR_PAD_LEFT), $e)), json_decode($response, true)['embeddings']['ubinary']);
}
$input = [
'The dog is barking',
'The cat is purring',
'The bear is growling'
];
$embeddings = fetchEmbeddings($input, 'search_document');
foreach ($input as $i => $content) {
pg_query_params($db, 'INSERT INTO documents (content, embedding) VALUES ($1, $2)', [$content, $embeddings[$i]]);
}
$query = 'forest';
$queryEmbedding = fetchEmbeddings([$query], 'search_query')[0];
$result = pg_query_params($db, 'SELECT * FROM documents ORDER BY embedding <~> $1 LIMIT 5', [$queryEmbedding]);
while ($row = pg_fetch_array($result)) {
echo $row['content'] . "\n";
}
pg_free_result($result);
pg_close($db);