feat(clients): cohere: whole API, CohereChatModel, spec checker, demo and tests

Package src/vendors/cohere (molo::cohere): Chat v2 with streaming, tools,
thinking and citations; embed, rerank, classify, tokenize, detokenize,
parse, transcription; datasets, embed jobs, batches, connectors,
fine-tuning, models, check-api-key; legacy v1 chat/generate/summarize;
CohereChatModel : ChatModel. All 42 operations of cohere-openapi.yaml are
in one call table, verified by tools/check_spec.py. Demo routes under
/ai/cohere, 51 unit tests and 2 route tests; live tests are opt-in.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-25 10:49:17 +01:00
co-authored by Claude Sonnet 5.5
parent 3db67fcf1f
commit f32947427b
36 changed files with 6045 additions and 3 deletions
+835 -3
View File
@@ -1,13 +1,845 @@
#include "../AiDemos.hpp"
#include <functional>
#include "../../fw/Router.hpp"
#include "../../fw/Templates.hpp"
#include "security/crypto/Base64.h"
#include "vendors/cohere/calls.h"
#include "vendors/cohere/caps.h"
#include "vendors/cohere/catalog.h"
#include "vendors/cohere/chat.h"
#include "vendors/cohere/inference.h"
#include "vendors/cohere/platform.h"
#include "vendors/cohere/resources.h"
#include "vendors/cohere/rules.h"
namespace molo::example::app {
// cohere. Stub: the package agent fills this in.
// Cohere platform demo over src/vendors/cohere. GET routes need no credential.
// Every route that calls Cohere is a POST: 422 on bad input, 503
// cohere_not_configured without COHERE_API_KEY. Routes that cost money (chat,
// embed, rerank, classify, parse, legacy) or change resources (create, update,
// cancel, delete) are dry runs that show the exact request unless the body has
// "confirm":true. Reads (list, get, usage, check key, models, tokenize) run
// directly once a key exists.
namespace {
namespace co = cohere;
using Json = clients::body::Json;
constexpr std::size_t kMaxBody = 8 * 1024 * 1024; // dataset uploads travel as base64
const char kPage[] = R"COHERE({% extends "base.html" %}
{% block content %}
<h1>Cohere API</h1>
<p class="hint">Chat v2 (streaming, tools, thinking, citations), embed, rerank, classify, tokenize,
datasets, embed jobs, batches, connectors, fine-tuning, models, parse, legacy. Paid and
destructive calls are dry runs until the body says <code>"confirm":true</code>.</p>
<p id="co-config" class="hint">Checking credential...</p>
<section class="co">
<label>Route
<select id="co-route"></select></label>
<label>Body (JSON object) <textarea id="co-body" rows="8"></textarea></label>
<div class="co-actions">
<button id="co-send" type="button" class="primary">Send</button>
<button id="co-confirm" type="button">Send with confirm</button>
</div>
<p id="co-error" class="error" role="alert"></p>
<pre id="co-out" class="co-out"></pre>
</section>
<style>
.co label{display:block;margin:.5rem 0}
.co select,.co textarea{width:100%;box-sizing:border-box}
.co-actions{display:flex;flex-wrap:wrap;gap:.5rem;margin:.5rem 0}
.co-out{overflow:auto;max-height:24rem;white-space:pre-wrap;word-break:break-word}
</style>
<script src="/embedded/ai_cohere.js"></script>
{% endblock %}
)COHERE";
[[noreturn]] void bad(const std::string& why) { throw fw::HttpError(422, "validation_error", why); }
fw::Response json_out(const Json& j, int status = 200) {
fw::Response res = fw::JsonResponse(j.dump(), status);
res.headers.set("Cache-Control", "no-store");
return res;
}
Json parse_body(const fw::Request& req) {
if (req.body.size() > kMaxBody) throw fw::HttpError(413, "payload_too_large", "body too large");
if (req.body.empty()) return Json::object();
try {
Json j = Json::parse(req.body);
if (!j.is_object()) bad("body must be a JSON object");
return j;
} catch (const clients::body::JsonError&) {
bad("body is not valid JSON");
}
}
std::string text(const Json& b, const char* key, bool required = false) {
const Json* v = b.find(key);
if (!v || v->is_null()) {
if (required) bad(std::string(key) + " is required");
return "";
}
if (!v->is_string()) bad(std::string(key) + " must be a string");
return v->as_string();
}
std::optional<int> number(const Json& b, const char* key) {
const Json* v = b.find(key);
if (!v || v->is_null()) return std::nullopt;
if (!v->is_number()) bad(std::string(key) + " must be a number");
return static_cast<int>(v->as_int());
}
std::optional<double> real(const Json& b, const char* key) {
const Json* v = b.find(key);
if (!v || v->is_null()) return std::nullopt;
if (!v->is_number()) bad(std::string(key) + " must be a number");
return v->as_number();
}
bool flag(const Json& b, const char* key) {
const Json* v = b.find(key);
if (!v || v->is_null()) return false;
if (!v->is_bool()) bad(std::string(key) + " must be true or false");
return v->as_bool();
}
std::vector<std::string> strings(const Json& b, const char* key) {
std::vector<std::string> out;
const Json* v = b.find(key);
if (!v || v->is_null()) return out;
if (!v->is_array()) bad(std::string(key) + " must be an array of strings");
for (const Json& s : v->as_array()) {
if (!s.is_string()) bad(std::string(key) + " must be an array of strings");
out.push_back(s.as_string());
}
return out;
}
Json usage_json(const co::TokenUsage& u) {
Json o = Json::object();
o.set("input_tokens", Json(static_cast<int>(u.input_tokens)));
o.set("output_tokens", Json(static_cast<int>(u.output_tokens)));
o.set("billed_input_tokens", Json(static_cast<int>(u.billed_input_tokens)));
o.set("billed_output_tokens", Json(static_cast<int>(u.billed_output_tokens)));
o.set("cached_tokens", Json(static_cast<int>(u.cached_tokens)));
return o;
}
Json chat_json(const co::ChatResult& r) {
Json o = Json::object();
o.set("id", Json(r.id));
o.set("finish_reason", Json(r.finish_reason));
o.set("text", Json(r.text));
o.set("thinking", Json(r.thinking));
o.set("tool_plan", Json(r.tool_plan));
Json calls = Json::array();
for (const co::ToolCallRecord& t : r.tool_calls) {
Json x = Json::object();
x.set("id", Json(t.id));
x.set("name", Json(t.name));
x.set("arguments", Json(t.arguments));
x.set("arguments_valid", Json(t.arguments_valid()));
calls.push(x);
}
o.set("tool_calls", calls);
o.set("citations", Json(static_cast<int>(r.citations.size())));
o.set("usage", usage_json(r.usage));
return o;
}
Json dry_run(const Json& request, const std::string& path) {
Json o = Json::object();
o.set("dry_run", Json(true));
o.set("would_send_to", Json(path));
o.set("request", request);
o.set("hint", Json("send confirm:true to run it"));
return o;
}
co::ChatParams chat_params(const Json& b) {
co::ChatParams p;
std::string model = text(b, "model");
if (!model.empty()) p.model = model;
std::string system = text(b, "system");
if (!system.empty()) p.add(co::Messages::system(system));
p.add(co::Messages::user(text(b, "prompt", true)));
std::string thinking = text(b, "thinking");
if (thinking == "enabled") p.thinking = co::Thinking::kEnabled;
else if (thinking == "disabled") p.thinking = co::Thinking::kDisabled;
else if (!thinking.empty()) bad("thinking must be enabled or disabled");
p.thinking_budget = number(b, "thinking_budget");
p.max_tokens = number(b, "max_tokens");
p.json_mode = flag(b, "json");
p.stop = strings(b, "stop");
p.temperature = real(b, "temperature");
p.citation_mode = text(b, "citation_mode");
p.safety_mode = text(b, "safety_mode");
for (const std::string& d : strings(b, "documents")) p.documents.push_back(Json(d));
return p;
}
// One route = validate into a Plan, then the generic runner decides dry run,
// credential, call and error mapping.
struct Plan {
Json request; // what would be sent (shown in a dry run)
std::string path; // for the dry-run message
bool needs_confirm = false; // paid or changes a resource
std::function<Json()> call;
};
Json id_only(const std::string& id) {
Json r = Json::object();
r.set("id", Json(id));
return r;
}
std::string need_id(const Json& b) {
std::string id = text(b, "id", true);
if (!llm::Service::valid_id(id)) bad("id has invalid characters");
return id;
}
class CohereRoutes {
public:
explicit CohereRoutes(co::Platform platform) : p_(std::move(platform)) {}
const co::Platform& platform() const { return p_; }
using Planner = std::function<Plan(const Json&)>;
fw::Response run(const fw::Request& req, const Planner& planner) const {
Json body = parse_body(req);
Plan plan;
try {
plan = planner(body);
} catch (const llm::ApiError& e) {
map(e);
}
if (plan.needs_confirm && !flag(body, "confirm")) return json_out(dry_run(plan.request, plan.path));
try {
return json_out(plan.call());
} catch (const llm::ApiError& e) {
map(e);
}
}
// ---- chat family -------------------------------------------------------
Plan chat(const Json& b) const {
co::ChatParams params = chat_params(b);
Plan pl{params.to_json(), "POST /v2/chat", true, nullptr};
auto api = std::make_shared<co::Chat>(p_.client);
pl.call = [api, params] { return chat_json(api->create(params)); };
return pl;
}
Plan stream(const Json& b) const {
co::ChatParams params = chat_params(b);
Plan pl{params.to_json(true), "POST /v2/chat (stream)", true, nullptr};
auto api = std::make_shared<co::Chat>(p_.client);
pl.call = [api, params] {
std::size_t events = 0;
co::ChatResult r = api->stream(params, [&](const co::StreamDelta&) {
return ++events < co::CohereCaps::kMaxStreamEvents;
});
Json o = chat_json(r);
o.set("events", Json(static_cast<int>(events)));
return o;
};
return pl;
}
Plan tools(const Json& b) const {
co::ChatParams params = chat_params(b);
co::ToolDef t;
t.name = "get_weather";
t.description = "Get the weather of a city.";
t.parameters = Json::parse(
"{\"type\":\"object\",\"properties\":{\"location\":{\"type\":\"string\",\"description\":\"The city\"}},"
"\"required\":[\"location\"]}");
params.tools.push_back(t);
params.strict_tools = flag(b, "strict");
std::string choice = text(b, "tool_choice");
params.tool_choice = choice;
Plan pl{params.to_json(), "POST /v2/chat", true, nullptr};
auto api = std::make_shared<co::Chat>(p_.client);
pl.call = [api, params] { return chat_json(api->create(params)); };
return pl;
}
Plan legacy(const Json& b) const {
std::string action = text(b, "action", true);
Json body = Json::object();
if (const Json* x = b.find("body")) {
if (!x->is_object()) bad("body must be an object");
body = *x;
}
auto chat = std::make_shared<co::Chat>(p_.client);
auto old = std::make_shared<co::Legacy>(p_.client);
if (action == "chat") {
co::Calls::check_body(co::Call::kChatV1, body);
return Plan{body, "POST /v1/chat", true, [chat, body] { return chat->legacy(body); }};
}
if (action == "generate") {
co::Calls::check_body(co::Call::kGenerate, body);
return Plan{body, "POST /v1/generate", true, [old, body] { return old->generate(body); }};
}
if (action == "summarize") {
co::Calls::check_body(co::Call::kSummarize, body);
return Plan{body, "POST /v1/summarize", true, [old, body] { return old->summarize(body); }};
}
bad("action must be chat, generate or summarize");
}
// ---- model calls -------------------------------------------------------
Plan embed(const Json& b) const {
co::EmbedParams p;
std::string model = text(b, "model");
if (!model.empty()) p.model = model;
std::string input_type = text(b, "input_type");
if (!input_type.empty()) p.input_type = input_type;
p.texts = strings(b, "texts");
p.images = strings(b, "images");
p.embedding_types = strings(b, "embedding_types");
p.truncate = text(b, "truncate");
p.output_dimension = number(b, "output_dimension");
Plan pl{p.to_json(), "POST /v2/embed", true, nullptr};
auto api = std::make_shared<co::Embeddings>(p_.client);
pl.call = [api, p] {
co::EmbedResult r = api->embed(p);
auto rows = r.floats();
Json o = Json::object();
o.set("id", Json(r.id));
o.set("count", Json(static_cast<int>(rows.size())));
o.set("dimensions", Json(static_cast<int>(rows.empty() ? 0 : rows[0].size())));
Json head = Json::array();
if (!rows.empty())
for (std::size_t i = 0; i < rows[0].size() && i < 8; ++i) head.push(Json(rows[0][i]));
o.set("first_vector_head", head);
o.set("usage", usage_json(r.usage));
return o;
};
return pl;
}
Plan rerank(const Json& b) const {
co::RerankParams p;
std::string model = text(b, "model");
if (!model.empty()) p.model = model;
p.query = text(b, "query", true);
p.documents = strings(b, "documents");
p.top_n = number(b, "top_n");
Plan pl{p.to_json(), "POST /v2/rerank", true, nullptr};
auto api = std::make_shared<co::Rerank>(p_.client);
pl.call = [api, p] {
co::RerankResult r = api->rerank(p);
Json arr = Json::array();
for (const co::RerankHit& h : r.hits) {
Json x = Json::object();
x.set("index", Json(h.index));
x.set("relevance_score", Json(h.score));
arr.push(x);
}
Json o = Json::object();
o.set("results", arr);
o.set("usage", usage_json(r.usage));
return o;
};
return pl;
}
Plan classify(const Json& b) const {
co::ClassifyParams p;
p.inputs = strings(b, "inputs");
p.model = text(b, "model");
if (const Json* ex = b.find("examples")) {
if (!ex->is_array()) bad("examples must be an array");
for (const Json& e : ex->as_array()) {
if (!e.is_object()) bad("examples must be objects with text and label");
p.examples.push_back({text(e, "text", true), text(e, "label", true)});
}
}
Plan pl{p.to_json(), "POST /v1/classify", true, nullptr};
auto api = std::make_shared<co::Classify>(p_.client);
pl.call = [api, p] {
co::ClassifyResult r = api->classify(p);
Json arr = Json::array();
for (const co::Classification& c : r.items) {
Json x = Json::object();
x.set("input", Json(c.input));
x.set("prediction", Json(c.prediction));
x.set("confidence", Json(c.confidence));
arr.push(x);
}
Json o = Json::object();
o.set("classifications", arr);
return o;
};
return pl;
}
Plan tokenize(const Json& b) const {
std::string model = text(b, "model");
if (model.empty()) model = co::ModelCatalog::default_chat();
std::string t = text(b, "text", true);
if (t.size() > co::CohereCaps::kMaxTokenizeChars) bad("text over 65536 chars");
Json req = Json::object();
req.set("model", Json(model));
req.set("text", Json(t));
auto api = std::make_shared<co::Tokenizer>(p_.client);
return Plan{req, "POST /v1/tokenize", false, [api, model, t] {
co::TokenizeResult r = api->tokenize(model, t);
Json toks = Json::array();
for (long x : r.tokens) toks.push(Json(x));
Json strs = Json::array();
for (const std::string& s : r.token_strings) strs.push(Json(s));
Json o = Json::object();
o.set("tokens", toks);
o.set("token_strings", strs);
return o;
}};
}
Plan detokenize(const Json& b) const {
std::string model = text(b, "model");
if (model.empty()) model = co::ModelCatalog::default_chat();
std::vector<long> tokens;
const Json* arr = b.find("tokens");
if (!arr || !arr->is_array()) bad("tokens must be an array of integers");
for (const Json& t : arr->as_array()) {
if (!t.is_number()) bad("tokens must be an array of integers");
tokens.push_back(t.as_int());
}
auto api = std::make_shared<co::Tokenizer>(p_.client);
Json req = Json::object();
req.set("model", Json(model));
return Plan{req, "POST /v1/detokenize", false, [api, model, tokens] {
Json o = Json::object();
o.set("text", Json(api->detokenize(model, tokens)));
return o;
}};
}
Plan parse(const Json& b) const {
std::string model = text(b, "model", true);
std::string url = text(b, "image_url", true);
std::string format = text(b, "output_format");
co::Rules::model(model);
co::Rules::image_url(url);
Json req = Json::object();
req.set("model", Json(model));
req.set("image_url", Json(url.size() > 200 ? url.substr(0, 200) + "..." : url));
auto api = std::make_shared<co::Documents>(p_.client);
return Plan{req, "POST /v2/parse", true, [api, model, url, format] { return api->parse(model, url, format); }};
}
// ---- account -----------------------------------------------------------
Plan models(const Json& b) const {
std::string action = text(b, "action");
auto api = std::make_shared<co::Account>(p_.client);
if (action == "get") {
std::string name = text(b, "name", true);
co::Rules::model(name);
return Plan{id_only(name), "GET /v1/models/{model}", false, [api, name] { return model_json(api->model(name)); }};
}
if (!action.empty() && action != "list") bad("action must be list or get");
std::string endpoint = text(b, "endpoint");
bool default_only = flag(b, "default_only");
return Plan{Json::object(), "GET /v1/models", false, [api, endpoint, default_only] {
co::ModelList l = api->models(endpoint, default_only);
Json arr = Json::array();
for (const co::ModelInfo& m : l.models) arr.push(model_json(m));
Json o = Json::object();
o.set("models", arr);
o.set("next_page_token", Json(l.page.next_page_token));
return o;
}};
}
Plan key(const Json&) const {
auto api = std::make_shared<co::Account>(p_.client);
return Plan{Json::object(), "POST /v1/check-api-key", false, [api] {
co::ApiKeyCheck c = api->check_api_key();
Json o = Json::object();
o.set("valid", Json(c.valid));
o.set("organization_id", Json(c.organization_id));
return o;
}};
}
// ---- resources ---------------------------------------------------------
Plan datasets(const Json& b) const {
std::string action = text(b, "action", true);
auto api = std::make_shared<co::Datasets>(p_.client);
if (action == "list") {
co::DatasetFilter f;
f.dataset_type = text(b, "dataset_type");
f.validation_status = text(b, "validation_status");
f.limit = number(b, "limit");
f.validate();
return Plan{Json::object(), "GET /v1/datasets", false, [api, f] {
Json arr = Json::array();
for (const co::Dataset& d : api->list(f)) arr.push(dataset_json(d));
Json o = Json::object();
o.set("datasets", arr);
return o;
}};
}
if (action == "usage")
return Plan{Json::object(), "GET /v1/datasets/usage", false, [api] {
Json o = Json::object();
o.set("organization_usage", api->usage());
return o;
}};
if (action == "get") {
std::string id = need_id(b);
return Plan{id_only(id), "GET /v1/datasets/{id}", false, [api, id] { return dataset_json(api->get(id)); }};
}
if (action == "delete") {
std::string id = need_id(b);
return Plan{id_only(id), "DELETE /v1/datasets/{id}", true, [api, id] {
api->remove(id);
Json o = Json::object();
o.set("deleted", Json(id));
return o;
}};
}
if (action == "create") {
co::DatasetUpload u;
u.name = text(b, "name", true);
u.type = text(b, "type", true);
u.filename = text(b, "filename", true);
std::string b64 = text(b, "data_base64");
if (!b64.empty()) {
if (!security::crypto::Base64::decode(b64, u.data)) bad("data_base64 is not valid base64");
} else {
u.data = text(b, "data", true);
}
u.keep_original_file = flag(b, "keep_original_file");
u.skip_malformed_input = flag(b, "skip_malformed_input");
u.validate();
Json req = Json::object();
req.set("name", Json(u.name));
req.set("type", Json(u.type));
req.set("filename", Json(u.filename));
req.set("bytes", Json(static_cast<int>(u.data.size())));
return Plan{req, "POST /v1/datasets", true, [api, u] {
Json o = Json::object();
o.set("id", Json(api->create(u)));
return o;
}};
}
bad("action must be list, get, usage, create or delete");
}
Plan batches(const Json& b) const {
std::string action = text(b, "action", true);
auto api = std::make_shared<co::Batches>(p_.client);
if (action == "list") {
auto size = number(b, "page_size");
std::string order = text(b, "order_by");
return Plan{Json::object(), "GET /v2/batches", false, [api, size, order] {
co::BatchList l = api->list(size, "", order);
Json arr = Json::array();
for (const co::Batch& x : l.batches) arr.push(batch_json(x));
Json o = Json::object();
o.set("batches", arr);
o.set("next_page_token", Json(l.page.next_page_token));
return o;
}};
}
if (action == "get") {
std::string id = need_id(b);
return Plan{id_only(id), "GET /v2/batches/{id}", false, [api, id] { return batch_json(api->get(id)); }};
}
if (action == "cancel") {
std::string id = need_id(b);
return Plan{id_only(id), "POST /v2/batches/{id}:cancel", true, [api, id] {
api->cancel(id);
Json o = Json::object();
o.set("canceled", Json(id));
return o;
}};
}
if (action == "create") {
std::string name = text(b, "name", true), ds = text(b, "input_dataset_id", true), model = text(b, "model", true);
co::Rules::model(model);
co::Rules::id("dataset", ds);
Json req = Json::object();
req.set("name", Json(name));
req.set("input_dataset_id", Json(ds));
req.set("model", Json(model));
return Plan{req, "POST /v2/batches", true, [api, name, ds, model] { return batch_json(api->create(name, ds, model)); }};
}
bad("action must be list, get, create or cancel");
}
Plan embedjobs(const Json& b) const {
std::string action = text(b, "action", true);
auto api = std::make_shared<co::EmbedJobs>(p_.client);
if (action == "list")
return Plan{Json::object(), "GET /v1/embed-jobs", false, [api] {
Json arr = Json::array();
for (const co::EmbedJob& j : api->list()) arr.push(job_json(j));
Json o = Json::object();
o.set("embed_jobs", arr);
return o;
}};
if (action == "get") {
std::string id = need_id(b);
return Plan{id_only(id), "GET /v1/embed-jobs/{id}", false, [api, id] { return job_json(api->get(id)); }};
}
if (action == "cancel") {
std::string id = need_id(b);
return Plan{id_only(id), "POST /v1/embed-jobs/{id}/cancel", true, [api, id] {
api->cancel(id);
Json o = Json::object();
o.set("cancelled", Json(id));
return o;
}};
}
if (action == "create") {
co::EmbedJobParams p;
std::string model = text(b, "model");
if (!model.empty()) p.model = model;
p.dataset_id = text(b, "dataset_id", true);
std::string it = text(b, "input_type");
if (!it.empty()) p.input_type = it;
p.name = text(b, "name");
p.embedding_types = strings(b, "embedding_types");
p.truncate = text(b, "truncate");
return Plan{p.to_json(), "POST /v1/embed-jobs", true, [api, p] {
Json o = Json::object();
o.set("job_id", Json(api->create(p)));
return o;
}};
}
bad("action must be list, get, create or cancel");
}
Plan connectors(const Json& b) const {
std::string action = text(b, "action", true);
auto api = std::make_shared<co::Connectors>(p_.client);
auto conn = [](const co::Connector& c) {
Json x = Json::object();
x.set("id", Json(c.id));
x.set("name", Json(c.name));
x.set("url", Json(c.url));
x.set("active", Json(c.active));
return x;
};
if (action == "list") {
auto limit = number(b, "limit");
return Plan{Json::object(), "GET /v1/connectors", false, [api, limit, conn] {
Json arr = Json::array();
for (const co::Connector& c : api->list(limit)) arr.push(conn(c));
Json o = Json::object();
o.set("connectors", arr);
return o;
}};
}
std::string id = action == "create" ? "" : need_id(b);
if (action == "get") return Plan{id_only(id), "GET /v1/connectors/{id}", false, [api, id, conn] { return conn(api->get(id)); }};
if (action == "delete")
return Plan{id_only(id), "DELETE /v1/connectors/{id}", true, [api, id] {
api->remove(id);
Json o = Json::object();
o.set("deleted", Json(id));
return o;
}};
if (action == "authorize")
return Plan{id_only(id), "POST /v1/connectors/{id}/oauth/authorize", true, [api, id] {
Json o = Json::object();
o.set("redirect_url", Json(api->oauth_authorize(id)));
return o;
}};
if (action == "create" || action == "update") {
co::ConnectorParams p;
p.name = text(b, "name");
p.url = text(b, "url");
p.description = text(b, "description");
p.excludes = strings(b, "excludes");
if (b.find("active")) p.active = flag(b, "active");
Json req = p.to_json(action == "create");
bool create = action == "create";
return Plan{req, create ? "POST /v1/connectors" : "PATCH /v1/connectors/{id}", true, [api, p, id, create, conn] {
return conn(create ? api->create(p) : api->update(id, p));
}};
}
bad("action must be list, get, create, update, delete or authorize");
}
Plan finetune(const Json& b) const {
std::string action = text(b, "action", true);
auto api = std::make_shared<co::FineTuning>(p_.client);
auto model = [](const co::FinetunedModel& m) {
Json x = Json::object();
x.set("id", Json(m.id));
x.set("name", Json(m.name));
x.set("status", Json(m.status));
return x;
};
if (action == "list") {
auto size = number(b, "page_size");
return Plan{Json::object(), "GET /v1/finetuning/finetuned-models", false, [api, size, model] {
auto page = api->list(size);
Json arr = Json::array();
for (const co::FinetunedModel& m : page.models) arr.push(model(m));
Json o = Json::object();
o.set("finetuned_models", arr);
o.set("next_page_token", Json(page.page.next_page_token));
return o;
}};
}
if (action == "create") {
std::string name = text(b, "name", true);
const Json* settings = b.find("settings");
if (!settings || !settings->is_object()) bad("settings must be an object");
Json s = *settings;
Json req = Json::object();
req.set("name", Json(name));
req.set("settings", s);
return Plan{req, "POST /v1/finetuning/finetuned-models", true, [api, name, s, model] { return model(api->create(name, s)); }};
}
std::string id = need_id(b);
if (action == "get") return Plan{id_only(id), "GET /v1/finetuning/finetuned-models/{id}", false, [api, id, model] { return model(api->get(id)); }};
if (action == "events")
return Plan{id_only(id), "GET .../{id}/events", false, [api, id] { return api->events(id); }};
if (action == "metrics")
return Plan{id_only(id), "GET .../{id}/training-step-metrics", false, [api, id] { return api->step_metrics(id); }};
if (action == "delete")
return Plan{id_only(id), "DELETE /v1/finetuning/finetuned-models/{id}", true, [api, id] {
api->remove(id);
Json o = Json::object();
o.set("deleted", Json(id));
return o;
}};
if (action == "update") {
std::string name = text(b, "name");
Json s;
if (const Json* settings = b.find("settings")) {
if (!settings->is_object()) bad("settings must be an object");
s = *settings;
}
if (name.empty() && s.is_null()) bad("give name or settings");
Json req = id_only(id);
if (!name.empty()) req.set("name", Json(name));
return Plan{req, "PATCH /v1/finetuning/finetuned-models/{id}", true, [api, id, name, s, model] { return model(api->update(id, name, s)); }};
}
bad("action must be list, get, create, update, delete, events or metrics");
}
private:
static Json model_json(const co::ModelInfo& m) {
Json x = Json::object();
x.set("name", Json(m.name));
x.set("context_length", Json(m.context_length));
x.set("deprecated", Json(m.deprecated));
Json ep = Json::array();
for (const std::string& e : m.endpoints) ep.push(Json(e));
x.set("endpoints", ep);
return x;
}
static Json dataset_json(const co::Dataset& d) {
Json x = Json::object();
x.set("id", Json(d.id));
x.set("name", Json(d.name));
x.set("type", Json(d.dataset_type));
x.set("validation_status", Json(d.validation_status));
x.set("parts", Json(static_cast<int>(d.parts.size())));
return x;
}
static Json batch_json(const co::Batch& b) {
Json x = Json::object();
x.set("id", Json(b.id));
x.set("name", Json(b.name));
x.set("status", Json(b.status));
x.set("terminal", Json(b.terminal()));
x.set("num_records", Json(static_cast<int>(b.num_records)));
return x;
}
static Json job_json(const co::EmbedJob& j) {
Json x = Json::object();
x.set("job_id", Json(j.job_id));
x.set("status", Json(j.status));
x.set("output_dataset_id", Json(j.output_dataset_id));
return x;
}
[[noreturn]] static void map(const llm::ApiError& e) {
if (e.http_status() == 503 && e.type() == "NOT_CONFIGURED")
throw fw::HttpError(503, "cohere_not_configured", e.message());
if (e.type() == "INVALID_ARGUMENT" && e.http_status() == 400 && e.request_id().empty())
throw fw::HttpError(422, "validation_error", e.message());
throw fw::HttpError(502, "cohere_error", std::to_string(e.http_status()) + " " + e.type() + ": " + e.message());
}
co::Platform p_;
};
} // namespace
void register_ai_cohere_demo(fw::Router& router, Services& services) {
(void)router;
(void)services;
auto routes = std::make_shared<CohereRoutes>(co::Platform::from_env());
fw::Environment* env = services.templates.get();
services.templates->add_template("ai_cohere.html", kPage);
services.add_feature("ai-cohere", "/ai/cohere");
router.get("/ai/cohere", "ai-cohere-page", [env](fw::Request req) {
(void)req;
fw::TemplateCtx ctx;
ctx["title"] = std::string("Cohere API");
return fw::Response(200, env->render("ai_cohere.html", ctx), "text/html; charset=utf-8");
});
router.get("/ai/cohere/status", "ai-cohere-status", [routes](fw::Request req) {
(void)req;
Json o = Json::object();
o.set("configured", Json(routes->platform().configured()));
o.set("credential_env", Json(co::Platform::kKeyEnv));
o.set("credential_env_alt", Json(co::Platform::kKeyEnvAlt));
o.set("base_url", Json(co::Platform::kHost));
o.set("calls", Json(static_cast<int>(co::Calls::count())));
Json models = Json::array();
for (std::size_t i = 0; i < co::ModelCatalog::count(); ++i) {
Json m = Json::object();
m.set("id", Json(co::ModelCatalog::at(i).id));
m.set("note", Json(co::ModelCatalog::at(i).note));
models.push(m);
}
o.set("models", models);
o.set("max_embed_texts", Json(static_cast<int>(co::CohereCaps::kMaxEmbedTexts)));
return json_out(o);
});
router.get("/ai/cohere/calls", "ai-cohere-calls", [](fw::Request req) {
(void)req;
Json arr = Json::array();
for (std::size_t i = 0; i < co::Calls::count(); ++i) {
const co::CallSpec& s = co::Calls::at(i);
Json row = Json::object();
row.set("http", Json(s.http_method));
row.set("path", Json(s.path));
row.set("operation", Json(s.operation_id));
arr.push(row);
}
Json o = Json::object();
o.set("calls", arr);
return json_out(o);
});
using Member = CohereRoutes::Planner;
CohereRoutes* r = routes.get();
auto add = [&](const char* path, const char* name, Plan (CohereRoutes::*m)(const Json&) const) {
Member planner = [r, m](const Json& b) { return (r->*m)(b); };
router.post(path, name, [routes, planner](fw::Request req) { return routes->run(req, planner); });
};
add("/ai/cohere/chat", "ai-cohere-chat", &CohereRoutes::chat);
add("/ai/cohere/stream", "ai-cohere-stream", &CohereRoutes::stream);
add("/ai/cohere/tools", "ai-cohere-tools", &CohereRoutes::tools);
add("/ai/cohere/legacy", "ai-cohere-legacy", &CohereRoutes::legacy);
add("/ai/cohere/embed", "ai-cohere-embed", &CohereRoutes::embed);
add("/ai/cohere/rerank", "ai-cohere-rerank", &CohereRoutes::rerank);
add("/ai/cohere/classify", "ai-cohere-classify", &CohereRoutes::classify);
add("/ai/cohere/tokenize", "ai-cohere-tokenize", &CohereRoutes::tokenize);
add("/ai/cohere/detokenize", "ai-cohere-detokenize", &CohereRoutes::detokenize);
add("/ai/cohere/parse", "ai-cohere-parse", &CohereRoutes::parse);
add("/ai/cohere/models", "ai-cohere-models", &CohereRoutes::models);
add("/ai/cohere/key", "ai-cohere-key", &CohereRoutes::key);
add("/ai/cohere/datasets", "ai-cohere-datasets", &CohereRoutes::datasets);
add("/ai/cohere/batches", "ai-cohere-batches", &CohereRoutes::batches);
add("/ai/cohere/embedjobs", "ai-cohere-embedjobs", &CohereRoutes::embedjobs);
add("/ai/cohere/connectors", "ai-cohere-connectors", &CohereRoutes::connectors);
add("/ai/cohere/finetune", "ai-cohere-finetune", &CohereRoutes::finetune);
}
} // namespace molo::example::app