Skip to content

Commit d8d11ef

Browse files
committed
Added package
1 parent dab0bd0 commit d8d11ef

File tree

14 files changed

+219
-29
lines changed

14 files changed

+219
-29
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,4 @@ jobs:
2121
make
2222
sudo make install
2323
- run: nimble -y install db_connector
24-
- run: nim c example.nim
25-
- run: ./example
24+
- run: nimble test --features:dev --parser:declarative

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

README.md

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

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

55
Supports [db_connector](https://github.com/nim-lang/db_connector)
66

@@ -63,7 +63,11 @@ db.exec(sql"CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (
6363

6464
Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance
6565

66-
See a [full example](example.nim)
66+
See a [full example](tests/tdb_connector.nim)
67+
68+
## History
69+
70+
View the [changelog](https://github.com/pgvector/pgvector-nim/blob/master/CHANGELOG.md)
6771

6872
## Contributing
6973

@@ -81,13 +85,13 @@ git clone https://github.com/pgvector/pgvector-nim.git
8185
cd pgvector-nim
8286
createdb pgvector_nim_test
8387
nimble install db_connector
84-
nim c --run example.nim
88+
nimble test --features:dev --parser:declarative
8589
```
8690

8791
Specify the path to libpq if needed:
8892

8993
```sh
90-
nim c --run --dynlibOverride:pq --passL:"/opt/homebrew/opt/libpq/lib/libpq.dylib" example.nim
94+
nimble test --features:dev --parser:declarative --dynlibOverride:pq --passL:"/opt/homebrew/opt/libpq/lib/libpq.dylib"
9195
```
9296

9397
To run an example:
@@ -97,3 +101,9 @@ cd examples/openai
97101
createdb pgvector_example
98102
nim c --run example.nim
99103
```
104+
105+
Specify the path to libpq if needed:
106+
107+
```sh
108+
nim c --run --dynlibOverride:pq --passL:"/opt/homebrew/opt/libpq/lib/libpq.dylib" example.nim
109+
```

example.nim

Lines changed: 0 additions & 23 deletions
This file was deleted.

pgvector.nimble

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Package
2+
3+
version = "0.1.0"
4+
author = "Andrew Kane"
5+
description = "pgvector support for Nim"
6+
license = "MIT"
7+
srcDir = "src"
8+
9+
10+
# Dependencies
11+
12+
requires "nim >= 2.0.0"
13+
14+
dev:
15+
requires "db_connector >= 0.1.0"

src/pgvector.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import pgvector/[halfvec, sparsevec, vector]
2+
3+
export halfvec, sparsevec, vector

src/pgvector/halfvec.nim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import std/[json, sequtils, sugar]
2+
3+
type HalfVector* = object
4+
vec: seq[float32]
5+
6+
proc `$`*(v: HalfVector): string =
7+
result = $(%* v.vec)
8+
9+
proc toSeq*(v: HalfVector): seq[float32] =
10+
result = v.vec
11+
12+
proc toHalfVector*[T](a: openArray[T]): HalfVector =
13+
result = HalfVector(vec: a.toSeq.map(v => float32(v)))
14+
15+
proc toHalfVector*(s: string): HalfVector =
16+
result = HalfVector(vec: to(parseJson(s), seq[float32]))

src/pgvector/sparsevec.nim

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import std/[strutils, sugar, tables]
2+
3+
type SparseVector* = object
4+
elements*: Table[int, float32]
5+
dim*: int
6+
7+
proc `$`*(v: SparseVector): string =
8+
let elements = collect(newSeqOfCap(v.elements.len)):
9+
for k, v in v.elements: $(k + 1) & ":" & $v
10+
result = "{" & elements.join(",") & "}/" & $v.dim
11+
12+
proc toSparseVector*[T](a: openArray[T]): SparseVector =
13+
var elements = initTable[int, float32]()
14+
for i, v in a:
15+
let f = float32(v)
16+
if f != 0:
17+
elements[i] = f
18+
result = SparseVector(elements: elements, dim: a.len)
19+
20+
proc toSparseVector*[T](elements: Table[int, T], dim: int): SparseVector =
21+
var e = initTable[int, float32]()
22+
for k, v in elements:
23+
e[k] = float32(v)
24+
result = SparseVector(elements: e, dim: dim)
25+
26+
proc toSparseVector*(s: string): SparseVector =
27+
let parts = s.split("}/", maxsplit = 2)
28+
var elements = initTable[int, float32]()
29+
for e in parts[0][1..^1].split(","):
30+
let ep = e.split(":", maxsplit = 2)
31+
let index = parseInt(ep[0])
32+
let value = float32(parseFloat(ep[1]))
33+
elements[index - 1] = value
34+
let dim = parseInt(parts[1])
35+
result = SparseVector(elements: elements, dim: dim)

src/pgvector/vector.nim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import std/[json, sequtils, sugar]
2+
3+
type Vector* = object
4+
vec: seq[float32]
5+
6+
proc `$`*(v: Vector): string =
7+
result = $(%* v.vec)
8+
9+
proc toSeq*(v: Vector): seq[float32] =
10+
result = v.vec
11+
12+
proc toVector*[T](a: openArray[T]): Vector =
13+
result = Vector(vec: a.toSeq.map(v => float32(v)))
14+
15+
proc toVector*(s: string): Vector =
16+
result = Vector(vec: to(parseJson(s), seq[float32]))

tests/config.nims

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
switch("path", "$projectDir/../src")

0 commit comments

Comments
 (0)