161 lines
6.2 KiB
C++
161 lines
6.2 KiB
C++
#include "vendors/retrieval/mixedbread/mixedbread.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include "vendors/retrieval/caps.h"
|
|
|
|
namespace molo::retrieval {
|
|
|
|
llm::ApiClientConfig Mixedbread::config() {
|
|
llm::ApiClientConfig c;
|
|
c.vendor = "mixedbread";
|
|
c.base_url = kBaseUrl;
|
|
c.key_env = kKeyEnv;
|
|
c.auth = llm::AuthStyle::kBearer;
|
|
return c;
|
|
}
|
|
|
|
namespace {
|
|
[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("mixedbread", why); }
|
|
CallArgs with_body(Json body) {
|
|
CallArgs a;
|
|
a.body = std::move(body);
|
|
return a;
|
|
}
|
|
CallArgs store_arg(const std::string& store) {
|
|
CallArgs a;
|
|
a.path["store_identifier"] = store;
|
|
return a;
|
|
}
|
|
void check_stores(const std::vector<std::string>& stores) {
|
|
if (stores.empty() || stores.size() > 16) bad("stores must hold 1..16 entries");
|
|
for (const std::string& s : stores)
|
|
if (!llm::Service::valid_id(s)) bad("bad store identifier");
|
|
}
|
|
} // namespace
|
|
|
|
Mixedbread Mixedbread::from_env() {
|
|
return Mixedbread(make_gateway(mixedbread_calls(), config(), {{"main", kBaseUrl}}, "", nullptr));
|
|
}
|
|
Mixedbread Mixedbread::with(const TransportFactory& factory, std::string key) {
|
|
return Mixedbread(make_gateway(mixedbread_calls(), config(), {{"main", kBaseUrl}}, std::move(key), factory));
|
|
}
|
|
|
|
EmbedResult Mixedbread::parse_embeddings(const Json& doc) {
|
|
EmbedResult out;
|
|
out.model = J::str(doc, "model");
|
|
std::vector<std::pair<int, std::vector<float>>> rows;
|
|
for (const Json& d : J::array(doc, "data")) {
|
|
const Json* e = J::at(d, "embedding");
|
|
rows.emplace_back(static_cast<int>(J::num(d, "index", static_cast<double>(rows.size()))),
|
|
e ? J::floats(*e) : std::vector<float>{});
|
|
}
|
|
std::stable_sort(rows.begin(), rows.end(), [](const auto& a, const auto& b) { return a.first < b.first; });
|
|
for (auto& r : rows) out.vectors.push_back(std::move(r.second));
|
|
if (const Json* u = J::at(doc, "usage")) out.total_tokens = static_cast<long>(J::num(*u, "total_tokens"));
|
|
return out;
|
|
}
|
|
|
|
RerankResult Mixedbread::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);
|
|
}
|
|
std::stable_sort(out.hits.begin(), out.hits.end(), [](const RerankHit& a, const RerankHit& b) { return a.score > b.score; });
|
|
if (const Json* u = J::at(doc, "usage")) out.total_tokens = static_cast<long>(J::num(*u, "total_tokens"));
|
|
return out;
|
|
}
|
|
|
|
EmbedResult Mixedbread::embed(const EmbedRequest& request) {
|
|
Embedder::validate("mixedbread", request);
|
|
if (request.texts.size() > kMaxEmbedInputs) bad("at most 256 texts per request");
|
|
Json body = Json::object();
|
|
body.set("model", Json(request.model.empty() ? std::string(kDefaultEmbedModel) : request.model));
|
|
body.set("input", J::strings(request.texts));
|
|
if (request.dimensions > 0) body.set("dimensions", Json(request.dimensions));
|
|
if (request.input_type == "query") body.set("prompt", Json("Represent this sentence for searching relevant passages:"));
|
|
EmbedResult r = parse_embeddings(gw_->call("create_embeddings", with_body(body)));
|
|
if (r.vectors.size() != request.texts.size())
|
|
throw llm::ApiError("mixedbread", 502, "BAD_RESPONSE", "embedding count differs from input count");
|
|
return r;
|
|
}
|
|
|
|
RerankResult Mixedbread::rerank(const RerankRequest& request) {
|
|
Reranker::validate("mixedbread", request);
|
|
Json body = Json::object();
|
|
body.set("model", Json(request.model.empty() ? std::string(kDefaultRerankModel) : request.model));
|
|
body.set("query", Json(request.query));
|
|
body.set("input", J::strings(request.documents));
|
|
body.set("return_input", Json(false));
|
|
if (request.top_n > 0) body.set("top_k", Json(request.top_n));
|
|
return parse_rerank(gw_->call("rerank", with_body(body)));
|
|
}
|
|
|
|
Json Mixedbread::list_stores() { return gw_->call("list_stores"); }
|
|
|
|
Json Mixedbread::create_store(const std::string& name, const std::string& description) {
|
|
if (name.empty() || name.size() > 128) bad("store name must be 1..128 chars");
|
|
Json body = Json::object();
|
|
body.set("name", Json(name));
|
|
if (!description.empty()) body.set("description", Json(description));
|
|
return gw_->call("create_store", with_body(body));
|
|
}
|
|
|
|
Json Mixedbread::retrieve_store(const std::string& store) { return gw_->call("retrieve_store", store_arg(store)); }
|
|
Json Mixedbread::delete_store(const std::string& store) { return gw_->destroy("delete_store", store_arg(store)); }
|
|
|
|
Json Mixedbread::search_stores(const std::vector<std::string>& stores, const std::string& query, int top_k) {
|
|
check_stores(stores);
|
|
if (query.empty() || query.size() > RetrievalCaps::kMaxQueryChars) bad("query must be 1..8192 chars");
|
|
if (top_k < 1 || top_k > 100) bad("top_k must be 1..100");
|
|
Json body = Json::object();
|
|
body.set("query", Json(query));
|
|
body.set("store_identifiers", J::strings(stores));
|
|
body.set("top_k", Json(top_k));
|
|
return gw_->call("search_store_chunks", with_body(body));
|
|
}
|
|
|
|
Json Mixedbread::answer(const std::vector<std::string>& stores, const std::string& query) {
|
|
check_stores(stores);
|
|
if (query.empty() || query.size() > RetrievalCaps::kMaxQueryChars) bad("query must be 1..8192 chars");
|
|
Json body = Json::object();
|
|
body.set("query", Json(query));
|
|
body.set("store_identifiers", J::strings(stores));
|
|
return gw_->call("create_question_answering", with_body(body));
|
|
}
|
|
|
|
Json Mixedbread::add_file_to_store(const std::string& store, const std::string& file_id) {
|
|
if (!llm::Service::valid_id(file_id)) bad("bad file id");
|
|
CallArgs a = store_arg(store);
|
|
Json body = Json::object();
|
|
body.set("file_id", Json(file_id));
|
|
a.body = body;
|
|
return gw_->call("create_store_file", a);
|
|
}
|
|
|
|
Json Mixedbread::delete_store_file(const std::string& store, const std::string& file) {
|
|
CallArgs a = store_arg(store);
|
|
a.path["file_identifier"] = file;
|
|
return gw_->destroy("delete_store_file", a);
|
|
}
|
|
|
|
Json Mixedbread::list_files() { return gw_->call("list_files"); }
|
|
|
|
Json Mixedbread::retrieve_file(const std::string& file_id) {
|
|
CallArgs a;
|
|
a.path["file_id"] = file_id;
|
|
return gw_->call("retrieve_file", a);
|
|
}
|
|
|
|
Json Mixedbread::delete_file(const std::string& file_id) {
|
|
CallArgs a;
|
|
a.path["file_id"] = file_id;
|
|
return gw_->destroy("delete_file", a);
|
|
}
|
|
|
|
} // namespace molo::retrieval
|