196 lines
7.4 KiB
C++
196 lines
7.4 KiB
C++
#include "vendors/retrieval/qdrant/qdrant.h"
|
|||
|
|
|
||
|
|
#include "llm/config.h"
|
||
|
|
#include "vendors/retrieval/caps.h"
|
||
|
|
|
||
|
|
namespace molo::retrieval {
|
||
|
|
|
||
|
|
llm::ApiClientConfig Qdrant::config() {
|
||
|
|
llm::ApiClientConfig c;
|
||
|
|
c.vendor = "qdrant";
|
||
|
|
c.base_url = "";
|
||
|
|
c.key_env = kKeyEnv;
|
||
|
|
c.auth = llm::AuthStyle::kHeader;
|
||
|
|
c.auth_header = "api-key";
|
||
|
|
return c;
|
||
|
|
}
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("qdrant", why); }
|
||
|
|
|
||
|
|
std::string trim_url(std::string url) {
|
||
|
|
while (!url.empty() && url.back() == '/') url.pop_back();
|
||
|
|
return url;
|
||
|
|
}
|
||
|
|
|
||
|
|
Qdrant build(const std::string& url, std::string key, const TransportFactory& factory) {
|
||
|
|
std::string base = trim_url(url);
|
||
|
|
bool ok = base.rfind("https://", 0) == 0 || base.rfind("http://", 0) == 0;
|
||
|
|
auto gw = make_gateway(qdrant_calls(), Qdrant::config(), {{"main", ok ? base : "https://qdrant.invalid"}},
|
||
|
|
std::move(key), factory);
|
||
|
|
if (!ok) gw->require_setting(Qdrant::kUrlEnv);
|
||
|
|
return Qdrant(gw);
|
||
|
|
}
|
||
|
|
|
||
|
|
CallArgs coll(const std::string& name) {
|
||
|
|
CallArgs a;
|
||
|
|
a.path["collection_name"] = name;
|
||
|
|
return a;
|
||
|
|
}
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
Qdrant Qdrant::from_env() { return build(llm::Config::get(kUrlEnv), "", nullptr); }
|
||
|
|
Qdrant Qdrant::with(const TransportFactory& factory, std::string key, const std::string& base_url) {
|
||
|
|
return build(base_url, std::move(key), factory);
|
||
|
|
}
|
||
|
|
|
||
|
|
const Json& Qdrant::result_of(const Json& doc) {
|
||
|
|
static const Json empty = Json::object();
|
||
|
|
const Json* r = doc.is_object() ? doc.find("result") : nullptr;
|
||
|
|
return r ? *r : empty;
|
||
|
|
}
|
||
|
|
|
||
|
|
Json Qdrant::point_json(const QdrantPoint& p) {
|
||
|
|
bool id_ok = (p.id.is_number() && p.id.as_number() >= 0) || (p.id.is_string() && !p.id.as_string().empty() && p.id.as_string().size() <= 64);
|
||
|
|
if (!id_ok) bad("point id must be an unsigned integer or a UUID string");
|
||
|
|
if (p.vector.empty() || p.vector.size() > static_cast<std::size_t>(RetrievalCaps::kMaxDimensions)) bad("vector must hold 1..20000 numbers");
|
||
|
|
Json o = Json::object();
|
||
|
|
o.set("id", p.id);
|
||
|
|
Json v = Json::array();
|
||
|
|
for (float f : p.vector) v.push(Json(static_cast<double>(f)));
|
||
|
|
o.set("vector", v);
|
||
|
|
if (!p.payload.is_null()) {
|
||
|
|
if (!p.payload.is_object()) bad("payload must be an object");
|
||
|
|
o.set("payload", p.payload);
|
||
|
|
}
|
||
|
|
return o;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::vector<QdrantHit> Qdrant::parse_hits(const Json& doc) {
|
||
|
|
std::vector<QdrantHit> out;
|
||
|
|
const Json& r = result_of(doc);
|
||
|
|
for (const Json& p : J::array(r, "points")) {
|
||
|
|
QdrantHit h;
|
||
|
|
if (const Json* id = J::at(p, "id")) h.id = *id;
|
||
|
|
h.score = J::num(p, "score");
|
||
|
|
if (const Json* pl = J::at(p, "payload")) h.payload = *pl;
|
||
|
|
out.push_back(std::move(h));
|
||
|
|
}
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
|
||
|
|
Json Qdrant::list_collections() { return gw_->call("get_collections"); }
|
||
|
|
|
||
|
|
bool Qdrant::collection_exists(const std::string& name) {
|
||
|
|
Json doc = gw_->call("collection_exists", coll(name));
|
||
|
|
const Json* e = J::at(result_of(doc), "exists");
|
||
|
|
return e && e->is_bool() && e->as_bool();
|
||
|
|
}
|
||
|
|
|
||
|
|
Json Qdrant::get_collection(const std::string& name) { return gw_->call("get_collection", coll(name)); }
|
||
|
|
|
||
|
|
Json Qdrant::create_collection(const std::string& name, int size, const std::string& distance, bool on_disk_payload) {
|
||
|
|
if (size < 1 || size > RetrievalCaps::kMaxDimensions) bad("size must be 1..20000");
|
||
|
|
if (distance != "Cosine" && distance != "Euclid" && distance != "Dot" && distance != "Manhattan")
|
||
|
|
bad("distance must be Cosine, Euclid, Dot or Manhattan");
|
||
|
|
Json vectors = Json::object();
|
||
|
|
vectors.set("size", Json(size));
|
||
|
|
vectors.set("distance", Json(distance));
|
||
|
|
Json body = Json::object();
|
||
|
|
body.set("vectors", vectors);
|
||
|
|
body.set("on_disk_payload", Json(on_disk_payload));
|
||
|
|
CallArgs a = coll(name);
|
||
|
|
a.body = body;
|
||
|
|
return gw_->call("create_collection", a);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json Qdrant::delete_collection(const std::string& name) { return gw_->destroy("delete_collection", coll(name)); }
|
||
|
|
|
||
|
|
Json Qdrant::upsert(const std::string& collection, const std::vector<QdrantPoint>& points, bool wait) {
|
||
|
|
if (points.empty() || points.size() > RetrievalCaps::kMaxPointsPerUpsert) bad("points must hold 1..1000 entries");
|
||
|
|
Json arr = Json::array();
|
||
|
|
for (const QdrantPoint& p : points) arr.push(point_json(p));
|
||
|
|
Json body = Json::object();
|
||
|
|
body.set("points", arr);
|
||
|
|
CallArgs a = coll(collection);
|
||
|
|
a.body = body;
|
||
|
|
a.query["wait"] = Query::flag(wait);
|
||
|
|
return gw_->call("upsert_points", a);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::vector<QdrantHit> Qdrant::query(const std::string& collection, const std::vector<float>& vector, int limit,
|
||
|
|
const Json& filter, bool with_payload) {
|
||
|
|
if (vector.empty() || vector.size() > static_cast<std::size_t>(RetrievalCaps::kMaxDimensions)) bad("vector must hold 1..20000 numbers");
|
||
|
|
if (limit < 1 || limit > RetrievalCaps::kMaxPointLimit) bad("limit must be 1..10000");
|
||
|
|
Json v = Json::array();
|
||
|
|
for (float f : vector) v.push(Json(static_cast<double>(f)));
|
||
|
|
Json body = Json::object();
|
||
|
|
body.set("query", v);
|
||
|
|
body.set("limit", Json(limit));
|
||
|
|
body.set("with_payload", Json(with_payload));
|
||
|
|
if (!filter.is_null()) body.set("filter", filter);
|
||
|
|
return parse_hits(query_raw(collection, body));
|
||
|
|
}
|
||
|
|
|
||
|
|
Json Qdrant::query_raw(const std::string& collection, const Json& body) {
|
||
|
|
CallArgs a = coll(collection);
|
||
|
|
a.body = body;
|
||
|
|
return gw_->call("query_points", a);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json Qdrant::scroll(const std::string& collection, int limit, const Json& offset, const Json& filter) {
|
||
|
|
if (limit < 1 || limit > RetrievalCaps::kMaxPointLimit) bad("limit must be 1..10000");
|
||
|
|
Json body = Json::object();
|
||
|
|
body.set("limit", Json(limit));
|
||
|
|
if (!offset.is_null()) body.set("offset", offset);
|
||
|
|
if (!filter.is_null()) body.set("filter", filter);
|
||
|
|
CallArgs a = coll(collection);
|
||
|
|
a.body = body;
|
||
|
|
return gw_->call("scroll_points", a);
|
||
|
|
}
|
||
|
|
|
||
|
|
long Qdrant::count(const std::string& collection, const Json& filter, bool exact) {
|
||
|
|
Json body = Json::object();
|
||
|
|
body.set("exact", Json(exact));
|
||
|
|
if (!filter.is_null()) body.set("filter", filter);
|
||
|
|
CallArgs a = coll(collection);
|
||
|
|
a.body = body;
|
||
|
|
return static_cast<long>(J::num(result_of(gw_->call("count_points", a)), "count"));
|
||
|
|
}
|
||
|
|
|
||
|
|
Json Qdrant::delete_points(const std::string& collection, const std::vector<Json>& ids) {
|
||
|
|
if (ids.empty() || ids.size() > RetrievalCaps::kMaxPointsPerUpsert) bad("ids must hold 1..1000 entries");
|
||
|
|
Json arr = Json::array();
|
||
|
|
for (const Json& id : ids) {
|
||
|
|
if (!id.is_number() && !id.is_string()) bad("an id must be a number or a string");
|
||
|
|
arr.push(id);
|
||
|
|
}
|
||
|
|
Json body = Json::object();
|
||
|
|
body.set("points", arr);
|
||
|
|
CallArgs a = coll(collection);
|
||
|
|
a.body = body;
|
||
|
|
a.query["wait"] = "true";
|
||
|
|
return gw_->destroy("delete_points", a);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json Qdrant::delete_points_by_filter(const std::string& collection, const Json& filter) {
|
||
|
|
if (!filter.is_object() || filter.as_object().empty()) bad("filter must be a non-empty object");
|
||
|
|
Json body = Json::object();
|
||
|
|
body.set("filter", filter);
|
||
|
|
CallArgs a = coll(collection);
|
||
|
|
a.body = body;
|
||
|
|
a.query["wait"] = "true";
|
||
|
|
return gw_->destroy("delete_points", a);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json Qdrant::create_snapshot(const std::string& collection) { return gw_->call("create_snapshot", coll(collection)); }
|
||
|
|
Json Qdrant::list_snapshots(const std::string& collection) { return gw_->call("list_snapshots", coll(collection)); }
|
||
|
|
Json Qdrant::delete_snapshot(const std::string& collection, const std::string& snapshot) {
|
||
|
|
CallArgs a = coll(collection);
|
||
|
|
a.path["snapshot_name"] = snapshot;
|
||
|
|
return gw_->destroy("delete_snapshot", a);
|
||
|
|
}
|
||
|
|
Json Qdrant::cluster_status() { return gw_->call("cluster_status"); }
|
||
|
|
|
||
|
|
} // namespace molo::retrieval
|