Skip to content

Commit

Permalink
Added binary_quantize and subvector functions for Ecto
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jun 19, 2024
1 parent c547f0d commit e55f76f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Added support for `bit` type to Ecto
- Added `Pgvector.extensions/0` function
- Added `l1_distance`, `hamming_distance`, and `jaccard_distance` functions for Ecto
- Added `binary_quantize` and `subvector` functions for Ecto
- Dropped support for Elixir < 1.12

## 0.2.1 (2023-09-25)
Expand Down
18 changes: 18 additions & 0 deletions lib/pgvector/ecto/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,23 @@ if Code.ensure_loaded?(Ecto) do
fragment("(? <%> ?)", unquote(column), unquote(value))
end
end

@doc """
Returns the binary quantization
"""
defmacro binary_quantize(value) do
quote do
fragment("binary_quantize(?)", unquote(value))
end
end

@doc """
Returns a subvector
"""
defmacro subvector(value, start, count) do
quote do
fragment("subvector(?, ?, ?)", unquote(value), unquote(start), unquote(count))
end
end
end
end
20 changes: 20 additions & 0 deletions test/ecto_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ defmodule EctoTest do
assert Enum.map(items, fn v -> v.id end) == [1, 3, 2]
end

test "vector binary_quantize" do
item = Repo.one(first(from i in Item, select: binary_quantize(i.embedding)))
assert item == <<1::1, 1::1, 1::1>>
end

test "vector subvector" do
result = Repo.one(last(from i in Item, select: subvector(i.embedding, 2, 2)))
assert result |> Pgvector.to_list() == [1, 2]
end

test "halfvec l2 distance" do
items = Repo.all(from i in Item, order_by: l2_distance(i.half_embedding, ^Pgvector.HalfVector.new([1, 1, 1])), limit: 5)
assert Enum.map(items, fn v -> v.id end) == [1, 3, 2]
Expand All @@ -82,6 +92,16 @@ defmodule EctoTest do
assert Enum.map(items, fn v -> v.id end) == [1, 3, 2]
end

test "halfvec binary_quantize" do
item = Repo.one(first(from i in Item, select: binary_quantize(i.half_embedding)))
assert item == <<1::1, 1::1, 1::1>>
end

test "halfvec subvector" do
result = Repo.one(last(from i in Item, select: subvector(i.half_embedding, 2, 2)))
assert result |> Pgvector.to_list() == [1, 2]
end

test "bit hamming distance" do
items = Repo.all(from i in Item, order_by: hamming_distance(i.binary_embedding, ^<<1::1, 0::1, 1::1>>), limit: 5)
assert Enum.map(items, fn v -> v.id end) == [2, 3, 1]
Expand Down

0 comments on commit e55f76f

Please sign in to comment.