1
1
# pgvector-dart
2
2
3
- [ pgvector] ( https://github.com/pgvector/pgvector ) examples for Dart
3
+ [ pgvector] ( https://github.com/pgvector/pgvector ) support for Dart
4
4
5
5
Supports the [ postgres] ( https://github.com/isoos/postgresql-dart ) package
6
6
7
7
[ ![ Build Status] ( https://github.com/pgvector/pgvector-dart/workflows/build/badge.svg?branch=master )] ( https://github.com/pgvector/pgvector-dart/actions )
8
8
9
9
## Getting Started
10
10
11
- Follow the instructions for your database library:
11
+ Run:
12
+
13
+ ``` sh
14
+ dart pub add pgvector
15
+ ```
16
+
17
+ And follow the instructions for your database library:
12
18
13
19
- [ postgres] ( #postgres )
14
20
15
21
## postgres
16
22
23
+ Import the library
24
+
25
+ ``` dart
26
+ import 'package:pgvector/pgvector.dart';
27
+ ```
28
+
17
29
Enable the extension
18
30
19
31
``` dart
@@ -33,23 +45,23 @@ Insert vectors
33
45
await connection.execute(
34
46
"INSERT INTO items (embedding) VALUES (@a), (@b), (@c)",
35
47
substitutionValues: {
36
- "a": [1, 1, 1].toString( ),
37
- "b": [2, 2, 2].toString( ),
38
- "c": [1, 1, 2].toString( )
48
+ "a": pgvector.encode( [1, 1, 1]),
49
+ "b": pgvector.encode( [2, 2, 2]),
50
+ "c": pgvector.encode( [1, 1, 2])
39
51
});
40
52
```
41
53
42
54
Get the nearest neighbors
43
55
44
56
``` dart
45
57
List<List<dynamic>> results = await connection.query(
46
- "SELECT id, embedding::text FROM items ORDER BY embedding <-> @embedding LIMIT 5",
58
+ "SELECT id, embedding FROM items ORDER BY embedding <-> @embedding LIMIT 5",
47
59
substitutionValues: {
48
- "embedding": [1, 1, 1].toString( )
60
+ "embedding": pgvector.encode( [1, 1, 1])
49
61
});
50
62
for (final row in results) {
51
63
print(row[0]);
52
- print(jsonDecode (row[1]));
64
+ print(pgvector.decode (row[1]));
53
65
}
54
66
```
55
67
0 commit comments