313 lines
12 KiB
C++
313 lines
12 KiB
C++
#include "vendors/retrieval/pinecone/pinecone.h"
|
|
|
|
#include "vendors/retrieval/caps.h"
|
|
|
|
namespace molo::retrieval {
|
|
|
|
llm::ApiClientConfig Pinecone::config() {
|
|
llm::ApiClientConfig c;
|
|
c.vendor = "pinecone";
|
|
c.base_url = kControlUrl;
|
|
c.key_env = kKeyEnv;
|
|
c.auth = llm::AuthStyle::kHeader;
|
|
c.auth_header = "Api-Key";
|
|
c.default_headers["X-Pinecone-Api-Version"] = kApiVersion;
|
|
return c;
|
|
}
|
|
|
|
namespace {
|
|
std::vector<HostSpec> hosts_for(const std::string& index_host) {
|
|
std::vector<HostSpec> h = {{"control", Pinecone::kControlUrl}, {"inference", Pinecone::kControlUrl}};
|
|
if (!index_host.empty()) {
|
|
check_host("pinecone", index_host);
|
|
h.push_back({"data", "https://" + index_host});
|
|
}
|
|
return h;
|
|
}
|
|
[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("pinecone", why); }
|
|
CallArgs with_body(Json body) {
|
|
CallArgs a;
|
|
a.body = std::move(body);
|
|
return a;
|
|
}
|
|
CallArgs with_path(const char* key, const std::string& value) {
|
|
CallArgs a;
|
|
a.path[key] = value;
|
|
return a;
|
|
}
|
|
void put_ns(Json& body, const std::string& ns) {
|
|
if (!ns.empty()) body.set("namespace", Json(ns));
|
|
}
|
|
} // namespace
|
|
|
|
Pinecone Pinecone::from_env(const std::string& index_host) {
|
|
return Pinecone(make_gateway(pinecone_calls(), config(), hosts_for(index_host), "", nullptr));
|
|
}
|
|
|
|
Pinecone Pinecone::with(const TransportFactory& factory, std::string key, const std::string& index_host) {
|
|
return Pinecone(make_gateway(pinecone_calls(), config(), hosts_for(index_host), std::move(key), factory));
|
|
}
|
|
|
|
// ---- parsers ---------------------------------------------------------------
|
|
|
|
EmbedResult Pinecone::parse_embeddings(const Json& doc) {
|
|
EmbedResult out;
|
|
out.model = J::str(doc, "model");
|
|
for (const Json& d : J::array(doc, "data")) {
|
|
const Json* v = J::at(d, "values");
|
|
out.vectors.push_back(v ? J::floats(*v) : std::vector<float>{});
|
|
}
|
|
if (const Json* u = J::at(doc, "usage")) out.total_tokens = static_cast<long>(J::num(*u, "total_tokens"));
|
|
return out;
|
|
}
|
|
|
|
RerankResult Pinecone::parse_rerank(const Json& doc) {
|
|
RerankResult out;
|
|
out.model = J::str(doc, "model");
|
|
for (const Json& d : J::array(doc, "data")) {
|
|
RerankHit h;
|
|
h.index = static_cast<int>(J::num(d, "index"));
|
|
h.score = J::num(d, "score");
|
|
out.hits.push_back(h);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::vector<PineconeMatch> Pinecone::parse_matches(const Json& doc) {
|
|
std::vector<PineconeMatch> out;
|
|
for (const Json& m : J::array(doc, "matches")) {
|
|
PineconeMatch x;
|
|
x.id = J::str(m, "id");
|
|
x.score = J::num(m, "score");
|
|
if (const Json* v = J::at(m, "values")) x.values = J::floats(*v);
|
|
if (const Json* md = J::at(m, "metadata")) x.metadata = *md;
|
|
out.push_back(std::move(x));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
PineconePage Pinecone::parse_list(const Json& doc) {
|
|
PineconePage p;
|
|
for (const Json& v : J::array(doc, "vectors")) p.ids.push_back(J::str(v, "id"));
|
|
if (const Json* pg = J::at(doc, "pagination")) p.next_token = J::str(*pg, "next");
|
|
return p;
|
|
}
|
|
|
|
Json Pinecone::vector_json(const PineconeVector& v) {
|
|
if (v.id.empty() || v.id.size() > 512) bad("vector id must be 1..512 chars");
|
|
if (v.values.empty() || v.values.size() > static_cast<std::size_t>(RetrievalCaps::kMaxDimensions))
|
|
bad("vector values must hold 1..20000 numbers");
|
|
Json o = Json::object();
|
|
o.set("id", Json(v.id));
|
|
Json vals = Json::array();
|
|
for (float f : v.values) vals.push(Json(static_cast<double>(f)));
|
|
o.set("values", vals);
|
|
if (!v.metadata.is_null()) {
|
|
if (!v.metadata.is_object()) bad("metadata must be an object");
|
|
o.set("metadata", v.metadata);
|
|
}
|
|
return o;
|
|
}
|
|
|
|
// ---- inference -------------------------------------------------------------
|
|
|
|
EmbedResult Pinecone::embed(const EmbedRequest& request) {
|
|
Embedder::validate("pinecone", request);
|
|
Json body = Json::object();
|
|
body.set("model", Json(request.model.empty() ? std::string(kDefaultEmbedModel) : request.model));
|
|
Json inputs = Json::array();
|
|
for (const std::string& t : request.texts) {
|
|
Json i = Json::object();
|
|
i.set("text", Json(t));
|
|
inputs.push(i);
|
|
}
|
|
body.set("inputs", inputs);
|
|
Json params = Json::object();
|
|
if (!request.input_type.empty()) params.set("input_type", Json(request.input_type == "query" ? "query" : "passage"));
|
|
params.set("truncate", Json("END"));
|
|
body.set("parameters", params);
|
|
EmbedResult r = parse_embeddings(gw_->call("embed", with_body(body)));
|
|
if (r.vectors.size() != request.texts.size())
|
|
throw llm::ApiError("pinecone", 502, "BAD_RESPONSE", "embedding count differs from input count");
|
|
return r;
|
|
}
|
|
|
|
RerankResult Pinecone::rerank(const RerankRequest& request) {
|
|
Reranker::validate("pinecone", request);
|
|
Json body = Json::object();
|
|
body.set("model", Json(request.model.empty() ? std::string(kDefaultRerankModel) : request.model));
|
|
body.set("query", Json(request.query));
|
|
Json docs = Json::array();
|
|
for (const std::string& d : request.documents) {
|
|
Json i = Json::object();
|
|
i.set("text", Json(d));
|
|
docs.push(i);
|
|
}
|
|
body.set("documents", docs);
|
|
body.set("return_documents", Json(false));
|
|
if (request.top_n > 0) body.set("top_n", Json(request.top_n));
|
|
return parse_rerank(gw_->call("rerank", with_body(body)));
|
|
}
|
|
|
|
// ---- control plane ---------------------------------------------------------
|
|
|
|
Json Pinecone::list_indexes() { return gw_->call("list_indexes"); }
|
|
Json Pinecone::describe_index(const std::string& name) { return gw_->call("describe_index", with_path("index_name", name)); }
|
|
|
|
Json Pinecone::create_index(const std::string& name, int dimension, const std::string& metric,
|
|
const std::string& cloud, const std::string& region) {
|
|
if (name.empty() || name.size() > 45) bad("index name must be 1..45 chars");
|
|
if (dimension < 1 || dimension > RetrievalCaps::kMaxDimensions) bad("dimension must be 1..20000");
|
|
if (metric != "cosine" && metric != "dotproduct" && metric != "euclidean") bad("metric must be cosine, dotproduct or euclidean");
|
|
if (cloud.empty() || region.empty()) bad("cloud and region are required");
|
|
Json field = Json::object();
|
|
field.set("type", Json("dense_vector"));
|
|
field.set("dimension", Json(dimension));
|
|
field.set("metric", Json(metric));
|
|
Json fields = Json::object();
|
|
fields.set("vector", field);
|
|
Json schema = Json::object();
|
|
schema.set("fields", fields);
|
|
Json deployment = Json::object();
|
|
deployment.set("deployment_type", Json("managed"));
|
|
deployment.set("cloud", Json(cloud));
|
|
deployment.set("region", Json(region));
|
|
Json body = Json::object();
|
|
body.set("name", Json(name));
|
|
body.set("schema", schema);
|
|
body.set("deployment", deployment);
|
|
return gw_->call("create_index", with_body(body));
|
|
}
|
|
|
|
Json Pinecone::configure_index(const std::string& name, const Json& body) {
|
|
if (!body.is_object()) bad("body must be an object");
|
|
CallArgs a = with_path("index_name", name);
|
|
a.body = body;
|
|
return gw_->call("configure_index", a);
|
|
}
|
|
|
|
Json Pinecone::delete_index(const std::string& name) { return gw_->destroy("delete_index", with_path("index_name", name)); }
|
|
Json Pinecone::list_collections() { return gw_->call("list_collections"); }
|
|
Json Pinecone::describe_collection(const std::string& name) {
|
|
return gw_->call("describe_collection", with_path("collection_name", name));
|
|
}
|
|
Json Pinecone::create_collection(const std::string& name, const std::string& source_index) {
|
|
if (name.empty() || source_index.empty()) bad("name and source are required");
|
|
Json body = Json::object();
|
|
body.set("name", Json(name));
|
|
body.set("source", Json(source_index));
|
|
return gw_->call("create_collection", with_body(body));
|
|
}
|
|
Json Pinecone::delete_collection(const std::string& name) {
|
|
return gw_->destroy("delete_collection", with_path("collection_name", name));
|
|
}
|
|
Json Pinecone::list_backups(const std::string& index_name) {
|
|
return gw_->call("list_index_backups", with_path("index_name", index_name));
|
|
}
|
|
Json Pinecone::create_backup(const std::string& index_name, const std::string& name, const std::string& description) {
|
|
Json body = Json::object();
|
|
if (!name.empty()) body.set("name", Json(name));
|
|
if (!description.empty()) body.set("description", Json(description));
|
|
CallArgs a = with_path("index_name", index_name);
|
|
a.body = body;
|
|
return gw_->call("create_backup", a);
|
|
}
|
|
Json Pinecone::describe_backup(const std::string& backup_id) { return gw_->call("describe_backup", with_path("backup_id", backup_id)); }
|
|
Json Pinecone::delete_backup(const std::string& backup_id) { return gw_->destroy("delete_backup", with_path("backup_id", backup_id)); }
|
|
|
|
// ---- data plane ------------------------------------------------------------
|
|
|
|
Json Pinecone::upsert(const std::vector<PineconeVector>& vectors, const std::string& ns) {
|
|
if (vectors.empty() || vectors.size() > RetrievalCaps::kMaxRecordsPerUpsert) bad("vectors must hold 1..1000 entries");
|
|
Json arr = Json::array();
|
|
for (const PineconeVector& v : vectors) arr.push(vector_json(v));
|
|
Json body = Json::object();
|
|
body.set("vectors", arr);
|
|
put_ns(body, ns);
|
|
return gw_->call("upsert_vectors", with_body(body));
|
|
}
|
|
|
|
std::vector<PineconeMatch> Pinecone::query(const PineconeQuery& q) {
|
|
if (q.top_k < 1 || q.top_k > RetrievalCaps::kMaxTopK) bad("top_k must be 1..10000");
|
|
if (q.vector.empty() == q.id.empty()) bad("give exactly one of vector and id");
|
|
Json body = Json::object();
|
|
body.set("topK", Json(q.top_k));
|
|
if (!q.vector.empty()) {
|
|
if (q.vector.size() > static_cast<std::size_t>(RetrievalCaps::kMaxDimensions)) bad("vector too long");
|
|
Json v = Json::array();
|
|
for (float f : q.vector) v.push(Json(static_cast<double>(f)));
|
|
body.set("vector", v);
|
|
} else {
|
|
body.set("id", Json(q.id));
|
|
}
|
|
put_ns(body, q.ns);
|
|
if (!q.filter.is_null()) {
|
|
if (!q.filter.is_object()) bad("filter must be an object");
|
|
body.set("filter", q.filter);
|
|
}
|
|
body.set("includeValues", Json(q.include_values));
|
|
body.set("includeMetadata", Json(q.include_metadata));
|
|
return parse_matches(gw_->call("query_vectors", with_body(body)));
|
|
}
|
|
|
|
Json Pinecone::fetch(const std::vector<std::string>& ids, const std::string& ns) {
|
|
if (ids.empty() || ids.size() > kFetchIdsPerCall) bad("ids must hold 1..100 entries per call");
|
|
CallArgs a;
|
|
for (const std::string& id : ids) a.query_multi.emplace_back("ids", id);
|
|
if (!ns.empty()) a.query["namespace"] = ns;
|
|
return gw_->call("fetch_vectors", a);
|
|
}
|
|
|
|
PineconePage Pinecone::list(const std::string& prefix, const std::string& ns, int limit, const std::string& token) {
|
|
if (limit < 1 || limit > RetrievalCaps::kMaxListLimit) bad("limit must be 1..100");
|
|
CallArgs a;
|
|
a.query["limit"] = Query::number(limit);
|
|
if (!prefix.empty()) a.query["prefix"] = prefix;
|
|
if (!ns.empty()) a.query["namespace"] = ns;
|
|
if (!token.empty()) a.query["paginationToken"] = token;
|
|
return parse_list(gw_->call("list_vectors", a));
|
|
}
|
|
|
|
Json Pinecone::stats(const Json& filter) {
|
|
Json body = Json::object();
|
|
if (!filter.is_null()) body.set("filter", filter);
|
|
return gw_->call("describe_index_stats", with_body(body));
|
|
}
|
|
|
|
Json Pinecone::list_namespaces(int limit, const std::string& token) {
|
|
if (limit < 1 || limit > RetrievalCaps::kMaxListLimit) bad("limit must be 1..100");
|
|
CallArgs a;
|
|
a.query["limit"] = Query::number(limit);
|
|
if (!token.empty()) a.query["paginationToken"] = token;
|
|
return gw_->call("list_namespaces", a);
|
|
}
|
|
|
|
Json Pinecone::describe_namespace(const std::string& ns) { return gw_->call("describe_namespace", with_path("namespace", ns)); }
|
|
|
|
Json Pinecone::delete_ids(const std::vector<std::string>& ids, const std::string& ns) {
|
|
if (ids.empty() || ids.size() > RetrievalCaps::kMaxIdsPerFetch) bad("ids must hold 1..1000 entries");
|
|
Json body = Json::object();
|
|
body.set("ids", J::strings(ids));
|
|
put_ns(body, ns);
|
|
return gw_->destroy("delete_vectors", with_body(body));
|
|
}
|
|
|
|
Json Pinecone::delete_all(const std::string& ns) {
|
|
Json body = Json::object();
|
|
body.set("deleteAll", Json(true));
|
|
put_ns(body, ns);
|
|
return gw_->destroy("delete_vectors", with_body(body));
|
|
}
|
|
|
|
Json Pinecone::delete_by_filter(const Json& filter, const std::string& ns) {
|
|
if (!filter.is_object() || filter.as_object().empty()) bad("filter must be a non-empty object");
|
|
Json body = Json::object();
|
|
body.set("filter", filter);
|
|
put_ns(body, ns);
|
|
return gw_->destroy("delete_vectors", with_body(body));
|
|
}
|
|
|
|
Json Pinecone::delete_namespace(const std::string& ns) { return gw_->destroy("delete_namespace", with_path("namespace", ns)); }
|
|
|
|
} // namespace molo::retrieval
|