Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

pgvector/pgvector-dart

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
68 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pgvector-dart

pgvector support for Dart

Supports the postgres package

Build Status

Getting Started

Run:

dart pub add pgvector

And follow the instructions for your database library:

Or check out some examples:

postgres

Import the library

import 'package:pgvector/pgvector.dart';

Add the encoder

var connection = await Connection.open(endpoint,
    settings: ConnectionSettings(
        typeRegistry: TypeRegistry(encoders: [pgvectorEncoder])));

Enable the extension

await connection.execute('CREATE EXTENSION IF NOT EXISTS vector');

Create a table

await connection.execute('CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))');

Insert vectors

await connection.execute(
    Sql.named('INSERT INTO items (embedding) VALUES (@a), (@b), (@c)'),
    parameters: {
      'a': Vector([1, 1, 1]),
      'b': Vector([2, 2, 2]),
      'c': Vector([1, 1, 2])
    });

Get the nearest neighbors

List<List<dynamic>> results = await connection.execute(
    Sql.named('SELECT id, embedding FROM items ORDER BY embedding <-> @embedding LIMIT 5'),
    parameters: {
      'embedding': Vector([1, 1, 1])
    });
for (final row in results) {
  print(row[0]);
  print(Vector.fromBinary(row[1].bytes));
}

Add an approximate index

await connection.execute('CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)');
// or
await connection.execute('CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)');

Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance

See a full example

Reference

Vectors

Create a vector from a list

var vec = Vector([1, 2, 3]);

Get a list

var list = vec.toList();

Half Vectors

Create a half vector from a list

var vec = HalfVector([1, 2, 3]);

Get a list

var list = vec.toList();

Binary Vectors

Create a binary vector from a list

var vec = Bit([true, false, true]);

Or a string

var vec = Bit("101");

Get a list

var list = vec.toList();

Get a string

var str = vec.toString();

Sparse Vectors

Create a sparse vector from a list

var vec = SparseVector([1, 0, 2, 0, 3, 0]);

Or a map of non-zero elements

var vec = SparseVector.fromMap({0: 1.0, 2: 2.0, 4: 3.0}, 6);

Note: Indices start at 0

Get the number of dimensions

var dim = vec.dimensions;

Get the indices of non-zero elements

var indices = vec.indices;

Get the values of non-zero elements

var values = vec.values;

Get a list

var list = vec.toList();

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/pgvector/pgvector-dart.git
cd pgvector-dart
createdb pgvector_dart_test
dart test

To run an example:

cd examples/openai
createdb pgvector_example
dart pub get
dart run example.dart

About

pgvector support for Dart

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.