-
Notifications
You must be signed in to change notification settings - Fork 11
/
sequelize.test.mjs
132 lines (112 loc) · 3.83 KB
/
sequelize.test.mjs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import assert from 'node:assert';
import test from 'node:test';
import { Sequelize, DataTypes } from 'sequelize';
import pgvector from 'pgvector/sequelize';
import { l2Distance, maxInnerProduct, cosineDistance, l1Distance, hammingDistance, jaccardDistance } from 'pgvector/sequelize';
import { SparseVector } from 'pgvector';
test('sequelize example', async () => {
pgvector.registerTypes(Sequelize);
let sequelize = new Sequelize('postgres://localhost/pgvector_node_test', {
logging: false
});
await sequelize.query('CREATE EXTENSION IF NOT EXISTS vector');
// need to reconnect after the vector extension has been created
sequelize.close();
sequelize = new Sequelize('postgres://localhost/pgvector_node_test', {
logging: false
});
const Item = sequelize.define('Item', {
embedding: {
type: DataTypes.VECTOR(3)
},
half_embedding: {
type: DataTypes.HALFVEC(3)
},
binary_embedding: {
type: 'BIT(3)'
},
sparse_embedding: {
type: DataTypes.SPARSEVEC(3)
}
}, {
modelName: 'Item',
tableName: 'sequelize_items',
indexes: [
{
fields: ['embedding'],
using: 'hnsw',
operator: 'vector_l2_ops'
}
]
});
await Item.sync({force: true});
await Item.create({embedding: [1, 1, 1], half_embedding: [1, 1, 1], binary_embedding: '000', sparse_embedding: new SparseVector([1, 1, 1])});
await Item.create({embedding: [2, 2, 2], half_embedding: [2, 2, 2], binary_embedding: '101', sparse_embedding: new SparseVector([2, 2, 2])});
await Item.create({embedding: [1, 1, 2], half_embedding: [1, 1, 2], binary_embedding: '111', sparse_embedding: new SparseVector([1, 1, 2])});
// L2 distance
let items = await Item.findAll({
order: l2Distance('embedding', [1, 1, 1], sequelize),
limit: 5
});
assert.deepEqual(items.map(v => v.id), [1, 3, 2]);
assert.deepEqual(items[0].embedding, [1, 1, 1]);
assert.deepEqual(items[1].embedding, [1, 1, 2]);
assert.deepEqual(items[2].embedding, [2, 2, 2]);
// L2 distance - halfvec
items = await Item.findAll({
order: l2Distance('half_embedding', [1, 1, 1], sequelize),
limit: 5
});
assert.deepEqual(items.map(v => v.id), [1, 3, 2]);
// L2 distance - sparsevec
items = await Item.findAll({
order: l2Distance('sparse_embedding', new SparseVector([1, 1, 1]), sequelize),
limit: 5
});
assert.deepEqual(items.map(v => v.id), [1, 3, 2]);
await Item.create({});
// max inner product
items = await Item.findAll({
order: maxInnerProduct(sequelize.literal('"Item".embedding'), [1, 1, 1], sequelize),
limit: 5
});
assert.deepEqual(items.map(v => v.id), [2, 3, 1, 4]);
// cosine distance
items = await Item.findAll({
order: cosineDistance('embedding', [1, 1, 1], sequelize),
limit: 5
});
assert.deepEqual(items.map(v => v.id).slice(2), [3, 4]);
// L1 distance
items = await Item.findAll({
order: l1Distance('embedding', [1, 1, 1], sequelize),
limit: 5
});
assert.deepEqual(items.map(v => v.id), [1, 3, 2, 4]);
// Hamming distance
items = await Item.findAll({
order: hammingDistance('binary_embedding', '101', sequelize),
limit: 5
});
assert.deepEqual(items.map(v => v.id), [2, 3, 1, 4]);
// Jaccard distance
items = await Item.findAll({
order: jaccardDistance('binary_embedding', '101', sequelize),
limit: 5
});
assert.deepEqual(items.map(v => v.id), [2, 3, 1, 4]);
// bad value
await assert.rejects(Item.create({embedding: 'bad'}), {message: /invalid input syntax for type vector/})
sequelize.close();
});
test('dimensions', () => {
assert.equal(DataTypes.VECTOR(3).toSql(), 'VECTOR(3)');
});
test('no dimensions', () => {
assert.equal(DataTypes.VECTOR().toSql(), 'VECTOR');
});
test('bad dimensions', () => {
assert.throws(() => {
DataTypes.VECTOR('bad').toSql();
}, {message: 'expected integer'});
});