Files
moloapp/src/vendors/huggingface/request_plan.cpp
T

272 lines
9.3 KiB
C++

#include "vendors/huggingface/request_plan.h"
#include <cctype>
#include <set>
#include "vendors/huggingface/caps.h"
#include "llm/api_client.h"
#include "llm/error.h"
namespace molo::huggingface {
namespace {
[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("huggingface", why); }
bool has_control(const std::string& s) {
for (unsigned char c : s)
if (c < 0x20 || c == 0x7f) return true;
return false;
}
bool all_digits(const std::string& s) {
if (s.empty()) return false;
for (char c : s)
if (!std::isdigit(static_cast<unsigned char>(c))) return false;
return true;
}
std::string segment(const std::string& name, const std::string& value) {
if (value.empty()) bad(name + " must not be empty");
if (value.size() > HfCaps::kMaxSegmentChars) bad(name + " is too long");
if (has_control(value)) bad(name + " has control characters");
if (value == "." || value == "..") bad(name + " must not be . or ..");
return RequestPlan::encode(value);
}
std::string render_value(const std::string& name, const std::string& value) {
if (name == "namespace" || name == "repo" || name == "name") {
if (!RequestPlan::valid_name(value)) bad("bad " + name + " (letters, digits, - _ . only)");
return value;
}
if (name == "repoType") {
RepoType t;
if (!parse_repo_type(value, t)) bad("repoType must be models, datasets or spaces");
return repo_type_plural(t);
}
if (name == "typePrefix") {
RepoType t;
if (!parse_repo_type(value, t)) bad("repoType must be models, datasets or spaces");
return t == RepoType::kModel ? "" : std::string("/") + repo_type_plural(t);
}
if (name == "path") return RequestPlan::encode_file_path(value);
if (name == "model") return RequestPlan::check_model(value);
if (name == "num") {
if (!all_digits(value) || value.size() > 9) bad("num must be a discussion number");
return value;
}
if (name == "action") {
if (value != "enable" && value != "disable") bad("action must be enable or disable");
return value;
}
if (name == "task" || name == "provider") {
if (!llm::Service::valid_id(value)) bad("bad " + name);
return value;
}
return segment(name, value);
}
std::string query_encode_value(const std::string& v) {
std::string out = RequestPlan::encode(v);
// ApiClient refuses ".." anywhere in the request path, query included.
if (v.find("..") != std::string::npos) {
std::string fixed;
for (char c : out) {
if (c == '.') fixed += "%2E";
else fixed += c;
}
return fixed;
}
return out;
}
} // namespace
const char* repo_type_plural(RepoType type) {
switch (type) {
case RepoType::kModel: return "models";
case RepoType::kDataset: return "datasets";
case RepoType::kSpace: return "spaces";
}
return "models";
}
bool parse_repo_type(const std::string& text, RepoType& out) {
if (text == "models" || text == "model") out = RepoType::kModel;
else if (text == "datasets" || text == "dataset") out = RepoType::kDataset;
else if (text == "spaces" || text == "space") out = RepoType::kSpace;
else return false;
return true;
}
RepoRef RepoRef::parse(const std::string& id) {
const std::size_t slash = id.find('/');
if (slash == std::string::npos)
bad("repo id must be owner/name (for example openai-community/gpt2); legacy ids without an owner redirect");
RepoRef r;
r.owner_ = id.substr(0, slash);
r.name_ = id.substr(slash + 1);
if (!RequestPlan::valid_name(r.owner_) || !RequestPlan::valid_name(r.name_))
bad("bad repo id (owner/name, letters, digits, - _ . only)");
return r;
}
PathArgs& PathArgs::set(const std::string& name, std::string value) {
values_[name] = std::move(value);
return *this;
}
PathArgs& PathArgs::repo(const RepoRef& ref) {
values_["namespace"] = ref.owner();
values_["repo"] = ref.name();
return *this;
}
PathArgs& PathArgs::repo_type(RepoType type) {
values_["repoType"] = repo_type_plural(type);
values_["typePrefix"] = repo_type_plural(type);
return *this;
}
const std::string* PathArgs::find(const std::string& name) const {
auto it = values_.find(name);
return it == values_.end() ? nullptr : &it->second;
}
Query& Query::add(const std::string& name, const std::string& value) {
if (items_.size() >= HfCaps::kMaxQueryParams) bad("too many query parameters");
if (value.size() > HfCaps::kMaxQueryValueChars) bad("query parameter " + name + " is too long");
if (has_control(value)) bad("query parameter " + name + " has control characters");
items_.emplace_back(name, value);
return *this;
}
Query& Query::add(const std::string& name, long value) { return add(name, std::to_string(value)); }
Query& Query::flag(const std::string& name, bool value) { return add(name, value ? "true" : "false"); }
bool Query::has(const std::string& name) const {
for (const auto& kv : items_)
if (kv.first == name) return true;
return false;
}
Query& Query::replace(const std::string& name, const std::string& value) {
std::vector<std::pair<std::string, std::string>> kept;
for (auto& kv : items_)
if (kv.first != name) kept.push_back(kv);
items_ = std::move(kept);
return add(name, value);
}
std::string RequestPlan::encode(const std::string& text) {
static const char* hex = "0123456789ABCDEF";
std::string out;
for (unsigned char c : text) {
if (std::isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~') {
out += static_cast<char>(c);
} else {
out += '%';
out += hex[c >> 4];
out += hex[c & 15];
}
}
return out;
}
bool RequestPlan::valid_name(const std::string& text) {
if (text.empty() || text.size() > HfCaps::kMaxSegmentChars || text == "." || text == "..") return false;
for (char c : text)
if (!(std::isalnum(static_cast<unsigned char>(c)) || c == '-' || c == '_' || c == '.')) return false;
return true;
}
std::string RequestPlan::encode_file_path(const std::string& path) {
if (path.empty()) bad("path must not be empty");
if (path.size() > HfCaps::kMaxFilePathChars) bad("path is too long");
if (path.front() == '/' || path.back() == '/') bad("path must not start or end with /");
std::string out;
std::size_t segments = 0, start = 0;
while (start <= path.size()) {
std::size_t end = path.find('/', start);
if (end == std::string::npos) end = path.size();
std::string seg = path.substr(start, end - start);
if (seg.empty()) bad("path has an empty segment");
if (++segments > HfCaps::kMaxPathSegments) bad("path has too many segments");
if (!out.empty()) out += '/';
out += segment("path segment", seg);
start = end + 1;
}
return out;
}
std::string RequestPlan::check_model(const std::string& model) {
if (model.empty() || model.size() > HfCaps::kMaxModelChars) bad("model id is empty or too long");
const std::size_t slash = model.find('/');
if (slash == std::string::npos) {
if (!valid_name(model)) bad("bad model id");
return model;
}
if (model.find('/', slash + 1) != std::string::npos) bad("model id must be owner/name");
if (!valid_name(model.substr(0, slash)) || !valid_name(model.substr(slash + 1))) bad("bad model id");
return model;
}
Plan RequestPlan::make(Call call, const PathArgs& args, const Query& query, const clients::body::Json* body) {
const CallSpec& spec = Calls::spec_of(call);
if (spec.has_flag("unsupported"))
bad(std::string("call is listed but not supported: ") + spec.path + " (see SPEC.md ## Gaps)");
std::string out;
std::set<std::string> used;
const std::string tpl = spec.path;
for (std::size_t i = 0; i < tpl.size();) {
if (tpl[i] != '{') {
out += tpl[i++];
continue;
}
const std::size_t close = tpl.find('}', i);
if (close == std::string::npos) bad("malformed call table path");
const std::string name = tpl.substr(i + 1, close - i - 1);
i = close + 1;
const std::string* v = args.find(name);
const bool last_optional = name == "path" && spec.has_flag("optpath") && i == tpl.size();
if (!v || (v->empty() && last_optional)) {
if (last_optional) {
if (!out.empty() && out.back() == '/') out.pop_back();
used.insert(name);
continue;
}
bad("missing path argument " + name);
}
used.insert(name);
out += render_value(name, *v);
}
for (const auto& kv : args.all()) {
// repo_type() sets both repoType and typePrefix; only the ones the path uses count.
if (!used.count(kv.first) && kv.first != "repoType" && kv.first != "typePrefix")
bad("path argument " + kv.first + " is not used by " + spec.path);
}
if (spec.path[0] == '{' && out.empty()) out = "/";
std::string qs;
for (const auto& kv : query.items()) {
if (!Calls::in_list(spec.query_params, kv.first))
bad("query parameter " + kv.first + " is not accepted by " + spec.http_method + " " + spec.path);
if (!qs.empty()) qs += '&';
qs += encode(kv.first) + "=" + query_encode_value(kv.second);
}
if (body) {
if (!spec.has_body()) bad(std::string(spec.http_method) + " " + spec.path + " takes no body");
if (!body->is_object()) bad("body must be a JSON object");
for (const auto& kv : body->as_object())
if (!Calls::in_list(spec.body_fields, kv.first))
bad("body field " + kv.first + " is not accepted by " + spec.http_method + " " + spec.path);
}
Plan plan;
plan.call = call;
plan.method = Calls::method_of(call);
plan.target = out + (qs.empty() ? "" : "?" + qs);
return plan;
}
} // namespace molo::huggingface