Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

pgvector support for Nim

License

NotificationsYou must be signed in to change notification settings

pgvector/pgvector-nim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pgvector support for Nim

Supportsdb_connector

Build Status

Getting Started

Run:

nimble add pgvector

And follow the instructions for your database library:

Or check out an example:

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 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

Reference

Vectors

Create a vector from a sequence or array

let vec=@[1.0,2.0,3.0].toVector

Get a sequence

let s= vec.toSeq

Half Vectors

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

Sparse Vectors

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

History

View thechangelog

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.gitcd pgvector-nimcreatedb pgvector_nim_testnimble install db_connectornimbletest

Specify the path to libpq if needed:

nimbletest --passL:-Wl,-rpath,/opt/homebrew/opt/libpq/lib

To run an example:

cd examples/openaicreatedb pgvector_examplenim c --run --path:../../src example.nim

Specify 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

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp