10 #include <unordered_map>
15 #include <nlohmann/json.hpp>
21 using json = nlohmann::json;
23 namespace pinecone::types
30 [[nodiscard]]
auto vector_count()
const noexcept -> uint64_t {
return vectorCount; }
38 NLOHMANN_DEFINE_TYPE_INTRUSIVE(
index_stats, namespaces, dimension, indexFullness,
41 [[nodiscard]]
auto stat_namespaces() const noexcept
47 [[nodiscard]]
auto stat_dimension() const noexcept -> uint64_t {
return dimension; }
49 [[nodiscard]]
auto stat_index_fullness() const noexcept ->
double {
return indexFullness; }
51 [[nodiscard]]
auto stat_total_vector_count() const noexcept -> uint64_t
53 return totalVectorCount;
60 uint64_t totalVectorCount;
68 template <
typename filter>
75 builder(uint64_t top_k, std::string_view vector_id) noexcept
76 : _top_k(top_k), _query(vector_id), _filter(filters::none())
83 builder(filter f, uint64_t top_k, std::string_view vector_id) noexcept
84 : _top_k(top_k), _query(vector_id), _filter(
std::move(f))
88 [[nodiscard]]
auto with_namespace(std::string_view ns) noexcept ->
builder&
94 [[nodiscard]]
auto with_include_values(
bool inc) noexcept ->
builder&
96 _include_values = inc;
100 [[nodiscard]]
auto with_include_metadata(
bool inc) noexcept ->
builder&
102 _include_metadata = inc;
106 [[nodiscard]]
auto build()
const noexcept ->
query
108 return query(_namespace, _top_k, _filter, _include_values, _include_metadata, _query);
113 std::variant<std::vector<double>, std::string_view> _query;
115 std::optional<std::string_view> _namespace;
116 std::optional<bool> _include_values;
117 std::optional<bool> _include_metadata;
120 friend void to_json(nlohmann ::json& nlohmann_json_j,
const query& nlohmann_json_t)
122 to_json(nlohmann_json_j[
"filter"], nlohmann_json_t._filter);
123 nlohmann_json_j[
"topK"] = nlohmann_json_t._top_k;
126 nlohmann_json_j[
"vector"] = std::get<std::vector<double>>(nlohmann_json_t._query);
127 }
else if (std::holds_alternative<std::string_view>(nlohmann_json_t._query)) {
128 nlohmann_json_j[
"id"] = std::get<std::string_view>(nlohmann_json_t._query);
131 if (nlohmann_json_t._namespace) {
132 nlohmann_json_j[
"namespace"] = *nlohmann_json_t._namespace;
134 if (nlohmann_json_t._include_values) {
135 nlohmann_json_j[
"includeValues"] = *nlohmann_json_t._include_values;
137 if (nlohmann_json_t._include_metadata) {
138 nlohmann_json_j[
"includeMetadata"] = *nlohmann_json_t._include_metadata;
144 std::variant<std::vector<double>, std::string_view> _query;
145 std::optional<std::string_view> _namespace;
147 std::optional<bool> _include_values;
148 std::optional<bool> _include_metadata;
150 query(std::optional<std::string_view> ns, uint64_t top_k, filter f,
151 std::optional<bool> include_values, std::optional<bool> include_metadata,
157 _include_values(include_values),
158 _include_metadata(include_metadata)
183 inline auto query_builder(uint64_t top_k, std::string_view vector_id) noexcept
186 return {top_k, vector_id};
199 template <
typename filter>
216 template <
typename filter>
217 inline auto query_builder(filter f, uint64_t top_k, std::string_view vector_id) noexcept ->
220 return {f, top_k, vector_id};
232 [[nodiscard]]
auto id()
const noexcept ->
std::string const& {
return _id; }
234 [[nodiscard]]
auto score()
const noexcept ->
double {
return _score; }
236 [[nodiscard]]
auto values()
const noexcept -> std::optional<std::vector<double>>
const&
241 [[nodiscard]]
auto md()
const noexcept -> std::optional<metadata>
const& {
return _metadata; }
243 friend void from_json(
const nlohmann ::json& nlohmann_json_j,
scored_vector& nlohmann_json_t)
245 if (nlohmann_json_j.contains(
"values")) {
246 nlohmann_json_t._values = nlohmann_json_j[
"values"];
249 if (nlohmann_json_j.contains(
"metadata")) {
251 from_json(nlohmann_json_j[
"metadata"], m);
252 nlohmann_json_t._metadata =
std::move(m);
255 nlohmann_json_j.at(
"id").get_to(nlohmann_json_t._id);
256 nlohmann_json_j.at(
"score").get_to(nlohmann_json_t._score);
262 std::optional<std::vector<double>> _values;
263 std::optional<metadata> _metadata;
266 friend void from_json(
const nlohmann ::json& nlohmann_json_j,
query_result& nlohmann_json_t)
268 nlohmann_json_j.
at(
"namespace").get_to(nlohmann_json_t._namespace);
269 nlohmann_json_j.at(
"matches").get_to(nlohmann_json_t.matches);
272 [[nodiscard]]
auto query_ns() const noexcept ->
std::
string const& {
return _namespace; }
274 [[nodiscard]]
auto query_matches() const noexcept ->
std::vector<scored_vector> const&
291 template <
typename filter = no_filter>
293 using delete_mode = std::variant<ids, bool, filter>;
297 builder() noexcept : _mode(
true) {}
305 auto with_namespace(std::string_view ns) noexcept ->
builder&
313 std::optional<std::string_view> _namespace;
316 friend void to_json(nlohmann ::json& nlohmann_json_j,
const delete_request& nlohmann_json_t)
318 if (nlohmann_json_t._namespace) {
319 nlohmann_json_j[
"namespace"] = *nlohmann_json_t._namespace;
322 if (std::holds_alternative<ids>(nlohmann_json_t._mode)) {
323 nlohmann_json_j[
"ids"] = json::array({});
324 for (
auto const&
id : std::get<ids>(nlohmann_json_t._mode)) {
325 nlohmann_json_j[
"ids"].emplace_back(
id);
327 }
else if (std::holds_alternative<bool>(nlohmann_json_t._mode)) {
328 nlohmann_json_j[
"deleteAll"] = std::get<bool>(nlohmann_json_t._mode);
329 }
else if (std::holds_alternative<filter>(nlohmann_json_t._mode)) {
330 to_json(nlohmann_json_j, std::get<filter>(nlohmann_json_t._mode));
336 std::optional<std::string_view> _namespace;
338 delete_request(delete_mode mode, std::optional<std::string_view> ns) noexcept
359 [[nodiscard]]
auto id()
const noexcept ->
std::string const& {
return _id; }
361 [[nodiscard]]
auto values()
const noexcept ->
std::vector<double> const& {
return _values; }
363 [[nodiscard]]
auto md()
const noexcept -> std::optional<metadata>
const& {
return _metadata; }
365 friend void to_json(nlohmann ::json& nlohmann_json_j,
const vector& nlohmann_json_t)
367 nlohmann_json_j[
"id"] = nlohmann_json_t._id;
368 nlohmann_json_j[
"values"] = nlohmann_json_t._values;
369 if (nlohmann_json_t._metadata) {
370 nlohmann_json_j[
"metadata"] = *nlohmann_json_t._metadata;
374 friend void from_json(
const nlohmann ::json& nlohmann_json_j,
vector& nlohmann_json_t)
376 if (nlohmann_json_j.contains(
"metadata")) {
378 nlohmann_json_j.at(
"metadata").get_to(m);
379 nlohmann_json_t._metadata =
std::move(m);
382 nlohmann_json_j.at(
"values").get_to(nlohmann_json_t._values);
383 nlohmann_json_j.at(
"id").get_to(nlohmann_json_t._id);
389 std::optional<metadata> _metadata;
399 [[nodiscard]]
auto build()
const noexcept ->
upsert_request {
return {_vectors, _namespace}; }
401 auto with_namespace(std::string_view ns) noexcept ->
builder&
409 std::optional<std::string_view> _namespace;
412 friend void to_json(nlohmann ::json& nlohmann_json_j,
const upsert_request& nlohmann_json_t)
414 nlohmann_json_j[
"vectors"] = nlohmann_json_t._vectors;
416 if (nlohmann_json_t._namespace) {
417 nlohmann_json_j[
"namespace"] = *nlohmann_json_t._namespace;
423 std::optional<std::string_view> _namespace;
426 : _vectors(
std::move(vectors)), _namespace(ns)
436 explicit builder(std::string_view
id) noexcept : _id(
id) {}
440 return {_id, _values, _metadata, _namespace};
443 auto with_namespace(std::string_view ns) noexcept ->
builder&
462 std::string_view _id;
463 std::optional<std::vector<double>> _values;
464 std::optional<metadata> _metadata;
465 std::optional<std::string_view> _namespace;
468 friend void to_json(nlohmann ::json& nlohmann_json_j,
const update_request& nlohmann_json_t)
470 nlohmann_json_j[
"id"] = nlohmann_json_t._id;
471 if (nlohmann_json_t._values) {
472 nlohmann_json_j[
"values"] = *nlohmann_json_t._values;
474 if (nlohmann_json_t._metadata) {
475 to_json(nlohmann_json_j[
"setMetadata"], *nlohmann_json_t._metadata);
477 if (nlohmann_json_t._namespace) {
478 nlohmann_json_j[
"namespace"] = *nlohmann_json_t._namespace;
483 std::string_view _id;
484 std::optional<std::vector<double>> _values;
485 std::optional<metadata> _metadata;
486 std::optional<std::string_view> _namespace;
489 std::optional<metadata> md, std::optional<std::string_view> ns) noexcept
Filters supported by complex vector API operations.
Models the possibility of failure for Pinecone operations.
Deletes one or more vectors from an index.
Statistics for a Pinecone index.
A vector with its corresponding search score.
The result of a vector search.
Update existing vectors in an index.
Insert or update vectors in an index.
Representation of an individual Pinecone vector.
auto query_builder(uint64_t top_k, std::vector< double > vector) noexcept -> query< no_filter >::builder
Construct a query builder given a number of results and a target vector.