-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.dart
73 lines (64 loc) · 2.26 KB
/
example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:pgvector/pgvector.dart';
import 'package:postgres/postgres.dart';
Future<List<dynamic>> embed(
List<String> texts, String inputType, String apiKey) async {
var url = Uri.https('api.cohere.com', 'v1/embed');
var headers = {
'Authorization': 'Bearer ${apiKey}',
'Content-Type': 'application/json'
};
var data = {
'texts': texts,
'model': 'embed-english-v3.0',
'input_type': inputType,
'embedding_types': ['ubinary']
};
var response = await http.post(url, body: jsonEncode(data), headers: headers);
var embeddings = jsonDecode(response.body)['embeddings']['ubinary']
.map((v) => v.map((d) => d.toRadixString(2).padLeft(8, '0')).join())
.toList();
return Future<List<dynamic>>.value(embeddings);
}
void main() async {
var apiKey = Platform.environment['CO_API_KEY'];
if (apiKey == null) {
print('Set CO_API_KEY');
exit(0);
}
var connection = await Connection.open(
Endpoint(
host: 'localhost',
port: 5432,
database: 'pgvector_example',
username: Platform.environment['USER']),
settings: ConnectionSettings(sslMode: SslMode.disable));
await connection.execute('CREATE EXTENSION IF NOT EXISTS vector');
await connection.execute('DROP TABLE IF EXISTS documents');
await connection.execute(
'CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding bit(1024))');
var input = [
'The dog is barking',
'The cat is purring',
'The bear is growling'
];
var embeddings = await embed(input, 'search_document', apiKey);
for (var i = 0; i < input.length; i++) {
await connection.execute(
Sql.named(
'INSERT INTO documents (content, embedding) VALUES (@content, @embedding)'),
parameters: {'content': input[i], 'embedding': embeddings[i]});
}
var query = 'forest';
var queryEmbedding = (await embed([query], 'search_query', apiKey))[0];
var result = await connection.execute(
Sql.named(
'SELECT content FROM documents ORDER BY embedding <~> @embedding LIMIT 5'),
parameters: {'embedding': queryEmbedding});
for (final row in result) {
print(row[0]);
}
await connection.close();
}