Skip to content

Commit 2ad0adf

Browse files
committed
Improved examples [skip ci]
1 parent d8bf52f commit 2ad0adf

3 files changed

Lines changed: 13 additions & 13 deletions

File tree

examples/cohere/example.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ int main() {
6262
auto embeddings = fetch_embeddings(input, "search_document", api_key);
6363

6464
for (size_t i = 0; i < input.size(); i++) {
65-
tx.exec("INSERT INTO documents (content, embedding) VALUES ($1, $2)", {input[i], embeddings[i]});
65+
tx.exec("INSERT INTO documents (content, embedding) VALUES ($1, $2)", pqxx::params{input[i], embeddings[i]});
6666
}
6767
tx.commit();
6868

6969
std::string query = "forest";
7070
auto query_embedding = fetch_embeddings({query}, "search_query", api_key)[0];
7171
pqxx::result result = tx.exec("SELECT content FROM documents ORDER BY embedding <~> $1 LIMIT 5", pqxx::params{query_embedding});
72-
for (auto const& row : result) {
73-
std::cout << row[0].c_str() << std::endl;
72+
for (const auto& row : result) {
73+
std::cout << row[0].as<std::string>() << std::endl;
7474
}
7575

7676
return 0;

examples/disco/example.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,26 @@ int main() {
7979

8080
for (auto& user_id : recommender.user_ids()) {
8181
auto factors = pgvector::Vector(*recommender.user_factors(user_id));
82-
tx.exec("INSERT INTO users (id, factors) VALUES ($1, $2)", {user_id, factors});
82+
tx.exec("INSERT INTO users (id, factors) VALUES ($1, $2)", pqxx::params{user_id, factors});
8383
}
8484

8585
for (auto& item_id : recommender.item_ids()) {
8686
auto factors = pgvector::Vector(*recommender.item_factors(item_id));
87-
tx.exec("INSERT INTO movies (name, factors) VALUES ($1, $2)", {item_id, factors});
87+
tx.exec("INSERT INTO movies (name, factors) VALUES ($1, $2)", pqxx::params{item_id, factors});
8888
}
8989

9090
std::string movie = "Star Wars (1977)";
9191
std::cout << "Item-based recommendations for " << movie << std::endl;
9292
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});
9393
for (auto const& row : result) {
94-
std::cout << "- " << row[0].c_str() << std::endl;
94+
std::cout << "- " << row[0].as<std::string>() << std::endl;
9595
}
9696

9797
int user_id = 123;
9898
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", {user_id});
100-
for (auto const& row : result) {
101-
std::cout << "- " << row[0].c_str() << 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});
100+
for (const auto& row : result) {
101+
std::cout << "- " << row[0].as<std::string>() << std::endl;
102102
}
103103

104104
return 0;

examples/openai/example.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ int main() {
5454
auto embeddings = fetch_embeddings(input, api_key);
5555

5656
for (size_t i = 0; i < input.size(); i++) {
57-
tx.exec("INSERT INTO documents (content, embedding) VALUES ($1, $2)", {input[i], pgvector::Vector(embeddings[i])});
57+
tx.exec("INSERT INTO documents (content, embedding) VALUES ($1, $2)", pqxx::params{input[i], pgvector::Vector(embeddings[i])});
5858
}
5959
tx.commit();
6060

6161
int document_id = 1;
62-
pqxx::result result = tx.exec("SELECT content FROM documents WHERE id != $1 ORDER BY embedding <=> (SELECT embedding FROM documents WHERE id = $1) LIMIT 5", {document_id});
63-
for (auto const& row : result) {
64-
std::cout << row[0].c_str() << std::endl;
62+
pqxx::result result = tx.exec("SELECT content FROM documents WHERE id != $1 ORDER BY embedding <=> (SELECT embedding FROM documents WHERE id = $1) LIMIT 5", pqxx::params{document_id});
63+
for (const auto& row : result) {
64+
std::cout << row[0].as<std::string>() << std::endl;
6565
}
6666

6767
return 0;

0 commit comments

Comments
 (0)