pgvector examples for Julia
Supports LibPQ.jl
Follow the instructions for your database library:
Or check out some examples:
- Embeddings with OpenAI
- Binary embeddings with Cohere
- Hybrid search with Ollama (Reciprocal Rank Fusion)
- Sparse search with Text Embeddings Inference
Enable the extension
execute(conn, "CREATE EXTENSION IF NOT EXISTS vector")
Create a table
execute(conn, "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))")
Insert vectors
module Pgvector
convert(vec::AbstractVector{T}) where T<:Number = string("[", join(vec, ","), "]")
end
embeddings = [[1, 1, 1], [2, 2, 2], [1, 1, 2]]
LibPQ.load!(
(embedding = map(Pgvector.convert, embeddings),),
conn,
"INSERT INTO items (embedding) VALUES (\$1)",
)
Get the nearest neighbors
embedding = Pgvector.convert([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
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
To get started with development:
git clone https://github.com/pgvector/pgvector-julia.git
cd pgvector-julia
createdb pgvector_julia_test
julia --project=. -e "using Pkg; Pkg.instantiate()"
julia --project=. LibPQ/example.jl
To run an example:
cd examples/openai
createdb pgvector_example
julia --project=. -e "using Pkg; Pkg.instantiate()"
julia --project=. example.jl