Skip to content

Commit acd822a

Browse files
committed
Ran clang-format [skip ci]
1 parent d3f5414 commit acd822a

File tree

13 files changed

+349
-222
lines changed

13 files changed

+349
-222
lines changed

examples/citus/example.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ int main() {
6666

6767
std::cout << "Creating distributed table" << std::endl;
6868
tx2.exec("DROP TABLE IF EXISTS items");
69-
tx2.exec("CREATE TABLE items (id bigserial, embedding vector(128), category_id bigint, PRIMARY KEY (id, category_id))");
69+
tx2.exec(
70+
"CREATE TABLE items (id bigserial, embedding vector(128), category_id bigint, PRIMARY KEY (id, category_id))"
71+
);
7072
tx2.exec("SET citus.shard_count = 4");
7173
tx2.exec("SELECT create_distributed_table('items', 'category_id')");
7274

examples/cohere/example.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,22 @@ int main() {
6363
tx.exec("CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding bit(1536))");
6464

6565
std::vector<std::string> input{
66-
"The dog is barking",
67-
"The cat is purring",
68-
"The bear is growling"
66+
"The dog is barking", "The cat is purring", "The bear is growling"
6967
};
7068
std::vector<std::string> embeddings = embed(input, "search_document", api_key);
7169
for (size_t i = 0; i < input.size(); i++) {
72-
tx.exec("INSERT INTO documents (content, embedding) VALUES ($1, $2)", pqxx::params{input[i], embeddings[i]});
70+
tx.exec(
71+
"INSERT INTO documents (content, embedding) VALUES ($1, $2)",
72+
pqxx::params{input[i], embeddings[i]}
73+
);
7374
}
7475

7576
std::string query{"forest"};
7677
std::string query_embedding = embed({query}, "search_query", api_key)[0];
77-
pqxx::result result = tx.exec("SELECT content FROM documents ORDER BY embedding <~> $1 LIMIT 5", pqxx::params{query_embedding});
78+
pqxx::result result = tx.exec(
79+
"SELECT content FROM documents ORDER BY embedding <~> $1 LIMIT 5",
80+
pqxx::params{query_embedding}
81+
);
7882
for (const auto& row : result) {
7983
std::cout << row[0].as<std::string>() << std::endl;
8084
}

examples/disco/example.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ Dataset<int, std::string> load_movielens(const std::string& path) {
3636
while (std::getline(movies_file, line)) {
3737
std::string::size_type n = line.find('|');
3838
std::string::size_type n2 = line.find('|', n + 1);
39-
movies.emplace(std::make_pair(line.substr(0, n), convert_to_utf8(line.substr(n + 1, n2 - n - 1))));
39+
movies.emplace(
40+
std::make_pair(line.substr(0, n), convert_to_utf8(line.substr(n + 1, n2 - n - 1)))
41+
);
4042
}
4143

4244
// read ratings and create dataset
@@ -75,7 +77,7 @@ int main() {
7577
tx.exec("CREATE TABLE movies (name text PRIMARY KEY, factors vector(20))");
7678

7779
Dataset<int, std::string> data = load_movielens(movielens_path);
78-
auto recommender = Recommender<int, std::string>::fit_explicit(data, { .factors = 20 });
80+
auto recommender = Recommender<int, std::string>::fit_explicit(data, {.factors = 20});
7981

8082
for (const auto& user_id : recommender.user_ids()) {
8183
pgvector::Vector factors{*recommender.user_factors(user_id)};
@@ -84,19 +86,27 @@ int main() {
8486

8587
for (const auto& item_id : recommender.item_ids()) {
8688
pgvector::Vector factors{*recommender.item_factors(item_id)};
87-
tx.exec("INSERT INTO movies (name, factors) VALUES ($1, $2)", pqxx::params{item_id, factors});
89+
tx.exec(
90+
"INSERT INTO movies (name, factors) VALUES ($1, $2)", pqxx::params{item_id, factors}
91+
);
8892
}
8993

9094
std::string movie{"Star Wars (1977)"};
9195
std::cout << "Item-based recommendations for " << movie << std::endl;
92-
pqxx::result result = tx.exec("SELECT name FROM movies WHERE name != $1 ORDER BY factors <=> (SELECT factors FROM movies WHERE name = $1) LIMIT 5", pqxx::params{movie});
96+
pqxx::result result = tx.exec(
97+
"SELECT name FROM movies WHERE name != $1 ORDER BY factors <=> (SELECT factors FROM movies WHERE name = $1) LIMIT 5",
98+
pqxx::params{movie}
99+
);
93100
for (const auto& row : result) {
94101
std::cout << "- " << row[0].as<std::string>() << std::endl;
95102
}
96103

97104
int user_id = 123;
98105
std::cout << std::endl << "User-based recommendations for " << user_id << std::endl;
99-
result = tx.exec("SELECT name FROM movies ORDER BY factors <#> (SELECT factors FROM users WHERE id = $1) LIMIT 5", pqxx::params{user_id});
106+
result = tx.exec(
107+
"SELECT name FROM movies ORDER BY factors <#> (SELECT factors FROM users WHERE id = $1) LIMIT 5",
108+
pqxx::params{user_id}
109+
);
100110
for (const auto& row : result) {
101111
std::cout << "- " << row[0].as<std::string>() << std::endl;
102112
}

examples/hybrid/example.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,10 @@ std::vector<std::vector<float>> embed(
2626
}
2727

2828
std::string url{"http://localhost:8080/v1/embeddings"};
29-
json data{
30-
{"input", input}
31-
};
29+
json data{{"input", input}};
3230

3331
cpr::Response r = cpr::Post(
34-
cpr::Url{url},
35-
cpr::Body{data.dump()},
36-
cpr::Header{{"Content-Type", "application/json"}}
32+
cpr::Url{url}, cpr::Body{data.dump()}, cpr::Header{{"Content-Type", "application/json"}}
3733
);
3834
if (r.status_code != 200) {
3935
throw std::runtime_error{"Bad status: " + std::to_string(r.status_code)};
@@ -53,18 +49,21 @@ int main() {
5349
pqxx::nontransaction tx{conn};
5450
tx.exec("CREATE EXTENSION IF NOT EXISTS vector");
5551
tx.exec("DROP TABLE IF EXISTS documents");
56-
tx.exec("CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector(768))");
52+
tx.exec(
53+
"CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector(768))"
54+
);
5755
tx.exec("CREATE INDEX ON documents USING GIN (to_tsvector('english', content))");
5856

5957
std::vector<std::string> input{
60-
"The dog is barking",
61-
"The cat is purring",
62-
"The bear is growling"
58+
"The dog is barking", "The cat is purring", "The bear is growling"
6359
};
6460
std::vector<std::vector<float>> embeddings = embed(input, "search_document");
6561

6662
for (size_t i = 0; i < input.size(); i++) {
67-
tx.exec("INSERT INTO documents (content, embedding) VALUES ($1, $2)", pqxx::params{input[i], pgvector::Vector{embeddings[i]}});
63+
tx.exec(
64+
"INSERT INTO documents (content, embedding) VALUES ($1, $2)",
65+
pqxx::params{input[i], pgvector::Vector{embeddings[i]}}
66+
);
6867
}
6968

7069
std::string sql{R"(
@@ -95,7 +94,8 @@ int main() {
9594
double k = 60;
9695
pqxx::result result = tx.exec(sql, pqxx::params{query, pgvector::Vector{query_embedding}, k});
9796
for (const auto& row : result) {
98-
std::cout << "document: " << row[0].as<std::string>() << ", RRF score: " << row[1].as<double>() << std::endl;
97+
std::cout << "document: " << row[0].as<std::string>()
98+
<< ", RRF score: " << row[1].as<double>() << std::endl;
9999
}
100100

101101
return 0;

examples/openai/example.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ using json = nlohmann::json;
1313
// input can be an array with 2048 elements
1414
std::vector<std::vector<float>> embed(const std::vector<std::string>& input, char* api_key) {
1515
std::string url{"https://api.openai.com/v1/embeddings"};
16-
json data{
17-
{"input", input},
18-
{"model", "text-embedding-3-small"}
19-
};
16+
json data{{"input", input}, {"model", "text-embedding-3-small"}};
2017

2118
cpr::Response r = cpr::Post(
2219
cpr::Url{url},
@@ -48,21 +45,27 @@ int main() {
4845
pqxx::nontransaction tx{conn};
4946
tx.exec("CREATE EXTENSION IF NOT EXISTS vector");
5047
tx.exec("DROP TABLE IF EXISTS documents");
51-
tx.exec("CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector(1536))");
48+
tx.exec(
49+
"CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector(1536))"
50+
);
5251

5352
std::vector<std::string> input{
54-
"The dog is barking",
55-
"The cat is purring",
56-
"The bear is growling"
53+
"The dog is barking", "The cat is purring", "The bear is growling"
5754
};
5855
std::vector<std::vector<float>> embeddings = embed(input, api_key);
5956
for (size_t i = 0; i < input.size(); i++) {
60-
tx.exec("INSERT INTO documents (content, embedding) VALUES ($1, $2)", pqxx::params{input[i], pgvector::Vector{embeddings[i]}});
57+
tx.exec(
58+
"INSERT INTO documents (content, embedding) VALUES ($1, $2)",
59+
pqxx::params{input[i], pgvector::Vector{embeddings[i]}}
60+
);
6161
}
6262

6363
std::string query{"forest"};
6464
std::vector<float> query_embedding = embed({query}, api_key)[0];
65-
pqxx::result result = tx.exec("SELECT content FROM documents ORDER BY embedding <=> $1 LIMIT 5", pqxx::params{pgvector::Vector{query_embedding}});
65+
pqxx::result result = tx.exec(
66+
"SELECT content FROM documents ORDER BY embedding <=> $1 LIMIT 5",
67+
pqxx::params{pgvector::Vector{query_embedding}}
68+
);
6669
for (const auto& row : result) {
6770
std::cout << row[0].as<std::string>() << std::endl;
6871
}

examples/rdkit/example.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
std::string generate_fingerprint(const std::string& molecule) {
1313
std::unique_ptr<RDKit::ROMol> mol{RDKit::SmilesToMol(molecule)};
14-
std::unique_ptr<ExplicitBitVect> fp{RDKit::MorganFingerprints::getFingerprintAsBitVect(*mol, 3, 2048)};
14+
std::unique_ptr<ExplicitBitVect> fp{
15+
RDKit::MorganFingerprints::getFingerprintAsBitVect(*mol, 3, 2048)
16+
};
1517
std::stringstream buf;
1618
for (size_t i = 0; i < fp->getNumBits(); i++) {
1719
buf << (fp->getBit(i) ? '1' : '0');
@@ -30,12 +32,18 @@ int main() {
3032
std::vector<std::string> molecules{"Cc1ccccc1", "Cc1ncccc1", "c1ccccn1"};
3133
for (const auto& molecule : molecules) {
3234
std::string fingerprint = generate_fingerprint(molecule);
33-
tx.exec("INSERT INTO molecules (id, fingerprint) VALUES ($1, $2)", pqxx::params{molecule, fingerprint});
35+
tx.exec(
36+
"INSERT INTO molecules (id, fingerprint) VALUES ($1, $2)",
37+
pqxx::params{molecule, fingerprint}
38+
);
3439
}
3540

3641
std::string query_molecule{"c1ccco1"};
3742
std::string query_fingerprint = generate_fingerprint(query_molecule);
38-
pqxx::result result = tx.exec("SELECT id, fingerprint <%> $1 AS distance FROM molecules ORDER BY distance LIMIT 5", pqxx::params{query_fingerprint});
43+
pqxx::result result = tx.exec(
44+
"SELECT id, fingerprint <%> $1 AS distance FROM molecules ORDER BY distance LIMIT 5",
45+
pqxx::params{query_fingerprint}
46+
);
3947
for (const auto& row : result) {
4048
std::cout << row[0].as<std::string>() << ": " << row[1].as<double>() << std::endl;
4149
}

examples/sparse/example.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@ using json = nlohmann::json;
2020

2121
std::vector<pgvector::SparseVector> embed(const std::vector<std::string>& inputs) {
2222
std::string url{"http://localhost:3000/embed_sparse"};
23-
json data{
24-
{"inputs", inputs}
25-
};
23+
json data{{"inputs", inputs}};
2624

2725
cpr::Response r = cpr::Post(
28-
cpr::Url{url},
29-
cpr::Body{data.dump()},
30-
cpr::Header{{"Content-Type", "application/json"}}
26+
cpr::Url{url}, cpr::Body{data.dump()}, cpr::Header{{"Content-Type", "application/json"}}
3127
);
3228
if (r.status_code != 200) {
3329
throw std::runtime_error{"Bad status: " + std::to_string(r.status_code)};
@@ -51,21 +47,27 @@ int main() {
5147
pqxx::nontransaction tx{conn};
5248
tx.exec("CREATE EXTENSION IF NOT EXISTS vector");
5349
tx.exec("DROP TABLE IF EXISTS documents");
54-
tx.exec("CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding sparsevec(30522))");
50+
tx.exec(
51+
"CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding sparsevec(30522))"
52+
);
5553

5654
std::vector<std::string> input{
57-
"The dog is barking",
58-
"The cat is purring",
59-
"The bear is growling"
55+
"The dog is barking", "The cat is purring", "The bear is growling"
6056
};
6157
std::vector<pgvector::SparseVector> embeddings = embed(input);
6258
for (size_t i = 0; i < input.size(); i++) {
63-
tx.exec("INSERT INTO documents (content, embedding) VALUES ($1, $2)", pqxx::params{input[i], embeddings[i]});
59+
tx.exec(
60+
"INSERT INTO documents (content, embedding) VALUES ($1, $2)",
61+
pqxx::params{input[i], embeddings[i]}
62+
);
6463
}
6564

6665
std::string query{"forest"};
6766
pgvector::SparseVector query_embedding = embed({query})[0];
68-
pqxx::result result = tx.exec("SELECT content FROM documents ORDER BY embedding <#> $1 LIMIT 5", pqxx::params{query_embedding});
67+
pqxx::result result = tx.exec(
68+
"SELECT content FROM documents ORDER BY embedding <#> $1 LIMIT 5",
69+
pqxx::params{query_embedding}
70+
);
6971
for (const auto& row : result) {
7072
std::cout << row[0].as<std::string>() << std::endl;
7173
}

include/pgvector/halfvec.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class HalfVector {
3939
explicit HalfVector(std::vector<Half>&& value) : value_{std::move(value)} {}
4040

4141
/// Creates a half vector from a span.
42-
explicit HalfVector(std::span<const Half> value) : value_{std::vector<Half>(value.begin(), value.end())} {}
42+
explicit HalfVector(std::span<const Half> value) :
43+
value_{std::vector<Half>(value.begin(), value.end())} {}
4344

4445
/// Returns the number of dimensions.
4546
size_t dimensions() const {

include/pgvector/pqxx.hpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ struct string_traits<pgvector::HalfVector> {
126126
return pgvector::HalfVector{std::move(values)};
127127
}
128128

129-
static std::string_view to_buf(std::span<char> buf, const pgvector::HalfVector& value, ctx c = {}) {
129+
static std::string_view to_buf(
130+
std::span<char> buf,
131+
const pgvector::HalfVector& value,
132+
ctx c = {}
133+
) {
130134
// confirm caller provided estimated buffer space
131135
if (buf.size() < size_buffer(value)) {
132136
throw conversion_overrun{"Not enough space in buffer for halfvec"};
@@ -227,7 +231,11 @@ struct string_traits<pgvector::SparseVector> {
227231
}
228232
}
229233

230-
static std::string_view to_buf(std::span<char> buf, const pgvector::SparseVector& value, ctx c = {}) {
234+
static std::string_view to_buf(
235+
std::span<char> buf,
236+
const pgvector::SparseVector& value,
237+
ctx c = {}
238+
) {
231239
// confirm caller provided estimated buffer space
232240
if (buf.size() < size_buffer(value)) {
233241
throw conversion_overrun{"Not enough space in buffer for sparsevec"};
@@ -253,7 +261,9 @@ struct string_traits<pgvector::SparseVector> {
253261
here += pqxx::into_buf(buf.subspan(here), ",", c);
254262
}
255263
// cast to avoid undefined behavior and require less buffer space
256-
here += pqxx::into_buf(buf.subspan(here), static_cast<unsigned int>(indices.at(i)) + 1, c);
264+
here += pqxx::into_buf(
265+
buf.subspan(here), static_cast<unsigned int>(indices.at(i)) + 1, c
266+
);
257267
here += pqxx::into_buf(buf.subspan(here), ":", c);
258268
here += pqxx::into_buf(buf.subspan(here), values.at(i), c);
259269
}

include/pgvector/sparsevec.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ namespace pgvector {
2020
class SparseVector {
2121
public:
2222
/// Creates a sparse vector from a dense vector.
23-
explicit SparseVector(const std::vector<float>& value) : SparseVector(std::span<const float>{value}) {}
23+
explicit SparseVector(const std::vector<float>& value) :
24+
SparseVector(std::span<const float>{value}) {}
2425

2526
/// Creates a sparse vector from a span.
2627
explicit SparseVector(std::span<const float> value) {
@@ -82,7 +83,8 @@ class SparseVector {
8283
}
8384

8485
friend bool operator==(const SparseVector& lhs, const SparseVector& rhs) {
85-
return lhs.dimensions_ == rhs.dimensions_ && lhs.indices_ == rhs.indices_ && lhs.values_ == rhs.values_;
86+
return lhs.dimensions_ == rhs.dimensions_ && lhs.indices_ == rhs.indices_
87+
&& lhs.values_ == rhs.values_;
8688
}
8789

8890
friend std::ostream& operator<<(std::ostream& os, const SparseVector& value) {

0 commit comments

Comments
 (0)