Skip to content

pgvector/pgvector-nim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pgvector-nim

pgvector examples for Nim

Supports db_connector

Build Status

Getting Started

Follow the instructions for your database library:

db_connector

Enable the extension

db.exec(sql"CREATE EXTENSION IF NOT EXISTS vector")

Create a table

db.exec(sql"CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))")

Insert vectors

import std/json

let embedding1 = @[1, 1, 1]
let embedding2 = @[1, 1, 2]
let embedding3 = @[2, 2, 2]
db.exec(sql"INSERT INTO items (embedding) VALUES (?), (?), (?)", %* embedding1, %* embedding2, %* embedding3)

Get the nearest neighbors

let embedding = @[1, 1, 1]
let rows = db.getAllRows(sql"SELECT * FROM items ORDER BY embedding <-> ? LIMIT 5", %* embedding)
for row in rows:
  echo row

Add an approximate index

db.exec(sql"CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)")
# or
db.exec(sql"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

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-nim.git
cd pgvector-nim
createdb pgvector_nim_test
nimble install db_connector
nim c --run example.nim

Specify the path to libpq if needed:

nim c --run --dynlibOverride:pq --passL:"/opt/homebrew/opt/libpq/lib/libpq.dylib" example.nim

About

pgvector examples for Nim

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages