Skip to content

Commit 94c2088

Browse files
committed
Improved examples [skip ci]
1 parent 0fbe919 commit 94c2088

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

examples/hybrid-search/example.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,21 @@ await client.query('DROP TABLE IF EXISTS documents');
1212
await client.query('CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector(384))');
1313
await client.query("CREATE INDEX ON documents USING GIN (to_tsvector('english', content))");
1414

15-
const input = [
16-
'The dog is barking',
17-
'The cat is purring',
18-
'The bear is growling'
19-
];
20-
2115
const extractor = await pipeline('feature-extraction', 'Xenova/multi-qa-MiniLM-L6-cos-v1', {dtype: 'fp32'});
2216

2317
async function embed(content) {
2418
const output = await extractor(content, {pooling: 'mean', normalize: true});
25-
return Array.from(output.data);
19+
return output.tolist();
2620
}
2721

28-
for (let content of input) {
29-
const embedding = await embed(content);
30-
await client.query('INSERT INTO documents (content, embedding) VALUES ($1, $2)', [content, pgvector.toSql(embedding)]);
22+
const input = [
23+
'The dog is barking',
24+
'The cat is purring',
25+
'The bear is growling'
26+
];
27+
const embeddings = await embed(input);
28+
for (let [i, content] of input.entries()) {
29+
await client.query('INSERT INTO documents (content, embedding) VALUES ($1, $2)', [content, pgvector.toSql(embeddings[i])]);
3130
}
3231

3332
const sql = `
@@ -54,9 +53,9 @@ ORDER BY score DESC
5453
LIMIT 5
5554
`;
5655
const query = 'growling bear'
57-
const embedding = await embed(query);
56+
const queryEmbedding = (await embed([query]))[0];
5857
const k = 60
59-
const { rows } = await client.query(sql, [query, pgvector.toSql(embedding), k]);
58+
const { rows } = await client.query(sql, [query, pgvector.toSql(queryEmbedding), k]);
6059
for (let row of rows) {
6160
console.log(row);
6261
}

examples/transformers/example.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { pipeline } from '@xenova/transformers';
1+
import { pipeline } from '@huggingface/transformers';
22
import pg from 'pg';
33
import pgvector from 'pgvector/pg';
44

@@ -11,22 +11,26 @@ await pgvector.registerTypes(client);
1111
await client.query('DROP TABLE IF EXISTS documents');
1212
await client.query('CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector(384))');
1313

14+
const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2', {dtype: 'fp32'});
15+
16+
async function embed(input) {
17+
const output = await extractor(input, {pooling: 'mean', normalize: true});
18+
return output.tolist();
19+
}
20+
1421
const input = [
1522
'The dog is barking',
1623
'The cat is purring',
1724
'The bear is growling'
1825
];
19-
20-
const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2');
21-
22-
for (let content of input) {
23-
const output = await extractor(content, {pooling: 'mean', normalize: true});
24-
const embedding = Array.from(output.data);
25-
await client.query('INSERT INTO documents (content, embedding) VALUES ($1, $2)', [content, pgvector.toSql(embedding)]);
26+
const embeddings = await embed(input);
27+
for (let [i, content] of input.entries()) {
28+
await client.query('INSERT INTO documents (content, embedding) VALUES ($1, $2)', [content, pgvector.toSql(embeddings[i])]);
2629
}
2730

28-
const documentId = 2;
29-
const { rows } = await client.query('SELECT * FROM documents WHERE id != $1 ORDER BY embedding <=> (SELECT embedding FROM documents WHERE id = $1) LIMIT 5', [documentId]);
31+
const query = 'forest';
32+
const queryEmbedding = (await embed([query]))[0];
33+
const { rows } = await client.query('SELECT content FROM documents ORDER BY embedding <=> $1 LIMIT 5', [pgvector.toSql(queryEmbedding)]);
3034
for (let row of rows) {
3135
console.log(row.content);
3236
}

examples/transformers/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"private": true,
33
"type": "module",
44
"dependencies": {
5-
"@xenova/transformers": "^2.6.0",
5+
"@huggingface/transformers": "^3.3.3",
66
"pg": "^8.11.3",
77
"pgvector": "file:../.."
88
}

0 commit comments

Comments
 (0)