Cppinecone
A C++ client for the Pinecone vector database
pinecone.hpp
Go to the documentation of this file.
1 #pragma once
10 #include <memory>
11 #include <optional>
12 #include <ostream>
13 #include <sstream>
14 #include <string>
15 #include <string_view>
16 #include <variant>
17 #include <vector>
18 
34 #include "pinecone/util/result.hpp"
35 
36 namespace pinecone
37 {
38 using type = domain::operation_type;
39 template <type t>
40 using args = domain::operation_args<t>;
41 template <type t, typename f>
42 using vec_args = domain::operation_args<t, f>;
43 
66 template <net::threading_mode Mode>
68  pinecone_client() = default;
76  static auto build(net::connection_args args) -> std::variant<pinecone_client<Mode>, std::string>
77  {
78  net::url_builder url_builder(args.environment());
80  if (client) {
81  auto api_metadata = client->request(
83  if (api_metadata.is_failed()) {
85  err << "Failed to construct Pinecone client due to API metadata retrieval failure: "
86  << api_metadata.to_string() << std::endl;
87  return {err.str()};
88  }
89  url_builder.set_metadata(std::move(*api_metadata));
90  return pinecone_client(std::move(url_builder), std::move(client));
91  }
92 
93  return {"Failed to construct HTTP client; CURL seems to be somehow misconfigured"};
94  }
95 
96  [[nodiscard]] auto get_api_metdata() const noexcept -> util::result<types::api_metadata>
97  {
98  util::logger()->info("Retrieving API metadata");
99  return _http_client->request(args<type::actions_whoami>{_url_builder});
100  }
101 
102  [[nodiscard]] auto create_index(types::new_index index) const noexcept
103  -> util::result<types::accepted>
104  {
105  util::logger()->info("Creating index");
106  return _http_client->request(args<type::index_create>{_url_builder, std::move(index)});
107  }
108 
109  [[nodiscard]] auto list_indexes() const noexcept -> util::result<std::vector<std::string>>
110  {
111  util::logger()->info("Listing indices");
112  return _http_client->request(args<type::index_list>{_url_builder});
113  }
114 
115  [[nodiscard]] auto describe_index(std::string_view name) const noexcept
116  -> util::result<types::database>
117  {
118  util::logger()->info("Describing index");
119  return _http_client->request(args<type::index_describe>{_url_builder, name});
120  }
121 
122  [[nodiscard]] auto configure_index(std::string_view name,
123  types::index_configuration config) const noexcept
124  -> util::result<types::accepted>
125  {
126  util::logger()->info("Configuring index");
127  return _http_client->request(args<type::index_configure>(_url_builder, name, config));
128  }
129 
130  [[nodiscard]] auto delete_index(std::string_view name) const noexcept
131  -> util::result<types::accepted>
132  {
133  util::logger()->info("Deleting index");
134  return _http_client->request(args<type::index_delete>{_url_builder, name});
135  }
136 
137  [[nodiscard]] auto list_collections() const noexcept -> util::result<std::vector<std::string>>
138  {
139  util::logger()->info("Listing collections");
140  return _http_client->request(args<type::collection_list>{_url_builder});
141  }
142 
143  [[nodiscard]] auto describe_collection(std::string_view name) const noexcept
144  -> util::result<types::collection>
145  {
146  util::logger()->info("Describing collection");
147  return _http_client->request(args<type::collection_describe>{_url_builder, name});
148  }
149 
150  [[nodiscard]] auto delete_collection(std::string_view name) const noexcept
151  -> util::result<types::accepted>
152  {
153  util::logger()->info("Deleting collection");
154  return _http_client->request(args<type::collection_delete>{_url_builder, name});
155  }
156 
157  [[nodiscard]] auto create_collection(types::new_collection collection) const noexcept
158  -> util::result<types::accepted>
159  {
160  util::logger()->info("Creating collection");
161  return _http_client->request(
162  args<type::collection_create>(_url_builder, std::move(collection)));
163  }
164 
165  [[nodiscard]] auto describe_index_stats(std::string_view name) const noexcept
166  -> util::result<types::index_stats>
167  {
168  util::logger()->info("Descriving index stats");
169  return _http_client->request(vec_args<type::vector_describe_index_stats, types::no_filter>{
170  _url_builder, name, types::filters::none()});
171  }
172 
173  template <typename filter>
174  [[nodiscard]] auto query(std::string_view name, types::query<filter> query) const noexcept
175  -> util::result<types::query_result>
176  {
177  util::logger()->info("Querying index");
178  return _http_client->request(
179  vec_args<type::vector_query, filter>{_url_builder, name, std::move(query)});
180  }
181 
182  template <typename filter>
183  [[nodiscard]] auto delete_vectors(std::string_view name,
184  types::delete_request<filter> req) const noexcept
185  -> util::result<types::accepted>
186  {
187  util::logger()->info("Deleting vectors");
188  return _http_client->request(
189  vec_args<type::vector_delete, filter>{_url_builder, name, std::move(req)});
190  }
191 
192  [[nodiscard]] auto upsert_vectors(std::string_view name, types::upsert_request req) const noexcept
193  -> util::result<types::accepted>
194  {
195  util::logger()->info("Upserting vectors");
196  return _http_client->request(args<type::vector_upsert>{_url_builder, name, std::move(req)});
197  }
198 
199  [[nodiscard]] auto update_vector(std::string_view name, types::update_request req) const noexcept
200  -> util::result<types::accepted>
201  {
202  util::logger()->info("Updating vectors");
203  return _http_client->request(args<type::vector_update>{_url_builder, name, std::move(req)});
204  }
205 
206  private:
207  net::url_builder _url_builder;
209 
210  pinecone_client(net::url_builder url_builder,
211  std::unique_ptr<net::http_client<Mode>> client) noexcept
212  : _url_builder(std::move(url_builder)), _http_client(std::move(client))
213  {
214  util::logger()->info("Client construction completed successfully");
215  }
216 
217  template <typename filter>
218  [[nodiscard]] auto describe_index_stats(std::string_view name, filter f) const noexcept
219  -> util::result<types::index_stats>
220  {
221  return _http_client->request(
222  vec_args<type::vector_describe_index_stats, filter>{_url_builder, name, std::move(f)});
223  }
224 };
225 
226 using synchronous_client = pinecone_client<net::threading_mode::sync>;
227 } // namespace pinecone
A monostate sentinel type for operations that have no associated response.
Metadata required by the Pinecone API.
User-facing arguments for networking operations.
T endl(T... args)
Filters supported by complex vector API operations.
A bare-bones HTTP client implementation.
API operations providing functionality for Pinecone indices.
Native C++ type modeling the results of index operations.
Provides logging capability.
API operations serving Pinecone-specific metadata.
T move(T... args)
STL namespace.
Metaprogamming infrastructure for generation of per-operation code.
Operations made available by the Pinecone API.
Models the possibility of failure for Pinecone operations.
T str(T... args)
Operation-specific arguments.
Definition: operation.hpp:172
Arguments required to connect to a Pinecone server.
Definition: arguments.hpp:25
An HTTP client responsible for making requests to a Pinecone instance.
Definition: http_client.hpp:42
Constructs Pinecone API URLs.
Definition: url_builder.hpp:20
auto set_metadata(types::api_metadata metadata) noexcept
Set the API metadata.
Definition: url_builder.hpp:43
The Pinecone REST API client.
Definition: pinecone.hpp:67
static auto build(net::connection_args args) -> std::variant< pinecone_client< Mode >, std::string >
Attempts to initialize a new Pinecone API client.
Definition: pinecone.hpp:76
Constructs Pinecone API URLs.
Encoding for Pinecone's vector metadata API.
API operations providing functionality for Pinecone vectors.
Native C++ types modeling the results of vector operations.