pgvector support for Node.js
Supports Sequelize and node-postgres
Run:
npm install pgvectorAnd follow the instructions for your database library:
Register the type
const { Sequelize } = require('sequelize');
const pgvector = require('pgvector/sequelize');
pgvector.registerType(Sequelize);Add a vector field
Item.init({
factors: {
type: DataTypes.VECTOR(3)
}
}, ...);Insert a vector
await Item.create({factors: [1, 2, 3]});Get the nearest neighbors to a vector
const items = await Item.findAll({
order: [sequelize.literal(`factors <-> '[1, 2, 3]'`)],
limit: 5
});Register the type
const pgvector = require('pgvector/pg');
await pgvector.registerType(client);Insert a vector
const factors = [1, 2, 3];
await client.query('INSERT INTO items (factors) VALUES ($1)', [pgvector.toSql(factors)]);Get the nearest neighbors to a vector
const result = await client.query('SELECT * FROM items ORDER BY factors <-> $1 LIMIT 5', [pgvector.toSql(factors)]);View the changelog
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
To get started with development:
git clone https://github.com/ankane/pgvector-node.git
cd pgvector-node
npm install
npm test