1
- Postgrex.Types . define ( PostgrexApp.PostgrexTypes , [ Pgvector.Extensions.Vector ] , [ ] )
1
+ Postgrex.Types . define ( PostgrexApp.PostgrexTypes , Pgvector . extensions ( ) , [ ] )
2
2
3
3
# needed if postgrex is optional
4
4
# Application.ensure_all_started(:postgrex)
@@ -10,7 +10,7 @@ defmodule PostgrexTest do
10
10
{ :ok , pid } = Postgrex . start_link ( database: "pgvector_elixir_test" , types: PostgrexApp.PostgrexTypes )
11
11
Postgrex . query! ( pid , "CREATE EXTENSION IF NOT EXISTS vector" , [ ] )
12
12
Postgrex . query! ( pid , "DROP TABLE IF EXISTS postgrex_items" , [ ] )
13
- Postgrex . query! ( pid , "CREATE TABLE postgrex_items (id bigserial primary key, embedding vector(3), binary_embedding bit(3))" , [ ] )
13
+ Postgrex . query! ( pid , "CREATE TABLE postgrex_items (id bigserial primary key, embedding vector(3), half_embedding halfvec(3), binary_embedding bit(3))" , [ ] )
14
14
{ :ok , pid: pid }
15
15
end
16
16
@@ -19,7 +19,7 @@ defmodule PostgrexTest do
19
19
context
20
20
end
21
21
22
- test "l2 distance" , % { pid: pid } = _context do
22
+ test "vector l2 distance" , % { pid: pid } = _context do
23
23
embeddings = [ Pgvector . new ( [ 1 , 1 , 1 ] ) , [ 2 , 2 , 2 ] , Nx . tensor ( [ 1 , 1 , 2 ] , type: :f32 ) ]
24
24
Postgrex . query! ( pid , "INSERT INTO postgrex_items (embedding) VALUES ($1), ($2), ($3)" , embeddings )
25
25
@@ -30,6 +30,17 @@ defmodule PostgrexTest do
30
30
assert Enum . map ( result . rows , fn v -> Enum . at ( v , 1 ) |> Pgvector . to_list ( ) end ) == [ [ 1.0 , 1.0 , 1.0 ] , [ 1.0 , 1.0 , 2.0 ] , [ 2.0 , 2.0 , 2.0 ] ]
31
31
end
32
32
33
+ test "halfvec l2 distance" , % { pid: pid } = _context do
34
+ embeddings = [ Pgvector.HalfVector . new ( [ 1 , 1 , 1 ] ) , [ 2 , 2 , 2 ] , Nx . tensor ( [ 1 , 1 , 2 ] , type: :f16 ) ]
35
+ Postgrex . query! ( pid , "INSERT INTO postgrex_items (half_embedding) VALUES ($1), ($2), ($3)" , embeddings )
36
+
37
+ result = Postgrex . query! ( pid , "SELECT id, half_embedding FROM postgrex_items ORDER BY half_embedding <-> $1 LIMIT 5" , [ [ 1 , 1 , 1 ] ] )
38
+
39
+ assert [ "id" , "half_embedding" ] == result . columns
40
+ assert Enum . map ( result . rows , fn v -> Enum . at ( v , 0 ) end ) == [ 1 , 3 , 2 ]
41
+ assert Enum . map ( result . rows , fn v -> Enum . at ( v , 1 ) |> Pgvector . to_list ( ) end ) == [ [ 1.0 , 1.0 , 1.0 ] , [ 1.0 , 1.0 , 2.0 ] , [ 2.0 , 2.0 , 2.0 ] ]
42
+ end
43
+
33
44
test "create index" , % { pid: pid } = _context do
34
45
Postgrex . query! ( pid , "CREATE INDEX my_index ON postgrex_items USING ivfflat (embedding vector_l2_ops) WITH (lists = 1)" , [ ] )
35
46
end
0 commit comments