Skip to content

Commit 0bb643e

Browse files
committed
Updated readme [skip ci]
1 parent d8d11ef commit 0bb643e

File tree

1 file changed

+69
-4
lines changed

1 file changed

+69
-4
lines changed

README.md

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ Supports [db_connector](https://github.com/nim-lang/db_connector)
88

99
## Getting Started
1010

11-
Follow the instructions for your database library:
11+
Run:
12+
13+
```sh
14+
nimble add https://github.com/pgvector/pgvector-nim.git
15+
```
16+
17+
And follow the instructions for your database library:
1218

1319
- [db_connector](#db_connector)
1420

@@ -36,19 +42,19 @@ db.exec(sql"CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))")
3642
Insert vectors
3743

3844
```nim
39-
import std/json
45+
import pgvector
4046
4147
let embedding1 = @[1, 1, 1]
4248
let embedding2 = @[1, 1, 2]
4349
let embedding3 = @[2, 2, 2]
44-
db.exec(sql"INSERT INTO items (embedding) VALUES (?), (?), (?)", %* embedding1, %* embedding2, %* embedding3)
50+
db.exec(sql"INSERT INTO items (embedding) VALUES (?), (?), (?)", embedding1.toVector, embedding2.toVector, embedding3.toVector)
4551
```
4652

4753
Get the nearest neighbors
4854

4955
```nim
5056
let embedding = @[1, 1, 1]
51-
let rows = db.getAllRows(sql"SELECT * FROM items ORDER BY embedding <-> ? LIMIT 5", %* embedding)
57+
let rows = db.getAllRows(sql"SELECT * FROM items ORDER BY embedding <-> ? LIMIT 5", embedding.toVector)
5258
for row in rows:
5359
echo row
5460
```
@@ -65,6 +71,65 @@ Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distanc
6571

6672
See a [full example](tests/tdb_connector.nim)
6773

74+
## Reference
75+
76+
### Vectors
77+
78+
Create a vector from a sequence or array
79+
80+
```nim
81+
let vec = @[1.0, 2.0, 3.0].toVector
82+
```
83+
84+
Get a sequence
85+
86+
```nim
87+
let s = vec.toSeq
88+
```
89+
90+
### Half Vectors
91+
92+
Create a half vector from a sequence or array
93+
94+
```nim
95+
let vec = @[1.0, 2.0, 3.0].toHalfVector
96+
```
97+
98+
Get a sequence
99+
100+
```nim
101+
let s = vec.toSeq
102+
```
103+
104+
### Sparse Vectors
105+
106+
Create a sparse vector from a sequence or array
107+
108+
```nim
109+
let vec = @[1.0, 0.0, 2.0, 0.0, 3.0, 0.0].toSparseVector
110+
```
111+
112+
Or a table of non-zero elements
113+
114+
```nim
115+
let elements = {0: 1.0, 2: 2.0, 4: 3.0}.toTable
116+
let vec = table.toSparseVector(6)
117+
```
118+
119+
Note: Indices start at 0
120+
121+
Get the number of dimensions
122+
123+
```nim
124+
let dim = vec.dim
125+
```
126+
127+
Get the non-zero elements
128+
129+
```nim
130+
let elements = vec.elements
131+
```
132+
68133
## History
69134

70135
View the [changelog](https://github.com/pgvector/pgvector-nim/blob/master/CHANGELOG.md)

0 commit comments

Comments
 (0)