Skip to content

Commit 392a3b5

Browse files
committed
Improved examples [skip ci]
1 parent 79e2c08 commit 392a3b5

4 files changed

Lines changed: 18 additions & 15 deletions

File tree

examples/cohere/example.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
pg_query($db, 'CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding bit(1024))');
1010

1111
// https://docs.cohere.com/reference/embed
12-
function fetchEmbeddings($texts, $inputType)
12+
function embed($texts, $inputType)
1313
{
1414
$apiKey = getenv('CO_API_KEY') or die("Set CO_API_KEY\n");
1515
$url = 'https://api.cohere.com/v1/embed';
@@ -36,13 +36,13 @@ function fetchEmbeddings($texts, $inputType)
3636
'The cat is purring',
3737
'The bear is growling'
3838
];
39-
$embeddings = fetchEmbeddings($input, 'search_document');
39+
$embeddings = embed($input, 'search_document');
4040
foreach ($input as $i => $content) {
4141
pg_query_params($db, 'INSERT INTO documents (content, embedding) VALUES ($1, $2)', [$content, $embeddings[$i]]);
4242
}
4343

4444
$query = 'forest';
45-
$queryEmbedding = fetchEmbeddings([$query], 'search_query')[0];
45+
$queryEmbedding = embed([$query], 'search_query')[0];
4646
$result = pg_query_params($db, 'SELECT * FROM documents ORDER BY embedding <~> $1 LIMIT 5', [$queryEmbedding]);
4747
while ($row = pg_fetch_array($result)) {
4848
echo $row['content'] . "\n";

examples/hybrid/example.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
pg_query($db, 'CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector(768))');
1212
pg_query($db, "CREATE INDEX ON documents USING GIN (to_tsvector('english', content))");
1313

14-
function fetchEmbeddings($input)
14+
function embed($input, $taskType)
1515
{
16+
// nomic-embed-text uses a task prefix
17+
// https://huggingface.co/nomic-ai/nomic-embed-text-v1.5
18+
$input = array_map(fn($v) => $taskType . ': ' . $v, $input);
19+
1620
$url = 'http://localhost:11434/api/embed';
1721
$data = [
1822
'input' => $input,
@@ -35,7 +39,7 @@ function fetchEmbeddings($input)
3539
'The cat is purring',
3640
'The bear is growling'
3741
];
38-
$embeddings = fetchEmbeddings($input);
42+
$embeddings = embed($input, 'search_document');
3943

4044
foreach ($input as $i => $content) {
4145
pg_query_params($db, 'INSERT INTO documents (content, embedding) VALUES ($1, $2)', [$content, new Vector($embeddings[$i])]);
@@ -65,7 +69,7 @@ function fetchEmbeddings($input)
6569
LIMIT 5
6670
SQL;
6771
$query = 'growling bear';
68-
$queryEmbedding = fetchEmbeddings($query)[0];
72+
$queryEmbedding = embed([$query], 'search_query')[0];
6973
$k = 60;
7074
$result = pg_query_params($db, $sql, [$query, new Vector($queryEmbedding), $k]);
7175
while ($row = pg_fetch_array($result)) {

examples/openai/example.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
pg_query($db, 'DROP TABLE IF EXISTS documents');
1111
pg_query($db, 'CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector(1536))');
1212

13-
function fetchEmbeddings($input)
13+
function embed($input)
1414
{
1515
$apiKey = getenv('OPENAI_API_KEY') or die("Set OPENAI_API_KEY\n");
1616
$url = 'https://api.openai.com/v1/embeddings';
@@ -35,14 +35,14 @@ function fetchEmbeddings($input)
3535
'The cat is purring',
3636
'The bear is growling'
3737
];
38-
$embeddings = fetchEmbeddings($input);
39-
38+
$embeddings = embed($input);
4039
foreach ($input as $i => $content) {
4140
pg_query_params($db, 'INSERT INTO documents (content, embedding) VALUES ($1, $2)', [$content, new Vector($embeddings[$i])]);
4241
}
4342

44-
$documentId = 2;
45-
$result = pg_query_params($db, 'SELECT * FROM documents WHERE id != $1 ORDER BY embedding <=> (SELECT embedding FROM documents WHERE id = $1) LIMIT 5', [$documentId]);
43+
$query = 'forest';
44+
$queryEmbedding = embed([$query])[0];
45+
$result = pg_query_params($db, 'SELECT * FROM documents ORDER BY embedding <=> $1 LIMIT 5', [new Vector($queryEmbedding)]);
4646
while ($row = pg_fetch_array($result)) {
4747
echo $row['content'] . "\n";
4848
}

examples/sparse/example.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
pg_query($db, 'DROP TABLE IF EXISTS documents');
1818
pg_query($db, 'CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding sparsevec(30522))');
1919

20-
function fetchEmbeddings($inputs)
20+
function embed($inputs)
2121
{
2222
$url = 'http://localhost:3000/embed_sparse';
2323
$data = [
@@ -48,14 +48,13 @@ function fetchEmbeddings($inputs)
4848
'The cat is purring',
4949
'The bear is growling'
5050
];
51-
$embeddings = fetchEmbeddings($input);
52-
51+
$embeddings = embed($input);
5352
foreach ($input as $i => $content) {
5453
pg_query_params($db, 'INSERT INTO documents (content, embedding) VALUES ($1, $2)', [$content, new SparseVector($embeddings[$i], 30522)]);
5554
}
5655

5756
$query = 'forest';
58-
$queryEmbedding = fetchEmbeddings([$query])[0];
57+
$queryEmbedding = embed([$query])[0];
5958
$result = pg_query_params($db, 'SELECT content FROM documents ORDER BY embedding <#> $1 LIMIT 5', [new SparseVector($queryEmbedding, 30522)]);
6059
while ($row = pg_fetch_array($result)) {
6160
echo $row['content'] . "\n";

0 commit comments

Comments
 (0)