Skip to content

Commit d44c22d

Browse files
committed
Added package
1 parent 0d8bdf5 commit d44c22d

File tree

17 files changed

+149
-38
lines changed

17 files changed

+149
-38
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ jobs:
1616
make
1717
sudo make install
1818
- run: julia --project=. -e "using Pkg; Pkg.instantiate()"
19+
- run: julia --project=. -e "using Pkg; Pkg.test()"
1920
- run: julia --project=. LibPQ/example.jl

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.1.0 (unreleased)
2+
3+
- First release

LibPQ/example.jl

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
1-
using LibPQ, Tables
1+
using LibPQ, Pgvector, Tables
22

33
conn = LibPQ.Connection("dbname=pgvector_julia_test host=localhost")
44

55
execute(conn, "CREATE EXTENSION IF NOT EXISTS vector")
66
execute(conn, "DROP TABLE IF EXISTS items")
77
execute(conn, "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))")
88

9-
module Pgvector
10-
convert(vec::AbstractVector{T}) where T<:Number = string("[", join(vec, ","), "]")
11-
12-
parse(str::String) = map(x -> Base.parse(Float32, x), split(str[2:end-1], ","))
13-
end
9+
Pgvector.register!(conn)
1410

1511
embeddings = [[1, 1, 1], [2, 2, 2], [1, 1, 2]]
1612
LibPQ.load!(
17-
(embedding = map(Pgvector.convert, embeddings),),
13+
(embedding = map(Pgvector.Vector, embeddings),),
1814
conn,
1915
"INSERT INTO items (embedding) VALUES (\$1)",
2016
)
2117

22-
embedding = Pgvector.convert([1, 1, 1])
23-
result = execute(conn, "SELECT * FROM items ORDER BY embedding <-> \$1 LIMIT 5", [embedding])
18+
embedding = [1, 1, 1]
19+
result = execute(conn, "SELECT * FROM items ORDER BY embedding <-> \$1 LIMIT 5", [Pgvector.Vector(embedding)])
2420
data = columntable(result)
25-
println(map(Pgvector.parse, data.embedding))
21+
println(data)
2622

2723
execute(conn, "CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)")
2824

Project.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
name = "Pgvector"
2+
uuid = "ab7f0314-681e-439b-b2a8-1689834ece97"
3+
authors = ["Andrew Kane <[email protected]>"]
4+
version = "0.1.0"
5+
6+
[compat]
7+
julia = "1.10"
8+
19
[deps]
210
LibPQ = "194296ae-ab2e-5f79-8cd4-7183a0a5a0d1"
11+
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
312
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
13+
14+
[extras]
15+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
16+
17+
[targets]
18+
test = ["Test"]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pgvector-julia
22

3-
[pgvector](https://github.com/pgvector/pgvector) examples for Julia
3+
[pgvector](https://github.com/pgvector/pgvector) support for Julia
44

55
Supports [LibPQ.jl](https://github.com/iamed2/LibPQ.jl)
66

@@ -85,7 +85,7 @@ git clone https://github.com/pgvector/pgvector-julia.git
8585
cd pgvector-julia
8686
createdb pgvector_julia_test
8787
julia --project=. -e "using Pkg; Pkg.instantiate()"
88-
julia --project=. LibPQ/example.jl
88+
julia --project=. -e "using Pkg; Pkg.test()"
8989
```
9090

9191
To run an example:

examples/hybrid/Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
33
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
44
LibPQ = "194296ae-ab2e-5f79-8cd4-7183a0a5a0d1"
5+
Pgvector = "ab7f0314-681e-439b-b2a8-1689834ece97"
56
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
7+
8+
[sources]
9+
Pgvector = {path = "../.."}

examples/hybrid/example.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using HTTP, JSON, LibPQ, Tables
1+
using HTTP, JSON, LibPQ, Pgvector, Tables
22

33
conn = LibPQ.Connection("dbname=pgvector_example host=localhost")
44

@@ -7,10 +7,6 @@ execute(conn, "DROP TABLE IF EXISTS documents")
77
execute(conn, "CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector(768))")
88
execute(conn, "CREATE INDEX ON documents USING GIN (to_tsvector('english', content))")
99

10-
module Pgvector
11-
convert(v::AbstractVector{T}) where T<:Real = string("[", join(v, ","), "]")
12-
end
13-
1410
function embed(input, task)
1511
# nomic-embed-text uses a task prefix
1612
# https://huggingface.co/nomic-ai/nomic-embed-text-v1.5
@@ -35,7 +31,7 @@ input = [
3531
]
3632
embeddings = embed(input, "search_document")
3733
LibPQ.load!(
38-
(content = input, embedding = map(Pgvector.convert, embeddings)),
34+
(content = input, embedding = map(Pgvector.Vector, embeddings)),
3935
conn,
4036
"INSERT INTO documents (content, embedding) VALUES (\$1, \$2)",
4137
)
@@ -66,7 +62,7 @@ LIMIT 5
6662
query = "growling bear"
6763
embedding = embed([query], "search_query")[1]
6864
k = 60
69-
result = execute(conn, sql, [query, Pgvector.convert(embedding), k])
65+
result = execute(conn, sql, [query, Pgvector.Vector(embedding), k])
7066
rows = Tables.rows(columntable(result))
7167
for row in rows
7268
id = Tables.getcolumn(row, 1)

examples/openai/Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
33
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
44
LibPQ = "194296ae-ab2e-5f79-8cd4-7183a0a5a0d1"
5+
Pgvector = "ab7f0314-681e-439b-b2a8-1689834ece97"
56
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
7+
8+
[sources]
9+
Pgvector = {path = "../.."}

examples/openai/example.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
using HTTP, JSON, LibPQ, Tables
1+
using HTTP, JSON, LibPQ, Pgvector, Tables
22

33
conn = LibPQ.Connection("dbname=pgvector_example host=localhost")
44

55
execute(conn, "CREATE EXTENSION IF NOT EXISTS vector")
66
execute(conn, "DROP TABLE IF EXISTS documents")
77
execute(conn, "CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector(1536))")
88

9-
module Pgvector
10-
convert(v::AbstractVector{T}) where T<:Real = string("[", join(v, ","), "]")
11-
end
12-
139
function embed(input)
1410
url = "https://api.openai.com/v1/embeddings"
1511
data = Dict(
@@ -31,14 +27,14 @@ input = [
3127
]
3228
embeddings = embed(input)
3329
LibPQ.load!(
34-
(content = input, embedding = map(Pgvector.convert, embeddings)),
30+
(content = input, embedding = map(Pgvector.Vector, embeddings)),
3531
conn,
3632
"INSERT INTO documents (content, embedding) VALUES (\$1, \$2)",
3733
)
3834

3935
query = "forest"
4036
embedding = embed([query])[1]
41-
result = execute(conn, "SELECT content FROM documents ORDER BY embedding <=> \$1 LIMIT 5", [Pgvector.convert(embedding)])
37+
result = execute(conn, "SELECT content FROM documents ORDER BY embedding <=> \$1 LIMIT 5", [Pgvector.Vector(embedding)])
4238
rows = Tables.rows(columntable(result))
4339
for row in rows
4440
println(Tables.getcolumn(row, 1))

examples/sparse/Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
33
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
44
LibPQ = "194296ae-ab2e-5f79-8cd4-7183a0a5a0d1"
5+
Pgvector = "ab7f0314-681e-439b-b2a8-1689834ece97"
56
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
7+
8+
[sources]
9+
Pgvector = {path = "../.."}

0 commit comments

Comments
 (0)