Skip to content

pgvector/Pgvector.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pgvector.jl

pgvector support for Julia

Supports LibPQ.jl

Build Status

Getting Started

Follow the instructions for your database library:

Or check out some examples:

LibPQ.jl

Add the package

pkg> add Pgvector

Load the package

using Pgvector

Enable the extension

execute(conn, "CREATE EXTENSION IF NOT EXISTS vector")

Register the types with your connection

Pgvector.register!(conn)

Create a table

execute(conn, "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))")

Insert vectors

embeddings = [[1, 1, 1], [2, 2, 2], [1, 1, 2]]
LibPQ.load!(
    (embedding = map(Pgvector.Vector, embeddings),),
    conn,
    "INSERT INTO items (embedding) VALUES (\$1)",
)

Get the nearest neighbors

embedding = Pgvector.Vector([1, 1, 1])
result = execute(conn, "SELECT * FROM items ORDER BY embedding <-> \$1 LIMIT 5", [embedding])
columntable(result)

Add an approximate index

execute(conn, "CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)")
# or
execute(conn, "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

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.jl.git
cd Pgvector.jl
createdb pgvector_julia_test
julia --project=. -e "using Pkg; Pkg.instantiate()"
julia --project=. -e "using Pkg; Pkg.test()"

To run an example:

cd examples/openai
createdb pgvector_example
julia --project=. -e "using Pkg; Pkg.instantiate()"
julia --project=. example.jl

About

pgvector support for Julia

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages