- Notifications
You must be signed in to change notification settings - Fork0
pgvector/pgvector-nim
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
pgvector support for Nim
Supportsdb_connector
Run:
nimble add pgvector
And follow the instructions for your database library:
Or check out an example:
- Embeddings with OpenAI
- Binary embeddings with Cohere
- Hybrid search with Ollama (Reciprocal Rank Fusion)
- Sparse search with Text Embeddings Inference
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 pgvectorlet embedding1=@[1,1,1]let embedding2=@[1,1,2]let embedding3=@[2,2,2]db.exec(sql"INSERT INTO items (embedding) VALUES (?), (?), (?)", embedding1.toVector, embedding2.toVector, embedding3.toVector)
Get the nearest neighbors
let embedding=@[1,1,1]let rows= db.getAllRows(sql"SELECT * FROM items ORDER BY embedding <-> ? LIMIT 5", embedding.toVector)for rowin rows:echo row
Add an approximate index
db.exec(sql"CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)")# ordb.exec(sql"CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)")
Usevector_ip_ops for inner product andvector_cosine_ops for cosine distance
See afull example
Create a vector from a sequence or array
let vec=@[1.0,2.0,3.0].toVector
Get a sequence
let s= vec.toSeq
Create a half vector from a sequence or array
let vec=@[1.0,2.0,3.0].toHalfVector
Get a sequence
let s= vec.toSeq
Create a sparse vector from a sequence or array
let vec=@[1.0,0.0,2.0,0.0,3.0,0.0].toSparseVector
Or a table of non-zero elements
let elements= {0:1.0,2:2.0,4:3.0}.toTablelet vec= elements.toSparseVector(6)
Note: Indices start at 0
Get the number of dimensions
let dim= vec.dim
Get the non-zero elements
let elements= vec.elements
View thechangelog
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs andsubmit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
To get started with development:
git clone https://github.com/pgvector/pgvector-nim.gitcd pgvector-nimcreatedb pgvector_nim_testnimble install db_connectornimbletest
Specify the path to libpq if needed:
nimbletest --passL:-Wl,-rpath,/opt/homebrew/opt/libpq/libTo run an example:
cd examples/openaicreatedb pgvector_examplenim c --run --path:../../src example.nimSpecify the path to libpq if needed:
nim c --run --path:../../src --passL:-Wl,-rpath,/opt/homebrew/opt/libpq/lib example.nim
About
pgvector support for Nim
Resources
License
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.