Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexample.cpp
More file actions
Latest commit
76 lines (65 loc) · 2.39 KB
/
Copy pathexample.cpp
File metadata and controls
76 lines (65 loc) · 2.39 KB
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
74
75
76
// good resources
// https://opensearch.org/blog/improving-document-retrieval-with-sparse-semantic-encoders/
// https://huggingface.co/opensearch-project/opensearch-neural-sparse-encoding-v1
//
// run with
// text-embeddings-router --model-id opensearch-project/opensearch-neural-sparse-encoding-v1 --pooling splade
#include<cstdint>
#include<iostream>
#include<string>
#include<unordered_map>
#include<vector>
#include<cpr/cpr.h>
#include<nlohmann/json.hpp>
#include<pgvector/pqxx.hpp>
#include<pqxx/pqxx>
using json = nlohmann::json;
std::vector<pgvector::SparseVector> embed(const std::vector<std::string>& inputs) {
std::string url{"http://localhost:3000/embed_sparse"};
json data{{"inputs", inputs}};
cpr::Response r = cpr::Post(
cpr::Url{url}, cpr::Body{data.dump()}, cpr::Header{{"Content-Type", "application/json"}}
);
if (r.status_code != 200) {
throw std::runtime_error{"Bad status: " + std::to_string(r.status_code)};
}
json response = json::parse(r.text);
std::vector<pgvector::SparseVector> embeddings;
for (constauto& item : response) {
std::unordered_map<int, float> map;
for (constauto& e : item) {
map.insert({e["index"], e["value"]});
}
embeddings.emplace_back(pgvector::SparseVector{map, 30522});
}
return embeddings;
}
intmain() {
pqxx::connection conn{"dbname=pgvector_example"};
pqxx::nontransaction tx{conn};
tx.exec("CREATE EXTENSION IF NOT EXISTS vector");
tx.exec("DROP TABLE IF EXISTS documents");
tx.exec(
"CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding sparsevec(30522))"
);
std::vector<std::string> input{
"The dog is barking", "The cat is purring", "The bear is growling"
};
std::vector<pgvector::SparseVector> embeddings = embed(input);
for (size_t i = 0; i < input.size(); i++) {
tx.exec(
"INSERT INTO documents (content, embedding) VALUES ($1, $2)",
pqxx::params{input[i], embeddings[i]}
);
}
std::string query{"forest"};
pgvector::SparseVector query_embedding = embed({query})[0];
pqxx::result result = tx.exec(
"SELECT content FROM documents ORDER BY embedding <#> $1 LIMIT 5",
pqxx::params{query_embedding}
);
for (constauto& row : result) {
std::cout << row[0].as<std::string>() << std::endl;
}
return0;
}