pgvector-elixir
pgvector support for Elixir
Installation
Add this line to your application’s mix.exs under deps:
{:pgvector, "~> 0.1.0"}
And follow the instructions for your database library:
Ecto
Create lib/postgrex_types.ex with:
Postgrex.Types.define(MyApp.PostgrexTypes, [Pgvector.Extensions.Vector] ++ Ecto.Adapters.Postgres.extensions(), [])
And add to config/config.exs:
config :my_app, MyApp.Repo, types: MyApp.PostgrexTypes
Create a migration
mix ecto.gen.migration create_vector_extension
with:
defmodule MyApp.Repo.Migrations.CreateVectorExtension do
use Ecto.Migration
def up do
execute "CREATE EXTENSION IF NOT EXISTS vector"
end
def down do
execute "DROP EXTENSION vector"
end
end
Run the migration
mix ecto.migrate
You can now use the vector type in future migrations
create table(:items) do
add :factors, :vector, size: 3
end
Update the model
schema "items" do
field :factors, Pgvector.Ecto.Vector
end
Insert a vector
alias MyApp.{Repo, Item}
Repo.insert(%Item{factors: [1, 2, 3]})
Get the nearest neighbors
import Ecto.Query
import Pgvector.Ecto.Query
Repo.all(from i in Item, order_by: l2_distance(i.factors, [1, 2, 3]), limit: 5)
Also supports max_inner_product and cosine_distance
Add an approximate index in a migration
create index("items", ["factors vector_l2_ops"], using: :ivfflat)
Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance
Postgrex
Register the extension
Postgrex.Types.define(MyApp.PostgrexTypes, [Pgvector.Extensions.Vector], [])
And pass it to start_link
{:ok, pid} = Postgrex.start_link(types: MyApp.PostgrexTypes)
Create a table
Postgrex.query!(pid, "CREATE TABLE items (factors vector(3))", [])
Insert a vector
Postgrex.query!(pid, "INSERT INTO items (factors) VALUES ($1)", [[1, 2, 3]])
Get the nearest neighbors
Postgrex.query!(pid, "SELECT * FROM items ORDER BY factors <-> $1 LIMIT 5", [[1, 2, 3]])
Add an approximate index
Postgrex.query!(pid, "CREATE INDEX my_index ON items USING ivfflat (factors vector_l2_ops)", [])
Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance
History
View the changelog
Contributing
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-elixir.git
cd pgvector-elixir
mix deps.get
createdb pgvector_elixir_test
mix test