feat(clients): openrouter: whole public API, spec-checked call table, chat model, demo
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,374 @@
|
||||
#include "openrouter/models.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "llm/error.h"
|
||||
#include "openrouter/caps.h"
|
||||
|
||||
namespace molo::openrouter {
|
||||
|
||||
namespace {
|
||||
|
||||
[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid(kVendor, "models: " + why); }
|
||||
|
||||
bool in(const std::string& v, std::initializer_list<const char*> all) {
|
||||
return std::any_of(all.begin(), all.end(), [&](const char* s) { return v == s; });
|
||||
}
|
||||
|
||||
std::string str(const Json& o, const char* k) {
|
||||
const Json* v = o.is_object() ? o.find(k) : nullptr;
|
||||
return v && v->is_string() ? v->as_string() : "";
|
||||
}
|
||||
|
||||
long integer(const Json& o, const char* k) {
|
||||
const Json* v = o.is_object() ? o.find(k) : nullptr;
|
||||
return v && v->is_number() ? v->as_int() : 0;
|
||||
}
|
||||
|
||||
std::optional<double> real(const Json& o, const char* k) {
|
||||
const Json* v = o.is_object() ? o.find(k) : nullptr;
|
||||
if (v && v->is_number()) return v->as_number();
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::vector<std::string> strings(const Json& o, const char* k) {
|
||||
std::vector<std::string> out;
|
||||
const Json* v = o.is_object() ? o.find(k) : nullptr;
|
||||
if (v && v->is_array())
|
||||
for (const Json& s : v->as_array())
|
||||
if (s.is_string()) out.push_back(s.as_string());
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string join(const std::vector<std::string>& v) {
|
||||
std::string out;
|
||||
for (const std::string& s : v) out += (out.empty() ? "" : ",") + s;
|
||||
return out;
|
||||
}
|
||||
|
||||
void check_csv(const char* what, const std::vector<std::string>& v) {
|
||||
if (v.size() > Caps::kMaxProviderList) bad(std::string(what) + " has too many entries");
|
||||
for (const std::string& s : v) {
|
||||
if (s.empty() || s.size() > 128 || s.find(',') != std::string::npos)
|
||||
bad(std::string(what) + " entries must be non-empty, without commas");
|
||||
}
|
||||
}
|
||||
|
||||
void check_pair(const char* what, const std::optional<double>& lo, const std::optional<double>& hi,
|
||||
double min_allowed, double max_allowed) {
|
||||
for (const auto* v : {&lo, &hi})
|
||||
if (*v && (!std::isfinite(**v) || **v < min_allowed || **v > max_allowed))
|
||||
bad(std::string(what) + " out of range");
|
||||
if (lo && hi && *lo > *hi) bad(std::string(what) + ": min is above max");
|
||||
}
|
||||
|
||||
void check_limit(const std::optional<int>& offset, const std::optional<int>& limit) {
|
||||
if (offset && *offset < 0) bad("offset must be >= 0");
|
||||
if (limit && (*limit < 1 || *limit > Caps::kMaxListLimit)) bad("limit must be 1..1000");
|
||||
}
|
||||
|
||||
ModelPage parse_page(const Json& doc) {
|
||||
ModelPage p;
|
||||
const Json* data = doc.is_object() ? doc.find("data") : nullptr;
|
||||
if (!data || !data->is_array()) throw llm::ApiError(kVendor, 502, "BAD_RESPONSE", "models response has no data array");
|
||||
for (const Json& m : data->as_array()) p.models.push_back(ModelInfo::parse(m));
|
||||
p.total_count = integer(doc, "total_count");
|
||||
if (p.total_count == 0) p.total_count = static_cast<long>(p.models.size());
|
||||
if (const Json* links = doc.find("links"); links && links->is_object()) p.next = str(*links, "next");
|
||||
return p;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool ModelFilter::valid_sort(const std::string& s) {
|
||||
return in(s, {"most-popular", "newest", "top-weekly", "pricing-low-to-high", "pricing-high-to-low",
|
||||
"context-high-to-low", "throughput-high-to-low", "latency-low-to-high",
|
||||
"intelligence-high-to-low", "coding-high-to-low", "agentic-high-to-low",
|
||||
"design-arena-elo-high-to-low"});
|
||||
}
|
||||
bool ModelFilter::valid_category(const std::string& s) {
|
||||
return in(s, {"programming", "roleplay", "marketing", "marketing/seo", "technology", "science",
|
||||
"translation", "legal", "finance", "health", "trivia", "academia"});
|
||||
}
|
||||
bool ModelFilter::valid_output_modality(const std::string& s) {
|
||||
return in(s, {"text", "image", "embeddings", "audio", "video", "rerank", "decisions", "speech",
|
||||
"transcription", "all"});
|
||||
}
|
||||
bool ModelFilter::valid_input_modality(const std::string& s) {
|
||||
return in(s, {"text", "image", "audio", "file"});
|
||||
}
|
||||
|
||||
void ModelFilter::validate() const {
|
||||
check_limit(offset, limit);
|
||||
if (!category.empty() && !valid_category(category)) bad("unknown category");
|
||||
if (!sort.empty() && !valid_sort(sort)) bad("unknown sort");
|
||||
check_csv("supported_parameters", supported_parameters);
|
||||
check_csv("model_authors", model_authors);
|
||||
check_csv("providers", providers);
|
||||
for (const std::string& m : output_modalities)
|
||||
if (!valid_output_modality(m)) bad("unknown output modality: " + m);
|
||||
for (const std::string& m : input_modalities)
|
||||
if (!valid_input_modality(m)) bad("unknown input modality: " + m);
|
||||
if (!region.empty() && region != "eu" && region != "us") bad("region must be eu or us");
|
||||
if (context && *context < 1) bad("context must be >= 1");
|
||||
if (q.size() > 256 || arch.size() > 128) bad("q or arch too long");
|
||||
constexpr double kBig = 1e9;
|
||||
check_pair("price", min_price, max_price, 0, kBig);
|
||||
check_pair("output price", min_output_price, max_output_price, 0, kBig);
|
||||
check_pair("intelligence index", min_intelligence_index, max_intelligence_index, 0, kBig);
|
||||
check_pair("coding index", min_coding_index, max_coding_index, 0, kBig);
|
||||
check_pair("agentic index", min_agentic_index, max_agentic_index, 0, kBig);
|
||||
check_pair("tool success rate", min_tool_success_rate, max_tool_success_rate, 0, 1);
|
||||
if ((min_age_days && *min_age_days < 0) || (max_age_days && *max_age_days < 0)) bad("age days must be >= 0");
|
||||
if (min_age_days && max_age_days && *min_age_days > *max_age_days) bad("age days: min is above max");
|
||||
}
|
||||
|
||||
Query ModelFilter::to_query() const {
|
||||
validate();
|
||||
Query q_;
|
||||
if (offset) q_.add("offset", *offset);
|
||||
if (limit) q_.add("limit", *limit);
|
||||
q_.add_if("category", category);
|
||||
q_.add_if("supported_parameters", join(supported_parameters));
|
||||
q_.add_if("output_modalities", join(output_modalities));
|
||||
q_.add_if("input_modalities", join(input_modalities));
|
||||
q_.add_if("sort", sort);
|
||||
q_.add_if("q", q);
|
||||
if (context) q_.add("context", *context);
|
||||
auto d = [&](const char* k, const std::optional<double>& v) { if (v) q_.add(k, *v); };
|
||||
d("min_price", min_price);
|
||||
d("max_price", max_price);
|
||||
q_.add_if("arch", arch);
|
||||
q_.add_if("model_authors", join(model_authors));
|
||||
q_.add_if("providers", join(providers));
|
||||
if (distillable) q_.add("distillable", *distillable ? "true" : "false");
|
||||
if (zdr) q_.add("zdr", "true");
|
||||
q_.add_if("region", region);
|
||||
d("min_output_price", min_output_price);
|
||||
d("max_output_price", max_output_price);
|
||||
if (min_age_days) q_.add("min_age_days", *min_age_days);
|
||||
if (max_age_days) q_.add("max_age_days", *max_age_days);
|
||||
d("min_intelligence_index", min_intelligence_index);
|
||||
d("max_intelligence_index", max_intelligence_index);
|
||||
d("min_coding_index", min_coding_index);
|
||||
d("max_coding_index", max_coding_index);
|
||||
d("min_agentic_index", min_agentic_index);
|
||||
d("max_agentic_index", max_agentic_index);
|
||||
d("min_tool_success_rate", min_tool_success_rate);
|
||||
d("max_tool_success_rate", max_tool_success_rate);
|
||||
return q_;
|
||||
}
|
||||
|
||||
ModelInfo ModelInfo::parse(const Json& doc) {
|
||||
if (!doc.is_object()) throw llm::ApiError(kVendor, 502, "BAD_RESPONSE", "model is not an object");
|
||||
ModelInfo m;
|
||||
m.raw = doc;
|
||||
m.id = str(doc, "id");
|
||||
m.canonical_slug = str(doc, "canonical_slug");
|
||||
m.name = str(doc, "name");
|
||||
m.description = str(doc, "description");
|
||||
m.created = integer(doc, "created");
|
||||
m.context_length = integer(doc, "context_length");
|
||||
if (const Json* a = doc.find("architecture"); a && a->is_object()) {
|
||||
m.modality = str(*a, "modality");
|
||||
m.input_modalities = strings(*a, "input_modalities");
|
||||
m.output_modalities = strings(*a, "output_modalities");
|
||||
m.tokenizer = str(*a, "tokenizer");
|
||||
}
|
||||
m.supported_parameters = strings(doc, "supported_parameters");
|
||||
if (const Json* p = doc.find("pricing")) m.pricing = *p;
|
||||
if (const Json* t = doc.find("top_provider")) m.top_provider = *t;
|
||||
if (const Json* d = doc.find("default_parameters")) m.default_parameters = *d;
|
||||
if (const Json* r = doc.find("reasoning")) m.reasoning = *r;
|
||||
m.expiration_date = str(doc, "expiration_date");
|
||||
if (const Json* at = doc.find("alias_target")) m.alias_target = at->is_string() ? at->as_string() : at->dump();
|
||||
return m;
|
||||
}
|
||||
|
||||
bool ModelInfo::supports(const std::string& parameter) const {
|
||||
return std::find(supported_parameters.begin(), supported_parameters.end(), parameter) !=
|
||||
supported_parameters.end();
|
||||
}
|
||||
|
||||
std::optional<double> ModelInfo::price_per_million(const std::string& key) const {
|
||||
const Json* v = pricing.is_object() ? pricing.find(key) : nullptr;
|
||||
if (!v) return std::nullopt;
|
||||
double per_token;
|
||||
if (v->is_number()) {
|
||||
per_token = v->as_number();
|
||||
} else if (v->is_string()) {
|
||||
char* end = nullptr;
|
||||
per_token = std::strtod(v->as_string().c_str(), &end);
|
||||
if (end == v->as_string().c_str()) return std::nullopt;
|
||||
} else {
|
||||
return std::nullopt;
|
||||
}
|
||||
return per_token * 1e6;
|
||||
}
|
||||
|
||||
bool ModelInfo::is_free() const {
|
||||
if (id.size() > 5 && id.compare(id.size() - 5, 5, ":free") == 0) return true;
|
||||
auto p = price_per_million("prompt");
|
||||
auto c = price_per_million("completion");
|
||||
return p && c && *p == 0 && *c == 0;
|
||||
}
|
||||
|
||||
EndpointInfo EndpointInfo::parse(const Json& d) {
|
||||
if (!d.is_object()) throw llm::ApiError(kVendor, 502, "BAD_RESPONSE", "endpoint is not an object");
|
||||
EndpointInfo e;
|
||||
e.raw = d;
|
||||
e.name = str(d, "name");
|
||||
e.model_id = str(d, "model_id");
|
||||
e.model_name = str(d, "model_name");
|
||||
e.provider_name = str(d, "provider_name");
|
||||
e.tag = str(d, "tag");
|
||||
e.quantization = str(d, "quantization");
|
||||
e.service_tier = str(d, "service_tier");
|
||||
e.context_length = integer(d, "context_length");
|
||||
if (const Json* v = d.find("max_completion_tokens"); v && v->is_number()) e.max_completion_tokens = v->as_int();
|
||||
if (const Json* v = d.find("max_prompt_tokens"); v && v->is_number()) e.max_prompt_tokens = v->as_int();
|
||||
e.status = static_cast<int>(integer(d, "status"));
|
||||
e.uptime_last_5m = real(d, "uptime_last_5m");
|
||||
e.uptime_last_30m = real(d, "uptime_last_30m");
|
||||
e.uptime_last_1d = real(d, "uptime_last_1d");
|
||||
if (const Json* v = d.find("latency_last_30m")) e.latency_last_30m = *v;
|
||||
if (const Json* v = d.find("throughput_last_30m")) e.throughput_last_30m = *v;
|
||||
if (const Json* v = d.find("pricing")) e.pricing = *v;
|
||||
if (const Json* v = d.find("supports_tool_choice")) e.supports_tool_choice = *v;
|
||||
e.supported_parameters = strings(d, "supported_parameters");
|
||||
if (const Json* v = d.find("supports_implicit_caching"); v && v->is_bool()) e.supports_implicit_caching = v->as_bool();
|
||||
return e;
|
||||
}
|
||||
|
||||
ProviderInfo ProviderInfo::parse(const Json& d) {
|
||||
if (!d.is_object()) throw llm::ApiError(kVendor, 502, "BAD_RESPONSE", "provider is not an object");
|
||||
ProviderInfo p;
|
||||
p.name = str(d, "name");
|
||||
p.slug = str(d, "slug");
|
||||
p.privacy_policy_url = str(d, "privacy_policy_url");
|
||||
p.terms_of_service_url = str(d, "terms_of_service_url");
|
||||
p.status_page_url = str(d, "status_page_url");
|
||||
p.headquarters = str(d, "headquarters");
|
||||
p.datacenters = strings(d, "datacenters");
|
||||
return p;
|
||||
}
|
||||
|
||||
ModelPage ModelsService::page(const ModelFilter& filter) const {
|
||||
return parse_page(run("getModels", {}, filter.to_query()));
|
||||
}
|
||||
|
||||
ModelList ModelsService::list_all(const ModelFilter& filter, std::size_t max_models) const {
|
||||
if (max_models == 0) bad("max_models must be >= 1");
|
||||
ModelList out;
|
||||
ModelFilter f = filter;
|
||||
f.limit = Caps::kModelPageSize;
|
||||
int offset = filter.offset.value_or(0);
|
||||
while (true) {
|
||||
if (out.pages >= Caps::kMaxModelPages || out.models.size() >= max_models) {
|
||||
out.truncated = true;
|
||||
break;
|
||||
}
|
||||
f.offset = offset;
|
||||
ModelPage p = page(f);
|
||||
++out.pages;
|
||||
out.total_count = p.total_count;
|
||||
for (ModelInfo& m : p.models) {
|
||||
if (out.models.size() >= max_models) {
|
||||
out.truncated = true;
|
||||
return out;
|
||||
}
|
||||
out.models.push_back(std::move(m));
|
||||
}
|
||||
if (p.next.empty() || p.models.empty()) break;
|
||||
offset += static_cast<int>(p.models.size());
|
||||
if (static_cast<long>(offset) >= p.total_count) break;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
ModelInfo ModelsService::get(const std::string& model_id) const {
|
||||
auto [a, s] = split_model_id(model_id);
|
||||
Json doc = run("getModel", {{"author", a}, {"slug", s}});
|
||||
const Json* d = doc.find("data");
|
||||
return ModelInfo::parse(d ? *d : doc);
|
||||
}
|
||||
|
||||
ModelEndpoints ModelsService::endpoints(const std::string& model_id) const {
|
||||
auto [a, s] = split_model_id(model_id);
|
||||
Json doc = run("listEndpoints", {{"author", a}, {"slug", s}});
|
||||
const Json* d = doc.find("data");
|
||||
if (!d || !d->is_object()) throw llm::ApiError(kVendor, 502, "BAD_RESPONSE", "endpoints response has no data");
|
||||
ModelEndpoints out;
|
||||
out.raw = doc;
|
||||
out.id = str(*d, "id");
|
||||
out.name = str(*d, "name");
|
||||
out.created = integer(*d, "created");
|
||||
out.description = str(*d, "description");
|
||||
if (const Json* a2 = d->find("architecture")) out.architecture = *a2;
|
||||
if (const Json* eps = d->find("endpoints"); eps && eps->is_array())
|
||||
for (const Json& e : eps->as_array()) out.endpoints.push_back(EndpointInfo::parse(e));
|
||||
return out;
|
||||
}
|
||||
|
||||
long ModelsService::count(const std::string& output_modalities) const {
|
||||
Query q;
|
||||
if (!output_modalities.empty()) {
|
||||
std::size_t pos = 0;
|
||||
while (pos <= output_modalities.size()) {
|
||||
std::size_t comma = output_modalities.find(',', pos);
|
||||
std::string one = output_modalities.substr(pos, comma == std::string::npos ? comma : comma - pos);
|
||||
if (!ModelFilter::valid_output_modality(one)) bad("unknown output modality: " + one);
|
||||
if (comma == std::string::npos) break;
|
||||
pos = comma + 1;
|
||||
}
|
||||
q.add("output_modalities", output_modalities);
|
||||
}
|
||||
Json doc = run("listModelsCount", {}, q);
|
||||
const Json* d = doc.find("data");
|
||||
return d ? integer(*d, "count") : 0;
|
||||
}
|
||||
|
||||
Json ModelsService::zdr_endpoints() const { return run("listEndpointsZdr"); }
|
||||
|
||||
Json ModelsService::embeddings_models(std::optional<int> offset, std::optional<int> limit) const {
|
||||
check_limit(offset, limit);
|
||||
Query q;
|
||||
if (offset) q.add("offset", *offset);
|
||||
if (limit) q.add("limit", *limit);
|
||||
return run("listEmbeddingsModels", {}, q);
|
||||
}
|
||||
|
||||
Json ModelsService::image_models() const { return run("listImageModels"); }
|
||||
Json ModelsService::video_models() const { return run("listVideosModels"); }
|
||||
|
||||
Json ModelsService::image_model_endpoints(const std::string& model_id) const {
|
||||
auto [a, s] = split_model_id(model_id);
|
||||
return run("listImageModelEndpoints", {{"author", a}, {"slug", s}});
|
||||
}
|
||||
|
||||
ModelPage ModelsService::user_models(std::optional<int> offset, std::optional<int> limit,
|
||||
const std::string& output_modalities,
|
||||
const std::string& sort) const {
|
||||
check_limit(offset, limit);
|
||||
if (!sort.empty() && !ModelFilter::valid_sort(sort)) bad("unknown sort");
|
||||
Query q;
|
||||
if (offset) q.add("offset", *offset);
|
||||
if (limit) q.add("limit", *limit);
|
||||
q.add_if("output_modalities", output_modalities);
|
||||
q.add_if("sort", sort);
|
||||
if (!gateway_->api_configured()) throw llm::NotConfiguredError(kVendor, kApiKeyEnv);
|
||||
return parse_page(run("listModelsUser", {}, q));
|
||||
}
|
||||
|
||||
std::vector<ProviderInfo> ProvidersService::list() const {
|
||||
Json doc = run("listProviders");
|
||||
const Json* d = doc.find("data");
|
||||
if (!d || !d->is_array()) throw llm::ApiError(kVendor, 502, "BAD_RESPONSE", "providers response has no data array");
|
||||
std::vector<ProviderInfo> out;
|
||||
for (const Json& p : d->as_array()) out.push_back(ProviderInfo::parse(p));
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace molo::openrouter
|
||||
Reference in New Issue
Block a user