Merge branch 'worktree-agent-a52dc99d769b245fb'

This commit is contained in:
2026-10-25 15:24:27 +01:00
36 changed files with 6045 additions and 3 deletions
+835 -3
View File
@@ -1,13 +1,845 @@
#include "../AiDemos.hpp" #include "../AiDemos.hpp"
#include <functional>
#include "../../fw/Router.hpp" #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 { 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 register_ai_cohere_demo(fw::Router& router, Services& services) {
(void)router; auto routes = std::make_shared<CohereRoutes>(co::Platform::from_env());
(void)services; 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 } // namespace molo::example::app
+65
View File
@@ -0,0 +1,65 @@
// Cohere page: thin client over /ai/cohere/* (all limits server side).
(function () {
"use strict";
var $ = function (id) { return document.getElementById(id); };
var out = $("co-out"), err = $("co-error");
var templates = {
"chat": { prompt: "Tell me about LLMs", max_tokens: 256, thinking: "disabled" },
"stream": { prompt: "Count to five.", max_tokens: 128 },
"tools": { prompt: "Weather in Paris?", tool_choice: "REQUIRED" },
"embed": { texts: ["hello world"], input_type: "search_document", embedding_types: ["float"] },
"rerank": { query: "capital of the United States", documents: ["Carson City is the capital of Nevada.", "Washington, D.C. is the capital of the United States."], top_n: 2 },
"classify": { inputs: ["win a free prize now"], examples: [
{ text: "free money", label: "spam" }, { text: "claim your prize", label: "spam" },
{ text: "meeting at noon", label: "ok" }, { text: "see attached notes", label: "ok" }] },
"tokenize": { text: "tokenize me! :D" },
"detokenize": { tokens: [10002, 2261, 2012, 8, 2792, 43] },
"parse": { model: "MODEL", image_url: "https://example.com/page.png", output_format: "markdown" },
"models": { action: "list", endpoint: "chat" },
"key": {},
"datasets": { action: "list" },
"batches": { action: "list" },
"embedjobs": { action: "list" },
"connectors": { action: "list" },
"finetune": { action: "list" },
"legacy": { action: "summarize", body: { text: "Paste at least a few sentences of text here to summarize." } }
};
var sel = $("co-route");
Object.keys(templates).forEach(function (k) {
var o = document.createElement("option");
o.value = k; o.textContent = k;
sel.appendChild(o);
});
function fill() { $("co-body").value = JSON.stringify(templates[sel.value], null, 1); }
sel.onchange = fill;
fill();
function send(confirm) {
var body;
try { body = JSON.parse($("co-body").value || "{}"); }
catch (e) { err.textContent = "Body is not valid JSON"; return; }
if (confirm) body.confirm = true;
err.textContent = "";
out.textContent = "...";
fetch("/ai/cohere/" + sel.value, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
}).then(function (res) {
return res.text().then(function (t) {
var j;
try { j = JSON.parse(t); } catch (e) { j = { message: t }; }
if (!res.ok) { err.textContent = j.message || ("HTTP " + res.status); out.textContent = ""; }
else { out.textContent = JSON.stringify(j, null, 2); }
});
}).catch(function (e) { err.textContent = String(e); out.textContent = ""; });
}
$("co-send").onclick = function () { send(false); };
$("co-confirm").onclick = function () { send(true); };
fetch("/ai/cohere/status").then(function (r) { return r.json(); }).then(function (s) {
$("co-config").textContent = s.configured
? "COHERE_API_KEY found. Calls are real; paid ones need confirm."
: "No COHERE_API_KEY: calls answer 503 until one is configured; dry runs still work.";
});
})();
+196
View File
@@ -0,0 +1,196 @@
# Cohere API platform - Specification
Owned directory: `src/vendors/cohere/`. Namespace `molo::cohere`. Depends on the
`src/llm` foundation (`ApiClient`, `Service`, `SseDecoder`, `ApiError`,
`Config`, `ChatModel`). Plans: `plans/17-llm-vendor-platforms.md`,
`plans/18-breadth.md`. Credential: `COHERE_API_KEY` via `llm::Config`
(`CO_API_KEY`, the official SDKs' name, is accepted as a fallback).
Tests: `tests/src/vendors/cohere/`. Demo: `example/app/ai/CohereDemo.cpp`,
routes `/ai/cohere`, script `example/assets/ai_cohere.js`, route tests
`tests/example/ai/cohere/`.
## 1. Scope
Every operation of Cohere's OpenAPI document (42 of 42, host
`https://api.cohere.com`, Bearer auth):
- Chat v2 (`POST /v2/chat`): messages (system, user with text and image parts,
assistant, tool), tools and `strict_tools`, `tool_choice`, documents and
citation options, JSON response format with schema, safety mode, thinking
(`token_budget`), sampling and penalties, logprobs, priority, streaming
(all eleven event types), `Conversation` with a capped tool loop.
- Legacy v1 that remains in the spec: chat, generate, summarize (Json bodies
checked against the call table), embed, rerank, classify.
- Embed v2 (texts, images, mixed `inputs`, `embedding_types`, `output_dimension`),
Rerank v2 (`relevant_spans`, `max_tokens_per_doc`), Tokenize, Detokenize,
Parse (`/v2/parse`), audio transcription (`/v2/audio/transcriptions`, multipart).
- Datasets (multipart upload, list, get, usage, delete), Embed jobs, Batches
(`/v2/batches`, cancel), Connectors (spec marks them deprecated), Fine-tuning
(spec marks it deprecated: list, create, get, update, delete, events,
training-step metrics), Models (list, get), `check-api-key`.
- `CohereChatModel : llm::ChatModel` over Chat v2.
Not here, said plainly: the spec defines cluster-job schemas
(`CreateClusterJobRequest` and friends) and `FeedbackResponse` as components,
but no path uses them, so there is nothing to call.
## 2. Module map (one class per concern)
| File | Concern |
|---|---|
| `caps.h` | Static ceilings, never autotuned |
| `calls.h/.cpp` | THE call table (42 rows) and the schema-field table; `render` (id and query validation), `check_body` |
| `rules.h/.cpp` | Input rules (model, ids, ranges, enums, URLs); all throw `ApiError::invalid` before any traffic |
| `types.h/.cpp` | Tolerant readers `J`, `TokenUsage`, `Page`, `ModelInfo`, `Batch`, `EmbedJob`, `Dataset`, `Connector`, `FinetunedModel` |
| `platform.h/.cpp` | Host, Bearer auth, `X-Client-Name: molo` header, key lookup |
| `chat_params.h/.cpp` | `Messages` builders, `ToolDef`, `ChatParams` (validate + body) |
| `chat_result.h/.cpp` | `ChatResult`, `StreamDelta`, `ChatStreamAccumulator` |
| `chat.h/.cpp` | `Chat` (create, stream, legacy v1), `Conversation` (history, tool loop) |
| `chat_model.h/.cpp` | `CohereChatModel : llm::ChatModel` and its documented mapping |
| `inference.h/.cpp` | `Embeddings`, `Rerank`, `Classify`, `Tokenizer`, `Legacy` |
| `resources.h/.cpp` | `Account`, `Datasets`, `Batches`, `EmbedJobs`, `Connectors`, `FineTuning`, `Documents` |
| `catalog.h/.cpp` | Model names from the docs page, defaults |
| `tools/check_spec.py` | Spec checker (see Verified against spec) |
## 3. Static caps
| Knob | Value | Origin |
|---|---|---|
| Embed texts / images / inputs per call | 96 | spec `maxItems` (texts); ours for the rest |
| Classify inputs | 96 | spec `maxItems` |
| Tokenize text / detokenize tokens | 65536 | spec `maxLength` / `maxItems` |
| Chat stop sequences | 5 | spec description |
| `k` | 0..500, `p` 0.01..0.99, `temperature` 0..1, `priority` 0..999, penalties 0..1 | spec |
| Page size | 1..1000 | spec description |
| Messages / tools / documents | 4096 / 256 / 1000 | ours |
| Stream events | 200000 | ours |
| List walk | 20 pages, 5000 items | ours; truncation is reported (`Page::truncated`) |
| Upload (datasets, audio) | 256 MiB | ours (llm cap is 512 MiB) |
| Tool loop rounds | 16 | ours |
| Dataset validation polls | 60, wait supplied by the caller | ours |
| Body / response / stream bytes | `llm::Caps` | foundation |
## 4. Contracts
- No key: `configured()` is false and every call throws `NotConfiguredError`
(503, `NOT_CONFIGURED`) before any network traffic. The key travels in the
`Authorization` header only.
- Every request body is built from typed params or checked with
`Calls::check_body`: a field that is not in the call table is rejected
loudly. Path ids go through `Service::valid_id`; query names must be in the row.
- Destructive or cancelling calls (`Datasets::remove`, `Batches::cancel`,
`EmbedJobs::cancel`, `Connectors::remove`, `FineTuning::remove`) are explicit
methods with no defaults. Calls that cost money or train a model
(`FineTuning::create`, `Batches::create`, `EmbedJobs::create`) validate their
settings first.
- A stream without `message-end` throws `502 STREAM_TRUNCATED`; a `message-end`
with `delta.error` throws `502 STREAM_ERROR`; a JSON error body in place of a
stream throws (SSE decoder).
- Tool arguments arrive as the model's JSON text; `ToolCallRecord::arguments_valid()`
tells, and `CohereChatModel` keeps invalid text as `{"_invalid_json": "..."}`.
- `CohereChatModel` mapping (also in `chat_model.h`): `tool_choice` required/none
-> `REQUIRED`/`NONE`, a tool name is rejected (Cohere cannot force one tool);
`json_mode`/`response_schema` -> `response_format` json_object (+ `json_schema`);
`reasoning_effort` none/off/disabled -> thinking disabled, anything else ->
enabled; finish reasons COMPLETE and STOP_SEQUENCE -> stop, MAX_TOKENS ->
length, TOOL_CALL -> tool_calls; usage from `usage.tokens` with `billed_units`
as the fallback.
- Datasets: `keep_fields` and `optional_fields` are repeated query parameters
(what the official Python SDK does by passing a sequence to httpx).
- `Datasets::await_validation` never sleeps; the caller passes the wait.
## 5. Example routes (`/ai/cohere`)
GET: page, `/status`, `/calls` (no credential). POST: `chat`, `stream`,
`tools`, `legacy`, `embed`, `rerank`, `classify`, `tokenize`, `detokenize`,
`parse`, `models`, `key`, `datasets`, `batches`, `embedjobs`, `connectors`,
`finetune` (resource routes take an `action`). 422 on bad input, 503
`cohere_not_configured` without a key, 502 `cohere_error` for vendor errors.
Chat, stream, tools, embed, rerank, classify, parse, legacy and every create,
update, cancel and delete are dry runs unless the body has `"confirm":true`.
Reads and tokenize run directly once a key exists.
## Sources
1. `cohere-openapi.yaml` in `cohere-ai/cohere-developer-experience` (branch
`main`, fetched 2026-10-25, 1,140,155 bytes, ETag
`148fca0f27bb02cdf86b2eb95b7d9a02efa1bca0263cae8d6c9dcc571cb17d60`),
https://raw.githubusercontent.com/cohere-ai/cohere-developer-experience/main/cohere-openapi.yaml .
OpenAPI 3.1, 42 operations, 191 component schemas. Its `x-fern-examples` and
`x-examples` are the fixtures in `tests/src/vendors/cohere/fixtures.h`.
2. Official Python SDK `cohere-ai/cohere-python` (`src/cohere/*/raw_client.py`,
fetched 2026-10-25): 31 path templates match the spec; the repeated query
parameter behaviour for datasets comes from here.
3. Docs model list https://docs.cohere.com/docs/models (read 2026-10-25): model
names, status and limits in `catalog.cpp`.
4. Live unauthenticated probes (below).
## Verified against spec
`python3 -I src/vendors/cohere/tools/check_spec.py` (2026-10-25, against the
fetched document): `cohere spec ok: 42 calls of 42 spec operations (exclusions: 0),
75 schema rows, 263 fields`. The checker parses the YAML with a small stdlib
reader (compared with PyYAML on the whole file: identical except for long flow
arrays inside examples, which it does not use). It fails on a method+path or
operationId missing from the spec, a body field or query parameter we send that
the operation does not have, a spec-required field missing from the row, a
schema field we read that is not a property (dotted paths descend into nested
objects and arrays), a response name that is neither a component nor an
operationId, and any spec operation that is neither in the table nor in the
(empty) exclusion list. Negative runs with a wrong field, a missing required
field, a wrong response name and a wrong schema field each fail as expected.
## Verified live
Unauthenticated probes, 2026-10-25, no credential used:
- TLS and DNS for `api.cohere.com` work (HTTP/2).
- `POST /v2/chat` with no key: 401 `{"id":"...","message":"no api key supplied"}`.
- `GET /v1/models` and `POST /v2/chat` with an invalid bearer: 401
`{"id":"...","message":"Incorrect API key provided: ***...robe. You can find your API key at https://dashboard.cohere.com/api-keys."}`.
- `POST /v1/check-api-key` with an invalid bearer: HTTP 200 `{"valid":false}`.
- All 42 documented method+path pairs, called without a key: 42 x 401. A made-up
path (`GET /v1/nothing-here`) also answers 401, so the gateway authenticates
before routing and a 401 does NOT prove a route exists. Existence is verified
against the spec and the SDK only.
- Opt-in tests: `MOLO_LIVE_NET=1` runs the invalid-key probes
(`cohere_live.invalid_key_probes` passed on 2026-10-25); with `COHERE_API_KEY`
also key check, chat model list and ONE tiny chat; `MOLO_LIVE_PAID=1` adds one
embed and one rerank. Without them each prints a loud SKIP. Never run so far
with a key (none exists here).
## Unverified
No success response from Cohere has been seen. Everything below is inferred:
- Real reply bodies for models list/get, datasets, batches, embed jobs,
connectors, fine-tuning, parse, transcription and generate. Tests use minimal
documents labelled SHAPE, built from the OpenAPI schemas.
- Streaming of tool calls and citations beyond the two documented transcripts
(text, tools). Citation, thinking and logprob stream events are handled from
the schema.
- That `keep_fields`/`optional_fields` are repeated query parameters on the wire.
- `X-Client-Name` is optional in the spec; we send `molo`.
- Whether `command-a-03-2025`, `embed-v4.0`, `rerank-v3.5` stay valid defaults;
the docs page lists them (2026-10-25) and the live list is the truth.
- Tokenize and detokenize pricing and whether they need a model that supports
them; the demo defaults to `command-a-03-2025`.
- Spec says `GET /v1/models` pages with `page_size` up to 1000 (our walk uses 1000).
- Classify without a fine-tuned model: the 2-example minimum is our guard, not a
documented rule. The endpoint is marked deprecated in the spec.
- Behaviour of deprecated surfaces (connectors, fine-tuning, classify, generate,
summarize): present in the spec, not exercised.
## Gaps
- `src/llm`: `ApiClient::stream` is POST-only; `post_multipart` parses JSON replies
only; no per-call timeout; `ApiError` keeps no `Retry-After`. Cohere's 429
semantics (trial key limits) are therefore not surfaced beyond the status.
- No `CO-Api-Key` style header or v1 streaming (`/v1/chat` stream, `/v1/generate`
stream use a different event format); both are rejected by `stream:true` on
purpose.
- Embed results are kept as `Json`; typed accessors exist for floats only.
- `FineTuning` settings and connector OAuth are passed as `Json` (validated for
the required keys only).
- Credential console: https://dashboard.cohere.com/api-keys (named in Cohere's own
401 body, 2026-10-25). Owner action: add the row to `src/llm/SPEC.md` section 5.
+214
View File
@@ -0,0 +1,214 @@
#include "vendors/cohere/calls.h"
#include <cstring>
#include <set>
#include "clients/url.h"
#include "llm/api_client.h"
#include "llm/error.h"
namespace molo::cohere {
// CALLS-BEGIN (parsed by tools/check_spec.py; one row per line, strings in order:
// http method, path, operationId, body fields, query params, response schema)
static const CallSpec kCalls[] = {
{Call::kChatV1, "POST", "/v1/chat", "chat", "message,model,stream,preamble,chat_history,conversation_id,prompt_truncation,connectors,search_queries_only,documents,citation_quality,temperature,max_tokens,max_input_tokens,k,p,seed,stop_sequences,frequency_penalty,presence_penalty,raw_prompting,tool_results,tools,force_single_step,response_format,safety_mode", "", "chat"},
{Call::kChat, "POST", "/v2/chat", "chatv2", "model,messages,stream,tools,strict_tools,documents,citation_options,response_format,safety_mode,max_tokens,stop_sequences,temperature,seed,frequency_penalty,presence_penalty,k,p,logprobs,tool_choice,thinking,priority", "", "ChatResponseV2"},
{Call::kParse, "POST", "/v2/parse", "parsev2", "model,document,output_format", "", "ParseResponse"},
{Call::kGenerate, "POST", "/v1/generate", "generate", "model,prompt,stream,num_generations,max_tokens,truncate,temperature,seed,preset,end_sequences,stop_sequences,k,p,frequency_penalty,presence_penalty,return_likelihoods,raw_prompting", "", "Generation"},
{Call::kEmbedV1, "POST", "/v1/embed", "embed", "texts,images,model,input_type,embedding_types,truncate", "", "embed"},
{Call::kEmbed, "POST", "/v2/embed", "embedv2", "model,input_type,texts,images,inputs,max_tokens,output_dimension,embedding_types,truncate,priority", "", "EmbedByTypeResponse"},
{Call::kTranscribe, "POST", "/v2/audio/transcriptions", "create-transcription", "model,language,temperature,file", "", "create-transcription"},
{Call::kBatchList, "GET", "/v2/batches", "ListBatches", "", "page_size,page_token,order_by", "ListBatchesResponse"},
{Call::kBatchCreate, "POST", "/v2/batches", "CreateBatch", "name,input_dataset_id,model", "", "CreateBatchResponse"},
{Call::kBatchGet, "GET", "/v2/batches/{id}", "GetBatch", "", "", "GetBatchResponse"},
{Call::kBatchCancel, "POST", "/v2/batches/{id}:cancel", "CancelBatch", "", "", "CancelBatchResponse"},
{Call::kEmbedJobCreate, "POST", "/v1/embed-jobs", "create-embed-job", "model,dataset_id,input_type,name,embedding_types,truncate", "", "CreateEmbedJobResponse"},
{Call::kEmbedJobList, "GET", "/v1/embed-jobs", "list-embed-jobs", "", "", "ListEmbedJobResponse"},
{Call::kEmbedJobGet, "GET", "/v1/embed-jobs/{id}", "get-embed-job", "", "", "EmbedJob"},
{Call::kEmbedJobCancel, "POST", "/v1/embed-jobs/{id}/cancel", "cancel-embed-job", "", "", ""},
{Call::kRerankV1, "POST", "/v1/rerank", "rerank", "model,query,documents,top_n,rank_fields,return_documents,max_chunks_per_doc", "", "rerank"},
{Call::kRerank, "POST", "/v2/rerank", "rerankv2", "model,query,documents,top_n,max_tokens_per_doc,relevant_spans,priority", "", "rerankv2"},
{Call::kClassify, "POST", "/v1/classify", "classify", "inputs,examples,model,preset,truncate", "", "classify"},
{Call::kDatasetCreate, "POST", "/v1/datasets", "create-dataset", "data,eval_data", "name,type,keep_original_file,skip_malformed_input,keep_fields,optional_fields,text_separator,csv_delimiter", "create-dataset"},
{Call::kDatasetList, "GET", "/v1/datasets", "list-datasets", "", "datasetType,before,after,limit,offset,validationStatus", "list-datasets"},
{Call::kDatasetUsage, "GET", "/v1/datasets/usage", "get-dataset-usage", "", "", "get-dataset-usage"},
{Call::kDatasetGet, "GET", "/v1/datasets/{id}", "get-dataset", "", "", "get-dataset"},
{Call::kDatasetDelete, "DELETE", "/v1/datasets/{id}", "delete-dataset", "", "", ""},
{Call::kSummarize, "POST", "/v1/summarize", "summarize", "text,length,format,model,extractiveness,temperature,additional_command", "", "summarize"},
{Call::kTokenize, "POST", "/v1/tokenize", "tokenize", "text,model", "", "tokenize"},
{Call::kDetokenize, "POST", "/v1/detokenize", "detokenize", "tokens,model", "", "detokenize"},
{Call::kConnectorList, "GET", "/v1/connectors", "list-connectors", "", "limit,offset", "ListConnectorsResponse"},
{Call::kConnectorCreate, "POST", "/v1/connectors", "create-connector", "name,url,description,excludes,oauth,active,continue_on_failure,service_auth", "", "CreateConnectorResponse"},
{Call::kConnectorGet, "GET", "/v1/connectors/{id}", "get-connector", "", "", "GetConnectorResponse"},
{Call::kConnectorUpdate, "PATCH", "/v1/connectors/{id}", "update-connector", "name,url,excludes,oauth,active,continue_on_failure,service_auth", "", "UpdateConnectorResponse"},
{Call::kConnectorDelete, "DELETE", "/v1/connectors/{id}", "delete-connector", "", "", ""},
{Call::kConnectorAuthorize, "POST", "/v1/connectors/{id}/oauth/authorize", "oAuthAuthorize-connector", "", "after_token_redirect", "OAuthAuthorizeResponse"},
{Call::kModelGet, "GET", "/v1/models/{model}", "get-model", "", "", "GetModelResponse"},
{Call::kModelList, "GET", "/v1/models", "list-models", "", "page_size,page_token,endpoint,default_only", "ListModelsResponse"},
{Call::kCheckApiKey, "POST", "/v1/check-api-key", "checkAPIKey", "", "", "checkAPIKey"},
{Call::kFinetuneList, "GET", "/v1/finetuning/finetuned-models", "ListFinetunedModels", "", "page_size,page_token,order_by", "ListFinetunedModelsResponse"},
{Call::kFinetuneCreate, "POST", "/v1/finetuning/finetuned-models", "CreateFinetunedModel", "name,settings", "", "CreateFinetunedModelResponse"},
{Call::kFinetuneUpdate, "PATCH", "/v1/finetuning/finetuned-models/{id}", "UpdateFinetunedModel", "name,settings", "", "UpdateFinetunedModelResponse"},
{Call::kFinetuneGet, "GET", "/v1/finetuning/finetuned-models/{id}", "GetFinetunedModel", "", "", "GetFinetunedModelResponse"},
{Call::kFinetuneDelete, "DELETE", "/v1/finetuning/finetuned-models/{id}", "DeleteFinetunedModel", "", "", ""},
{Call::kFinetuneEvents, "GET", "/v1/finetuning/finetuned-models/{finetuned_model_id}/events", "ListEvents", "", "page_size,page_token,order_by", "ListEventsResponse"},
{Call::kFinetuneMetrics, "GET", "/v1/finetuning/finetuned-models/{finetuned_model_id}/training-step-metrics", "ListTrainingStepMetrics", "", "page_size,page_token", "ListTrainingStepMetricsResponse"},
};
// CALLS-END
// SCHEMAS-BEGIN (parsed by tools/check_spec.py; one row per line: schema, fields)
static const SchemaFields kSchemas[] = {
{"ChatResponseV2", "id,finish_reason,message,usage,logprobs"},
{"AssistantMessageResponse", "role,tool_calls,tool_plan,content,citations"},
{"ChatTextContent", "type,text"},
{"ChatThinkingContent", "type,thinking"},
{"ToolCallV2", "id,type,function"},
{"ToolCallV2.function", "name,arguments"},
{"Usage", "billed_units,tokens,cached_tokens"},
{"Usage.billed_units", "input_tokens,output_tokens,search_units,classifications"},
{"Usage.tokens", "input_tokens,output_tokens"},
{"Citation", "start,end,text,sources,content_index,type"},
{"ChatMessageStartEvent", "type,id,delta"},
{"ChatContentStartEvent", "type,index,delta"},
{"ChatContentStartEvent.delta.message.content", "type,text,thinking"},
{"ChatContentDeltaEvent", "type,index,delta,logprobs"},
{"ChatContentDeltaEvent.delta.message.content", "text,thinking"},
{"ChatContentEndEvent", "type,index"},
{"ChatToolPlanDeltaEvent", "type,delta"},
{"ChatToolPlanDeltaEvent.delta.message", "tool_plan"},
{"ChatToolCallStartEvent", "type,index,delta"},
{"ChatToolCallStartEvent.delta.message", "tool_calls"},
{"ChatToolCallDeltaEvent", "type,index,delta"},
{"ChatToolCallDeltaEvent.delta.message.tool_calls.function", "arguments"},
{"ChatToolCallEndEvent", "type,index"},
{"CitationStartEvent", "type,index,delta"},
{"CitationStartEvent.delta.message", "citations"},
{"ChatMessageEndEvent", "type,id,delta"},
{"ChatMessageEndEvent.delta", "error,finish_reason,usage"},
{"chat", "text,generation_id,response_id,citations,documents,is_search_required,search_queries,search_results,finish_reason,tool_calls,chat_history,meta"},
{"ParseResponse", "id,pages,meta,finish_reason"},
{"Generation", "id,prompt,generations,meta"},
{"embed", "id,embeddings,texts,images,meta,response_type"},
{"EmbedByTypeResponse", "id,embeddings,texts,images,meta,response_type"},
{"EmbedByTypeResponse.embeddings", "float,int8,uint8,binary,ubinary,base64"},
{"ApiMeta", "api_version,billed_units,tokens,cached_tokens,warnings"},
{"create-transcription", "text"},
{"ListBatchesResponse", "batches,next_page_token"},
{"Batch", "id,name,status,created_at,updated_at,input_dataset_id,output_dataset_id,model,input_tokens,output_tokens,num_records,num_successful_records,num_failed_records,status_reason,creator_id,org_id"},
{"CreateBatchResponse", "batch"},
{"GetBatchResponse", "batch"},
{"CreateEmbedJobResponse", "job_id,meta"},
{"ListEmbedJobResponse", "embed_jobs"},
{"EmbedJob", "job_id,name,status,created_at,input_dataset_id,output_dataset_id,model,truncate,meta"},
{"rerank", "id,results,meta"},
{"rerank.results", "index,relevance_score,document"},
{"rerankv2", "id,results,meta"},
{"rerankv2.results", "index,relevance_score,relevant_spans"},
{"classify", "id,classifications,meta"},
{"classify.classifications", "id,input,prediction,predictions,confidence,confidences,labels,classification_type"},
{"create-dataset", "id"},
{"list-datasets", "datasets"},
{"get-dataset", "dataset"},
{"get-dataset-usage", "organization_usage"},
{"Dataset", "id,name,created_at,updated_at,dataset_type,validation_status,validation_error,validation_warnings,dataset_parts,required_fields,preserve_fields"},
{"DatasetPart", "id,name,url,index,size_bytes,num_rows,original_url,samples"},
{"summarize", "id,summary,meta"},
{"tokenize", "tokens,token_strings,meta"},
{"detokenize", "text,meta"},
{"ListConnectorsResponse", "connectors,total_count"},
{"Connector", "id,organization_id,name,url,created_at,updated_at,excludes,auth_type,oauth,auth_status,active,continue_on_failure"},
{"CreateConnectorResponse", "connector"},
{"GetConnectorResponse", "connector"},
{"UpdateConnectorResponse", "connector"},
{"OAuthAuthorizeResponse", "redirect_url"},
{"GetModelResponse", "name,is_deprecated,endpoints,finetuned,context_length,tokenizer_url,default_endpoints,features,sampling_defaults"},
{"ListModelsResponse", "models,next_page_token"},
{"checkAPIKey", "valid,organization_id,owner_id"},
{"ListFinetunedModelsResponse", "finetuned_models,next_page_token,total_size"},
{"FinetunedModel", "id,name,creator_id,organization_id,settings,status,created_at,updated_at,completed_at,last_used"},
{"CreateFinetunedModelResponse", "finetuned_model"},
{"UpdateFinetunedModelResponse", "finetuned_model"},
{"GetFinetunedModelResponse", "finetuned_model"},
{"ListEventsResponse", "events,next_page_token,total_size"},
{"Event", "user_id,status,created_at"},
{"ListTrainingStepMetricsResponse", "step_metrics,next_page_token"},
{"TrainingStepMetrics", "created_at,step_number,metrics"},
};
// SCHEMAS-END
const CallSpec& Calls::spec_of(Call call) {
for (const CallSpec& s : kCalls)
if (s.call == call) return s;
throw llm::ApiError::invalid("cohere", "unknown call");
}
clients::Method Calls::method_of(Call call) {
const char* m = spec_of(call).http_method;
if (!std::strcmp(m, "GET")) return clients::Method::kGet;
if (!std::strcmp(m, "POST")) return clients::Method::kPost;
if (!std::strcmp(m, "PATCH")) return clients::Method::kPatch;
return clients::Method::kDelete;
}
std::size_t Calls::count() { return sizeof(kCalls) / sizeof(kCalls[0]); }
const CallSpec& Calls::at(std::size_t index) {
if (index >= count()) throw llm::ApiError::invalid("cohere", "call index out of range");
return kCalls[index];
}
std::size_t Calls::schema_count() { return sizeof(kSchemas) / sizeof(kSchemas[0]); }
const SchemaFields& Calls::schema_at(std::size_t index) {
if (index >= schema_count()) throw llm::ApiError::invalid("cohere", "schema index out of range");
return kSchemas[index];
}
namespace {
std::set<std::string> split(const char* list) {
std::set<std::string> out;
std::string cur;
for (const char* p = list;; ++p) {
if (*p == ',' || *p == '\0') {
if (!cur.empty()) out.insert(cur);
cur.clear();
if (*p == '\0') break;
} else {
cur += *p;
}
}
return out;
}
} // namespace
std::string Calls::render(Call call, const std::string& id,
const Query& query) {
const CallSpec& s = spec_of(call);
std::string path = s.path;
auto open = path.find('{');
if (open != std::string::npos) {
auto close = path.find('}', open);
if (!llm::Service::valid_id(id)) throw llm::ApiError::invalid("cohere", "bad id");
path.replace(open, close - open + 1, id);
} else if (!id.empty()) {
throw llm::ApiError::invalid("cohere", "this call takes no id");
}
std::set<std::string> allowed = split(s.query_params);
std::string qs;
for (const auto& [k, v] : query) {
if (!allowed.count(k)) throw llm::ApiError::invalid("cohere", "query parameter not in the call table: " + k);
qs += qs.empty() ? '?' : '&';
qs += clients::Url::encode_component(k) + "=" + clients::Url::encode_component(v);
}
return path + qs;
}
void Calls::check_body(Call call, const clients::body::Json& body) {
if (!body.is_object()) throw llm::ApiError::invalid("cohere", "body must be a JSON object");
std::set<std::string> allowed = split(spec_of(call).body_fields);
for (const auto& [k, v] : body.as_object())
if (!allowed.count(k)) throw llm::ApiError::invalid("cohere", "field not in the call table: " + k);
}
} // namespace molo::cohere
+66
View File
@@ -0,0 +1,66 @@
#pragma once
// The ONE table of REST calls this module makes. tools/check_spec.py checks
// every row against Cohere's OpenAPI document (cohere-openapi.yaml) and fails
// on any spec operation that has no row. Code builds every path, query string
// and body-field check from this table, never from a literal.
#include <cstddef>
#include <string>
#include <utility>
#include <vector>
#include <string>
#include "clients/body/json.h"
#include "clients/request.h"
namespace molo::cohere {
enum class Call {
kChatV1, kChat, kParse, kGenerate, kEmbedV1, kEmbed, kTranscribe,
kBatchList, kBatchCreate, kBatchGet, kBatchCancel,
kEmbedJobCreate, kEmbedJobList, kEmbedJobGet, kEmbedJobCancel,
kRerankV1, kRerank, kClassify,
kDatasetCreate, kDatasetList, kDatasetUsage, kDatasetGet, kDatasetDelete,
kSummarize, kTokenize, kDetokenize,
kConnectorList, kConnectorCreate, kConnectorGet, kConnectorUpdate, kConnectorDelete,
kConnectorAuthorize,
kModelGet, kModelList, kCheckApiKey,
kFinetuneList, kFinetuneCreate, kFinetuneUpdate, kFinetuneGet, kFinetuneDelete,
kFinetuneEvents, kFinetuneMetrics,
};
struct CallSpec {
Call call;
const char* http_method; // GET POST PATCH DELETE
const char* path; // from the host; one {placeholder} at most
const char* operation_id; // operationId in the OpenAPI document
const char* body_fields; // comma list: request body fields we send (JSON or multipart)
const char* query_params; // comma list: query parameters we send
const char* response; // component schema name, or the operationId of an inline 200 body
};
struct SchemaFields {
const char* schema; // component name, operationId (inline 200 body), optionally ".a.b" property path
const char* fields; // comma list: fields we read
};
using Query = std::vector<std::pair<std::string, std::string>>; // repeat a name for list values
struct Calls {
static const CallSpec& spec_of(Call call);
static clients::Method method_of(Call call);
static std::size_t count();
static const CallSpec& at(std::size_t index);
static std::size_t schema_count();
static const SchemaFields& schema_at(std::size_t index);
// Path with the placeholder replaced by a validated id and the query
// parameters appended (percent-encoded). A query name that is not in the
// row, or an id that is not a valid path segment, throws ApiError::invalid.
static std::string render(Call call, const std::string& id = "",
const Query& query = {});
// Throws ApiError::invalid when `body` has a field the row does not declare.
static void check_body(Call call, const clients::body::Json& body);
};
} // namespace molo::cohere
+37
View File
@@ -0,0 +1,37 @@
#pragma once
// Static ceilings for the Cohere module. Never autotuned. "Documented" caps
// come from cohere-openapi.yaml (see SPEC.md ## Sources); the rest are ours.
#include <cstddef>
namespace molo::cohere {
struct CohereCaps {
// Documented (OpenAPI constraints).
static constexpr std::size_t kMaxEmbedTexts = 96; // /v2/embed texts maxItems
static constexpr std::size_t kMaxClassifyInputs = 96; // /v1/classify inputs maxItems
static constexpr std::size_t kMaxTokenizeChars = 65536; // /v1/tokenize text maxLength
static constexpr std::size_t kMaxDetokenizeTokens = 65536;
static constexpr std::size_t kMaxStopSequences = 5; // chat stop_sequences ("up to 5")
static constexpr int kMaxTopK = 500;
static constexpr int kMaxPriority = 999;
static constexpr int kMaxPageSize = 1000; // batches, models, finetuning pages
// Our own caps.
static constexpr std::size_t kMaxMessages = 4096;
static constexpr std::size_t kMaxTools = 256;
static constexpr std::size_t kMaxDocuments = 1000; // chat documents, rerank documents
static constexpr std::size_t kMaxEmbedInputs = 96;
static constexpr std::size_t kMaxStreamEvents = 200000;
static constexpr std::size_t kMaxListPages = 20; // list_all page walks
static constexpr std::size_t kMaxItemsReturned = 5000;
static constexpr std::size_t kMaxUploadBytes = 256u * 1024u * 1024u; // dataset parts
static constexpr std::size_t kMaxFilenameBytes = 512;
static constexpr std::size_t kMaxIdChars = 256;
static constexpr std::size_t kMaxNameChars = 256;
static constexpr std::size_t kMaxModelChars = 128;
static constexpr int kDatasetListMaxLimit = 1000;
static constexpr int kConnectorListMaxLimit = 1000;
};
} // namespace molo::cohere
+43
View File
@@ -0,0 +1,43 @@
#include "vendors/cohere/catalog.h"
#include "llm/error.h"
namespace molo::cohere {
namespace {
constexpr ModelName kModels[] = {
{"command-a-plus-05-2026", ModelKind::kChat, ModelStatus::kLive, "MoE, text and images, 128k context, 64k output"},
{"command-a-03-2025", ModelKind::kChat, ModelStatus::kLive, "text, 256k context, 8k output, tool use and RAG"},
{"command-a-reasoning-08-2025", ModelKind::kChat, ModelStatus::kLive, "reasoning model"},
{"command-a-vision-07-2025", ModelKind::kChat, ModelStatus::kLive, "image input"},
{"command-a-translate-08-2025", ModelKind::kChat, ModelStatus::kLive, "translation"},
{"command-r7b-12-2024", ModelKind::kChat, ModelStatus::kLive, "small, 128k context, 4k output"},
{"command-r-plus-08-2024", ModelKind::kChat, ModelStatus::kLive, "128k context, 4k output"},
{"command-r-08-2024", ModelKind::kChat, ModelStatus::kLive, "128k context, 4k output"},
{"command-r-03-2024", ModelKind::kChat, ModelStatus::kDeprecated, "deprecated Sept 15, 2025"},
{"command-r-plus-04-2024", ModelKind::kChat, ModelStatus::kDeprecated, "deprecated Sept 15, 2025"},
{"embed-v5.0-pro", ModelKind::kEmbed, ModelStatus::kLive, "text, images, mixed; dimensions 256..2048 (default 2048)"},
{"embed-v5.0-fast", ModelKind::kEmbed, ModelStatus::kLive, "faster, lighter embed-v5.0-pro"},
{"embed-v4.0", ModelKind::kEmbed, ModelStatus::kLive, "text and images"},
{"rerank-v5.0-pro", ModelKind::kRerank, ModelStatus::kLive, "highest quality, 32k context, calibrated scores"},
{"rerank-v5.0-fast", ModelKind::kRerank, ModelStatus::kLive, "low latency"},
{"rerank-v4.0-pro", ModelKind::kRerank, ModelStatus::kLive, "listed on the models page"},
{"rerank-v4.0-fast", ModelKind::kRerank, ModelStatus::kLive, "listed on the models page"},
{"rerank-v3.5", ModelKind::kRerank, ModelStatus::kLive, "multilingual, listed on the models page"},
};
} // namespace
std::size_t ModelCatalog::count() { return sizeof(kModels) / sizeof(kModels[0]); }
const ModelName& ModelCatalog::at(std::size_t index) {
if (index >= count()) throw llm::ApiError::invalid("cohere", "model index out of range");
return kModels[index];
}
ModelStatus ModelCatalog::status_of(const std::string& id) {
for (const ModelName& m : kModels)
if (id == m.id) return m.status;
return ModelStatus::kUnknown;
}
} // namespace molo::cohere
+31
View File
@@ -0,0 +1,31 @@
#pragma once
// Model names the docs page https://docs.cohere.com/docs/models lists (read
// 2026-10-25). The live GET /v1/models list is the truth; this table only
// explains names and gives the defaults the module uses.
#include <cstddef>
#include <string>
namespace molo::cohere {
enum class ModelKind { kChat, kEmbed, kRerank };
enum class ModelStatus { kLive, kDeprecated, kUnknown };
struct ModelName {
const char* id;
ModelKind kind;
ModelStatus status;
const char* note;
};
struct ModelCatalog {
static std::size_t count();
static const ModelName& at(std::size_t index);
static ModelStatus status_of(const std::string& id);
static const char* default_chat() { return "command-a-03-2025"; }
static const char* default_embed() { return "embed-v4.0"; }
static const char* default_rerank() { return "rerank-v3.5"; }
};
} // namespace molo::cohere
+83
View File
@@ -0,0 +1,83 @@
#include "vendors/cohere/chat.h"
#include "vendors/cohere/caps.h"
#include "vendors/cohere/rules.h"
namespace molo::cohere {
ChatResult Chat::create(const ChatParams& params) {
Json doc = client_->post(Calls::render(Call::kChat), params.to_json(false));
return ChatResult::parse(doc);
}
ChatResult Chat::stream(const ChatParams& params, const DeltaSink& on_delta) {
Json body = params.to_json(true);
ChatStreamAccumulator acc;
bool stopped = false;
std::size_t events = 0;
client_->stream(Calls::render(Call::kChat), body, [&](const llm::SseEvent& ev) {
if (++events > CohereCaps::kMaxStreamEvents)
throw llm::ApiError("cohere", 502, "STREAM_TOO_LONG", "stream over the event cap");
StreamDelta d = acc.feed(ev.data);
if (on_delta && !on_delta(d)) {
stopped = true;
return false;
}
return true;
});
if (!stopped && !acc.done())
throw llm::ApiError("cohere", 502, "STREAM_TRUNCATED", "stream ended without message-end");
return acc.result();
}
Json Chat::legacy(const Json& body) {
Calls::check_body(Call::kChatV1, body);
Rules::require(body.find("message") && body.find("message")->is_string(), "message is required");
const Json* s = body.find("stream");
Rules::require(!s || (s->is_bool() && !s->as_bool()), "legacy chat streaming is not supported here");
return client_->post(Calls::render(Call::kChatV1), body);
}
Conversation& Conversation::system(const std::string& text) {
params_.add(Messages::system(text));
return *this;
}
Conversation& Conversation::user(const std::string& text) {
params_.add(Messages::user(text));
return *this;
}
Conversation& Conversation::tool_result(const std::string& call_id, const std::string& content) {
params_.add(Messages::tool(call_id, content));
return *this;
}
ChatResult Conversation::send(Chat& chat) {
ChatResult r = chat.create(params_);
params_.add(r.history_message());
return r;
}
ChatResult Conversation::send_stream(Chat& chat, const Chat::DeltaSink& on_delta) {
ChatResult r = chat.stream(params_, on_delta);
params_.add(r.history_message());
return r;
}
ChatResult Conversation::run_tools(Chat& chat, const ToolHandler& handler, int max_rounds) {
Rules::require(max_rounds >= 1 && max_rounds <= kMaxToolRounds, "max_rounds must be 1..16");
Rules::require(!params_.tools.empty(), "run_tools needs tools");
ChatResult r;
for (int round = 0; round < max_rounds; ++round) {
r = send(chat);
if (r.tool_calls.empty()) return r;
for (const ToolCallRecord& call : r.tool_calls) {
if (!call.arguments_valid())
throw llm::ApiError("cohere", 502, "BAD_TOOL_ARGUMENTS",
"tool call '" + call.name + "' has invalid JSON arguments");
tool_result(call.id, handler(call.name, call.parsed(), call.id));
}
}
throw llm::ApiError::invalid("cohere", "tool loop hit the round cap with calls still pending");
}
} // namespace molo::cohere
+56
View File
@@ -0,0 +1,56 @@
#pragma once
// Chat v2 (POST /v2/chat) with streaming, Conversation (history keeper and
// tool loop) and the legacy v1 chat (POST /v1/chat, still in the spec).
#include <functional>
#include "vendors/cohere/calls.h"
#include "vendors/cohere/chat_params.h"
#include "vendors/cohere/chat_result.h"
#include "llm/api_client.h"
namespace molo::cohere {
class Chat : public llm::Service {
public:
explicit Chat(std::shared_ptr<llm::ApiClient> client) : llm::Service(std::move(client)) {}
ChatResult create(const ChatParams& params);
using DeltaSink = std::function<bool(const StreamDelta&)>; // false: stop early
// Streams events; returns the folded result. A stream that ends without
// message-end (and was not stopped by the sink) throws 502 STREAM_TRUNCATED.
ChatResult stream(const ChatParams& params, const DeltaSink& on_delta);
// Legacy v1 chat. `body` fields must be in the call table; `message` and a
// non-streaming request are required (stream:true is rejected, v1 streams use a different event format).
Json legacy(const Json& body);
};
class Conversation {
public:
static constexpr int kMaxToolRounds = 16;
explicit Conversation(ChatParams base) : params_(std::move(base)) {}
Conversation& system(const std::string& text);
Conversation& user(const std::string& text);
Conversation& tool_result(const std::string& call_id, const std::string& content);
ChatResult send(Chat& chat);
ChatResult send_stream(Chat& chat, const Chat::DeltaSink& on_delta);
// handler(tool name, parsed arguments, call id) -> tool output text. Loops
// while the model asks for tools, at most `max_rounds` requests (<= 16).
using ToolHandler = std::function<std::string(const std::string&, const Json&, const std::string&)>;
ChatResult run_tools(Chat& chat, const ToolHandler& handler, int max_rounds = 8);
const ChatParams& params() const { return params_; }
ChatParams& params() { return params_; }
private:
ChatParams params_;
};
} // namespace molo::cohere
+206
View File
@@ -0,0 +1,206 @@
#include "vendors/cohere/chat_model.h"
#include <map>
#include "vendors/cohere/platform.h"
#include "vendors/cohere/rules.h"
#include "security/crypto/Base64.h"
namespace molo::cohere {
CohereChatModel::CohereChatModel(std::shared_ptr<Chat> chat, std::string default_model)
: chat_(std::move(chat)), default_model_(std::move(default_model)) {}
std::shared_ptr<CohereChatModel> CohereChatModel::from_env(const std::string& default_model) {
return std::make_shared<CohereChatModel>(std::make_shared<Chat>(Platform::from_env().client), default_model);
}
namespace {
Json image_part_of(const llm::ImageData& img) {
if (!img.url.empty()) return Messages::image_part(img.url);
Rules::require(!img.bytes.empty(), "image has neither url nor bytes");
Rules::require(!img.mime.empty(), "image bytes need a mime type");
return Messages::image_part("data:" + img.mime + ";base64," + security::crypto::Base64::encode(img.bytes));
}
void append(ChatParams& params, const llm::Message& m) {
switch (m.role) {
case llm::Role::kSystem:
params.add(Messages::system(m.text()));
return;
case llm::Role::kUser: {
bool has_image = false;
for (const llm::ContentPart& p : m.parts) has_image = has_image || p.image.has_value();
if (!has_image) {
params.add(Messages::user(m.text()));
return;
}
std::vector<Json> parts;
for (const llm::ContentPart& p : m.parts) {
if (!p.text.empty()) parts.push_back(Messages::text_part(p.text));
if (p.image) parts.push_back(image_part_of(*p.image));
}
params.add(Messages::user_parts(parts));
return;
}
case llm::Role::kAssistant: {
std::string thinking;
Json calls = Json::array();
for (const llm::ContentPart& p : m.parts) {
thinking += p.reasoning;
if (p.tool_call)
calls.push(Messages::tool_call(p.tool_call->id, p.tool_call->name,
p.tool_call->arguments.is_null() ? "{}" : p.tool_call->arguments.dump()));
}
params.add(Messages::assistant(m.text(), "", calls, thinking));
return;
}
case llm::Role::kTool: {
bool any = false;
for (const llm::ContentPart& p : m.parts)
if (p.tool_result) {
params.add(Messages::tool(p.tool_result->call_id, p.tool_result->content));
any = true;
}
Rules::require(any, "a tool message needs a tool result part");
return;
}
}
}
llm::Usage usage_of(const TokenUsage& u) {
llm::Usage out;
out.input_tokens = static_cast<int>(u.input_tokens ? u.input_tokens : u.billed_input_tokens);
out.output_tokens = static_cast<int>(u.output_tokens ? u.output_tokens : u.billed_output_tokens);
out.total_tokens = out.input_tokens + out.output_tokens;
out.cached_tokens = static_cast<int>(u.cached_tokens);
return out;
}
Json arguments_of(const ToolCallRecord& call) {
if (call.arguments_valid()) return call.parsed();
Json bad = Json::object();
bad.set("_invalid_json", Json(call.arguments));
return bad;
}
} // namespace
std::string CohereChatModel::neutral_finish(const std::string& r) {
if (r == "COMPLETE" || r == "STOP_SEQUENCE") return "stop";
if (r == "MAX_TOKENS") return "length";
if (r == "TOOL_CALL") return "tool_calls";
if (r == "ERROR") return "error";
if (r == "TIMEOUT") return "timeout";
return r;
}
ChatParams CohereChatModel::to_params(const llm::ChatRequest& r) const {
ChatParams p;
p.model = r.model.empty() ? default_model_ : r.model;
if (!r.system.empty()) p.add(Messages::system(r.system));
for (const llm::Message& m : r.messages) append(p, m);
for (const llm::ToolSpec& t : r.tools) {
ToolDef d;
d.name = t.name;
d.description = t.description;
d.parameters = t.parameters;
p.tools.push_back(d);
}
if (r.tool_choice == "required") p.tool_choice = "REQUIRED";
else if (r.tool_choice == "none") p.tool_choice = "NONE";
else if (!r.tool_choice.empty() && r.tool_choice != "auto")
Rules::fail("Cohere cannot force a named tool; use required, none or auto");
p.max_tokens = r.max_tokens;
p.temperature = r.temperature;
p.p = r.top_p;
p.stop = r.stop;
if (r.json_mode || r.response_schema.is_object()) {
p.json_mode = true;
if (r.response_schema.is_object()) p.json_schema = r.response_schema;
}
if (!r.reasoning_effort.empty()) {
bool off = r.reasoning_effort == "none" || r.reasoning_effort == "off" || r.reasoning_effort == "disabled";
p.thinking = off ? Thinking::kDisabled : Thinking::kEnabled;
}
if (r.extra.is_object()) {
Json rest = Json::object();
for (const auto& [k, v] : r.extra.as_object()) {
if (k == "thinking" && v.is_object()) {
std::string type = J::str(v, "type");
if (type == "enabled") p.thinking = Thinking::kEnabled;
else if (type == "disabled") p.thinking = Thinking::kDisabled;
else if (!type.empty()) Rules::fail("thinking.type must be enabled or disabled");
if (v.find("token_budget") && v.find("token_budget")->is_number()) {
if (type.empty() && p.thinking == Thinking::kDefault) p.thinking = Thinking::kEnabled;
p.thinking_budget = v.find("token_budget")->as_int();
}
} else {
rest.set(k, v);
}
}
p.extra = rest;
}
return p;
}
llm::ChatResponse CohereChatModel::to_response(const ChatResult& result) {
llm::ChatResponse out;
out.id = result.id;
out.message.role = llm::Role::kAssistant;
if (!result.thinking.empty()) {
llm::ContentPart r;
r.reasoning = result.thinking;
out.message.parts.push_back(r);
}
if (!result.text.empty() || result.tool_calls.empty())
out.message.parts.push_back(llm::ContentPart::of_text(result.text));
for (const ToolCallRecord& call : result.tool_calls) {
llm::ToolCall tc;
tc.id = call.id;
tc.name = call.name;
tc.arguments = arguments_of(call);
out.message.parts.push_back(llm::ContentPart::of_call(tc));
}
out.finish_reason = neutral_finish(result.finish_reason);
out.usage = usage_of(result.usage);
out.raw = result.raw;
return out;
}
llm::ChatResponse CohereChatModel::generate(const llm::ChatRequest& request) {
ChatParams params = to_params(request);
llm::ChatResponse out = to_response(chat_->create(params));
out.model = params.model;
return out;
}
void CohereChatModel::stream(const llm::ChatRequest& request,
const std::function<bool(const llm::ChatChunk&)>& on_chunk) {
ChatParams params = to_params(request);
chat_->stream(params, [&](const StreamDelta& d) {
if (!d.text.empty() || !d.thinking.empty()) {
llm::ChatChunk c;
c.text_delta = d.text;
c.reasoning_delta = d.thinking;
if (!on_chunk(c)) return false;
}
if (d.type == "tool-call-start" || d.type == "tool-call-delta") {
llm::ChatChunk c;
c.tool_call_id = d.tool_id;
c.tool_call_name = d.tool_name;
c.tool_args_delta = d.args_delta;
if (!on_chunk(c)) return false;
}
if (d.type == "message-end") {
llm::ChatChunk c;
c.finish_reason = neutral_finish(d.finish_reason);
if (d.has_usage) c.usage = usage_of(d.usage);
if (!on_chunk(c)) return false;
}
return true;
});
}
} // namespace molo::cohere
+47
View File
@@ -0,0 +1,47 @@
#pragma once
// CohereChatModel: the llm::ChatModel seam over Chat v2.
//
// Mapping notes (each one is a decision):
// - ChatRequest.system becomes a leading system message.
// - tool_choice: "required" -> REQUIRED, "none" -> NONE, ""/"auto" -> omitted;
// a tool NAME is rejected loudly (Cohere cannot force one named tool).
// - json_mode -> response_format json_object; response_schema -> the same plus
// json_schema (Cohere's json_object accepts a schema).
// - reasoning_effort "none"/"off"/"disabled" -> thinking disabled; any other
// non-empty value -> thinking enabled (Cohere has no effort levels, only
// token_budget, which ChatRequest.extra {"thinking":{"token_budget":N}} sets).
// - Assistant reasoning parts are sent back as thinking content.
// - Tool arguments that are not valid JSON arrive as {"_invalid_json": "<raw>"}.
// - finish_reason: COMPLETE and STOP_SEQUENCE -> stop, MAX_TOKENS -> length,
// TOOL_CALL -> tool_calls, ERROR -> error, TIMEOUT -> timeout.
// - Usage: counts come from usage.tokens (actual), billed_units is the fallback;
// cached_tokens from usage.cached_tokens.
#include <memory>
#include "vendors/cohere/chat.h"
#include "llm/chat_types.h"
namespace molo::cohere {
class CohereChatModel : public llm::ChatModel {
public:
explicit CohereChatModel(std::shared_ptr<Chat> chat, std::string default_model = "command-a-03-2025");
static std::shared_ptr<CohereChatModel> from_env(const std::string& default_model = "command-a-03-2025");
std::string vendor() const override { return "cohere"; }
llm::ChatResponse generate(const llm::ChatRequest& request) override;
void stream(const llm::ChatRequest& request,
const std::function<bool(const llm::ChatChunk&)>& on_chunk) override;
ChatParams to_params(const llm::ChatRequest& request) const;
static llm::ChatResponse to_response(const ChatResult& result);
static std::string neutral_finish(const std::string& finish_reason);
private:
std::shared_ptr<Chat> chat_;
std::string default_model_;
};
} // namespace molo::cohere
+205
View File
@@ -0,0 +1,205 @@
#include "vendors/cohere/chat_params.h"
#include "vendors/cohere/calls.h"
#include "vendors/cohere/caps.h"
#include "vendors/cohere/rules.h"
namespace molo::cohere {
Json Messages::text_part(const std::string& text) {
Json p = Json::object();
p.set("type", Json("text"));
p.set("text", Json(text));
return p;
}
Json Messages::image_part(const std::string& url, const std::string& detail) {
Rules::image_url(url);
if (!detail.empty()) Rules::one_of(detail, {"auto", "low", "high"}, "image detail");
Json u = Json::object();
u.set("url", Json(url));
if (!detail.empty()) u.set("detail", Json(detail));
Json p = Json::object();
p.set("type", Json("image_url"));
p.set("image_url", u);
return p;
}
static Json message(const char* role) {
Json m = Json::object();
m.set("role", Json(role));
return m;
}
Json Messages::system(const std::string& text) {
Json m = message("system");
m.set("content", Json(text));
return m;
}
Json Messages::user(const std::string& text) {
Json m = message("user");
m.set("content", Json(text));
return m;
}
Json Messages::user_parts(const std::vector<Json>& parts) {
Rules::require(!parts.empty(), "a user message needs at least one part");
Json arr = Json::array();
for (const Json& p : parts) arr.push(p);
Json m = message("user");
m.set("content", arr);
return m;
}
Json Messages::assistant(const std::string& text, const std::string& tool_plan, const Json& tool_calls,
const std::string& thinking) {
Json m = message("assistant");
Json parts = Json::array();
if (!thinking.empty()) {
Json t = Json::object();
t.set("type", Json("thinking"));
t.set("thinking", Json(thinking));
parts.push(t);
}
if (!text.empty()) parts.push(text_part(text));
if (!parts.as_array().empty()) m.set("content", parts);
if (!tool_plan.empty()) m.set("tool_plan", Json(tool_plan));
if (tool_calls.is_array() && !tool_calls.as_array().empty()) m.set("tool_calls", tool_calls);
return m;
}
Json Messages::tool(const std::string& call_id, const std::string& content) {
Rules::require(!call_id.empty(), "tool_call_id must not be empty");
Json m = message("tool");
m.set("tool_call_id", Json(call_id));
m.set("content", Json(content));
return m;
}
Json Messages::tool_call(const std::string& id, const std::string& name, const std::string& arguments_json) {
Json f = Json::object();
f.set("name", Json(name));
f.set("arguments", Json(arguments_json));
Json c = Json::object();
c.set("id", Json(id));
c.set("type", Json("function"));
c.set("function", f);
return c;
}
Json ToolDef::to_json() const {
Rules::require(!name.empty() && name.size() <= 128, "tool name must be 1..128 chars");
for (char c : name)
Rules::require((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-',
"tool name has invalid characters");
Json f = Json::object();
f.set("name", Json(name));
if (!description.empty()) f.set("description", Json(description));
f.set("parameters", parameters.is_object() ? parameters : Json::object());
Json t = Json::object();
t.set("type", Json("function"));
t.set("function", f);
return t;
}
ChatParams ChatParams::prompt(const std::string& text) {
ChatParams p;
p.add(Messages::user(text));
return p;
}
ChatParams& ChatParams::add(Json message) {
messages.push_back(std::move(message));
return *this;
}
void ChatParams::validate() const {
Rules::model(model);
Rules::require(!messages.empty(), "messages must not be empty");
Rules::require(messages.size() <= CohereCaps::kMaxMessages, "too many messages");
Rules::require(tools.size() <= CohereCaps::kMaxTools, "too many tools");
Rules::require(documents.size() <= CohereCaps::kMaxDocuments, "too many documents");
if (!citation_mode.empty())
Rules::one_of(citation_mode, {"ENABLED", "DISABLED", "FAST", "ACCURATE", "OFF"}, "citation mode");
if (!safety_mode.empty()) Rules::one_of(safety_mode, {"CONTEXTUAL", "STRICT", "OFF"}, "safety mode");
if (!tool_choice.empty()) {
Rules::one_of(tool_choice, {"REQUIRED", "NONE"}, "tool_choice");
Rules::require(tool_choice != "REQUIRED" || !tools.empty(), "tool_choice REQUIRED needs tools");
}
Rules::require(!strict_tools || !tools.empty(), "strict_tools needs tools");
Rules::require(!json_schema.is_object() || json_mode, "json_schema needs json_mode");
if (max_tokens) Rules::require(*max_tokens >= 1, "max_tokens must be >= 1");
Rules::stop(stop);
if (temperature) Rules::temperature(*temperature);
if (seed) Rules::require(*seed >= 0, "seed must be >= 0");
if (frequency_penalty) Rules::range(*frequency_penalty, 0.0, 1.0, "frequency_penalty");
if (presence_penalty) Rules::range(*presence_penalty, 0.0, 1.0, "presence_penalty");
if (k) Rules::top_k(*k);
if (p) Rules::top_p(*p);
if (priority) Rules::priority(*priority);
if (thinking_budget) {
Rules::require(*thinking_budget >= 1, "thinking token budget must be >= 1");
Rules::require(thinking == Thinking::kEnabled, "a thinking budget needs thinking enabled");
}
}
Json ChatParams::to_json(bool stream) const {
validate();
Json b = Json::object();
b.set("model", Json(model));
Json msgs = Json::array();
for (const Json& m : messages) msgs.push(m);
b.set("messages", msgs);
if (stream) b.set("stream", Json(true));
if (!tools.empty()) {
Json t = Json::array();
for (const ToolDef& d : tools) t.push(d.to_json());
b.set("tools", t);
}
if (strict_tools) b.set("strict_tools", Json(true));
if (!documents.empty()) {
Json d = Json::array();
for (const Json& x : documents) d.push(x);
b.set("documents", d);
}
if (!citation_mode.empty()) {
Json c = Json::object();
c.set("mode", Json(citation_mode));
b.set("citation_options", c);
}
if (json_mode) {
Json f = Json::object();
f.set("type", Json("json_object"));
if (json_schema.is_object()) f.set("json_schema", json_schema);
b.set("response_format", f);
}
if (!safety_mode.empty()) b.set("safety_mode", Json(safety_mode));
if (max_tokens) b.set("max_tokens", Json(*max_tokens));
if (!stop.empty()) {
Json s = Json::array();
for (const std::string& x : stop) s.push(Json(x));
b.set("stop_sequences", s);
}
if (temperature) b.set("temperature", Json(*temperature));
if (seed) b.set("seed", Json(*seed));
if (frequency_penalty) b.set("frequency_penalty", Json(*frequency_penalty));
if (presence_penalty) b.set("presence_penalty", Json(*presence_penalty));
if (k) b.set("k", Json(*k));
if (p) b.set("p", Json(*p));
if (logprobs) b.set("logprobs", Json(true));
if (!tool_choice.empty()) b.set("tool_choice", Json(tool_choice));
if (thinking != Thinking::kDefault) {
Json t = Json::object();
t.set("type", Json(thinking == Thinking::kEnabled ? "enabled" : "disabled"));
if (thinking_budget) t.set("token_budget", Json(*thinking_budget));
b.set("thinking", t);
}
if (priority) b.set("priority", Json(*priority));
if (extra.is_object())
for (const auto& [key, v] : extra.as_object()) b.set(key, v);
Calls::check_body(Call::kChat, b);
return b;
}
} // namespace molo::cohere
+70
View File
@@ -0,0 +1,70 @@
#pragma once
// Chat v2 request: message builders (Messages), tool definitions and
// ChatParams, which validates and serializes to the /v2/chat body.
#include <optional>
#include <string>
#include <vector>
#include "vendors/cohere/types.h"
namespace molo::cohere {
// Builders for the four v2 message roles. Content is a plain string or an
// array of typed parts (text, image_url; assistant: text, thinking).
struct Messages {
static Json system(const std::string& text);
static Json user(const std::string& text);
static Json user_parts(const std::vector<Json>& parts);
// tool_plan and tool_calls are omitted when empty.
static Json assistant(const std::string& text, const std::string& tool_plan = "",
const Json& tool_calls = Json::array(), const std::string& thinking = "");
// Result of one tool call; `content` is text (the docs also allow document parts).
static Json tool(const std::string& call_id, const std::string& content);
static Json text_part(const std::string& text);
static Json image_part(const std::string& url, const std::string& detail = "");
static Json tool_call(const std::string& id, const std::string& name, const std::string& arguments_json);
};
struct ToolDef {
std::string name;
std::string description;
Json parameters = Json::object(); // JSON Schema
Json to_json() const;
};
enum class Thinking { kDefault, kEnabled, kDisabled };
struct ChatParams {
std::string model = "command-a-03-2025";
std::vector<Json> messages;
std::vector<ToolDef> tools;
bool strict_tools = false;
std::vector<Json> documents; // each a string or {"data":{...},"id":"..."}
std::string citation_mode; // ENABLED DISABLED FAST ACCURATE OFF
bool json_mode = false;
Json json_schema; // null: none; else response_format.json_schema
std::string safety_mode; // CONTEXTUAL STRICT OFF
std::optional<int> max_tokens;
std::vector<std::string> stop;
std::optional<double> temperature;
std::optional<long> seed;
std::optional<double> frequency_penalty;
std::optional<double> presence_penalty;
std::optional<int> k;
std::optional<double> p;
bool logprobs = false;
std::string tool_choice; // REQUIRED, NONE or empty
Thinking thinking = Thinking::kDefault;
std::optional<long> thinking_budget;
std::optional<int> priority;
Json extra; // merged into the body last; keys must exist in the call table
static ChatParams prompt(const std::string& text);
ChatParams& add(Json message);
void validate() const;
Json to_json(bool stream = false) const;
};
} // namespace molo::cohere
+132
View File
@@ -0,0 +1,132 @@
#include "vendors/cohere/chat_result.h"
#include "vendors/cohere/chat_params.h"
#include "llm/error.h"
namespace molo::cohere {
bool ToolCallRecord::arguments_valid() const {
try {
return Json::parse(arguments.empty() ? "{}" : arguments).is_object();
} catch (const std::exception&) {
return false;
}
}
Json ToolCallRecord::parsed() const {
try {
Json j = Json::parse(arguments.empty() ? "{}" : arguments);
if (j.is_object()) return j;
} catch (const std::exception&) {
}
return Json::object();
}
static ToolCallRecord tool_of(const Json& c) {
ToolCallRecord t;
t.id = J::str(c, "id");
if (const Json* f = J::obj(c, "function")) {
t.name = J::str(*f, "name");
t.arguments = J::str(*f, "arguments");
}
return t;
}
ChatResult ChatResult::parse(const Json& doc) {
ChatResult r;
r.raw = doc;
r.id = J::str(doc, "id");
r.finish_reason = J::str(doc, "finish_reason");
if (const Json* m = J::obj(doc, "message")) {
if (const Json* content = J::arr(*m, "content"))
for (const Json& part : content->as_array()) {
if (J::str(part, "type") == "thinking") r.thinking += J::str(part, "thinking");
else r.text += J::str(part, "text");
}
r.tool_plan = J::str(*m, "tool_plan");
if (const Json* calls = J::arr(*m, "tool_calls"))
for (const Json& c : calls->as_array()) r.tool_calls.push_back(tool_of(c));
if (const Json* cites = J::arr(*m, "citations"))
for (const Json& c : cites->as_array()) r.citations.push_back(c);
}
r.usage = TokenUsage::parse(J::obj(doc, "usage"));
if (const Json* l = J::arr(doc, "logprobs")) r.logprobs = *l;
return r;
}
Json ChatResult::history_message() const {
Json calls = Json::array();
for (const ToolCallRecord& c : tool_calls) calls.push(Messages::tool_call(c.id, c.name, c.arguments));
return Messages::assistant(text, tool_plan, calls, thinking);
}
StreamDelta ChatStreamAccumulator::feed(const Json& ev) {
StreamDelta d;
d.type = J::str(ev, "type");
d.index = static_cast<int>(J::num(ev, "index"));
const Json* delta = J::obj(ev, "delta");
const Json* msg = delta ? J::obj(*delta, "message") : nullptr;
if (d.type == "message-start") {
result_.id = J::str(ev, "id");
} else if (d.type == "content-start" || d.type == "content-delta") {
const Json* content = msg ? J::obj(*msg, "content") : nullptr;
if (content) {
d.text = J::str(*content, "text");
d.thinking = J::str(*content, "thinking");
result_.text += d.text;
result_.thinking += d.thinking;
}
} else if (d.type == "tool-plan-delta") {
if (msg) d.tool_plan = J::str(*msg, "tool_plan");
result_.tool_plan += d.tool_plan;
} else if (d.type == "tool-call-start") {
if (const Json* c = msg ? J::obj(*msg, "tool_calls") : nullptr) {
ToolCallRecord t = tool_of(*c);
d.tool_start = true;
d.tool_id = t.id;
d.tool_name = t.name;
d.args_delta = t.arguments;
tools_[d.index] = t;
}
} else if (d.type == "tool-call-delta") {
if (const Json* c = msg ? J::obj(*msg, "tool_calls") : nullptr)
if (const Json* f = J::obj(*c, "function")) {
d.args_delta = J::str(*f, "arguments");
ToolCallRecord& t = tools_[d.index];
t.arguments += d.args_delta;
d.tool_id = t.id;
d.tool_name = t.name;
}
} else if (d.type == "citation-start") {
if (const Json* c = msg ? J::obj(*msg, "citations") : nullptr) {
d.citation = *c;
result_.citations.push_back(*c);
}
} else if (d.type == "message-end") {
if (delta) {
std::string error = J::str(*delta, "error");
if (!error.empty()) throw llm::ApiError("cohere", 502, "STREAM_ERROR", error);
d.finish_reason = J::str(*delta, "finish_reason");
result_.finish_reason = d.finish_reason;
if (const Json* u = J::obj(*delta, "usage")) {
d.usage = TokenUsage::parse(u);
d.has_usage = true;
result_.usage = d.usage;
}
}
done_ = true;
} else if (d.type.empty()) {
throw llm::ApiError("cohere", 502, "STREAM_ERROR", "stream event without a type");
}
// content-end, tool-call-end, citation-end and unknown future events carry nothing we keep.
return d;
}
ChatResult ChatStreamAccumulator::result() const {
ChatResult r = result_;
r.tool_calls.clear();
for (const auto& [i, t] : tools_) r.tool_calls.push_back(t);
return r;
}
} // namespace molo::cohere
+68
View File
@@ -0,0 +1,68 @@
#pragma once
// Chat v2 response and the streaming accumulator. Stream events are the
// `type` values of StreamedChatResponseV2 (message-start ... message-end).
#include <map>
#include <string>
#include <vector>
#include "vendors/cohere/types.h"
namespace molo::cohere {
struct ToolCallRecord {
std::string id;
std::string name;
std::string arguments; // JSON text, as sent by the model
bool arguments_valid() const;
Json parsed() const; // {} when invalid
};
struct ChatResult {
std::string id;
std::string finish_reason; // COMPLETE STOP_SEQUENCE MAX_TOKENS TOOL_CALL ERROR TIMEOUT
std::string text; // text parts joined
std::string thinking; // thinking parts joined
std::string tool_plan;
std::vector<ToolCallRecord> tool_calls;
std::vector<Json> citations;
TokenUsage usage;
Json logprobs; // array or null
Json raw;
static ChatResult parse(const Json& doc);
// The assistant message to put back into the history for the next turn.
Json history_message() const;
};
struct StreamDelta {
std::string type; // event type, e.g. "content-delta"
std::string text;
std::string thinking;
std::string tool_plan;
int index = 0;
bool tool_start = false; // tool-call-start: id and name are set
std::string tool_id;
std::string tool_name;
std::string args_delta;
Json citation;
std::string finish_reason; // message-end
bool has_usage = false;
TokenUsage usage;
};
class ChatStreamAccumulator {
public:
// Throws llm::ApiError (502 STREAM_ERROR) on an error event or a message-end error.
StreamDelta feed(const Json& event);
bool done() const { return done_; }
ChatResult result() const;
private:
ChatResult result_;
std::map<int, ToolCallRecord> tools_;
bool done_ = false;
};
} // namespace molo::cohere
+273
View File
@@ -0,0 +1,273 @@
#include "vendors/cohere/inference.h"
#include "vendors/cohere/calls.h"
#include "vendors/cohere/caps.h"
#include "vendors/cohere/rules.h"
namespace molo::cohere {
namespace {
Json string_array(const std::vector<std::string>& v) {
Json a = Json::array();
for (const std::string& s : v) a.push(Json(s));
return a;
}
} // namespace
void EmbedParams::validate() const {
Rules::model(model);
Rules::one_of(input_type, {"search_document", "search_query", "classification", "clustering", "image"}, "input_type");
Rules::require(!texts.empty() || !images.empty() || !inputs.empty(), "give texts, images or inputs");
Rules::require(texts.size() <= CohereCaps::kMaxEmbedTexts, "at most 96 texts per call");
Rules::require(images.size() <= CohereCaps::kMaxEmbedTexts, "at most 96 images per call");
Rules::require(inputs.size() <= CohereCaps::kMaxEmbedInputs, "at most 96 inputs per call");
for (const std::string& t : texts) Rules::require(!t.empty(), "texts must not be empty");
for (const std::string& i : images) Rules::image_url(i);
for (const std::string& t : embedding_types)
Rules::one_of(t, {"float", "int8", "uint8", "binary", "ubinary", "base64"}, "embedding type");
if (!truncate.empty()) Rules::one_of(truncate, {"NONE", "START", "END"}, "truncate");
if (max_tokens) Rules::require(*max_tokens >= 1, "max_tokens must be >= 1");
if (output_dimension) Rules::require(*output_dimension >= 1, "output_dimension must be >= 1");
if (priority) Rules::priority(*priority);
}
Json EmbedParams::to_json() const {
validate();
Json b = Json::object();
b.set("model", Json(model));
b.set("input_type", Json(input_type));
if (!texts.empty()) b.set("texts", string_array(texts));
if (!images.empty()) b.set("images", string_array(images));
if (!inputs.empty()) {
Json a = Json::array();
for (const Json& i : inputs) a.push(i);
b.set("inputs", a);
}
if (!embedding_types.empty()) b.set("embedding_types", string_array(embedding_types));
if (!truncate.empty()) b.set("truncate", Json(truncate));
if (max_tokens) b.set("max_tokens", Json(*max_tokens));
if (output_dimension) b.set("output_dimension", Json(*output_dimension));
if (priority) b.set("priority", Json(*priority));
return b;
}
std::vector<std::vector<double>> EmbedResult::floats() const {
std::vector<std::vector<double>> out;
const Json* rows = nullptr;
if (embeddings.is_array()) rows = &embeddings;
else if (embeddings.is_object() && embeddings.find("float") && embeddings.find("float")->is_array())
rows = embeddings.find("float");
if (!rows) return out;
for (const Json& row : rows->as_array()) {
std::vector<double> v;
if (row.is_array())
for (const Json& x : row.as_array())
if (x.is_number()) v.push_back(x.as_number());
out.push_back(std::move(v));
}
return out;
}
EmbedResult EmbedResult::parse(const Json& doc) {
EmbedResult r;
r.raw = doc;
r.id = J::str(doc, "id");
r.response_type = J::str(doc, "response_type");
r.texts = J::strings(doc, "texts");
if (const Json* e = doc.is_object() ? doc.find("embeddings") : nullptr) r.embeddings = *e;
r.usage = TokenUsage::parse(J::obj(doc, "meta"));
if (const Json* meta = J::obj(doc, "meta"))
if (const Json* b = J::obj(*meta, "billed_units")) {
r.usage.billed_input_tokens = J::num(*b, "input_tokens");
r.usage.present = true;
}
return r;
}
void RerankParams::validate() const {
Rules::model(model);
Rules::require(!query.empty(), "query must not be empty");
Rules::require(!documents.empty() && documents.size() <= CohereCaps::kMaxDocuments, "documents must be 1..1000");
for (const std::string& d : documents) Rules::require(!d.empty(), "documents must not be empty");
if (top_n) Rules::require(*top_n >= 1, "top_n must be >= 1");
if (max_tokens_per_doc) Rules::require(*max_tokens_per_doc >= 1, "max_tokens_per_doc must be >= 1");
if (!relevant_spans.empty()) Rules::one_of(relevant_spans, {"enabled", "disabled"}, "relevant_spans");
Rules::require(relevant_spans.empty() || relevant_spans == "enabled" || (!spans_threshold && !max_spans),
"span options need relevant_spans enabled");
if (spans_threshold) Rules::range(*spans_threshold, 0.0, 1.0, "span threshold");
if (max_spans) Rules::require(*max_spans >= 1, "max_spans must be >= 1");
if (priority) Rules::priority(*priority);
}
Json RerankParams::to_json() const {
validate();
Json b = Json::object();
b.set("model", Json(model));
b.set("query", Json(query));
b.set("documents", string_array(documents));
if (top_n) b.set("top_n", Json(*top_n));
if (max_tokens_per_doc) b.set("max_tokens_per_doc", Json(*max_tokens_per_doc));
if (!relevant_spans.empty()) {
Json s = Json::object();
s.set("mode", Json(relevant_spans));
if (spans_threshold) s.set("threshold", Json(*spans_threshold));
if (max_spans) s.set("max_spans", Json(*max_spans));
b.set("relevant_spans", s);
}
if (priority) b.set("priority", Json(*priority));
return b;
}
RerankResult RerankResult::parse(const Json& doc) {
RerankResult r;
r.raw = doc;
r.id = J::str(doc, "id");
if (const Json* a = J::arr(doc, "results"))
for (const Json& x : a->as_array()) {
RerankHit h;
h.index = static_cast<int>(J::num(x, "index"));
h.score = J::real(x, "relevance_score");
if (const Json* s = J::arr(x, "relevant_spans")) h.spans = *s;
r.hits.push_back(h);
}
r.usage = TokenUsage::parse(J::obj(doc, "meta"));
return r;
}
void ClassifyParams::validate() const {
Rules::require(!inputs.empty() && inputs.size() <= CohereCaps::kMaxClassifyInputs, "inputs must be 1..96");
for (const std::string& i : inputs) Rules::require(!i.empty(), "inputs must not be empty");
if (!model.empty()) Rules::model(model);
Rules::require(!model.empty() || examples.size() >= 2, "without a fine-tuned model, give at least 2 examples");
for (const ClassifyExample& e : examples)
Rules::require(!e.text.empty() && !e.label.empty(), "examples need text and label");
if (!truncate.empty()) Rules::one_of(truncate, {"NONE", "START", "END"}, "truncate");
}
Json ClassifyParams::to_json() const {
validate();
Json b = Json::object();
b.set("inputs", string_array(inputs));
if (!examples.empty()) {
Json a = Json::array();
for (const ClassifyExample& e : examples) {
Json x = Json::object();
x.set("text", Json(e.text));
x.set("label", Json(e.label));
a.push(x);
}
b.set("examples", a);
}
if (!model.empty()) b.set("model", Json(model));
if (!truncate.empty()) b.set("truncate", Json(truncate));
return b;
}
ClassifyResult ClassifyResult::parse(const Json& doc) {
ClassifyResult r;
r.raw = doc;
r.id = J::str(doc, "id");
if (const Json* a = J::arr(doc, "classifications"))
for (const Json& x : a->as_array()) {
Classification c;
c.id = J::str(x, "id");
c.input = J::str(x, "input");
c.prediction = J::str(x, "prediction");
if (c.prediction.empty()) {
std::vector<std::string> p = J::strings(x, "predictions");
if (!p.empty()) c.prediction = p.front();
}
c.confidence = J::real(x, "confidence");
if (c.confidence == 0)
if (const Json* cs = J::arr(x, "confidences"))
if (!cs->as_array().empty() && cs->as_array().front().is_number()) c.confidence = cs->as_array().front().as_number();
c.classification_type = J::str(x, "classification_type");
if (const Json* l = J::obj(x, "labels")) c.labels = *l;
r.items.push_back(c);
}
return r;
}
TokenizeResult TokenizeResult::parse(const Json& doc) {
TokenizeResult r;
if (const Json* a = J::arr(doc, "tokens"))
for (const Json& t : a->as_array())
if (t.is_number()) r.tokens.push_back(t.as_int());
r.token_strings = J::strings(doc, "token_strings");
return r;
}
EmbedResult Embeddings::embed(const EmbedParams& params) {
Json body = params.to_json();
Calls::check_body(Call::kEmbed, body);
return EmbedResult::parse(client_->post(Calls::render(Call::kEmbed), body));
}
EmbedResult Embeddings::embed_v1(const EmbedParams& params) {
Rules::require(params.inputs.empty() && !params.output_dimension && !params.max_tokens && !params.priority,
"v1 embed takes texts and images only");
Json body = params.to_json();
Calls::check_body(Call::kEmbedV1, body);
return EmbedResult::parse(client_->post(Calls::render(Call::kEmbedV1), body));
}
RerankResult Rerank::rerank(const RerankParams& params) {
Json body = params.to_json();
Calls::check_body(Call::kRerank, body);
return RerankResult::parse(client_->post(Calls::render(Call::kRerank), body));
}
Json Rerank::rerank_v1(const Json& body) {
Calls::check_body(Call::kRerankV1, body);
Rules::require(body.find("query") && body.find("query")->is_string(), "query is required");
const Json* docs = body.find("documents");
Rules::require(docs && docs->is_array() && !docs->as_array().empty() &&
docs->as_array().size() <= CohereCaps::kMaxDocuments,
"documents must be 1..1000");
return client_->post(Calls::render(Call::kRerankV1), body);
}
ClassifyResult Classify::classify(const ClassifyParams& params) {
Json body = params.to_json();
Calls::check_body(Call::kClassify, body);
return ClassifyResult::parse(client_->post(Calls::render(Call::kClassify), body));
}
TokenizeResult Tokenizer::tokenize(const std::string& model, const std::string& text) {
Rules::model(model);
Rules::require(!text.empty() && text.size() <= CohereCaps::kMaxTokenizeChars, "text must be 1..65536 chars");
Json b = Json::object();
b.set("model", Json(model));
b.set("text", Json(text));
return TokenizeResult::parse(client_->post(Calls::render(Call::kTokenize), b));
}
std::string Tokenizer::detokenize(const std::string& model, const std::vector<long>& tokens) {
Rules::model(model);
Rules::require(!tokens.empty() && tokens.size() <= CohereCaps::kMaxDetokenizeTokens, "tokens must be 1..65536");
Json arr = Json::array();
for (long t : tokens) {
Rules::require(t >= 0, "token ids must be >= 0");
arr.push(Json(t));
}
Json b = Json::object();
b.set("model", Json(model));
b.set("tokens", arr);
return J::str(client_->post(Calls::render(Call::kDetokenize), b), "text");
}
Json Legacy::generate(const Json& body) {
Calls::check_body(Call::kGenerate, body);
Rules::require(body.find("prompt") && body.find("prompt")->is_string(), "prompt is required");
const Json* s = body.find("stream");
Rules::require(!s || (s->is_bool() && !s->as_bool()), "generate streaming is not supported here");
return client_->post(Calls::render(Call::kGenerate), body);
}
Json Legacy::summarize(const Json& body) {
Calls::check_body(Call::kSummarize, body);
Rules::require(body.find("text") && body.find("text")->is_string(), "text is required");
return client_->post(Calls::render(Call::kSummarize), body);
}
} // namespace molo::cohere
+139
View File
@@ -0,0 +1,139 @@
#pragma once
// Stateless model calls: Embed (v2, v1), Rerank (v2, v1), Classify, Tokenize /
// Detokenize, and the legacy Generate and Summarize. Parameters validate
// before any network traffic; bodies are checked against the call table.
#include <optional>
#include <string>
#include <vector>
#include "vendors/cohere/types.h"
#include "llm/api_client.h"
namespace molo::cohere {
struct EmbedParams {
std::string model = "embed-v4.0";
std::string input_type = "search_document"; // search_document search_query classification clustering image
std::vector<std::string> texts; // <= 96
std::vector<std::string> images; // data URLs
std::vector<Json> inputs; // v2 mixed inputs: {"content":[{type,text|image_url}]}
std::vector<std::string> embedding_types; // float int8 uint8 binary ubinary base64
std::string truncate; // NONE START END
std::optional<int> max_tokens;
std::optional<int> output_dimension;
std::optional<int> priority;
void validate() const;
Json to_json() const;
};
struct EmbedResult {
std::string id;
std::string response_type;
std::vector<std::string> texts;
Json embeddings; // {"float":[[...]],...} (v2) or [[...]] (v1 floats)
TokenUsage usage;
Json raw;
// Float vectors (v2 "float", or the v1 array). Empty when not requested.
std::vector<std::vector<double>> floats() const;
static EmbedResult parse(const Json& doc);
};
struct RerankParams {
std::string model = "rerank-v3.5";
std::string query;
std::vector<std::string> documents; // 1..1000
std::optional<int> top_n;
std::optional<int> max_tokens_per_doc;
std::string relevant_spans; // "", enabled, disabled
std::optional<double> spans_threshold;
std::optional<int> max_spans;
std::optional<int> priority;
void validate() const;
Json to_json() const;
};
struct RerankHit {
int index = 0;
double score = 0;
Json spans; // array or null
};
struct RerankResult {
std::string id;
std::vector<RerankHit> hits; // best first, as sent
TokenUsage usage;
Json raw;
static RerankResult parse(const Json& doc);
};
struct ClassifyExample {
std::string text;
std::string label;
};
struct ClassifyParams {
std::vector<std::string> inputs; // 1..96
std::vector<ClassifyExample> examples;
std::string model; // empty: the default classifier
std::string truncate;
void validate() const;
Json to_json() const;
};
struct Classification {
std::string id, input, prediction, classification_type;
double confidence = 0;
Json labels;
};
struct ClassifyResult {
std::string id;
std::vector<Classification> items;
Json raw;
static ClassifyResult parse(const Json& doc);
};
struct TokenizeResult {
std::vector<long> tokens;
std::vector<std::string> token_strings;
static TokenizeResult parse(const Json& doc);
};
class Embeddings : public llm::Service {
public:
explicit Embeddings(std::shared_ptr<llm::ApiClient> client) : llm::Service(std::move(client)) {}
EmbedResult embed(const EmbedParams& params); // POST /v2/embed
EmbedResult embed_v1(const EmbedParams& params); // POST /v1/embed (texts/images only)
};
class Rerank : public llm::Service {
public:
explicit Rerank(std::shared_ptr<llm::ApiClient> client) : llm::Service(std::move(client)) {}
RerankResult rerank(const RerankParams& params); // POST /v2/rerank
Json rerank_v1(const Json& body); // POST /v1/rerank
};
class Classify : public llm::Service {
public:
explicit Classify(std::shared_ptr<llm::ApiClient> client) : llm::Service(std::move(client)) {}
ClassifyResult classify(const ClassifyParams& params); // POST /v1/classify (deprecated in the spec)
};
class Tokenizer : public llm::Service {
public:
explicit Tokenizer(std::shared_ptr<llm::ApiClient> client) : llm::Service(std::move(client)) {}
TokenizeResult tokenize(const std::string& model, const std::string& text);
std::string detokenize(const std::string& model, const std::vector<long>& tokens);
};
// Deprecated endpoints that remain in the spec; bodies are Json checked against the table.
class Legacy : public llm::Service {
public:
explicit Legacy(std::shared_ptr<llm::ApiClient> client) : llm::Service(std::move(client)) {}
Json generate(const Json& body); // POST /v1/generate (prompt required, stream:true rejected)
Json summarize(const Json& body); // POST /v1/summarize (text required)
};
} // namespace molo::cohere
+33
View File
@@ -0,0 +1,33 @@
#include "vendors/cohere/platform.h"
#include "clients/transports/http_transport.h"
#include "llm/config.h"
namespace molo::cohere {
llm::ApiClientConfig Platform::config() {
llm::ApiClientConfig c;
c.vendor = "cohere";
c.base_url = kHost;
c.key_env = kKeyEnv;
c.auth = llm::AuthStyle::kBearer;
c.default_headers["X-Client-Name"] = kClientName;
return c;
}
Platform Platform::from_env() {
std::string key = llm::Config::get(kKeyEnv);
if (key.empty()) key = llm::Config::get(kKeyEnvAlt);
Platform p;
p.client = std::make_shared<llm::ApiClient>(std::make_unique<clients::transports::HttpTransport>(),
config(), key);
return p;
}
Platform Platform::with(std::unique_ptr<clients::Transport> transport, const std::string& key) {
Platform p;
p.client = std::make_shared<llm::ApiClient>(std::move(transport), config(), key);
return p;
}
} // namespace molo::cohere
+26
View File
@@ -0,0 +1,26 @@
#pragma once
// api.cohere.com, Bearer auth. Credential COHERE_API_KEY (CO_API_KEY, the
// official SDKs' name, is accepted as a fallback), read through llm::Config.
#include <memory>
#include "llm/api_client.h"
namespace molo::cohere {
struct Platform {
static constexpr const char* kHost = "https://api.cohere.com";
static constexpr const char* kKeyEnv = "COHERE_API_KEY";
static constexpr const char* kKeyEnvAlt = "CO_API_KEY";
static constexpr const char* kClientName = "molo";
std::shared_ptr<llm::ApiClient> client;
static llm::ApiClientConfig config();
static Platform from_env();
static Platform with(std::unique_ptr<clients::Transport> transport, const std::string& key);
bool configured() const { return client && client->configured(); }
};
} // namespace molo::cohere
+445
View File
@@ -0,0 +1,445 @@
#include "vendors/cohere/resources.h"
#include <functional>
#include "clients/body/form.h"
#include "vendors/cohere/calls.h"
#include "vendors/cohere/caps.h"
#include "vendors/cohere/rules.h"
namespace molo::cohere {
namespace {
using clients::Method;
void add_page(Query& q, std::optional<int> page_size, const std::string& token) {
if (page_size) {
Rules::page_size(*page_size);
q.emplace_back("page_size", std::to_string(*page_size));
}
if (!token.empty()) {
Rules::require(token.size() <= 2048, "page_token too long");
q.emplace_back("page_token", token);
}
}
Json strings(const std::vector<std::string>& v) {
Json a = Json::array();
for (const std::string& s : v) a.push(Json(s));
return a;
}
const Json* field(const Json& doc, const char* key) { return doc.is_object() ? doc.find(key) : nullptr; }
Json require_object(const Json& j, const char* what) {
Rules::require(j.is_object(), std::string(what) + " must be a JSON object");
return j;
}
} // namespace
// ---- Account ---------------------------------------------------------------
ApiKeyCheck Account::check_api_key() {
return ApiKeyCheck::parse(client_->request(Method::kPost, Calls::render(Call::kCheckApiKey)));
}
ModelList Account::models(const std::string& endpoint, bool default_only, std::optional<int> page_size,
const std::string& page_token) {
Query q;
if (!endpoint.empty()) {
Rules::one_of(endpoint, {"chat", "embed", "classify", "summarize", "rerank", "rate", "generate"}, "endpoint");
q.emplace_back("endpoint", endpoint);
}
if (default_only) {
Rules::require(!endpoint.empty(), "default_only needs an endpoint");
q.emplace_back("default_only", "true");
}
add_page(q, page_size, page_token);
return ModelList::parse(client_->get(Calls::render(Call::kModelList, "", q)));
}
ModelList Account::all_models(const std::string& endpoint) {
ModelList all;
std::string token;
for (std::size_t page = 0; page < CohereCaps::kMaxListPages; ++page) {
ModelList l = models(endpoint, false, CohereCaps::kMaxPageSize, token);
for (ModelInfo& m : l.models) all.models.push_back(std::move(m));
token = l.page.next_page_token;
if (token.empty() || all.models.size() >= CohereCaps::kMaxItemsReturned) break;
}
all.page.next_page_token = token;
all.page.truncated = !token.empty();
return all;
}
ModelInfo Account::model(const std::string& name) {
Rules::model(name);
return ModelInfo::parse(client_->get(Calls::render(Call::kModelGet, name)));
}
// ---- Datasets --------------------------------------------------------------
const std::vector<std::string>& DatasetUpload::types() {
static const std::vector<std::string> t = {
"embed-input", "embed-result", "cluster-result", "cluster-outliers", "reranker-finetune-input",
"single-label-classification-finetune-input", "chat-finetune-input",
"multi-label-classification-finetune-input", "batch-chat-input", "batch-openai-chat-input",
"batch-embed-v2-input", "batch-chat-v2-input"};
return t;
}
static void file_name(const std::string& name) {
Rules::require(!name.empty() && name.size() <= CohereCaps::kMaxFilenameBytes, "filename must be 1..512 bytes");
for (char c : name)
Rules::require(c != '"' && c != '\r' && c != '\n' && c != '\\' && c != '/', "filename has invalid characters");
}
void DatasetUpload::validate() const {
Rules::require(!name.empty() && name.size() <= CohereCaps::kMaxNameChars, "dataset name must be 1..256 chars");
Rules::one_of(type, types(), "dataset type");
file_name(filename);
Rules::require(!data.empty(), "data is empty");
Rules::require(data.size() + eval_data.size() <= CohereCaps::kMaxUploadBytes, "upload over 256 MiB");
Rules::require(eval_data.empty() == eval_filename.empty(), "eval_data and eval_filename go together");
if (!eval_filename.empty()) file_name(eval_filename);
Rules::require(text_separator.size() <= 16 && csv_delimiter.size() <= 4, "separator too long");
}
void DatasetFilter::validate() const {
if (!dataset_type.empty()) Rules::one_of(dataset_type, DatasetUpload::types(), "dataset type");
if (!validation_status.empty())
Rules::one_of(validation_status, {"unknown", "queued", "processing", "failed", "validated", "skipped"},
"validation status");
if (limit) Rules::require(*limit >= 1 && *limit <= CohereCaps::kDatasetListMaxLimit, "limit must be 1..1000");
if (offset) Rules::require(*offset >= 0, "offset must be >= 0");
}
std::string Datasets::create(const DatasetUpload& u) {
u.validate();
Query q;
q.emplace_back("name", u.name);
q.emplace_back("type", u.type);
if (u.keep_original_file) q.emplace_back("keep_original_file", "true");
if (u.skip_malformed_input) q.emplace_back("skip_malformed_input", "true");
for (const std::string& f : u.keep_fields) q.emplace_back("keep_fields", f);
for (const std::string& f : u.optional_fields) q.emplace_back("optional_fields", f);
if (!u.text_separator.empty()) q.emplace_back("text_separator", u.text_separator);
if (!u.csv_delimiter.empty()) q.emplace_back("csv_delimiter", u.csv_delimiter);
clients::body::MultipartBody form;
form.add_file("data", u.filename, u.content_type, u.data);
if (!u.eval_data.empty()) form.add_file("eval_data", u.eval_filename, u.content_type, u.eval_data);
return J::str(client_->post_multipart(Calls::render(Call::kDatasetCreate, "", q), form), "id");
}
std::vector<Dataset> Datasets::list(const DatasetFilter& f) {
f.validate();
Query q;
if (!f.dataset_type.empty()) q.emplace_back("datasetType", f.dataset_type);
if (!f.validation_status.empty()) q.emplace_back("validationStatus", f.validation_status);
if (f.limit) q.emplace_back("limit", std::to_string(*f.limit));
if (f.offset) q.emplace_back("offset", std::to_string(*f.offset));
if (!f.before.empty()) q.emplace_back("before", f.before);
if (!f.after.empty()) q.emplace_back("after", f.after);
Json doc = client_->get(Calls::render(Call::kDatasetList, "", q));
std::vector<Dataset> out;
if (const Json* a = J::arr(doc, "datasets"))
for (const Json& d : a->as_array()) {
if (out.size() >= CohereCaps::kMaxItemsReturned) break;
out.push_back(Dataset::parse(d));
}
return out;
}
Dataset Datasets::get(const std::string& id) {
Json doc = client_->get(Calls::render(Call::kDatasetGet, id));
const Json* d = field(doc, "dataset");
return Dataset::parse(d ? *d : Json::object());
}
Json Datasets::usage() {
Json doc = client_->get(Calls::render(Call::kDatasetUsage));
const Json* u = field(doc, "organization_usage");
return u ? *u : Json();
}
void Datasets::remove(const std::string& id) { client_->del(Calls::render(Call::kDatasetDelete, id)); }
Dataset Datasets::await_validation(const std::string& id, int max_polls, const std::function<void()>& wait) {
Rules::require(max_polls >= 1 && max_polls <= 60, "max_polls must be 1..60");
Dataset d;
for (int i = 0; i < max_polls; ++i) {
d = get(id);
if (d.validation_status == "validated" || d.validation_status == "failed" || d.validation_status == "skipped")
return d;
if (wait) wait();
}
return d;
}
// ---- Batches ---------------------------------------------------------------
BatchList Batches::list(std::optional<int> page_size, const std::string& page_token, const std::string& order_by) {
Query q;
add_page(q, page_size, page_token);
if (!order_by.empty()) {
Rules::one_of(order_by, {"created_at", "updated_at"}, "order_by");
q.emplace_back("order_by", order_by);
}
return BatchList::parse(client_->get(Calls::render(Call::kBatchList, "", q)));
}
Batch Batches::create(const std::string& name, const std::string& input_dataset_id, const std::string& model) {
Rules::require(!name.empty() && name.size() <= CohereCaps::kMaxNameChars, "batch name must be 1..256 chars");
Rules::id("dataset", input_dataset_id);
Rules::model(model);
Json b = Json::object();
b.set("name", Json(name));
b.set("input_dataset_id", Json(input_dataset_id));
b.set("model", Json(model));
Calls::check_body(Call::kBatchCreate, b);
Json doc = client_->post(Calls::render(Call::kBatchCreate), b);
const Json* x = field(doc, "batch");
return Batch::parse(x ? *x : Json::object());
}
Batch Batches::get(const std::string& id) {
Json doc = client_->get(Calls::render(Call::kBatchGet, id));
const Json* x = field(doc, "batch");
return Batch::parse(x ? *x : Json::object());
}
void Batches::cancel(const std::string& id) {
client_->request(Method::kPost, Calls::render(Call::kBatchCancel, id));
}
// ---- Embed jobs ------------------------------------------------------------
Json EmbedJobParams::to_json() const {
Rules::model(model);
Rules::id("dataset", dataset_id);
Rules::one_of(input_type, {"search_document", "search_query", "classification", "clustering", "image"}, "input_type");
for (const std::string& t : embedding_types)
Rules::one_of(t, {"float", "int8", "uint8", "binary", "ubinary", "base64"}, "embedding type");
if (!truncate.empty()) Rules::one_of(truncate, {"START", "END"}, "truncate");
Rules::require(name.size() <= CohereCaps::kMaxNameChars, "name too long");
Json b = Json::object();
b.set("model", Json(model));
b.set("dataset_id", Json(dataset_id));
b.set("input_type", Json(input_type));
if (!name.empty()) b.set("name", Json(name));
if (!embedding_types.empty()) b.set("embedding_types", strings(embedding_types));
if (!truncate.empty()) b.set("truncate", Json(truncate));
Calls::check_body(Call::kEmbedJobCreate, b);
return b;
}
std::string EmbedJobs::create(const EmbedJobParams& p) {
return J::str(client_->post(Calls::render(Call::kEmbedJobCreate), p.to_json()), "job_id");
}
std::vector<EmbedJob> EmbedJobs::list() {
Json doc = client_->get(Calls::render(Call::kEmbedJobList));
std::vector<EmbedJob> out;
if (const Json* a = J::arr(doc, "embed_jobs"))
for (const Json& j : a->as_array()) {
if (out.size() >= CohereCaps::kMaxItemsReturned) break;
out.push_back(EmbedJob::parse(j));
}
return out;
}
EmbedJob EmbedJobs::get(const std::string& id) { return EmbedJob::parse(client_->get(Calls::render(Call::kEmbedJobGet, id))); }
void EmbedJobs::cancel(const std::string& id) {
client_->request(Method::kPost, Calls::render(Call::kEmbedJobCancel, id));
}
// ---- Connectors ------------------------------------------------------------
Json ConnectorParams::to_json(bool create) const {
Json b = Json::object();
if (create) Rules::require(!name.empty() && !url.empty(), "a connector needs name and url");
if (!name.empty()) {
Rules::require(name.size() <= CohereCaps::kMaxNameChars, "name too long");
b.set("name", Json(name));
}
if (!url.empty()) {
Rules::require(Rules::is_http_url(url), "connector url must be http(s)");
b.set("url", Json(url));
}
if (create && !description.empty()) b.set("description", Json(description));
if (!excludes.empty()) b.set("excludes", strings(excludes));
if (active) b.set("active", Json(*active));
if (continue_on_failure) b.set("continue_on_failure", Json(*continue_on_failure));
if (!oauth.is_null()) {
Json o = require_object(oauth, "oauth");
Rules::require(o.find("authorize_url") && o.find("token_url"), "oauth needs authorize_url and token_url");
b.set("oauth", o);
}
if (!service_auth.is_null()) {
Json s = require_object(service_auth, "service_auth");
Rules::require(s.find("type") && s.find("token"), "service_auth needs type and token");
b.set("service_auth", s);
}
Rules::require(!b.as_object().empty(), "nothing to send");
return b;
}
std::vector<Connector> Connectors::list(std::optional<int> limit, std::optional<int> offset) {
Query q;
if (limit) {
Rules::require(*limit >= 1 && *limit <= CohereCaps::kConnectorListMaxLimit, "limit must be 1..1000");
q.emplace_back("limit", std::to_string(*limit));
}
if (offset) {
Rules::require(*offset >= 0, "offset must be >= 0");
q.emplace_back("offset", std::to_string(*offset));
}
Json doc = client_->get(Calls::render(Call::kConnectorList, "", q));
std::vector<Connector> out;
if (const Json* a = J::arr(doc, "connectors"))
for (const Json& c : a->as_array()) {
if (out.size() >= CohereCaps::kMaxItemsReturned) break;
out.push_back(Connector::parse(c));
}
return out;
}
static Connector connector_of(const Json& doc) {
const Json* c = field(doc, "connector");
return Connector::parse(c ? *c : Json::object());
}
Connector Connectors::create(const ConnectorParams& p) {
Json b = p.to_json(true);
Calls::check_body(Call::kConnectorCreate, b);
return connector_of(client_->post(Calls::render(Call::kConnectorCreate), b));
}
Connector Connectors::get(const std::string& id) { return connector_of(client_->get(Calls::render(Call::kConnectorGet, id))); }
Connector Connectors::update(const std::string& id, const ConnectorParams& p) {
Json b = p.to_json(false);
Calls::check_body(Call::kConnectorUpdate, b);
return connector_of(client_->patch(Calls::render(Call::kConnectorUpdate, id), b));
}
void Connectors::remove(const std::string& id) { client_->del(Calls::render(Call::kConnectorDelete, id)); }
std::string Connectors::oauth_authorize(const std::string& id, const std::string& after_token_redirect) {
Query q;
if (!after_token_redirect.empty()) q.emplace_back("after_token_redirect", after_token_redirect);
return J::str(client_->request(Method::kPost, Calls::render(Call::kConnectorAuthorize, id, q)), "redirect_url");
}
// ---- Fine-tuning -----------------------------------------------------------
FineTuning::ModelPage FineTuning::list(std::optional<int> page_size, const std::string& page_token,
const std::string& order_by) {
Query q;
add_page(q, page_size, page_token);
if (!order_by.empty()) {
Rules::one_of(order_by, {"created_at", "updated_at"}, "order_by");
q.emplace_back("order_by", order_by);
}
Json doc = client_->get(Calls::render(Call::kFinetuneList, "", q));
ModelPage out;
if (const Json* a = J::arr(doc, "finetuned_models"))
for (const Json& m : a->as_array()) out.models.push_back(FinetunedModel::parse(m));
out.page.next_page_token = J::str(doc, "next_page_token");
out.page.total = J::num(doc, "total_size", -1);
return out;
}
static FinetunedModel finetuned_of(const Json& doc) {
const Json* m = field(doc, "finetuned_model");
return FinetunedModel::parse(m ? *m : Json::object());
}
static void check_settings(const Json& settings) {
Rules::require(settings.is_object(), "settings must be a JSON object");
const Json* base = settings.find("base_model");
Rules::require(base && base->is_object() && base->find("base_type"), "settings.base_model.base_type is required");
const Json* ds = settings.find("dataset_id");
Rules::require(ds && ds->is_string() && llm::Service::valid_id(ds->as_string()), "settings.dataset_id is required");
}
FinetunedModel FineTuning::create(const std::string& name, const Json& settings) {
Rules::require(!name.empty() && name.size() <= CohereCaps::kMaxNameChars, "name must be 1..256 chars");
check_settings(settings);
Json b = Json::object();
b.set("name", Json(name));
b.set("settings", settings);
Calls::check_body(Call::kFinetuneCreate, b);
return finetuned_of(client_->post(Calls::render(Call::kFinetuneCreate), b));
}
FinetunedModel FineTuning::get(const std::string& id) {
return finetuned_of(client_->get(Calls::render(Call::kFinetuneGet, id)));
}
FinetunedModel FineTuning::update(const std::string& id, const std::string& name, const Json& settings) {
Rules::require(!name.empty() || !settings.is_null(), "give a name or settings");
Json b = Json::object();
if (!name.empty()) b.set("name", Json(name));
if (!settings.is_null()) {
check_settings(settings);
b.set("settings", settings);
}
Calls::check_body(Call::kFinetuneUpdate, b);
return finetuned_of(client_->patch(Calls::render(Call::kFinetuneUpdate, id), b));
}
void FineTuning::remove(const std::string& id) { client_->del(Calls::render(Call::kFinetuneDelete, id)); }
Json FineTuning::events(const std::string& id, std::optional<int> page_size, const std::string& page_token) {
Query q;
add_page(q, page_size, page_token);
return client_->get(Calls::render(Call::kFinetuneEvents, id, q));
}
Json FineTuning::step_metrics(const std::string& id, std::optional<int> page_size, const std::string& page_token) {
Query q;
add_page(q, page_size, page_token);
return client_->get(Calls::render(Call::kFinetuneMetrics, id, q));
}
// ---- Parse and transcription ------------------------------------------------
Json Documents::parse(const std::string& model, const std::string& image_url, const std::string& output_format) {
Rules::model(model);
Rules::image_url(image_url);
Json doc = Json::object();
doc.set("type", Json("image_url"));
doc.set("image_url", Json(image_url));
Json b = Json::object();
b.set("model", Json(model));
b.set("document", doc);
if (!output_format.empty()) {
Rules::one_of(output_format, {"markdown", "blocks"}, "output_format");
b.set("output_format", Json(output_format));
}
Calls::check_body(Call::kParse, b);
return client_->post(Calls::render(Call::kParse), b);
}
std::string Documents::transcribe(const std::string& model, const std::string& language, const std::string& filename,
const std::string& content_type, const std::string& audio,
std::optional<double> temperature) {
Rules::model(model);
Rules::require(!language.empty() && language.size() <= 16, "language is required (for example en)");
file_name(filename);
Rules::require(!audio.empty(), "audio is empty");
Rules::require(audio.size() <= CohereCaps::kMaxUploadBytes, "audio over 256 MiB");
clients::body::MultipartBody form;
form.add_text("model", model);
form.add_text("language", language);
if (temperature) {
Rules::range(*temperature, 0.0, 2.0, "temperature");
form.add_text("temperature", std::to_string(*temperature));
}
form.add_file("file", filename, content_type.empty() ? "application/octet-stream" : content_type, audio);
return J::str(client_->post_multipart(Calls::render(Call::kTranscribe), form), "text");
}
} // namespace molo::cohere
+150
View File
@@ -0,0 +1,150 @@
#pragma once
// Account-level resources: models and key check, datasets, batches, embed
// jobs, connectors (deprecated in the spec), fine-tuned models (deprecated in
// the spec), plus Parse and audio Transcribe. Destructive and cancelling
// calls are explicit methods with no defaults. List walks are capped and
// report truncation.
#include <optional>
#include <string>
#include <vector>
#include "vendors/cohere/types.h"
#include "llm/api_client.h"
namespace molo::cohere {
class Account : public llm::Service {
public:
explicit Account(std::shared_ptr<llm::ApiClient> client) : llm::Service(std::move(client)) {}
ApiKeyCheck check_api_key(); // POST /v1/check-api-key
ModelList models(const std::string& endpoint = "", bool default_only = false,
std::optional<int> page_size = std::nullopt,
const std::string& page_token = ""); // GET /v1/models, one page
ModelList all_models(const std::string& endpoint = ""); // walks pages (cap 20)
ModelInfo model(const std::string& name); // GET /v1/models/{model}
};
struct DatasetUpload {
std::string name;
std::string type; // DatasetType, e.g. embed-input, chat-finetune-input, batch-chat-v2-input
std::string filename;
std::string content_type = "application/octet-stream";
std::string data;
std::string eval_filename;
std::string eval_data;
bool keep_original_file = false;
bool skip_malformed_input = false;
std::vector<std::string> keep_fields;
std::vector<std::string> optional_fields;
std::string text_separator;
std::string csv_delimiter;
static const std::vector<std::string>& types();
void validate() const;
};
struct DatasetFilter {
std::string dataset_type;
std::string validation_status; // unknown queued processing failed validated skipped
std::optional<int> limit; // 1..1000
std::optional<int> offset;
std::string before, after; // ISO timestamps
void validate() const;
};
class Datasets : public llm::Service {
public:
explicit Datasets(std::shared_ptr<llm::ApiClient> client) : llm::Service(std::move(client)) {}
std::string create(const DatasetUpload& upload); // POST /v1/datasets (multipart); returns the id
std::vector<Dataset> list(const DatasetFilter& filter = {});
Dataset get(const std::string& id);
Json usage(); // organization_usage (bytes as sent)
void remove(const std::string& id); // DELETE, explicit
// Polls get() until validated/failed/skipped, at most `max_polls` (<= 60) times; the caller
// supplies the wait through `wait` (no sleeping inside the library). Returns the last state.
Dataset await_validation(const std::string& id, int max_polls, const std::function<void()>& wait);
};
class Batches : public llm::Service {
public:
explicit Batches(std::shared_ptr<llm::ApiClient> client) : llm::Service(std::move(client)) {}
BatchList list(std::optional<int> page_size = std::nullopt, const std::string& page_token = "",
const std::string& order_by = "");
Batch create(const std::string& name, const std::string& input_dataset_id, const std::string& model);
Batch get(const std::string& id);
void cancel(const std::string& id); // POST /v2/batches/{id}:cancel, explicit
};
struct EmbedJobParams {
std::string model = "embed-v4.0";
std::string dataset_id;
std::string input_type = "search_document";
std::string name;
std::vector<std::string> embedding_types;
std::string truncate; // START or END
Json to_json() const;
};
class EmbedJobs : public llm::Service {
public:
explicit EmbedJobs(std::shared_ptr<llm::ApiClient> client) : llm::Service(std::move(client)) {}
std::string create(const EmbedJobParams& params); // returns job_id
std::vector<EmbedJob> list();
EmbedJob get(const std::string& id);
void cancel(const std::string& id); // explicit
};
struct ConnectorParams {
std::string name;
std::string url;
std::string description;
std::vector<std::string> excludes;
std::optional<bool> active;
std::optional<bool> continue_on_failure;
Json oauth; // {client_id, client_secret, authorize_url, token_url, scope}
Json service_auth; // {type, token}
Json to_json(bool create) const;
};
class Connectors : public llm::Service {
public:
explicit Connectors(std::shared_ptr<llm::ApiClient> client) : llm::Service(std::move(client)) {}
std::vector<Connector> list(std::optional<int> limit = std::nullopt, std::optional<int> offset = std::nullopt);
Connector create(const ConnectorParams& params);
Connector get(const std::string& id);
Connector update(const std::string& id, const ConnectorParams& params);
void remove(const std::string& id); // explicit
std::string oauth_authorize(const std::string& id, const std::string& after_token_redirect = "");
};
class FineTuning : public llm::Service {
public:
explicit FineTuning(std::shared_ptr<llm::ApiClient> client) : llm::Service(std::move(client)) {}
struct ModelPage {
std::vector<FinetunedModel> models;
Page page;
};
ModelPage list(std::optional<int> page_size = std::nullopt, const std::string& page_token = "",
const std::string& order_by = "");
// `settings` = {base_model:{base_type,...}, dataset_id, hyperparameters?, wandb?}. Costs money.
FinetunedModel create(const std::string& name, const Json& settings);
FinetunedModel get(const std::string& id);
FinetunedModel update(const std::string& id, const std::string& name, const Json& settings);
void remove(const std::string& id); // explicit
Json events(const std::string& id, std::optional<int> page_size = std::nullopt, const std::string& page_token = "");
Json step_metrics(const std::string& id, std::optional<int> page_size = std::nullopt, const std::string& page_token = "");
};
class Documents : public llm::Service {
public:
explicit Documents(std::shared_ptr<llm::ApiClient> client) : llm::Service(std::move(client)) {}
// POST /v2/parse: document is an image URL (http(s) or data URL); output_format markdown|blocks.
Json parse(const std::string& model, const std::string& image_url, const std::string& output_format = "");
// POST /v2/audio/transcriptions (multipart); returns the text.
std::string transcribe(const std::string& model, const std::string& language, const std::string& filename,
const std::string& content_type, const std::string& audio,
std::optional<double> temperature = std::nullopt);
};
} // namespace molo::cohere
+64
View File
@@ -0,0 +1,64 @@
#include "vendors/cohere/rules.h"
#include "vendors/cohere/caps.h"
#include "llm/api_client.h"
namespace molo::cohere {
void Rules::fail(const std::string& why) { throw llm::ApiError::invalid("cohere", why); }
void Rules::model(const std::string& name) {
require(!name.empty() && name.size() <= CohereCaps::kMaxModelChars, "model must be 1..128 chars");
for (char c : name)
require((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' ||
c == '_' || c == '.' || c == ':',
"model has invalid characters");
}
void Rules::id(const std::string& what, const std::string& id) {
require(llm::Service::valid_id(id), "bad " + what + " id");
}
void Rules::range(double v, double lo, double hi, const char* what) {
require(v >= lo && v <= hi, std::string(what) + " must be " + std::to_string(lo) + ".." + std::to_string(hi));
}
void Rules::temperature(double v) { range(v, 0.0, 1.0, "temperature"); }
void Rules::top_p(double v) { range(v, 0.01, 0.99, "p"); }
void Rules::top_k(long v) { require(v >= 0 && v <= CohereCaps::kMaxTopK, "k must be 0..500"); }
void Rules::priority(long v) { require(v >= 0 && v <= CohereCaps::kMaxPriority, "priority must be 0..999"); }
void Rules::stop(const std::vector<std::string>& stop) {
require(stop.size() <= CohereCaps::kMaxStopSequences, "at most 5 stop sequences");
for (const std::string& s : stop) require(!s.empty(), "stop sequences must not be empty");
}
void Rules::page_size(long v) { require(v >= 1 && v <= CohereCaps::kMaxPageSize, "page_size must be 1..1000"); }
void Rules::one_of(const std::string& value, const std::vector<std::string>& allowed, const char* what) {
for (const std::string& a : allowed)
if (a == value) return;
std::string list;
for (const std::string& a : allowed) list += (list.empty() ? "" : ", ") + a;
fail(std::string(what) + " must be one of: " + list);
}
bool Rules::is_http_url(const std::string& url) {
if (url.size() > 8192) return false;
bool ok = url.rfind("http://", 0) == 0 || url.rfind("https://", 0) == 0;
if (!ok) return false;
for (char c : url)
if (c == ' ' || c == '\r' || c == '\n') return false;
return url.find('/', 8) != std::string::npos || url.size() > 8;
}
void Rules::image_url(const std::string& url) {
if (url.rfind("data:image/", 0) == 0) {
require(url.find(";base64,") != std::string::npos, "data URL must be base64");
require(url.size() <= 8u * 1024u * 1024u, "image data URL over 8 MiB");
return;
}
require(is_http_url(url), "image url must be http(s) or a data:image/...;base64, URL");
}
} // namespace molo::cohere
+36
View File
@@ -0,0 +1,36 @@
#pragma once
// Input rules shared by every request type. All throw llm::ApiError::invalid
// (HTTP 400 INVALID_ARGUMENT, vendor "cohere") before any network traffic.
#include <string>
#include <vector>
#include "llm/error.h"
namespace molo::cohere {
struct Rules {
[[noreturn]] static void fail(const std::string& why);
static void require(bool ok, const std::string& why) {
if (!ok) fail(why);
}
// 1..128 of [A-Za-z0-9._:-]; the server decides which names exist.
static void model(const std::string& name);
// Single path segment id (llm::Service::valid_id).
static void id(const std::string& what, const std::string& id);
static void range(double v, double lo, double hi, const char* what);
static void temperature(double v); // 0..1
static void top_p(double v); // 0.01..0.99
static void top_k(long v); // 0..500
static void priority(long v); // 0..999
static void stop(const std::vector<std::string>& stop); // <= 5, non-empty
static void page_size(long v); // 1..1000
// One of the listed values.
static void one_of(const std::string& value, const std::vector<std::string>& allowed, const char* what);
// http(s) URL, or data:...;base64, URL for images, <= 8 MiB
static bool is_http_url(const std::string& url);
static void image_url(const std::string& url);
};
} // namespace molo::cohere
+424
View File
@@ -0,0 +1,424 @@
#!/usr/bin/env python3
"""Check src/vendors/cohere/calls.cpp against Cohere's published OpenAPI document.
Cohere publishes cohere-openapi.yaml in the cohere-ai/cohere-developer-experience
repository (the file their Fern SDK generation reads). This checker parses that
file with a small block-YAML reader (Python 3 standard library only, no PyYAML)
and fails (exit 1) when:
row a declared call's HTTP method + path or operationId is not in the spec;
field a request body field or query parameter we send is not in the spec's
request body / parameters of that operation;
required a field or parameter the spec marks required is not declared in the row;
schema a field we read (SCHEMAS table) is not a property of that schema;
response a declared response schema is neither a component nor an operationId;
coverage a spec operation is neither in the table nor in EXCLUDED_OPERATIONS.
python3 -I src/vendors/cohere/tools/check_spec.py [--spec path-to-openapi.yaml] [alternative calls.cpp]
Network required unless --spec is given. No credentials.
"""
import os
import re
import sys
import urllib.request
HERE = os.path.dirname(os.path.abspath(__file__))
CALLS = os.path.join(HERE, "..", "calls.cpp")
SPEC_URL = ("https://raw.githubusercontent.com/cohere-ai/cohere-developer-experience/main/"
"cohere-openapi.yaml")
# Operations that exist in the spec but are deliberately not wrapped (reason each).
EXCLUDED_OPERATIONS = {}
STR = re.compile(r'"([^"]*)"')
METHODS = ("get", "post", "put", "patch", "delete")
# ---------------------------------------------------------------- YAML reader
class Yaml:
"""Block-style YAML subset: mappings, sequences, plain / quoted scalars (single
line or folded continuation), block scalars (| >), empty flow [] and {}."""
def __init__(self, text):
self.lines = []
for raw in text.replace("\t", " ").split("\n"):
self.lines.append(raw.rstrip("\r"))
self.i = 0
@staticmethod
def indent_of(line):
return len(line) - len(line.lstrip(" "))
def skip_blank(self):
while self.i < len(self.lines):
s = self.lines[self.i].strip()
if s == "" or s.startswith("#") or s == "---":
self.i += 1
else:
return
def parse(self):
self.skip_blank()
return self.node(self.indent_of(self.lines[self.i]))
def node(self, indent):
self.skip_blank()
if self.i >= len(self.lines):
return None
line = self.lines[self.i]
if self.indent_of(line) < indent:
return None
body = line.strip()
if body.startswith("- ") or body == "-":
return self.seq(self.indent_of(line))
if self.key_split(body) is not None:
return self.mapping(self.indent_of(line))
self.i += 1
return self.scalar(body, self.indent_of(line))
@staticmethod
def key_split(body):
"""(key, rest) for `key: rest`, else None. Handles quoted keys."""
if body[:1] in ("'", '"'):
q = body[0]
j = 1
while j < len(body):
if body[j] == q and (q == "'" or body[j - 1] != "\\"):
break
j += 1
if j + 1 < len(body) and body[j + 1] == ":" and (j + 2 == len(body) or body[j + 2] == " "):
return body[1:j], body[j + 2:].strip()
return None
if body[:1] in ("[", "{", "|", ">", "#", "*", "&", "!"):
return None
m = re.match(r"^([^:#]+?|[^:#]*?[^:\s#][^#]*?):(?:\s+(.*))?$", body)
if not m:
return None
key = m.group(1)
if " #" in key:
return None
return key.strip(), (m.group(2) or "").strip()
def mapping(self, indent):
out = {}
while True:
self.skip_blank()
if self.i >= len(self.lines):
break
line = self.lines[self.i]
if self.indent_of(line) != indent:
break
body = line.strip()
kv = self.key_split(body)
if kv is None or body.startswith("- "):
break
key, rest = kv
self.i += 1
out[key] = self.value(rest, indent)
return out
def seq(self, indent):
out = []
while True:
self.skip_blank()
if self.i >= len(self.lines):
break
line = self.lines[self.i]
if self.indent_of(line) != indent:
break
body = line.strip()
if not (body.startswith("- ") or body == "-"):
break
rest = body[1:].strip()
if rest == "":
self.i += 1
out.append(self.node(indent + 1))
elif self.key_split(rest) is not None and not rest.startswith(("'", '"')) or \
(rest[:1] in ("'", '"') and self.key_split(rest) is not None):
# `- key: value`: treat the text after "- " as a mapping line at indent+2
self.lines[self.i] = " " * (indent + 2) + rest
out.append(self.mapping(indent + 2))
else:
self.i += 1
out.append(self.scalar(rest, indent))
return out
def value(self, rest, indent):
if rest == "" or rest.startswith("#"):
self.skip_blank()
if self.i < len(self.lines):
nxt = self.lines[self.i]
ni = self.indent_of(nxt)
nb = nxt.strip()
if ni > indent or (ni == indent and (nb.startswith("- ") or nb == "-")):
return self.node(ni)
return None
if rest[0] in "|>":
return self.block_scalar(rest, indent)
return self.scalar(rest, indent)
def block_scalar(self, header, indent):
fold = header[0] == ">"
buf = []
block_indent = None
while self.i < len(self.lines):
line = self.lines[self.i]
if line.strip() == "":
buf.append("")
self.i += 1
continue
li = self.indent_of(line)
if li <= indent:
break
if block_indent is None:
block_indent = li
buf.append(line[block_indent:] if li >= block_indent else line.strip())
self.i += 1
text = ("\n" if not fold else " ").join(buf).strip()
return text
def scalar(self, text, indent):
if text[0] in ("'", '"'):
q = text[0]
acc = text
# continuation lines until the closing quote
while not self.closed(acc, q) and self.i < len(self.lines):
acc += " " + self.lines[self.i].strip()
self.i += 1
body = acc[1:Yaml.end_of(acc, q)]
if q == '"':
body = body.replace('\\"', '"').replace("\\n", "\n").replace("\\\\", "\\")
else:
body = body.replace("''", "'")
return body
if text in ("[]", "{}"):
return [] if text == "[]" else {}
if text[0] in "[{":
acc = text
while acc.count("[") + acc.count("{") > acc.count("]") + acc.count("}") and self.i < len(self.lines):
acc += " " + self.lines[self.i].strip()
self.i += 1
return acc # flow collections beyond [] and {} are kept as text (not used by the checker)
# plain scalar: strip trailing comment, fold continuation lines that are more indented
text = re.sub(r"\s+#.*$", "", text)
while self.i < len(self.lines):
nxt = self.lines[self.i]
if nxt.strip() == "" or self.indent_of(nxt) <= indent:
break
if self.key_split(nxt.strip()) is not None and nxt.strip()[:1] not in ("'", '"'):
break
text += " " + nxt.strip()
self.i += 1
return text
@staticmethod
def end_of(acc, q):
j = 1
while j < len(acc):
c = acc[j]
if q == '"' and c == "\\":
j += 2
continue
if c == q:
if q == "'" and j + 1 < len(acc) and acc[j + 1] == "'":
j += 2
continue
return j
j += 1
return len(acc)
@staticmethod
def closed(acc, q):
"""True when the quoted scalar that starts acc has its closing quote."""
j = 1
while j < len(acc):
c = acc[j]
if q == '"' and c == "\\":
j += 2
continue
if c == q:
if q == "'" and j + 1 < len(acc) and acc[j + 1] == "'":
j += 2
continue
return True
j += 1
return False
# ------------------------------------------------------------------ spec model
class Spec:
def __init__(self, doc):
self.doc = doc
self.schemas = doc["components"]["schemas"]
self.params = doc["components"].get("parameters", {})
def ref(self, node):
seen = 0
while isinstance(node, dict) and "$ref" in node and seen < 20:
name = node["$ref"].split("/")[-1]
kind = node["$ref"].split("/")[-2]
node = (self.params if kind == "parameters" else self.schemas).get(name)
seen += 1
return node
def props(self, node, depth=0):
"""Union of property names (and required names) through $ref / allOf / oneOf / anyOf / items."""
props, required = {}, set()
node = self.ref(node)
if not isinstance(node, dict) or depth > 12:
return props, required
for key in ("allOf", "oneOf", "anyOf"):
for sub in node.get(key) or []:
p, r = self.props(sub, depth + 1)
props.update(p)
if key == "allOf":
required |= r
if isinstance(node.get("properties"), dict):
props.update(node["properties"])
if isinstance(node.get("required"), list):
required |= set(node["required"])
return props, required
def descend(self, node, path):
"""Follow a dotted property path; arrays are entered through items."""
for step in [p for p in path.split(".") if p]:
node = self.ref(node)
if isinstance(node, dict) and node.get("type") == "array" and "items" in node:
node = self.ref(node["items"])
props, _ = self.props(node)
if step not in props:
return None
node = props[step]
node = self.ref(node)
if isinstance(node, dict) and node.get("type") == "array" and "items" in node:
node = self.ref(node["items"])
return node
def operations(self):
for path, item in self.doc["paths"].items():
for method, op in item.items():
if method in METHODS and isinstance(op, dict):
yield method.upper(), path, op
def op_params(self, op):
out = {}
for p in op.get("parameters") or []:
p = self.ref(p)
if isinstance(p, dict) and p.get("in") in ("query", "path"):
out[p["name"]] = (p["in"], bool(p.get("required")))
return out
def op_body(self, op):
rb = op.get("requestBody")
if not rb:
return None, set()
content = rb.get("content") or {}
for ct in ("application/json", "multipart/form-data"):
if ct in content:
return self.props(content[ct].get("schema"))
return {}, set()
def op_response(self, op):
r = (op.get("responses") or {}).get("200") or {}
content = r.get("content") or {}
if "application/json" in content:
return content["application/json"].get("schema")
return None
# ------------------------------------------------------------------------ main
def rows(text, begin, end):
a, b = text.index(begin), text.index(end)
out = []
for line in text[a:b].splitlines()[1:]:
s = line.strip()
if s.startswith("{") and '"' in s:
out.append(STR.findall(s))
return out
def load_spec(argv):
if "--spec" in argv:
i = argv.index("--spec")
path = argv[i + 1]
del argv[i:i + 2]
with open(path, encoding="utf-8") as f:
return f.read()
req = urllib.request.Request(SPEC_URL, headers={"User-Agent": "molo-cohere-spec-check/1"})
with urllib.request.urlopen(req, timeout=120) as r:
return r.read().decode("utf-8", "replace")
def main():
argv = sys.argv[1:]
spec_text = load_spec(argv)
calls_path = argv[0] if argv else CALLS
text = open(calls_path).read()
calls = rows(text, "// CALLS-BEGIN", "// CALLS-END")
schemas = rows(text, "// SCHEMAS-BEGIN", "// SCHEMAS-END")
spec = Spec(Yaml(spec_text).parse())
ops = {(m, p): op for m, p, op in spec.operations()}
by_id = {op.get("operationId"): op for op in ops.values()}
bad = []
covered = set()
for method, path, op_id, body, query, response in calls:
op = ops.get((method, path))
if op is None:
bad.append("%s %s: not a path of the spec" % (method, path))
continue
covered.add((method, path))
if op.get("operationId") != op_id:
bad.append("%s %s: operationId is %s in the spec, %s in the table"
% (method, path, op.get("operationId"), op_id))
params = spec.op_params(op)
body_props, body_required = spec.op_body(op)
declared_body = {f for f in body.split(",") if f}
declared_query = {q for q in query.split(",") if q}
for f in sorted(declared_body):
if f not in body_props:
bad.append("%s %s: body field %s is not in the request body" % (method, path, f))
for q in sorted(declared_query):
if q not in params or params[q][0] != "query":
bad.append("%s %s: query parameter %s is not in the spec" % (method, path, q))
for name, (where, required) in params.items():
if where == "query" and required and name not in declared_query:
bad.append("%s %s: required query parameter %s missing in the row" % (method, path, name))
for f in sorted(body_required):
if f in body_props and f not in declared_body:
bad.append("%s %s: required body field %s missing in the row" % (method, path, f))
if response and response not in spec.schemas and response not in by_id:
bad.append("%s %s: response schema %s is neither a component nor an operationId" % (method, path, response))
for (method, path), op in sorted(ops.items()):
if (method, path) not in covered and op.get("operationId") not in EXCLUDED_OPERATIONS:
bad.append("unwrapped operation %s %s (%s): add a row or an exclusion"
% (method, path, op.get("operationId")))
checked_fields = 0
for name, fields in schemas:
head, _, tail = name.partition(".")
if head in spec.schemas:
node = spec.schemas[head]
elif head in by_id:
node = spec.op_response(by_id[head])
else:
bad.append("schema %s: neither a component nor an operationId" % head)
continue
target = spec.descend(node, tail) if tail else spec.ref(node)
if target is None:
bad.append("schema %s: property path not found" % name)
continue
props, _ = spec.props(target)
for f in filter(None, fields.split(",")):
checked_fields += 1
if f not in props:
bad.append("schema %s: field %s is not a property" % (name, f))
if bad:
print("SPEC CHECK FAILED")
for b in bad:
print(" " + b)
return 1
print("cohere spec ok: %d calls of %d spec operations (exclusions: %d), %d schema rows, %d fields"
% (len(calls), len(ops), len(EXCLUDED_OPERATIONS), len(schemas), checked_fields))
return 0
if __name__ == "__main__":
sys.exit(main())
+213
View File
@@ -0,0 +1,213 @@
#include "vendors/cohere/types.h"
#include <algorithm>
namespace molo::cohere {
std::string J::str(const Json& doc, const char* key, const std::string& fallback) {
const Json* v = doc.is_object() ? doc.find(key) : nullptr;
return v && v->is_string() ? v->as_string() : fallback;
}
long J::num(const Json& doc, const char* key, long fallback) {
const Json* v = doc.is_object() ? doc.find(key) : nullptr;
return v && v->is_number() ? v->as_int() : fallback;
}
double J::real(const Json& doc, const char* key, double fallback) {
const Json* v = doc.is_object() ? doc.find(key) : nullptr;
return v && v->is_number() ? v->as_number() : fallback;
}
bool J::flag(const Json& doc, const char* key, bool fallback) {
const Json* v = doc.is_object() ? doc.find(key) : nullptr;
return v && v->is_bool() ? v->as_bool() : fallback;
}
const Json* J::obj(const Json& doc, const char* key) {
const Json* v = doc.is_object() ? doc.find(key) : nullptr;
return v && v->is_object() ? v : nullptr;
}
const Json* J::arr(const Json& doc, const char* key) {
const Json* v = doc.is_object() ? doc.find(key) : nullptr;
return v && v->is_array() ? v : nullptr;
}
std::vector<std::string> J::strings(const Json& doc, const char* key) {
std::vector<std::string> out;
if (const Json* a = arr(doc, key))
for (const Json& v : a->as_array())
if (v.is_string()) out.push_back(v.as_string());
return out;
}
TokenUsage TokenUsage::parse(const Json* usage) {
TokenUsage u;
if (!usage || !usage->is_object()) return u;
u.present = true;
if (const Json* t = J::obj(*usage, "tokens")) {
u.input_tokens = J::num(*t, "input_tokens");
u.output_tokens = J::num(*t, "output_tokens");
}
if (const Json* b = J::obj(*usage, "billed_units")) {
u.billed_input_tokens = J::num(*b, "input_tokens");
u.billed_output_tokens = J::num(*b, "output_tokens");
u.search_units = J::num(*b, "search_units");
u.classifications = J::num(*b, "classifications");
}
u.cached_tokens = J::num(*usage, "cached_tokens");
return u;
}
static Page page_of(const Json& doc, const char* total_key = nullptr) {
Page p;
p.next_page_token = J::str(doc, "next_page_token");
if (total_key) p.total = J::num(doc, total_key, -1);
return p;
}
bool ModelInfo::supports(const std::string& endpoint) const {
return std::find(endpoints.begin(), endpoints.end(), endpoint) != endpoints.end();
}
ModelInfo ModelInfo::parse(const Json& doc) {
ModelInfo m;
m.name = J::str(doc, "name");
m.deprecated = J::flag(doc, "is_deprecated");
m.finetuned = J::flag(doc, "finetuned");
m.context_length = J::real(doc, "context_length");
m.tokenizer_url = J::str(doc, "tokenizer_url");
m.endpoints = J::strings(doc, "endpoints");
m.default_endpoints = J::strings(doc, "default_endpoints");
m.features = J::strings(doc, "features");
m.raw = doc;
return m;
}
ModelList ModelList::parse(const Json& doc) {
ModelList l;
if (const Json* a = J::arr(doc, "models"))
for (const Json& m : a->as_array()) l.models.push_back(ModelInfo::parse(m));
l.page = page_of(doc);
return l;
}
const ModelInfo* ModelList::find(const std::string& name) const {
for (const ModelInfo& m : models)
if (m.name == name) return &m;
return nullptr;
}
ApiKeyCheck ApiKeyCheck::parse(const Json& doc) {
ApiKeyCheck c;
c.valid = J::flag(doc, "valid");
c.organization_id = J::str(doc, "organization_id");
c.owner_id = J::str(doc, "owner_id");
return c;
}
bool Batch::terminal() const {
return status == "BATCH_STATUS_COMPLETED" || status == "BATCH_STATUS_FAILED" ||
status == "BATCH_STATUS_CANCELED";
}
Batch Batch::parse(const Json& doc) {
Batch b;
b.id = J::str(doc, "id");
b.name = J::str(doc, "name");
b.status = J::str(doc, "status");
b.created_at = J::str(doc, "created_at");
b.updated_at = J::str(doc, "updated_at");
b.input_dataset_id = J::str(doc, "input_dataset_id");
b.output_dataset_id = J::str(doc, "output_dataset_id");
b.model = J::str(doc, "model");
b.status_reason = J::str(doc, "status_reason");
b.input_tokens = J::str(doc, "input_tokens");
b.output_tokens = J::str(doc, "output_tokens");
b.num_records = J::num(doc, "num_records");
b.num_successful_records = J::num(doc, "num_successful_records");
b.num_failed_records = J::num(doc, "num_failed_records");
b.raw = doc;
return b;
}
BatchList BatchList::parse(const Json& doc) {
BatchList l;
if (const Json* a = J::arr(doc, "batches"))
for (const Json& b : a->as_array()) l.batches.push_back(Batch::parse(b));
l.page = page_of(doc);
return l;
}
bool EmbedJob::terminal() const { return status == "complete" || status == "cancelled" || status == "failed"; }
EmbedJob EmbedJob::parse(const Json& doc) {
EmbedJob j;
j.job_id = J::str(doc, "job_id");
j.name = J::str(doc, "name");
j.status = J::str(doc, "status");
j.created_at = J::str(doc, "created_at");
j.input_dataset_id = J::str(doc, "input_dataset_id");
j.output_dataset_id = J::str(doc, "output_dataset_id");
j.model = J::str(doc, "model");
j.truncate = J::str(doc, "truncate");
j.raw = doc;
return j;
}
DatasetPart DatasetPart::parse(const Json& doc) {
DatasetPart p;
p.id = J::str(doc, "id");
p.name = J::str(doc, "name");
p.url = J::str(doc, "url");
p.original_url = J::str(doc, "original_url");
p.index = J::num(doc, "index");
p.size_bytes = J::num(doc, "size_bytes");
p.num_rows = J::num(doc, "num_rows");
return p;
}
Dataset Dataset::parse(const Json& doc) {
Dataset d;
d.id = J::str(doc, "id");
d.name = J::str(doc, "name");
d.created_at = J::str(doc, "created_at");
d.updated_at = J::str(doc, "updated_at");
d.dataset_type = J::str(doc, "dataset_type");
d.validation_status = J::str(doc, "validation_status");
d.validation_error = J::str(doc, "validation_error");
d.validation_warnings = J::strings(doc, "validation_warnings");
if (const Json* a = J::arr(doc, "dataset_parts"))
for (const Json& p : a->as_array()) d.parts.push_back(DatasetPart::parse(p));
d.raw = doc;
return d;
}
Connector Connector::parse(const Json& doc) {
Connector c;
c.id = J::str(doc, "id");
c.organization_id = J::str(doc, "organization_id");
c.name = J::str(doc, "name");
c.url = J::str(doc, "url");
c.created_at = J::str(doc, "created_at");
c.updated_at = J::str(doc, "updated_at");
c.auth_type = J::str(doc, "auth_type");
c.auth_status = J::str(doc, "auth_status");
c.excludes = J::strings(doc, "excludes");
c.active = J::flag(doc, "active");
c.continue_on_failure = J::flag(doc, "continue_on_failure");
c.raw = doc;
return c;
}
FinetunedModel FinetunedModel::parse(const Json& doc) {
FinetunedModel m;
m.id = J::str(doc, "id");
m.name = J::str(doc, "name");
m.creator_id = J::str(doc, "creator_id");
m.organization_id = J::str(doc, "organization_id");
m.status = J::str(doc, "status");
m.created_at = J::str(doc, "created_at");
m.updated_at = J::str(doc, "updated_at");
m.completed_at = J::str(doc, "completed_at");
m.last_used = J::str(doc, "last_used");
if (const Json* s = J::obj(doc, "settings")) m.settings = *s;
return m;
}
} // namespace molo::cohere
+125
View File
@@ -0,0 +1,125 @@
#pragma once
// Typed views of Cohere documents. Every parser reads only fields the call
// table's schema rows declare; the full document stays available as `raw`.
#include <string>
#include <vector>
#include "clients/body/json.h"
namespace molo::cohere {
using Json = clients::body::Json;
// Tolerant readers: a missing or mistyped field gives the fallback.
struct J {
static std::string str(const Json& doc, const char* key, const std::string& fallback = "");
static long num(const Json& doc, const char* key, long fallback = 0);
static double real(const Json& doc, const char* key, double fallback = 0);
static bool flag(const Json& doc, const char* key, bool fallback = false);
static const Json* obj(const Json& doc, const char* key);
static const Json* arr(const Json& doc, const char* key);
static std::vector<std::string> strings(const Json& doc, const char* key);
};
// Usage (chat) and ApiMeta (everything else) share these fields.
struct TokenUsage {
long input_tokens = 0; // tokens.input_tokens
long output_tokens = 0; // tokens.output_tokens
long billed_input_tokens = 0; // billed_units.input_tokens
long billed_output_tokens = 0;
long search_units = 0;
long classifications = 0;
long cached_tokens = 0;
bool present = false;
static TokenUsage parse(const Json* usage);
};
struct Page {
std::string next_page_token;
long total = -1; // total_size / total_count when sent
bool truncated = false; // more pages existed than the walk cap allowed
};
struct ModelInfo {
std::string name;
bool deprecated = false;
bool finetuned = false;
double context_length = 0;
std::string tokenizer_url;
std::vector<std::string> endpoints;
std::vector<std::string> default_endpoints;
std::vector<std::string> features;
Json raw;
bool supports(const std::string& endpoint) const;
static ModelInfo parse(const Json& doc);
};
struct ModelList {
std::vector<ModelInfo> models;
Page page;
const ModelInfo* find(const std::string& name) const;
static ModelList parse(const Json& doc);
};
struct ApiKeyCheck {
bool valid = false;
std::string organization_id;
std::string owner_id;
static ApiKeyCheck parse(const Json& doc);
};
struct Batch {
std::string id, name, status, created_at, updated_at, input_dataset_id, output_dataset_id, model;
std::string status_reason, input_tokens, output_tokens; // token counts are strings in the spec
long num_records = 0, num_successful_records = 0, num_failed_records = 0;
Json raw;
bool terminal() const; // COMPLETED, FAILED or CANCELED
static Batch parse(const Json& doc);
};
struct BatchList {
std::vector<Batch> batches;
Page page;
static BatchList parse(const Json& doc);
};
struct EmbedJob {
std::string job_id, name, status, created_at, input_dataset_id, output_dataset_id, model, truncate;
Json raw;
bool terminal() const; // complete, cancelled or failed
static EmbedJob parse(const Json& doc);
};
struct DatasetPart {
std::string id, name, url, original_url;
long index = 0, size_bytes = 0, num_rows = 0;
static DatasetPart parse(const Json& doc);
};
struct Dataset {
std::string id, name, created_at, updated_at, dataset_type, validation_status, validation_error;
std::vector<std::string> validation_warnings;
std::vector<DatasetPart> parts;
Json raw;
bool validated() const { return validation_status == "validated"; }
static Dataset parse(const Json& doc);
};
struct Connector {
std::string id, organization_id, name, url, created_at, updated_at, auth_type, auth_status;
std::vector<std::string> excludes;
bool active = false, continue_on_failure = false;
Json raw;
static Connector parse(const Json& doc);
};
struct FinetunedModel {
std::string id, name, creator_id, organization_id, status, created_at, updated_at, completed_at, last_used;
Json settings;
bool ready() const { return status == "STATUS_READY"; }
static FinetunedModel parse(const Json& doc);
};
} // namespace molo::cohere
+33
View File
@@ -0,0 +1,33 @@
// Routes: GET /ai/cohere (page), /status, /calls (no credential needed)
#include <cstdlib>
#include "support/Assert.hpp"
using namespace molo::example::test;
int main() {
setenv("MOLO_ENV_FILES", "off", 1);
unsetenv("COHERE_API_KEY");
unsetenv("CO_API_KEY");
Fixture f;
auto page = f.client->get("/ai/cohere");
check(page.status == 200, "page 200");
check(contains(page.headers.get("Content-Type"), "text/html"), "html");
check(contains(page.body, "Cohere API") && contains(page.body, "viewport"), "title and mobile viewport");
check(contains(page.body, "/embedded/ai_cohere.js"), "script wired");
check(f.client->post("/ai/cohere", "{}", "application/json").status == 405, "post page is 405");
auto st = f.client->get("/ai/cohere/status");
check(st.status == 200 && contains(st.body, "\"configured\":false"), "status without a key");
check(contains(st.body, "COHERE_API_KEY") && contains(st.body, "command-a-03-2025") &&
contains(st.body, "embed-v4.0") && contains(st.body, "rerank-v3.5"),
"status names credential env and models");
check(f.client->post("/ai/cohere/status", "{}", "application/json").status == 405, "post status is 405");
auto calls = f.client->get("/ai/cohere/calls");
check(calls.status == 200 && contains(calls.body, "/v2/chat") && contains(calls.body, "/v2/embed") &&
contains(calls.body, "/v1/check-api-key") && contains(calls.body, "/v2/batches/{id}:cancel"),
"call table listed");
check(f.client->post("/ai/cohere/calls", "{}", "application/json").status == 405, "post calls is 405");
return finish("ai/cohere/page");
}
+114
View File
@@ -0,0 +1,114 @@
// Routes: POST /ai/cohere/* (405, 422, dry runs, 503 without a credential)
#include <cstdlib>
#include "support/Assert.hpp"
using namespace molo::example::test;
int main() {
setenv("MOLO_ENV_FILES", "off", 1);
unsetenv("COHERE_API_KEY");
unsetenv("CO_API_KEY");
Fixture f;
auto post = [&](const std::string& route, const std::string& body) {
return f.client->post("/ai/cohere/" + route, body, "application/json");
};
const char* all[] = {"chat", "stream", "tools", "legacy", "embed", "rerank", "classify", "tokenize",
"detokenize", "parse", "models", "key", "datasets", "batches", "embedjobs",
"connectors", "finetune"};
for (const char* r : all) {
std::string route = std::string("/ai/cohere/") + r;
check(f.client->get(route).status == 405, route + " get is 405");
check(post(r, "not json").status == 422, route + " bad json is 422");
check(post(r, "[1]").status == 422, route + " non-object is 422");
}
// 422: input rules run before anything else
check(post("chat", "{}").status == 422, "chat needs prompt");
check(post("chat", "{\"prompt\":\"hi\",\"max_tokens\":0}").status == 422, "max_tokens 0");
check(post("chat", "{\"prompt\":\"hi\",\"temperature\":2}").status == 422, "temperature over 1");
check(post("chat", "{\"prompt\":\"hi\",\"thinking\":\"maybe\"}").status == 422, "bad thinking");
check(post("chat", "{\"prompt\":\"hi\",\"safety_mode\":\"LOOSE\"}").status == 422, "bad safety mode");
check(post("chat", "{\"prompt\":\"hi\",\"model\":\"a b\"}").status == 422, "bad model");
check(post("chat", "{\"prompt\":\"hi\",\"stop\":[1]}").status == 422, "stop must be strings");
check(post("tools", "{\"prompt\":\"w\",\"tool_choice\":\"AUTO\"}").status == 422, "bad tool_choice");
check(post("embed", "{}").status == 422, "embed needs texts");
check(post("embed", "{\"texts\":[\"a\"],\"input_type\":\"x\"}").status == 422, "bad input_type");
check(post("rerank", "{\"query\":\"q\"}").status == 422, "rerank needs documents");
check(post("rerank", "{\"documents\":[\"a\"]}").status == 422, "rerank needs query");
check(post("classify", "{\"inputs\":[\"x\"]}").status == 422, "classify needs model or examples");
check(post("tokenize", "{\"text\":\"\"}").status == 422 || post("tokenize", "{}").status == 422, "tokenize needs text");
check(post("detokenize", "{\"tokens\":\"x\"}").status == 422, "tokens must be an array");
check(post("parse", "{\"model\":\"m\",\"image_url\":\"ftp://x\"}").status == 422, "parse needs an http image url");
check(post("models", "{\"action\":\"fly\"}").status == 422, "bad models action");
check(post("models", "{\"action\":\"get\"}").status == 422, "models get needs name");
check(post("datasets", "{\"action\":\"fly\"}").status == 422, "bad datasets action");
check(post("datasets", "{\"action\":\"get\",\"id\":\"../x\"}").status == 422, "bad dataset id");
check(post("datasets", "{\"action\":\"create\",\"name\":\"n\",\"type\":\"nope\",\"filename\":\"a.jsonl\",\"data\":\"x\"}").status == 422,
"bad dataset type");
check(post("datasets", "{\"action\":\"create\",\"name\":\"n\",\"type\":\"embed-input\",\"filename\":\"a.jsonl\",\"data_base64\":\"@@\"}").status == 422,
"bad base64");
check(post("batches", "{\"action\":\"create\",\"name\":\"n\"}").status == 422, "batch create needs dataset and model");
check(post("embedjobs", "{\"action\":\"create\"}").status == 422, "embed job needs dataset_id");
check(post("connectors", "{\"action\":\"create\",\"name\":\"n\",\"url\":\"ftp://x\"}").status == 422, "connector url must be http");
check(post("finetune", "{\"action\":\"create\",\"name\":\"n\"}").status == 422, "finetune needs settings");
check(post("finetune", "{\"action\":\"get\",\"id\":\"a/b\"}").status == 422, "bad finetune id");
check(post("legacy", "{\"action\":\"fly\"}").status == 422, "bad legacy action");
check(post("legacy", "{\"action\":\"generate\",\"body\":{\"bogus\":1}}").status == 422, "legacy fields come from the call table");
// dry runs: paid or resource-changing routes show the exact request
auto dry = post("chat", "{\"prompt\":\"hi\",\"thinking\":\"enabled\"}");
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, "\"model\":\"command-a-03-2025\"") &&
contains(dry.body, "/v2/chat"),
"chat is a dry run without confirm");
check(contains(post("stream", "{\"prompt\":\"hi\"}").body, "\"stream\":true"), "stream preview");
check(contains(post("tools", "{\"prompt\":\"w\",\"strict\":true}").body, "\"strict_tools\":true"), "strict tools preview");
check(contains(post("embed", "{\"texts\":[\"a\"]}").body, "/v2/embed"), "embed is a dry run");
check(contains(post("rerank", "{\"query\":\"q\",\"documents\":[\"a\"]}").body, "\"dry_run\":true"), "rerank is a dry run");
check(contains(post("classify", "{\"inputs\":[\"x\"],\"model\":\"m\"}").body, "/v1/classify"), "classify is a dry run");
check(contains(post("datasets", "{\"action\":\"delete\",\"id\":\"ds-1\"}").body, "\"dry_run\":true"), "dataset delete is a dry run");
check(contains(post("datasets", "{\"action\":\"create\",\"name\":\"n\",\"type\":\"embed-input\",\"filename\":\"a.jsonl\",\"data\":\"{}\"}").body, "\"bytes\":2"),
"dataset create is a dry run");
check(contains(post("batches", "{\"action\":\"cancel\",\"id\":\"b1\"}").body, "\"dry_run\":true"), "batch cancel is a dry run");
check(contains(post("batches", "{\"action\":\"create\",\"name\":\"n\",\"input_dataset_id\":\"d\",\"model\":\"m\"}").body, "/v2/batches"),
"batch create is a dry run");
check(contains(post("embedjobs", "{\"action\":\"create\",\"dataset_id\":\"d\"}").body, "\"dry_run\":true"), "embed job create is a dry run");
check(contains(post("embedjobs", "{\"action\":\"cancel\",\"id\":\"j\"}").body, "\"dry_run\":true"), "embed job cancel is a dry run");
check(contains(post("connectors", "{\"action\":\"delete\",\"id\":\"c1\"}").body, "\"dry_run\":true"), "connector delete is a dry run");
check(contains(post("finetune", "{\"action\":\"delete\",\"id\":\"f1\"}").body, "\"dry_run\":true"), "finetune delete is a dry run");
check(contains(post("finetune", "{\"action\":\"create\",\"name\":\"n\",\"settings\":{\"a\":1}}").body, "\"dry_run\":true"),
"finetune create is a dry run");
check(contains(post("legacy", "{\"action\":\"summarize\",\"body\":{\"text\":\"t\"}}").body, "/v1/summarize"), "legacy is a dry run");
check(contains(post("parse", "{\"model\":\"m\",\"image_url\":\"https://example.com/a.png\"}").body, "\"dry_run\":true"), "parse is a dry run");
// 503: reads and confirmed calls need the credential
auto not_configured = [&](const std::string& route, const std::string& body, const std::string& what) {
auto res = post(route, body);
check(res.status == 503 && contains(res.body, "cohere_not_configured"), what + " without key is 503");
};
not_configured("models", "{}", "models");
not_configured("models", "{\"action\":\"get\",\"name\":\"command-a-03-2025\"}", "model get");
not_configured("key", "{}", "key check");
not_configured("tokenize", "{\"text\":\"hello\"}", "tokenize");
not_configured("detokenize", "{\"tokens\":[1,2]}", "detokenize");
not_configured("datasets", "{\"action\":\"list\"}", "datasets list");
not_configured("datasets", "{\"action\":\"get\",\"id\":\"ds-1\"}", "datasets get");
not_configured("datasets", "{\"action\":\"usage\"}", "datasets usage");
not_configured("batches", "{\"action\":\"list\"}", "batches list");
not_configured("batches", "{\"action\":\"get\",\"id\":\"b1\"}", "batches get");
not_configured("embedjobs", "{\"action\":\"list\"}", "embed jobs list");
not_configured("connectors", "{\"action\":\"list\"}", "connectors list");
not_configured("finetune", "{\"action\":\"list\"}", "finetune list");
not_configured("finetune", "{\"action\":\"events\",\"id\":\"f1\"}", "finetune events");
not_configured("chat", "{\"prompt\":\"hi\",\"confirm\":true}", "confirmed chat");
not_configured("stream", "{\"prompt\":\"hi\",\"confirm\":true}", "confirmed stream");
not_configured("tools", "{\"prompt\":\"hi\",\"confirm\":true}", "confirmed tools");
not_configured("embed", "{\"texts\":[\"a\"],\"confirm\":true}", "confirmed embed");
not_configured("rerank", "{\"query\":\"q\",\"documents\":[\"a\"],\"confirm\":true}", "confirmed rerank");
not_configured("classify", "{\"inputs\":[\"x\"],\"model\":\"m\",\"confirm\":true}", "confirmed classify");
not_configured("datasets", "{\"action\":\"delete\",\"id\":\"ds-1\",\"confirm\":true}", "confirmed dataset delete");
not_configured("batches", "{\"action\":\"cancel\",\"id\":\"b1\",\"confirm\":true}", "confirmed batch cancel");
not_configured("connectors", "{\"action\":\"delete\",\"id\":\"c1\",\"confirm\":true}", "confirmed connector delete");
not_configured("finetune", "{\"action\":\"delete\",\"id\":\"f1\",\"confirm\":true}", "confirmed finetune delete");
not_configured("legacy", "{\"action\":\"summarize\",\"body\":{\"text\":\"t\"},\"confirm\":true}", "confirmed legacy");
return finish("ai/cohere/routes");
}
+400
View File
@@ -0,0 +1,400 @@
#pragma once
// FIXTURES taken verbatim from the x-fern-examples of cohere-openapi.yaml
// (cohere-ai/cohere-developer-experience, read 2026-10-25), JSON re-serialized.
// Generated once from the document; edit nothing by hand. Nothing here is a
// recorded live reply: the spec examples are the only vendor output we have.
namespace cohere_test {
// FIXTURE: v2/chat example 0 response
inline const char* kChatBasic =
R"COHERE({"id": "c14c80c3-18eb-4519-9460-6c92edd8cfb4", "message": {"role": "assistant", "content": [{"type": "text", "text": "LLMs stand for Large Language Models, which are a type of neural network model specialized in processing and generating human language. They are designed to understand and respond to natural language input and have become increasingly popular and valuable in recent years.\n\nLLMs are trained on vast amounts of text data, enabling them to learn patterns, grammar, and semantic meanings present in the language. These models can then be used for various natural language processing tasks, such as text generation, summarization, question answering, machine translation, sentiment analysis, and even some aspects of natural language understanding.\n\nSome well-known examples of LLMs include:\n\n1. GPT-3 (Generative Pre-trained Transformer 3) \u2014 An open-source LLM developed by OpenAI, capable of generating human-like text and performing various language tasks.\n\n2. BERT (Bidirectional Encoder Representations from Transformers) \u2014 A Google-developed LLM that is particularly good at understanding contextual relationships in text, and is widely used for natural language understanding tasks like sentiment analysis and named entity recognition.\n\n3. T5 (Text-to-Text Transfer Transformer) \u2014 Also from Google, T5 is a flexible LLM that frames all language tasks as text-to-text problems, where the model learns to generate output text based on input text prompts.\n\n4. RoBERTa (Robustly Optimized BERT Approach) \u2014 A variant of BERT that uses additional training techniques to improve performance.\n\n5. DeBERTa (Decoding-enhanced BERT with disentangled attention) \u2014 Another variant of BERT that introduces a new attention mechanism.\n\nLLMs have become increasingly powerful and larger in scale, improving the accuracy and sophistication of language tasks. They are also being used as a foundation for developing various applications, including chatbots,)COHERE"
R"COHERE( content recommendation systems, language translation services, and more.\nThe future of LLMs holds the potential for even more sophisticated language technologies, with ongoing research and development focused on enhancing their capabilities, improving efficiency, and exploring their applications in various domains."}]}, "finish_reason": "COMPLETE", "usage": {"billed_units": {"input_tokens": 5, "output_tokens": 418}, "tokens": {"input_tokens": 71, "output_tokens": 418}}})COHERE"
;
// FIXTURE: v2/chat example 1 response
inline const char* kChatCitations =
R"COHERE({"id": "c14c80c3-18eb-4519-9460-6c92edd8cfb4", "message": {"role": "assistant", "content": [{"type": "text", "text": "Both NSync and Backstreet Boys were extremely popular at the turn of the millennium. Backstreet Boys had massive album sales across the globe, great singles sales, plenty of chart-topping releases, hyped tours, and tremendous media coverage. NSync also had huge sales, with their album No Strings Attached selling 2.4 million units in its first week. They also had the second fastest-selling album of the Soundscan era before 2015, with Celebrity debuting at 1.88 million units sold.\n\nWhile it is difficult to say for sure which of the two bands was more popular, Backstreet Boys did have success in some countries that were traditionally hard to penetrate for Western artists."}], "citations": [{"start": 36, "end": 84, "text": "extremely popular at the turn of the millennium.", "sources": [{"type": "document", "id": "doc:1", "document": {"snippet": "\u2193 Skip to Main Content\n\nMusic industry \u2013 One step closer to being accurate\n\nCSPC: NSYNC Popularity Analysis\n\nMJD Posted on February 9, 2018 Posted in CSPC 27 Comments Tagged with Boy band, N'Sync\n\nAt the turn of the millennium three teen acts were huge in the US, the Backstreet Boys, Britney Spears and NSYNC. The latter is the only one we haven\u2019t study so far. It took 15 years and Adele to break their record of 2,4 million units sold of No Strings Attached in its first week alone.\n\nIt wasn\u2019t a fluke, as the second fastest selling album of the Soundscan era prior 2015, was also theirs since Celebrity debuted with 1,88 million units sold.", "title": "CSPC: NSYNC Popularity Analysis - ChartMasters"}}]}, {"start": 105, "end": 141, "text": "massive album sales across the globe", "sources": [{"type": "document", "id": "doc:0", "document": {"snippet": "\u2193 Skip to Main Content\n\nMusic industry \u2013 One step closer to being accurate\n\nCSPC: Backstreet Boys Popularity Analysis\n\nHer)COHERE"
R"COHERE(n\u00e1n Lopez Posted on February 9, 2017 Posted in CSPC 72 Comments Tagged with Backstreet Boys, Boy band\n\nAt one point, Backstreet Boys defined success: massive albums sales across the globe, great singles sales, plenty of chart topping releases, hugely hyped tours and tremendous media coverage.\n\nIt is true that they benefited from extraordinarily good market conditions in all markets. After all, the all-time record year for the music business, as far as revenues in billion dollars are concerned, was actually 1999. That is, back when this five men group was at its peak.", "title": "CSPC: Backstreet Boys Popularity Analysis - ChartMasters"}}]}, {"start": 143, "end": 162, "text": "great singles sales", "sources": [{"type": "document", "id": "doc:0", "document": {"snippet": "\u2193 Skip to Main Content\n\nMusic industry \u2013 One step closer to being accurate\n\nCSPC: Backstreet Boys Popularity Analysis\n\nHern\u00e1n Lopez Posted on February 9, 2017 Posted in CSPC 72 Comments Tagged with Backstreet Boys, Boy band\n\nAt one point, Backstreet Boys defined success: massive albums sales across the globe, great singles sales, plenty of chart topping releases, hugely hyped tours and tremendous media coverage.\n\nIt is true that they benefited from extraordinarily good market conditions in all markets. After all, the all-time record year for the music business, as far as revenues in billion dollars are concerned, was actually 1999. That is, back when this five men group was at its peak.", "title": "CSPC: Backstreet Boys Popularity Analysis - ChartMasters"}}]}, {"start": 164, "end": 196, "text": "plenty of chart-topping releases", "sources": [{"type": "document", "id": "doc:0", "document": {"snippet": "\u2193 Skip to Main Content\n\nMusic industry \u2013 One step closer to being accurate\n\nCSPC: Backstreet Boys Popularity Analysis\n\nHern\u00e1n Lopez Posted on February 9, 2017 Posted in CSPC 72 Comments Tagged with Backstreet Boys, Boy band\n\nAt one point, Backstree)COHERE"
R"COHERE(t Boys defined success: massive albums sales across the globe, great singles sales, plenty of chart topping releases, hugely hyped tours and tremendous media coverage.\n\nIt is true that they benefited from extraordinarily good market conditions in all markets. After all, the all-time record year for the music business, as far as revenues in billion dollars are concerned, was actually 1999. That is, back when this five men group was at its peak.", "title": "CSPC: Backstreet Boys Popularity Analysis - ChartMasters"}}]}, {"start": 198, "end": 209, "text": "hyped tours", "sources": [{"type": "document", "id": "doc:0", "document": {"snippet": "\u2193 Skip to Main Content\n\nMusic industry \u2013 One step closer to being accurate\n\nCSPC: Backstreet Boys Popularity Analysis\n\nHern\u00e1n Lopez Posted on February 9, 2017 Posted in CSPC 72 Comments Tagged with Backstreet Boys, Boy band\n\nAt one point, Backstreet Boys defined success: massive albums sales across the globe, great singles sales, plenty of chart topping releases, hugely hyped tours and tremendous media coverage.\n\nIt is true that they benefited from extraordinarily good market conditions in all markets. After all, the all-time record year for the music business, as far as revenues in billion dollars are concerned, was actually 1999. That is, back when this five men group was at its peak.", "title": "CSPC: Backstreet Boys Popularity Analysis - ChartMasters"}}]}, {"start": 215, "end": 241, "text": "tremendous media coverage.", "sources": [{"type": "document", "id": "doc:0", "document": {"snippet": "\u2193 Skip to Main Content\n\nMusic industry \u2013 One step closer to being accurate\n\nCSPC: Backstreet Boys Popularity Analysis\n\nHern\u00e1n Lopez Posted on February 9, 2017 Posted in CSPC 72 Comments Tagged with Backstreet Boys, Boy band\n\nAt one point, Backstreet Boys defined success: massive albums sales across the globe, great singles sales, plenty of chart topping releases, hugely hyped tours and tremen)COHERE"
R"COHERE(dous media coverage.\n\nIt is true that they benefited from extraordinarily good market conditions in all markets. After all, the all-time record year for the music business, as far as revenues in billion dollars are concerned, was actually 1999. That is, back when this five men group was at its peak.", "title": "CSPC: Backstreet Boys Popularity Analysis - ChartMasters"}}]}, {"start": 280, "end": 350, "text": "album No Strings Attached selling 2.4 million units in its first week.", "sources": [{"type": "document", "id": "doc:1", "document": {"snippet": "\u2193 Skip to Main Content\n\nMusic industry \u2013 One step closer to being accurate\n\nCSPC: NSYNC Popularity Analysis\n\nMJD Posted on February 9, 2018 Posted in CSPC 27 Comments Tagged with Boy band, N'Sync\n\nAt the turn of the millennium three teen acts were huge in the US, the Backstreet Boys, Britney Spears and NSYNC. The latter is the only one we haven\u2019t study so far. It took 15 years and Adele to break their record of 2,4 million units sold of No Strings Attached in its first week alone.\n\nIt wasn\u2019t a fluke, as the second fastest selling album of the Soundscan era prior 2015, was also theirs since Celebrity debuted with 1,88 million units sold.", "title": "CSPC: NSYNC Popularity Analysis - ChartMasters"}}]}, {"start": 369, "end": 430, "text": "second fastest-selling album of the Soundscan era before 2015", "sources": [{"type": "document", "id": "doc:1", "document": {"snippet": "\u2193 Skip to Main Content\n\nMusic industry \u2013 One step closer to being accurate\n\nCSPC: NSYNC Popularity Analysis\n\nMJD Posted on February 9, 2018 Posted in CSPC 27 Comments Tagged with Boy band, N'Sync\n\nAt the turn of the millennium three teen acts were huge in the US, the Backstreet Boys, Britney Spears and NSYNC. The latter is the only one we haven\u2019t study so far. It took 15 years and Adele to break their record of 2,4 million units sold of No Strings Attached in its first week alone.\n\nIt wasn\u2019t )COHERE"
R"COHERE(a fluke, as the second fastest selling album of the Soundscan era prior 2015, was also theirs since Celebrity debuted with 1,88 million units sold.", "title": "CSPC: NSYNC Popularity Analysis - ChartMasters"}}]}, {"start": 437, "end": 483, "text": "Celebrity debuting at 1.88 million units sold.", "sources": [{"type": "document", "id": "doc:1", "document": {"snippet": "\u2193 Skip to Main Content\n\nMusic industry \u2013 One step closer to being accurate\n\nCSPC: NSYNC Popularity Analysis\n\nMJD Posted on February 9, 2018 Posted in CSPC 27 Comments Tagged with Boy band, N'Sync\n\nAt the turn of the millennium three teen acts were huge in the US, the Backstreet Boys, Britney Spears and NSYNC. The latter is the only one we haven\u2019t study so far. It took 15 years and Adele to break their record of 2,4 million units sold of No Strings Attached in its first week alone.\n\nIt wasn\u2019t a fluke, as the second fastest selling album of the Soundscan era prior 2015, was also theirs since Celebrity debuted with 1,88 million units sold.", "title": "CSPC: NSYNC Popularity Analysis - ChartMasters"}}]}, {"start": 589, "end": 677, "text": "success in some countries that were traditionally hard to penetrate for Western artists.", "sources": [{"type": "document", "id": "doc:2", "document": {"snippet": "1997, 1998, 2000 and 2001 also rank amongst some of the very best years.\nYet the way many music consumers \u2013 especially teenagers and young women\u2019s \u2013 embraced their output deserves its own chapter. If Jonas Brothers and more recently One Direction reached a great level of popularity during the past decade, the type of success achieved by Backstreet Boys is in a completely different level as they really dominated the business for a few years all over the world, including in some countries that were traditionally hard to penetrate for Western artists.\n\nWe will try to analyze the extent of that hegemony with this new article with final results which will more than su)COHERE"
R"COHERE(rprise many readers.", "title": "CSPC: Backstreet Boys Popularity Analysis - ChartMasters"}}]}]}, "finish_reason": "COMPLETE", "usage": {"billed_units": {"input_tokens": 682, "output_tokens": 143}, "tokens": {"input_tokens": 1380, "output_tokens": 434}}})COHERE"
;
// FIXTURE: v2/chat example 3 response
inline const char* kChatToolThinking =
R"COHERE({"id": "9e5f00aa-bf1e-481a-abe3-0eceac18c3ec", "message": {"role": "assistant", "content": [{"type": "thinking", "thinking": "I will first find the sales summary for 29th September 2023. Then, I will find the details of the products in the 'Electronics' category."}], "tool_calls": [{"id": "query_daily_sales_report_hgxxmkby3wta", "type": "function", "function": {"name": "query_daily_sales_report", "arguments": "{\"day\": \"2023-09-29\"}"}}, {"id": "query_product_catalog_rpg0z5h8yyz2", "type": "function", "function": {"name": "query_product_catalog", "arguments": "{\"category\": \"Electronics\"}"}}]}, "finish_reason": "TOOL_CALL", "usage": {"billed_units": {"input_tokens": 127, "output_tokens": 69}, "tokens": {"input_tokens": 1032, "output_tokens": 124}}})COHERE"
;
// FIXTURE: v2/chat example 2 stream
inline const char* kStreamText =
R"COHERE(event: message-start
data: {"type": "message-start", "id": "29f14a5a-11de-4cae-9800-25e4747408ea", "delta": {"message": {"role": "assistant"}}}
event: content-start
data: {"type": "content-start", "index": 0, "delta": {"message": {"content": {"type": "text", "text": ""}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": "LL"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": "Ms"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " stand"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " for"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " Large"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " Language"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " Models"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": ","}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " which"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " are"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " a"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " type"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " of"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " neural"}}}}
event: content-delta
data: {"type": "content-delta)COHERE"
R"COHERE(", "index": 0, "delta": {"message": {"content": {"text": " network"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " model"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " specialized"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " in"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " processing"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " and"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " generating"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " human"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": " language"}}}}
event: content-delta
data: {"type": "content-delta", "index": 0, "delta": {"message": {"content": {"text": "."}}}}
event: content-end
data: {"type": "content-end", "index": 0}
event: message-end
data: {"type": "message-end", "delta": {"finish_reason": "COMPLETE", "usage": {"billed_units": {"input_tokens": 5, "output_tokens": 26}, "tokens": {"input_tokens": 71, "output_tokens": 26}}}}
)COHERE"
;
// FIXTURE: v2/chat example 6 stream
inline const char* kStreamTools =
R"COHERE(event: message-start
data: {"type": "message-start", "id": "2edfdf70-019c-4f7a-be20-3cdbfaa3dca6", "delta": {"message": {"role": "assistant"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "I"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " will"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " use"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " the"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " query"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "_"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "daily"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "_"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "sales"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "_"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "report"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " tool"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " to"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " find"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " the"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " sales"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " summary"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " for"}}}
)COHERE"
R"COHERE(
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " 2"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "9"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "th"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " September"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " 2"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "0"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "2"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "3"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "."}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " I"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " will"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " also"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " use"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " the"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " query"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "_"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "product"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "_"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "catalog"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta")COHERE"
R"COHERE(, "delta": {"message": {"tool_plan": " tool"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " to"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " find"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " the"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " details"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " of"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " the"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " products"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " in"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " the"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " Electronics"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": " category"}}}
event: tool-plan-delta
data: {"type": "tool-plan-delta", "delta": {"message": {"tool_plan": "."}}}
event: tool-call-start
data: {"type": "tool-call-start", "index": 0, "delta": {"message": {"tool_calls": {"id": "query_daily_sales_report_j3f0adww9pmr", "type": "function", "function": {"name": "query_daily_sales_report", "arguments": ""}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 0, "delta": {"message": {"tool_calls": {"function": {"arguments": "{\""}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 0, "delta": {"message": {"tool_calls": {"function": {"arguments": "day"}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 0, "delta": {"message": {"tool_calls": {"function": {"arguments": "\": "}}}}}
event: tool-call-delt)COHERE"
R"COHERE(a
data: {"type": "tool-call-delta", "index": 0, "delta": {"message": {"tool_calls": {"function": {"arguments": "\""}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 0, "delta": {"message": {"tool_calls": {"function": {"arguments": "2"}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 0, "delta": {"message": {"tool_calls": {"function": {"arguments": "0"}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 0, "delta": {"message": {"tool_calls": {"function": {"arguments": "2"}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 0, "delta": {"message": {"tool_calls": {"function": {"arguments": "3"}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 0, "delta": {"message": {"tool_calls": {"function": {"arguments": "-"}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 0, "delta": {"message": {"tool_calls": {"function": {"arguments": "0"}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 0, "delta": {"message": {"tool_calls": {"function": {"arguments": "9"}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 0, "delta": {"message": {"tool_calls": {"function": {"arguments": "-"}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 0, "delta": {"message": {"tool_calls": {"function": {"arguments": "2"}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 0, "delta": {"message": {"tool_calls": {"function": {"arguments": "9"}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 0, "delta": {"message": {"tool_calls": {"function": {"arguments": "\"}"}}}}}
event: tool-call-end
data: {"type": "tool-call-end", "index": 0}
event: tool-call-start
data: {"type": "tool-call-start", "index": 1, "delta": {"message": {"tool_calls": {"id": "query_product_catalog_c66nf11r6s8g", "type": "function", "function": {"name": "query_product_catalog", "arguments": ""}}}}}
event: too)COHERE"
R"COHERE(l-call-delta
data: {"type": "tool-call-delta", "index": 1, "delta": {"message": {"tool_calls": {"function": {"arguments": "{\""}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 1, "delta": {"message": {"tool_calls": {"function": {"arguments": "category"}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 1, "delta": {"message": {"tool_calls": {"function": {"arguments": "\": "}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 1, "delta": {"message": {"tool_calls": {"function": {"arguments": "\""}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 1, "delta": {"message": {"tool_calls": {"function": {"arguments": "Electron"}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 1, "delta": {"message": {"tool_calls": {"function": {"arguments": "ics"}}}}}
event: tool-call-delta
data: {"type": "tool-call-delta", "index": 1, "delta": {"message": {"tool_calls": {"function": {"arguments": "\"}"}}}}}
event: tool-call-end
data: {"type": "tool-call-end", "index": 1}
event: message-end
data: {"type": "message-end", "delta": {"finish_reason": "TOOL_CALL", "usage": {"billed_units": {"input_tokens": 126, "output_tokens": 84}, "tokens": {"input_tokens": 1589, "output_tokens": 135}}}}
)COHERE"
;
// FIXTURE: v2/rerank example 0 response
inline const char* kRerankV2 =
R"COHERE({"id": "9f75d94c-5047-4f9f-ad14-4a42ef5d3ff3", "results": [{"index": 2, "relevance_score": 0.8428898, "relevant_spans": [{"start": 0, "end": 139, "score": 0.9997249}, {"start": 139, "end": 165, "score": 0.33812714}]}, {"index": 0, "relevance_score": 0.085889876, "relevant_spans": [{"start": 0, "end": 64, "score": 0.33498195}]}, {"index": 4, "relevance_score": 0.04667134, "relevant_spans": [{"start": 0, "end": 82, "score": 0.15934227}, {"start": 82, "end": 175, "score": 0.118320465}]}], "meta": {"api_version": {"version": "2"}, "billed_units": {"input_tokens": 192}}})COHERE"
;
// FIXTURE: v1/tokenize example 0 response
inline const char* kTokenize =
R"COHERE({"tokens": [10002, 2261, 2012, 8, 2792, 43], "token_strings": ["token", "ize", " me", "!", " :", "D"], "meta": {"api_version": {"version": "1"}}})COHERE"
;
// FIXTURE: v1/detokenize example 0 response
inline const char* kDetokenize =
R"COHERE({"text": "tokenize me! :D", "meta": {"api_version": {"version": "1"}}})COHERE"
;
// FIXTURE: v1/classify example 0 response
inline const char* kClassify =
R"COHERE({"id": "86886163-b3f3-4e36-8554-60eca7696216", "classifications": [{"classification_type": "single-label", "confidence": 0.5661598, "confidences": [0.5661598], "id": "842d12fe-934b-4b71-82c2-c581eca00718", "input": "Confirm your email address", "labels": {"Not spam": {"confidence": 0.5661598}, "Spam": {"confidence": 0.43384025}}, "prediction": "Not spam", "predictions": ["Not spam"]}, {"classification_type": "single-label", "confidence": 0.9909811, "confidences": [0.9909811], "id": "e1a39b3e-1ecd-41d2-be75-90ed726f7b9e", "input": "hey i need u to send some $", "labels": {"Not spam": {"confidence": 0.009018883}, "Spam": {"confidence": 0.9909811}}, "prediction": "Spam", "predictions": ["Spam"]}], "meta": {"api_version": {"version": "1"}, "billed_units": {"classifications": 2}}})COHERE"
;
// FIXTURE: v2/embed example 1 response
inline const char* kEmbedV2 =
R"COHERE({"id": "5807ee2e-0cda-445a-9ec8-864c60a06606", "texts": [], "images": [{"width": 400, "height": 400, "format": "jpeg", "bit_depth": 24}], "embeddings": {"float": [[-0.007247925, -0.041229248, -0.023223877, -0.08392334, -0.03378296, -0.008308411, -0.049926758, 0.041625977, 0.043151855, 0.03652954, -0.05154419, 0.011787415, -0.02784729, -0.024230957, -0.018295288, -0.0440979, 0.032928467, -0.015007019, 0.009315491, -0.028213501, -0.00022602081, -0.0074157715, -0.000975132, 0.05783081, 0.029510498, 0.024871826, -0.009422302, -0.028701782, -0.021118164, -0.019088745, -0.0038433075, 0.04083252, 0.03024292, -0.010154724, -0.008163452, 0.04269409, 0.017471313, -0.010017395, 0.006629944, 0.011047363, 0.013542175, -0.007926941, -0.024932861, -0.05960083, -0.05404663, 0.037384033, -0.049621582, -0.024002075, 0.040039062, 0.02645874, 0.010261536, -0.028244019, 0.016479492, 0.014266968, -0.043823242, -0.022262573, -0.0057678223, -0.04800415, 0.041015625, 0.01537323, -0.021530151, -0.014663696, 0.051849365, -0.025558472, 0.045776367, -0.025665283, -0.005821228, 0.02973938, 0.053131104, 0.020706177, -0.004600525, 0.0046920776, 0.02558899, -0.05319214, -0.058013916, 0.080444336, -0.00068187714, 0.031311035, 0.032440186, -0.051086426, -0.003534317, 0.046325684, -0.032440186, -0.03894043, -0.0071907043, -0.004627228, -0.01826477, -0.027755737, 0.040802002, 0.019363403, -0.009727478, 0.0064468384, 0.056488037, 0.018585205, -0.017974854, -0.08514404, 5.0604343e-05, -0.014839172, 0.01586914, 0.00017666817, 0.02267456, -0.05105591, 0.007785797, -0.02684021, 0.0064849854, 0.014411926, 0.0013427734, -0.012611389, 0.043701172, 0.012290955, -0.030731201, 0.034729004, 0.015289307, -0.037475586, -0.030838013, 0.010009766, -0.028244019, 0.051635742, 0.01725769, 0.013977051, 0.008102417, 0.028121948, 0.02079773, 0.0027256012, 0.009185791, 0.0016012192, -0.038116455, -0.008331299, -0.028076172, 0.018463135, -0.02154541, 0.021240234, 0.023376465, 0.02961731, -0.028305054, -0.023101807, -0.0106811)COHERE"
R"COHERE(52, -0.0072021484, -0.04321289, 0.0058517456, 0.030792236, -0.021102905, 0.050933838, 0.0060157776, 0.0128479, 0.025146484, -0.006099701, 0.023345947, 0.023971558, 0.015510559, -0.009895325, -0.04071045, 0.049835205, 0.0053100586, -0.028930664, 0.017578125, -0.0048217773, -0.0042762756, -0.034240723, -0.03253174, 0.035827637, 0.01574707, 0.034851074, 0.070129395, 0.011749268, -0.009223938, 0.02470398, -0.005115509, 0.016723633, 0.04937744, -0.032928467, 0.031280518, -0.00023400784, 0.010169983, -0.01071167, 0.010520935, 0.022338867, -0.0259552, 0.044769287, 0.0070610046, -0.012451172, -0.04156494, 0.047088623, -0.017578125, 0.012741089, -0.016479492, 0.0023078918, -0.008331299, 0.021591187, 0.01473999, -0.018081665, 0.033081055, -0.057556152, 0.008621216, 0.013954163, -0.009742737, -0.015548706, 0.015281677, -0.005958557, 0.0065307617, 0.01979065, 0.041778564, -0.02684021, 0.027709961, -0.07672119, 0.023406982, -0.037902832, 0.035339355, -0.021881104, 0.056732178, 0.03466797, 0.0059318542, -0.058654785, 0.025375366, 0.015029907, 0.002380371, -0.024230957, 0.014541626, -0.006641388, -0.01864624, 0.012290955, 0.0007929802, -0.009277344, 0.04953003, -0.004081726, 0.0029258728, -0.017181396, 0.0074920654, -0.0001707077, 0.04220581, 0.008972168, -0.0071525574, 0.0015583038, 0.034362793, -0.019058228, 0.013626099, 0.022613525, -0.0061149597, 0.017669678, 0.015586853, 0.034973145, 0.02217102, -0.045013428, -0.009864807, 0.07244873, 0.010177612, 0.029724121, -0.018829346, -0.034057617, -0.018859863, 0.059936523, -0.0076408386, 0.021331787, -0.013786316, 0.015281677, 0.016235352, -0.039855957, -0.02748108, -0.033416748, 0.016174316, 0.026489258, 0.0049095154, -0.026000977, 0.00831604, -0.019851685, -0.021408081, 0.023010254, 0.030075073, 0.0335083, -0.05493164, 0.019515991, -0.020401001, -0.0061073303, 0.018997192, 0.020126343, -0.027740479, -0.038116455, 0.0052948, -0.008613586, -0.016494751, -0.001247406, 0.022644043, 0.008300781, -0.02104187, 0.016693115, -0.0032901764, 0)COHERE"
R"COHERE(.012046814, -0.023468018, -0.007259369, 0.031234741, 0.06604004, 0.051635742, 0.0009441376, -0.006084442, 0.025619507, -0.006881714, 0.02999878, 0.050964355, 0.017715454, -0.024856567, -0.010070801, 0.05319214, -0.03652954, 0.011810303, -0.011978149, 0.013046265, -0.016662598, 0.017166138, -0.005542755, -0.07989502, 0.029220581, 0.056488037, 0.015914917, -0.011184692, -0.018203735, -0.03894043, -0.026626587, 0.0010070801, -0.07397461, -0.060333252, 0.046020508, -0.017440796, -0.020385742, -0.0211792, -0.018295288, -0.01802063, 0.003211975, -0.012969971, -0.034576416, -0.022079468, 0.034606934, -0.022079468, -0.02154541, -0.0039367676, 0.015419006, -0.027023315, 0.024642944, -0.0007047653, -0.008293152, 0.02708435, 0.05267334, 0.010177612, 0.017822266, -0.021759033, -0.051116943, -0.02583313, -0.06427002, 0.03213501, -0.009635925, -0.04547119, 0.018997192, -0.024032593, -0.011024475, 0.033935547, 0.050842285, 0.011009216, -0.002527237, 0.04852295, 0.038360596, -0.035583496, -0.021377563, -0.016052246, -0.072143555, 0.03665161, 0.02897644, -0.03842163, -0.00068187714, 0.022415161, -0.0030879974, 0.043762207, 0.05392456, -0.0362854, -0.04647827, -0.034057617, -0.040374756, -0.03942871, 0.030761719, -0.068115234, 0.011329651, 0.011413574, -0.012435913, 0.01576233, 0.022766113, 0.05609131, 0.07092285, 0.017593384, 0.024337769, 0.027923584, 0.06994629, 0.00655365, -0.020248413, -0.03945923, -0.0491333, -0.049194336, 0.020050049, 0.010910034, 0.013511658, 0.01676941, -0.041900635, -0.046142578, 0.012268066, 0.026748657, -0.036499023, 0.021713257, -0.036590576, 0.014411926, 0.029174805, -0.029388428, 0.04119873, 0.04852295, 0.007068634, -0.00090408325, 0.0048332214, -0.015777588, -0.01499939, -0.0068206787, -0.02708435, 0.010543823, 0.004085541, -0.026901245, -0.0045661926, 0.0061912537, -0.0014343262, 0.028945923, -0.03552246, 0.030441284, -0.029281616, 0.050628662, -0.033599854, -0.085510254, -0.052520752, -0.07507324, -0.008003235, -0.026382446, -0.078063965, -0.02516174)COHERE"
R"COHERE(3, -0.025421143, -0.0073165894, 0.01889038, -0.05999756, -0.0051612854, 0.0072517395, -0.011497498, 0.01687622, 0.002231598, -0.034423828, -0.0013084412, -0.012413025, 0.008888245, 0.017486572, -0.03353882, 0.0069885254, -0.02722168, 0.02015686, -0.04510498, -0.038726807, -0.0031356812, 0.033233643, 0.025268555, -0.015106201, 0.02407837, -0.00024700165, -0.07409668, -0.012367249, 0.014785767, -0.04486084, 0.074401855, -0.020690918, -0.025222778, 0.029083252, -0.018997192, 0.0017557144, 0.03857422, -0.020111084, 0.03338623, -0.028213501, 0.0063705444, -0.010124207, -0.03112793, -0.03286743, 0.0046043396, -0.0052223206, 0.00023317337, 0.0423584, 0.028030396, 0.0005788803, -0.02708435, 0.006324768, 0.019821167, -0.0042686462, -0.026428223, -0.02293396, 0.036590576, -0.023376465, -0.022537231, 0.032226562, -0.020629883, 0.017929077, 0.0440979, -0.014038086, -0.022216797, 0.020446777, -0.05496216, -0.018859863, -0.039855957, 0.008300781, 0.07281494, 0.018295288, 0.042114258, 0.005519867, 0.017990112, -0.008773804, 0.011123657, -0.008239746, -0.045532227, 0.026153564, -0.015853882, 0.027557373, -0.049041748, -0.0022945404, -0.009399414, -0.045898438, 0.05053711, 0.038513184, -0.031799316, 0.012329102, 0.024871826, 0.04348755, -0.04788208, 0.01423645, 0.021240234, 0.05493164, 0.008956909, -0.056243896, 0.032043457, -0.01574707, -0.01285553, -0.009498596, -0.018951416, -0.029556274, 0.0069274902, -0.032348633, -0.022445679, -0.00093603134, -0.015808105, -0.027175903, 0.014091492, 0.025665283, -0.023468018, -0.03250122, -0.0004544258, 0.042633057, -0.06036377, -0.039611816, -0.042938232, -0.02418518, -0.0703125, 0.045135498, -0.001036644, -0.017913818, -0.004043579, 0.0138549805, -0.02532959, 0.010765076, 0.021575928, 0.013114929, 0.033935547, -0.010574341, 0.017990112, -0.026107788, -0.029144287, -0.046569824, -0.0030517578, -0.022994995, -0.017471313, -0.0070495605, -9.846687e-05, 0.029281616, 0.017440796, 0.045532227, 0.025650024, 0.0491333, -0.013145447, 0.070129395, -0.)COHERE"
R"COHERE(0051879883, -0.04043579, 0.023864746, 0.016830444, -0.014152527, -0.06359863, -0.005065918, -0.009880066, -0.0034618378, -0.081726074, -0.0289917, -0.007461548, -0.0013504028, 0.020523071, 0.0076446533, -0.011650085, 0.014549255, 0.010955811, 0.02180481, -0.027572632, -0.012252808, 0.009033203, -0.0048980713, 0.031173706, -0.020309448, 0.022979736, -0.013900757, -0.004108429, 0.018325806, -0.031402588, 0.01737976, 0.03201294, -0.02508545, -0.015625, -0.04626465, -0.014656067, 0.016036987, -0.030639648, 0.041748047, -0.0032978058, -0.03277588, 0.037719727, 0.023788452, -0.008140564, -0.041809082, 0.034698486, -0.022994995, -0.009979248, -0.03729248, -0.0904541, 0.00028443336, 0.080566406, -0.035125732, -0.054229736, -0.017700195, 0.060668945, 0.008979797, 0.015052795, -0.0072364807, -0.001490593, 0.0065231323, -0.014579773, 0.016067505, -0.020339966, -0.020217896, 0.02909851, 0.050628662, 0.04510498, -0.01979065, 0.008918762, 0.031799316, 0.031951904, -0.016906738, 0.031036377, 0.0040664673, -0.046905518, -0.04928589, 0.044403076, -0.0524292, -0.012832642, 0.049835205, 0.0040283203, -0.012649536, 0.06878662, -0.02859497, -0.014137268, 0.0036144257, -0.06262207, 0.046813965, 0.024978638, 0.0017976761, -0.032409668, -0.004108429, -0.013557434, -0.07196045, 0.026733398, 0.0024261475, -0.022735596, -0.0022182465, -0.0064315796, -0.03652954, 0.04135132, -0.032562256, 0.004524231, 0.020019531, -0.0113220215, -0.071777344, -0.03451538, 0.0022583008, -0.06512451, -0.005317688, 0.020248413, -0.036712646, 0.005809784, -0.018951416, -0.0026855469, 0.027572632, -0.00036668777, 0.0073623657, -0.018829346, 0.009101868, 0.051971436, 0.023132324, -0.022537231, 0.00932312, 0.00944519, 0.014183044, 0.020889282, 0.0032844543, -0.0073776245, -0.05807495, -0.032440186, 0.033996582, 0.0423584, 0.014259338, 0.061676025, -0.02154541, -0.031982422, 0.005493164, -0.01512146, 0.023101807, -0.011383057, -0.059539795, 0.021820068, 0.015487671, -0.004875183, -0.015640259, 0.015319824, -0.00543594)COHERE"
R"COHERE(36, -0.026229858, 0.0061454773, -0.032348633, 0.038513184, 0.004840851, -0.016021729, -0.017608643, -0.019577026, -0.009178162, 0.045013428, -0.01007843, 0.022323608, 0.034179688, 0.00566864, 0.055511475, -0.033355713, -0.019317627, -8.481741e-05, 0.017547607, -0.053344727, 0.012229919, 0.022384644, 0.018051147, 0.010734558, 0.004501343, -0.05911255, -0.0030918121, -0.0513916, -0.0050086975, -0.01600647, 0.05343628, -0.0008234978, 0.07293701, -0.056610107, -0.06549072, -0.01776123, -0.0022678375, 0.023239136, 0.01020813, -0.005153656, -0.00630188, -0.009880066, 0.022109985, 0.033203125, -0.03567505, -0.014129639, 0.015625, 0.022888184, -0.038726807, -0.026321411, -0.007259369, 0.005924225, 0.0010814667, 0.06665039, -0.008880615, 0.053771973, 0.062194824, 0.018981934, 0.022338867, 0.01361084, 0.025604248, 0.022109985, 0.0044288635, -0.008331299, -0.0019416809, 0.006454468, -0.045013428, -0.02519226, -0.012268066, -0.032165527, 7.2181225e-05, -0.021575928, -0.006324768, 0.029785156, 0.0063438416, -0.01210022, 0.029403687, 0.00592041, 0.008369446, 0.00818634, -0.04498291, -0.041809082, 0.0078086853, -0.05935669, -0.043518066, 0.007270813, 0.060424805, 0.033996582, 0.055908203, 0.013755798, 0.03982544, 0.014640808, -0.01373291, 0.033325195, -0.0047073364, 0.015899658, -0.00043344498, 0.022338867, -0.007095337, 0.02949524, 0.042633057, 0.030670166, 0.022415161, -0.0033683777, 0.018814087, -0.013031006, 0.031951904, 0.022094727, -0.009986877, 0.025665283, -0.0138168335, 0.049743652, 0.024307251, 0.0088272095, -0.03479004, 0.07318115, 0.009849548, 0.051635742, -0.05331421, -0.053131104, -0.0044898987, 0.029342651, 0.005596161, 0.044189453, -0.042388916, -0.012939453, -0.0007529259, -0.06088257, 0.036010742, -0.02355957, 0.004497528, -0.0023822784, -0.053588867, -0.04168701, -0.017868042, -0.01927185, -0.06011963, 0.028884888, 0.061401367, -0.005584717, 0.014823914, -0.02255249, 4.631281e-05, 0.039031982, -0.0055389404, 0.007194519, 0.0037631989, 0.008834839, 0.018692017, 0)COHERE"
R"COHERE(.033111572, -0.056274414, -0.021774292, 0.04727173, -0.03265381, 0.022140503, 0.027801514, 0.004043579, -0.016525269, -0.041809082, 0.024520874, 0.008529663, 0.049072266, 0.033447266, -0.028839111, 0.048675537, 0.021453857, -0.08087158, 0.034606934, -0.002910614, 0.012176514, 0.035705566, 0.040161133, -0.02355957, -0.01626587, -0.033721924, -0.013893127, -0.04156494, 0.06719971, 0.043151855, -0.033813477, 0.028045654, 0.0029525757, -0.022033691, -0.093811035, -0.0056114197, 0.00026154518, 0.058746338, -0.05065918, 0.02897644, -0.01550293, -0.02947998, -0.018249512, 0.034942627, -0.04574585, -0.037109375, -0.006160736, 0.006149292, -0.0012207031, -0.042907715, -0.016448975, 0.0052719116, 0.036590576, -0.045318604, -0.04220581, -0.018859863, -0.031021118, 0.06439209, -0.0056533813, -0.037200928, -0.026550293, 0.027786255, -0.028427124, 0.09161377, -0.0088272095, -0.003643036, -0.053253174, -0.01826477, -0.016540527, -0.012535095, -0.03942871, -0.0049095154, 0.031311035, 0.049468994, -0.066589355, -0.05029297, 7.5519085e-05, -0.0017404556, -0.013214111, -0.03756714, -0.009147644, -0.025466919, 0.026672363, 0.020965576, -0.0073432922, 0.0011005402, -0.04937744, -0.018463135, 0.00274086, -0.013252258, 0.0126953125, -0.077697754, 0.014045715, 0.00039935112, -0.019515991, -0.0027618408, -0.011672974, -0.043884277, 0.009231567, 0.062805176, -0.0137786865, -0.026229858, -0.034362793, -0.015090942, 0.016937256, 0.030639648, -0.02420044, 0.02482605, -0.0033740997, 0.046417236, -0.012008667, -0.04031372, -0.00032520294, 0.01525116, -0.0066375732, 0.0062713623, -0.01171875, -0.027191162, -0.014137268, -0.025390625, 0.002111435, -0.06561279, 0.031555176, -0.07519531, -0.04547119, 0.014472961, -0.0158844, -0.091552734, -0.03366089, 0.050323486, -0.0013589859, -0.033203125, 0.046539307, -0.030288696, 0.0046195984, 0.049835205, 0.02003479, -0.004196167, 0.013168335, -0.016403198, 0.01676941, -0.00340271]]}, "meta": {"api_version": {"version": "2"}, "billed_units": {"images": 1}}})COHERE"
;
// FIXTURE: v1/summarize example 0 response
inline const char* kSummarize =
R"COHERE({"id": "aa0b828e-f9cf-484d-a84d-cb62f556e841", "summary": "Ice cream is a frozen dessert made by whipping a cream base and liquid nitrogen together. It is then flavoured with sweeteners, spices and fruits. Ice cream can also be made using alternative milks, such as soy or almond, for those who are lactose intolerant or vegan.", "meta": {"api_version": {"version": "1"}, "billed_units": {"input_tokens": 321, "output_tokens": 55}}})COHERE"
;
} // namespace cohere_test
+51
View File
@@ -0,0 +1,51 @@
#pragma once
// Shared rig for the Cohere tests: a Platform over a MockTransport with a fake
// key. The mock only records the requests the real client builds; replies are
// the spec's documented examples (fixtures.h, verbatim) or minimal documents
// shaped from the OpenAPI schemas and labelled SHAPE where they appear. Nothing
// here verifies vendor behavior (see src/vendors/cohere/SPEC.md ## Unverified).
#include <memory>
#include <string>
#include "clients/transports/mock_transport.h"
#include "fixtures.h"
#include "framework.h"
#include "vendors/cohere/platform.h"
namespace cohere_test {
using molo::clients::ClientResponse;
using molo::clients::HeaderMap;
using molo::clients::Method;
using molo::clients::transports::MockExpectation;
using molo::clients::transports::MockTransport;
using Json = molo::clients::body::Json;
inline ClientResponse reply(const std::string& body, int status = 200,
const std::string& type = "application/json") {
HeaderMap h;
h.set("Content-Type", type);
return ClientResponse(status, h, body);
}
inline ClientResponse sse(const std::string& body) { return reply(body, 200, "text/event-stream"); }
inline bool has(const std::string& hay, const std::string& needle) { return hay.find(needle) != std::string::npos; }
inline const char* kKey = "co-TESTKEY-DO-NOT-LEAK";
struct Rig {
MockTransport* mock = nullptr;
molo::cohere::Platform platform;
explicit Rig(const std::string& key = kKey) {
auto t = std::make_unique<MockTransport>();
mock = t.get();
platform = molo::cohere::Platform::with(std::move(t), key);
}
std::shared_ptr<molo::llm::ApiClient> client() const { return platform.client; }
};
} // namespace cohere_test
+75
View File
@@ -0,0 +1,75 @@
// The call table: shape, rendering, id and query validation, body checks.
#include <set>
#include "support.h"
#include "vendors/cohere/calls.h"
#include "vendors/cohere/catalog.h"
using namespace molo::cohere;
using namespace cohere_test;
using molo::llm::ApiError;
TEST(cohere_calls, every_enum_value_has_exactly_one_row) {
ASSERT_EQ(Calls::count(), 42u);
std::set<int> seen;
std::set<std::string> routes;
for (std::size_t i = 0; i < Calls::count(); ++i) {
const CallSpec& s = Calls::at(i);
ASSERT_TRUE(seen.insert(static_cast<int>(s.call)).second);
ASSERT_TRUE(routes.insert(std::string(s.http_method) + " " + s.path).second);
ASSERT_EQ(&Calls::spec_of(s.call), &s);
ASSERT_TRUE(std::string(s.path).rfind("/v", 0) == 0);
ASSERT_TRUE(std::string(s.operation_id).size() > 0);
}
ASSERT_EQ(seen.size(), Calls::count());
ASSERT_TRUE(Calls::schema_count() > 60);
ASSERT_THROW(Calls::at(Calls::count()), ApiError);
ASSERT_THROW(Calls::schema_at(Calls::schema_count()), ApiError);
}
TEST(cohere_calls, render_fills_the_placeholder_with_a_validated_id) {
ASSERT_EQ(Calls::render(Call::kBatchGet, "b-1"), "/v2/batches/b-1");
ASSERT_EQ(Calls::render(Call::kBatchCancel, "b-1"), "/v2/batches/b-1:cancel");
ASSERT_EQ(Calls::render(Call::kFinetuneEvents, "ft_1"), "/v1/finetuning/finetuned-models/ft_1/events");
ASSERT_EQ(Calls::render(Call::kModelGet, "command-a-03-2025"), "/v1/models/command-a-03-2025");
ASSERT_THROW(Calls::render(Call::kBatchGet, ""), ApiError);
ASSERT_THROW(Calls::render(Call::kBatchGet, "../x"), ApiError);
ASSERT_THROW(Calls::render(Call::kBatchGet, "a/b"), ApiError);
ASSERT_THROW(Calls::render(Call::kBatchGet, "a?b=1"), ApiError);
ASSERT_THROW(Calls::render(Call::kChat, "x"), ApiError); // takes no id
}
TEST(cohere_calls, query_names_must_be_in_the_row_and_are_encoded) {
Query q = {{"page_size", "5"}, {"order_by", "created at"}};
ASSERT_EQ(Calls::render(Call::kBatchList, "", q), "/v2/batches?page_size=5&order_by=created%20at");
Query repeated = {{"keep_fields", "a"}, {"keep_fields", "b"}, {"name", "n"}, {"type", "embed-input"}};
ASSERT_TRUE(has(Calls::render(Call::kDatasetCreate, "", repeated), "keep_fields=a&keep_fields=b"));
ASSERT_THROW(Calls::render(Call::kBatchList, "", Query{{"nope", "1"}}), ApiError);
ASSERT_THROW(Calls::render(Call::kChat, "", Query{{"page_size", "1"}}), ApiError);
}
TEST(cohere_calls, check_body_rejects_fields_outside_the_row) {
Json ok = Json::parse("{\"model\":\"m\",\"messages\":[]}");
Calls::check_body(Call::kChat, ok);
Json bad = Json::parse("{\"model\":\"m\",\"max_completion_tokens\":5}");
ASSERT_THROW(Calls::check_body(Call::kChat, bad), ApiError);
ASSERT_THROW(Calls::check_body(Call::kChat, Json::parse("[1]")), ApiError);
}
TEST(cohere_calls, methods_map_from_the_table) {
ASSERT_TRUE(Calls::method_of(Call::kChat) == Method::kPost);
ASSERT_TRUE(Calls::method_of(Call::kModelList) == Method::kGet);
ASSERT_TRUE(Calls::method_of(Call::kConnectorUpdate) == Method::kPatch);
ASSERT_TRUE(Calls::method_of(Call::kDatasetDelete) == Method::kDelete);
}
TEST(cohere_catalog, names_and_status) {
ASSERT_TRUE(ModelCatalog::count() >= 15);
ASSERT_TRUE(ModelCatalog::status_of("command-a-03-2025") == ModelStatus::kLive);
ASSERT_TRUE(ModelCatalog::status_of("command-r-03-2024") == ModelStatus::kDeprecated);
ASSERT_TRUE(ModelCatalog::status_of("nonsense") == ModelStatus::kUnknown);
ASSERT_EQ(std::string(ModelCatalog::default_chat()), "command-a-03-2025");
ASSERT_EQ(std::string(ModelCatalog::default_embed()), "embed-v4.0");
ASSERT_EQ(std::string(ModelCatalog::default_rerank()), "rerank-v3.5");
ASSERT_THROW(ModelCatalog::at(ModelCatalog::count()), ApiError);
}
+304
View File
@@ -0,0 +1,304 @@
// Cohere chat v2 over the real ApiClient with a recording transport. Replies
// are the spec's documented examples (fixtures.h); assertions are on what WE
// send and on how WE parse.
#include "support.h"
#include "vendors/cohere/chat.h"
using namespace molo::cohere;
using namespace cohere_test;
using molo::llm::ApiError;
TEST(cohere_chat, create_sends_bearer_json_and_parses_the_documented_example) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v2/chat", reply(kChatBasic));
Chat chat(rig.client());
ChatParams p = ChatParams::prompt("Tell me about LLMs");
p.model = "command-a-plus-05-2026";
ChatResult r = chat.create(p);
ASSERT_EQ(r.id, "c14c80c3-18eb-4519-9460-6c92edd8cfb4");
ASSERT_EQ(r.finish_reason, "COMPLETE");
ASSERT_TRUE(has(r.text, "LLMs stand for Large Language Models"));
ASSERT_TRUE(r.usage.present);
ASSERT_TRUE(r.usage.input_tokens > 0 && r.usage.output_tokens > 0);
ASSERT_TRUE(r.usage.billed_input_tokens > 0);
auto s = rig.mock->seen()[0];
ASSERT_EQ(s.method, "POST");
ASSERT_EQ(s.target, "/v2/chat");
ASSERT_EQ(s.headers.get_or("Authorization", ""), std::string("Bearer ") + kKey);
ASSERT_EQ(s.headers.get_or("X-Client-Name", ""), "molo");
ASSERT_TRUE(has(s.headers.get_or("Content-Type", ""), "application/json"));
ASSERT_TRUE(has(s.body, "\"model\":\"command-a-plus-05-2026\""));
ASSERT_TRUE(has(s.body, "\"messages\":[{\"content\":\"Tell me about LLMs\",\"role\":\"user\"}]"));
ASSERT_FALSE(has(s.body, "\"stream\""));
ASSERT_EQ(s.target.find(kKey), std::string::npos);
}
TEST(cohere_chat, tool_call_with_thinking_documented_example) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v2/chat", reply(kChatToolThinking));
ChatResult r = Chat(rig.client()).create(ChatParams::prompt("sales?"));
ASSERT_EQ(r.finish_reason, "TOOL_CALL");
ASSERT_TRUE(has(r.thinking, "sales summary for 29th September 2023"));
ASSERT_EQ(r.text, "");
ASSERT_EQ(r.tool_calls.size(), 2u);
ASSERT_EQ(r.tool_calls[0].name, "query_daily_sales_report");
ASSERT_TRUE(r.tool_calls[0].arguments_valid());
ASSERT_EQ(r.tool_calls[0].parsed().find("day")->as_string(), "2023-09-29");
ASSERT_EQ(r.tool_calls[1].id, "query_product_catalog_rpg0z5h8yyz2");
ASSERT_EQ(r.usage.input_tokens, 1032);
ASSERT_EQ(r.usage.output_tokens, 124);
ASSERT_EQ(r.usage.billed_input_tokens, 127);
// The history message keeps thinking, tool_calls and no empty text.
Json h = r.history_message();
ASSERT_EQ(h.find("role")->as_string(), "assistant");
ASSERT_EQ(h.find("tool_calls")->as_array().size(), 2u);
ASSERT_EQ(h.find("content")->as_array()[0].find("type")->as_string(), "thinking");
}
TEST(cohere_chat, citations_documented_example) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v2/chat", reply(kChatCitations));
ChatParams p = ChatParams::prompt("Who is more popular?");
p.documents.push_back(Json("a plain string document"));
p.citation_mode = "FAST";
ChatResult r = Chat(rig.client()).create(p);
ASSERT_TRUE(!r.citations.empty());
ASSERT_TRUE(r.citations[0].find("sources") != nullptr);
ASSERT_TRUE(has(rig.mock->seen()[0].body, "\"citation_options\":{\"mode\":\"FAST\"}"));
ASSERT_TRUE(has(rig.mock->seen()[0].body, "\"documents\":[\"a plain string document\"]"));
}
TEST(cohere_chat, stream_documented_text_transcript) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v2/chat", sse(kStreamText));
Chat chat(rig.client());
std::string text;
int starts = 0, ends = 0;
ChatResult r = chat.stream(ChatParams::prompt("Tell me about LLMs"), [&](const StreamDelta& d) {
text += d.text;
if (d.type == "message-start") ++starts;
if (d.type == "message-end") {
++ends;
ASSERT_TRUE(d.has_usage);
}
return true;
});
ASSERT_EQ(starts, 1);
ASSERT_EQ(ends, 1);
ASSERT_TRUE(has(text, "LLMs stand for Large Language Models"));
ASSERT_EQ(r.text, text);
ASSERT_EQ(r.id, "29f14a5a-11de-4cae-9800-25e4747408ea");
ASSERT_TRUE(r.finish_reason == "COMPLETE");
auto s = rig.mock->seen()[0];
ASSERT_TRUE(has(s.body, "\"stream\":true"));
ASSERT_TRUE(has(s.headers.get_or("Accept", ""), "text/event-stream"));
}
TEST(cohere_chat, stream_documented_tool_transcript_folds_plan_and_calls) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v2/chat", sse(kStreamTools));
Chat chat(rig.client());
std::string plan;
int tool_starts = 0;
ChatResult r = chat.stream(ChatParams::prompt("sales"), [&](const StreamDelta& d) {
plan += d.tool_plan;
if (d.tool_start) ++tool_starts;
return true;
});
ASSERT_TRUE(has(plan, "I will use the query_daily_sales_report tool"));
ASSERT_EQ(r.tool_plan, plan);
ASSERT_EQ(r.finish_reason, "TOOL_CALL");
ASSERT_TRUE(tool_starts >= 2);
ASSERT_EQ(r.tool_calls.size(), static_cast<std::size_t>(tool_starts));
for (const ToolCallRecord& c : r.tool_calls) {
ASSERT_TRUE(!c.id.empty() && !c.name.empty());
ASSERT_TRUE(c.arguments_valid());
}
ASSERT_EQ(r.usage.input_tokens, 1589);
}
TEST(cohere_chat, sink_can_stop_the_stream_early) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v2/chat", sse(kStreamText));
int n = 0;
ChatResult r = Chat(rig.client()).stream(ChatParams::prompt("hi"), [&](const StreamDelta&) { return ++n < 4; });
ASSERT_EQ(n, 4);
ASSERT_TRUE(r.finish_reason.empty());
}
TEST(cohere_chat, stream_without_message_end_is_loud) {
Rig rig;
const std::string cut =
"event: message-start\ndata: {\"type\":\"message-start\",\"id\":\"x\",\"delta\":{\"message\":{\"role\":\"assistant\"}}}\n\n";
rig.mock->expect_response(Method::kPost, "/v2/chat", sse(cut));
try {
Chat(rig.client()).stream(ChatParams::prompt("hi"), nullptr);
FAIL("expected");
} catch (const ApiError& e) {
ASSERT_EQ(e.type(), "STREAM_TRUNCATED");
ASSERT_EQ(e.http_status(), 502);
}
}
TEST(cohere_chat, message_end_error_and_json_error_body_throw) {
Rig rig;
const std::string err =
"event: message-end\ndata: {\"type\":\"message-end\",\"delta\":{\"error\":\"model overloaded\",\"finish_reason\":\"ERROR\"}}\n\n";
rig.mock->expect_response(Method::kPost, "/v2/chat", sse(err));
try {
Chat(rig.client()).stream(ChatParams::prompt("hi"), nullptr);
FAIL("expected");
} catch (const ApiError& e) {
ASSERT_EQ(e.type(), "STREAM_ERROR");
ASSERT_TRUE(has(e.message(), "overloaded"));
}
}
TEST(cohere_chat, http_error_keeps_status_and_message_without_the_key) {
Rig rig;
// PROBE: shape of the unauthenticated answer seen 2026-10-25 (body {"id","message"}).
rig.mock->expect_response(Method::kPost, "/v2/chat",
reply("{\"id\":\"0045bb6a-672c-459a-937b-54654beeb342\",\"message\":\"Incorrect API key provided: ***robe.\"}", 401));
try {
Chat(rig.client()).create(ChatParams::prompt("hi"));
FAIL("expected");
} catch (const ApiError& e) {
ASSERT_EQ(e.http_status(), 401);
ASSERT_TRUE(has(e.message(), "Incorrect API key"));
ASSERT_EQ(e.message().find(kKey), std::string::npos);
}
}
TEST(cohere_chat, not_configured_has_no_traffic) {
Rig rig("");
try {
Chat(rig.client()).create(ChatParams::prompt("hi"));
FAIL("expected");
} catch (const molo::llm::NotConfiguredError& e) {
ASSERT_EQ(e.http_status(), 503);
ASSERT_TRUE(has(e.message(), "COHERE_API_KEY"));
}
ASSERT_EQ(rig.mock->send_count(), 0u);
ASSERT_FALSE(rig.platform.configured());
}
TEST(cohere_chat, invalid_input_never_reaches_the_network) {
Rig rig;
Chat chat(rig.client());
auto bad = [&](auto mutate) {
ChatParams p = ChatParams::prompt("hi");
mutate(p);
ASSERT_THROW(chat.create(p), ApiError);
};
ASSERT_THROW(chat.create(ChatParams()), ApiError); // no messages
bad([](ChatParams& p) { p.model = ""; });
bad([](ChatParams& p) { p.model = "a b"; });
bad([](ChatParams& p) { p.temperature = 1.5; });
bad([](ChatParams& p) { p.p = 1.0; });
bad([](ChatParams& p) { p.p = 0.0; });
bad([](ChatParams& p) { p.k = 501; });
bad([](ChatParams& p) { p.priority = 1000; });
bad([](ChatParams& p) { p.max_tokens = 0; });
bad([](ChatParams& p) { p.frequency_penalty = 1.1; });
bad([](ChatParams& p) { p.seed = -1; });
bad([](ChatParams& p) { p.stop = {"a", "b", "c", "d", "e", "f"}; });
bad([](ChatParams& p) { p.stop = {""}; });
bad([](ChatParams& p) { p.tool_choice = "AUTO"; });
bad([](ChatParams& p) { p.tool_choice = "REQUIRED"; }); // needs tools
bad([](ChatParams& p) { p.strict_tools = true; }); // needs tools
bad([](ChatParams& p) { p.safety_mode = "LOOSE"; });
bad([](ChatParams& p) { p.citation_mode = "MAYBE"; });
bad([](ChatParams& p) { p.thinking_budget = 100; }); // needs thinking enabled
bad([](ChatParams& p) { p.json_schema = Json::object(); }); // needs json_mode
bad([](ChatParams& p) { p.extra = Json::parse("{\"unknown\":1}"); });
bad([](ChatParams& p) { p.messages.assign(4097, Messages::user("x")); });
bad([](ChatParams& p) { p.tools.resize(1); }); // empty tool name
ASSERT_THROW(Messages::image_part("ftp://x"), ApiError);
ASSERT_THROW(Messages::tool("", "x"), ApiError);
ASSERT_THROW(Messages::user_parts({}), ApiError);
ASSERT_EQ(rig.mock->send_count(), 0u);
}
TEST(cohere_chat, every_optional_field_lands_in_the_body_and_in_the_table) {
ChatParams p = ChatParams::prompt("hi");
p.add(Messages::system("be brief"));
p.tools.push_back(ToolDef{"get_weather", "Weather", Json::parse("{\"type\":\"object\"}")});
p.strict_tools = true;
p.documents.push_back(Json::parse("{\"id\":\"d1\",\"data\":{\"title\":\"t\"}}"));
p.citation_mode = "ACCURATE";
p.json_mode = true;
p.json_schema = Json::parse("{\"type\":\"object\"}");
p.safety_mode = "STRICT";
p.max_tokens = 100;
p.stop = {"END"};
p.temperature = 0.3;
p.seed = 7;
p.frequency_penalty = 0.1;
p.presence_penalty = 0.2;
p.k = 40;
p.p = 0.75;
p.logprobs = true;
p.tool_choice = "REQUIRED";
p.thinking = Thinking::kEnabled;
p.thinking_budget = 2048;
p.priority = 5;
Json b = p.to_json(true);
for (const char* k : {"model", "messages", "stream", "tools", "strict_tools", "documents", "citation_options",
"response_format", "safety_mode", "max_tokens", "stop_sequences", "temperature", "seed",
"frequency_penalty", "presence_penalty", "k", "p", "logprobs", "tool_choice", "thinking",
"priority"})
ASSERT_TRUE(b.find(k) != nullptr);
ASSERT_EQ(b.find("thinking")->find("token_budget")->as_int(), 2048);
ASSERT_EQ(b.find("response_format")->find("type")->as_string(), "json_object");
ASSERT_EQ(b.find("tools")->as_array()[0].find("function")->find("name")->as_string(), "get_weather");
Calls::check_body(Call::kChat, b);
}
TEST(cohere_chat, conversation_runs_the_tool_loop_with_a_round_cap) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v2/chat", reply(kChatToolThinking));
rig.mock->expect_response(Method::kPost, "/v2/chat", reply(kChatBasic));
ChatParams base = ChatParams::prompt("sales summary and electronics?");
ToolDef t;
t.name = "query_daily_sales_report";
base.tools.push_back(t);
Conversation conv(base);
std::vector<std::string> called;
ChatResult r = conv.run_tools(*std::make_unique<Chat>(rig.client()),
[&](const std::string& name, const Json& args, const std::string& id) {
called.push_back(name + ":" + id);
ASSERT_TRUE(args.is_object());
return std::string("{\"ok\":true}");
});
ASSERT_EQ(called.size(), 2u);
ASSERT_TRUE(has(r.text, "LLMs stand for"));
// history: user, assistant(tool_calls), tool, tool, assistant
ASSERT_EQ(conv.params().messages.size(), 5u);
ASSERT_EQ(conv.params().messages[2].find("role")->as_string(), "tool");
ASSERT_EQ(conv.params().messages[2].find("tool_call_id")->as_string(), "query_daily_sales_report_hgxxmkby3wta");
ASSERT_TRUE(has(rig.mock->seen()[1].body, "\"tool_call_id\""));
// a model that never stops asking for tools hits the cap
Rig loop;
for (int i = 0; i < 2; ++i) loop.mock->expect_response(Method::kPost, "/v2/chat", reply(kChatToolThinking));
Conversation again(base);
Chat chat2(loop.client());
ASSERT_THROW(again.run_tools(chat2, [](const std::string&, const Json&, const std::string&) { return std::string("x"); }, 2),
ApiError);
ASSERT_EQ(loop.mock->send_count(), 2u);
ASSERT_THROW(again.run_tools(chat2, nullptr, 17), ApiError);
ASSERT_THROW(Conversation(ChatParams::prompt("x")).run_tools(chat2, nullptr), ApiError); // no tools
}
TEST(cohere_chat, legacy_v1_chat_checks_fields_against_the_table) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v1/chat", reply("{\"text\":\"hi\",\"generation_id\":\"g\",\"finish_reason\":\"COMPLETE\"}"));
Chat chat(rig.client());
Json out = chat.legacy(Json::parse("{\"message\":\"hello\",\"preamble\":\"be nice\"}"));
ASSERT_EQ(out.find("text")->as_string(), "hi");
ASSERT_TRUE(has(rig.mock->seen()[0].body, "\"message\":\"hello\""));
ASSERT_THROW(chat.legacy(Json::parse("{\"prompt\":\"x\"}")), ApiError);
ASSERT_THROW(chat.legacy(Json::parse("{\"message\":\"x\",\"stream\":true}")), ApiError);
ASSERT_THROW(chat.legacy(Json::parse("{\"message\":\"x\",\"bogus\":1}")), ApiError);
ASSERT_EQ(rig.mock->send_count(), 1u);
}
+198
View File
@@ -0,0 +1,198 @@
// CohereChatModel: the neutral ChatModel mapping, offline. The neutral request
// is mapped to wire parameters and back; the transport only records.
#include "llm/chat_types.h"
#include "support.h"
#include "vendors/cohere/chat_model.h"
using namespace molo::cohere;
using namespace cohere_test;
using molo::llm::ApiError;
namespace {
CohereChatModel make(Rig& rig) { return CohereChatModel(std::make_shared<Chat>(rig.client())); }
} // namespace
TEST(cohere_model, vendor_and_defaults) {
Rig rig;
CohereChatModel m = make(rig);
ASSERT_EQ(m.vendor(), "cohere");
molo::llm::ChatRequest r;
r.messages.push_back(molo::llm::Message::user("hi"));
ASSERT_EQ(m.to_params(r).model, "command-a-03-2025");
r.model = "command-r7b-12-2024";
ASSERT_EQ(m.to_params(r).model, "command-r7b-12-2024");
}
TEST(cohere_model, request_mapping_system_tools_json_and_sampling) {
Rig rig;
CohereChatModel m = make(rig);
molo::llm::ChatRequest r;
r.system = "be brief";
r.messages.push_back(molo::llm::Message::user("weather?"));
molo::llm::ToolSpec t;
t.name = "get_weather";
t.description = "Weather";
t.parameters = Json::parse("{\"type\":\"object\"}");
r.tools.push_back(t);
r.tool_choice = "required";
r.max_tokens = 50;
r.temperature = 0.2;
r.top_p = 0.9;
r.stop = {"END"};
r.json_mode = true;
r.response_schema = Json::parse("{\"type\":\"object\",\"properties\":{}}");
Json b = m.to_params(r).to_json();
ASSERT_EQ(b.find("messages")->as_array()[0].find("role")->as_string(), "system");
ASSERT_EQ(b.find("tool_choice")->as_string(), "REQUIRED");
ASSERT_EQ(b.find("max_tokens")->as_int(), 50);
ASSERT_EQ(b.find("p")->as_number(), 0.9);
ASSERT_TRUE(b.find("response_format")->find("json_schema") != nullptr);
r.tool_choice = "none";
ASSERT_EQ(m.to_params(r).tool_choice, "NONE");
r.tool_choice = "auto";
ASSERT_EQ(m.to_params(r).tool_choice, "");
r.tool_choice = "get_weather"; // a named tool cannot be forced
ASSERT_THROW(m.to_params(r), ApiError);
}
TEST(cohere_model, reasoning_effort_and_extra_thinking) {
Rig rig;
CohereChatModel m = make(rig);
molo::llm::ChatRequest r;
r.messages.push_back(molo::llm::Message::user("hi"));
ASSERT_TRUE(m.to_params(r).thinking == Thinking::kDefault);
r.reasoning_effort = "high";
ASSERT_TRUE(m.to_params(r).thinking == Thinking::kEnabled);
r.reasoning_effort = "none";
ASSERT_TRUE(m.to_params(r).thinking == Thinking::kDisabled);
r.reasoning_effort = "";
r.extra = Json::parse("{\"thinking\":{\"token_budget\":512},\"priority\":3}");
ChatParams p = m.to_params(r);
ASSERT_TRUE(p.thinking == Thinking::kEnabled);
ASSERT_EQ(*p.thinking_budget, 512L);
ASSERT_EQ(p.to_json().find("priority")->as_int(), 3L);
r.extra = Json::parse("{\"thinking\":{\"type\":\"sometimes\"}}");
ASSERT_THROW(m.to_params(r), ApiError);
}
TEST(cohere_model, messages_images_tool_calls_and_results_map_to_wire_roles) {
Rig rig;
CohereChatModel m = make(rig);
molo::llm::ChatRequest r;
molo::llm::Message user = molo::llm::Message::user("look");
molo::llm::ImageData img;
img.url = "https://example.com/a.png";
user.parts.push_back(molo::llm::ContentPart::of_image(img));
r.messages.push_back(user);
molo::llm::Message asst;
asst.role = molo::llm::Role::kAssistant;
molo::llm::ContentPart think;
think.reasoning = "hmm";
asst.parts.push_back(think);
molo::llm::ToolCall call;
call.id = "c1";
call.name = "f";
call.arguments = Json::parse("{\"a\":1}");
asst.parts.push_back(molo::llm::ContentPart::of_call(call));
r.messages.push_back(asst);
molo::llm::Message tool;
tool.role = molo::llm::Role::kTool;
tool.parts.push_back(molo::llm::ContentPart::of_result({"c1", "result", false}));
r.messages.push_back(tool);
Json b = m.to_params(r).to_json();
const auto& msgs = b.find("messages")->as_array();
ASSERT_EQ(msgs.size(), 3u);
ASSERT_EQ(msgs[0].find("content")->as_array()[1].find("type")->as_string(), "image_url");
ASSERT_EQ(msgs[1].find("tool_calls")->as_array()[0].find("function")->find("arguments")->as_string(), "{\"a\":1}");
ASSERT_EQ(msgs[1].find("content")->as_array()[0].find("type")->as_string(), "thinking");
ASSERT_EQ(msgs[2].find("role")->as_string(), "tool");
ASSERT_EQ(msgs[2].find("tool_call_id")->as_string(), "c1");
// image with raw bytes needs a mime type
molo::llm::ChatRequest bad;
molo::llm::Message u2 = molo::llm::Message::user("x");
molo::llm::ImageData raw;
raw.bytes = "abc";
u2.parts.push_back(molo::llm::ContentPart::of_image(raw));
bad.messages.push_back(u2);
ASSERT_THROW(m.to_params(bad), ApiError);
// a tool message with no result part
molo::llm::ChatRequest bad2;
molo::llm::Message t2;
t2.role = molo::llm::Role::kTool;
bad2.messages.push_back(t2);
ASSERT_THROW(m.to_params(bad2), ApiError);
}
TEST(cohere_model, generate_maps_the_documented_tool_example) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v2/chat", reply(kChatToolThinking));
CohereChatModel m = make(rig);
molo::llm::ChatRequest r;
r.messages.push_back(molo::llm::Message::user("sales"));
molo::llm::ChatResponse out = m.generate(r);
ASSERT_EQ(out.finish_reason, "tool_calls");
ASSERT_EQ(out.model, "command-a-03-2025");
ASSERT_EQ(out.tool_calls().size(), 2u);
ASSERT_EQ(out.tool_calls()[0].name, "query_daily_sales_report");
ASSERT_EQ(out.tool_calls()[0].arguments.find("day")->as_string(), "2023-09-29");
ASSERT_TRUE(out.message.parts[0].reasoning.find("sales summary") != std::string::npos);
ASSERT_EQ(out.usage.input_tokens, 1032);
ASSERT_EQ(out.usage.output_tokens, 124);
ASSERT_EQ(out.usage.total_tokens, 1156);
ASSERT_TRUE(out.raw.is_object());
}
TEST(cohere_model, invalid_tool_arguments_are_kept_not_dropped) {
ToolCallRecord bad;
bad.id = "c";
bad.name = "f";
bad.arguments = "{not json";
ChatResult r;
r.tool_calls.push_back(bad);
r.finish_reason = "TOOL_CALL";
molo::llm::ChatResponse out = CohereChatModel::to_response(r);
ASSERT_EQ(out.tool_calls()[0].arguments.find("_invalid_json")->as_string(), "{not json");
}
TEST(cohere_model, stream_maps_text_tool_fragments_finish_and_usage) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v2/chat", sse(kStreamTools));
CohereChatModel m = make(rig);
molo::llm::ChatRequest r;
r.messages.push_back(molo::llm::Message::user("sales"));
std::string args, finish;
std::string name, id;
bool usage = false;
m.stream(r, [&](const molo::llm::ChatChunk& c) {
args += c.tool_args_delta;
if (!c.tool_call_name.empty()) name = c.tool_call_name;
if (!c.tool_call_id.empty()) id = c.tool_call_id;
if (!c.finish_reason.empty()) finish = c.finish_reason;
if (c.usage) usage = true;
return true;
});
ASSERT_EQ(finish, "tool_calls");
ASSERT_TRUE(usage);
ASSERT_FALSE(name.empty());
ASSERT_FALSE(id.empty());
ASSERT_TRUE(has(args, "day"));
}
TEST(cohere_model, finish_reasons_map_to_the_neutral_words) {
ASSERT_EQ(CohereChatModel::neutral_finish("COMPLETE"), "stop");
ASSERT_EQ(CohereChatModel::neutral_finish("STOP_SEQUENCE"), "stop");
ASSERT_EQ(CohereChatModel::neutral_finish("MAX_TOKENS"), "length");
ASSERT_EQ(CohereChatModel::neutral_finish("TOOL_CALL"), "tool_calls");
ASSERT_EQ(CohereChatModel::neutral_finish("ERROR"), "error");
ASSERT_EQ(CohereChatModel::neutral_finish("TIMEOUT"), "timeout");
ASSERT_EQ(CohereChatModel::neutral_finish("SOMETHING_NEW"), "SOMETHING_NEW");
}
TEST(cohere_model, works_through_the_neutral_pointer) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v2/chat", reply(kChatBasic));
std::shared_ptr<molo::llm::ChatModel> model = std::make_shared<CohereChatModel>(std::make_shared<Chat>(rig.client()));
molo::llm::ChatRequest r;
r.messages.push_back(molo::llm::Message::user("Tell me about LLMs"));
ASSERT_TRUE(has(model->generate(r).text(), "Large Language Models"));
}
+498
View File
@@ -0,0 +1,498 @@
// Embed, rerank, classify, tokenize, legacy calls, account, datasets, batches,
// embed jobs, connectors, fine-tuning, parse, transcription. Documented
// examples are verbatim (fixtures.h); documents with no published example are
// minimal SHAPE fixtures built from the OpenAPI schema and labelled so.
#include "support.h"
#include "vendors/cohere/inference.h"
#include "vendors/cohere/resources.h"
using namespace molo::cohere;
using namespace cohere_test;
using molo::llm::ApiError;
// ---- inference -------------------------------------------------------------
TEST(cohere_embed, v2_request_body_and_documented_response) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v2/embed", reply(kEmbedV2));
Embeddings api(rig.client());
EmbedParams p;
p.texts = {"hello", "world"};
p.embedding_types = {"float", "int8"};
p.truncate = "END";
p.output_dimension = 1024;
EmbedResult r = api.embed(p);
ASSERT_TRUE(!r.id.empty());
auto floats = r.floats();
ASSERT_TRUE(!floats.empty());
ASSERT_TRUE(floats[0].size() > 100);
auto s = rig.mock->seen()[0];
ASSERT_TRUE(has(s.body, "\"model\":\"embed-v4.0\""));
ASSERT_TRUE(has(s.body, "\"input_type\":\"search_document\""));
ASSERT_TRUE(has(s.body, "\"embedding_types\":[\"float\",\"int8\"]"));
ASSERT_TRUE(has(s.body, "\"output_dimension\":1024"));
}
TEST(cohere_embed, v1_floats_array_and_input_rules) {
Rig rig;
// SHAPE: v1 embed answers embeddings as an array of float rows (schema: embeddings_floats).
rig.mock->expect_response(Method::kPost, "/v1/embed",
reply("{\"id\":\"e1\",\"response_type\":\"embeddings_floats\",\"texts\":[\"a\"],\"embeddings\":[[0.5,0.25]],\"meta\":{\"billed_units\":{\"input_tokens\":3}}}"));
Embeddings api(rig.client());
EmbedParams p;
p.texts = {"a"};
p.model = "embed-english-v3.0";
EmbedResult r = api.embed_v1(p);
ASSERT_EQ(r.floats().size(), 1u);
ASSERT_EQ(r.floats()[0][1], 0.25);
ASSERT_EQ(r.usage.billed_input_tokens, 3);
EmbedParams mixed;
mixed.inputs.push_back(Json::parse("{\"content\":[{\"type\":\"text\",\"text\":\"x\"}]}"));
ASSERT_THROW(api.embed_v1(mixed), ApiError); // v1 takes texts and images only
EmbedParams none;
ASSERT_THROW(api.embed(none), ApiError);
EmbedParams many;
many.texts.assign(97, "x");
ASSERT_THROW(api.embed(many), ApiError);
EmbedParams bad_type;
bad_type.texts = {"x"};
bad_type.input_type = "other";
ASSERT_THROW(api.embed(bad_type), ApiError);
bad_type.input_type = "search_query";
bad_type.embedding_types = {"float16"};
ASSERT_THROW(api.embed(bad_type), ApiError);
EmbedParams empty_text;
empty_text.texts = {""};
ASSERT_THROW(api.embed(empty_text), ApiError);
EmbedParams bad_img;
bad_img.images = {"file:///etc/passwd"};
ASSERT_THROW(api.embed(bad_img), ApiError);
ASSERT_EQ(rig.mock->send_count(), 1u);
}
TEST(cohere_rerank, documented_example_and_span_options) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v2/rerank", reply(kRerankV2));
Rerank api(rig.client());
RerankParams p;
p.model = "rerank-v5.0-pro";
p.query = "What is the capital of the United States?";
p.documents = {"a", "b", "c", "d", "e"};
p.top_n = 3;
p.relevant_spans = "enabled";
p.spans_threshold = 0.1;
p.max_spans = 3;
RerankResult r = api.rerank(p);
ASSERT_EQ(r.hits.size(), 3u);
ASSERT_EQ(r.hits[0].index, 2);
ASSERT_TRUE(r.hits[0].score > 0.84 && r.hits[0].score < 0.85);
ASSERT_TRUE(r.hits[0].spans.is_array());
ASSERT_EQ(r.usage.billed_input_tokens, 192);
ASSERT_TRUE(has(rig.mock->seen()[0].body, "\"relevant_spans\":{\"max_spans\":3,\"mode\":\"enabled\",\"threshold\":0.1}"));
RerankParams bad = p;
bad.documents.clear();
ASSERT_THROW(api.rerank(bad), ApiError);
bad = p;
bad.query = "";
ASSERT_THROW(api.rerank(bad), ApiError);
bad = p;
bad.relevant_spans = "disabled"; // options need enabled
ASSERT_THROW(api.rerank(bad), ApiError);
bad = p;
bad.top_n = 0;
ASSERT_THROW(api.rerank(bad), ApiError);
bad = p;
bad.documents.assign(1001, "x");
ASSERT_THROW(api.rerank(bad), ApiError);
}
TEST(cohere_rerank, v1_passthrough_checks_fields) {
Rig rig;
// SHAPE: v1 rerank result (schema rerank.results: index, relevance_score).
rig.mock->expect_response(Method::kPost, "/v1/rerank", reply("{\"id\":\"r\",\"results\":[{\"index\":0,\"relevance_score\":0.9}]}"));
Rerank api(rig.client());
Json out = api.rerank_v1(Json::parse("{\"model\":\"rerank-v3.5\",\"query\":\"q\",\"documents\":[\"a\"],\"return_documents\":true}"));
ASSERT_EQ(out.find("results")->as_array().size(), 1u);
ASSERT_THROW(api.rerank_v1(Json::parse("{\"query\":\"q\",\"documents\":[]}")), ApiError);
ASSERT_THROW(api.rerank_v1(Json::parse("{\"query\":\"q\",\"documents\":[\"a\"],\"extra\":1}")), ApiError);
}
TEST(cohere_classify, documented_example_and_rules) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v1/classify", reply(kClassify));
Classify api(rig.client());
ClassifyParams p;
p.inputs = {"Confirm your email address", "hey i need u to send some $"};
p.model = "YOUR-FINE-TUNED-MODEL-ID";
ClassifyResult r = api.classify(p);
ASSERT_EQ(r.items.size(), 2u);
ASSERT_EQ(r.items[0].prediction, "Not spam");
ASSERT_EQ(r.items[1].prediction, "Spam");
ASSERT_TRUE(r.items[1].confidence > 0.99);
ASSERT_EQ(r.items[0].classification_type, "single-label");
ASSERT_TRUE(r.items[0].labels.find("Spam") != nullptr);
ClassifyParams bad;
bad.inputs = {"x"};
ASSERT_THROW(api.classify(bad), ApiError); // no model and fewer than 2 examples
bad.examples = {{"a", "A"}, {"b", ""}};
ASSERT_THROW(api.classify(bad), ApiError);
ClassifyParams none;
none.model = "m";
ASSERT_THROW(api.classify(none), ApiError);
ASSERT_EQ(rig.mock->send_count(), 1u);
}
TEST(cohere_tokenizer, documented_examples_and_caps) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v1/tokenize", reply(kTokenize));
rig.mock->expect_response(Method::kPost, "/v1/detokenize", reply(kDetokenize));
Tokenizer api(rig.client());
TokenizeResult t = api.tokenize("command", "tokenize me! :D");
ASSERT_EQ(t.tokens.size(), 6u);
ASSERT_EQ(t.tokens[0], 10002L);
ASSERT_EQ(t.token_strings[2], " me");
ASSERT_EQ(api.detokenize("command", t.tokens), "tokenize me! :D");
ASSERT_TRUE(has(rig.mock->seen()[1].body, "\"tokens\":[10002,2261,2012,8,2792,43]"));
ASSERT_THROW(api.tokenize("command", ""), ApiError);
ASSERT_THROW(api.tokenize("command", std::string(65537, 'a')), ApiError);
ASSERT_THROW(api.detokenize("command", {}), ApiError);
ASSERT_THROW(api.detokenize("command", std::vector<long>(65537, 1)), ApiError);
ASSERT_THROW(api.detokenize("command", {-1}), ApiError);
ASSERT_THROW(api.tokenize("bad model", "x"), ApiError);
ASSERT_EQ(rig.mock->send_count(), 2u);
}
TEST(cohere_legacy, summarize_and_generate_validate_before_sending) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v1/summarize", reply(kSummarize));
rig.mock->expect_response(Method::kPost, "/v1/generate", reply("{\"id\":\"g\",\"generations\":[{\"id\":\"1\",\"text\":\"x\"}]}")); // SHAPE
Legacy api(rig.client());
Json s = api.summarize(Json::parse("{\"text\":\"a long text\",\"length\":\"short\"}"));
ASSERT_TRUE(s.find("summary") != nullptr);
ASSERT_TRUE(api.generate(Json::parse("{\"prompt\":\"hi\",\"max_tokens\":5}")).find("generations") != nullptr);
ASSERT_THROW(api.summarize(Json::parse("{\"length\":\"short\"}")), ApiError);
ASSERT_THROW(api.generate(Json::parse("{\"prompt\":\"x\",\"stream\":true}")), ApiError);
ASSERT_THROW(api.generate(Json::parse("{\"prompt\":\"x\",\"n\":2}")), ApiError);
ASSERT_EQ(rig.mock->send_count(), 2u);
}
// ---- account ---------------------------------------------------------------
TEST(cohere_account, check_api_key_posts_without_a_body) {
Rig rig;
// PROBE: an invalid key answered HTTP 200 {"valid":false} on 2026-10-25.
rig.mock->expect_response(Method::kPost, "/v1/check-api-key", reply("{\"valid\":false}"));
ApiKeyCheck c = Account(rig.client()).check_api_key();
ASSERT_FALSE(c.valid);
ASSERT_EQ(rig.mock->seen()[0].body, "");
}
TEST(cohere_account, models_list_filters_and_walk_reports_truncation) {
Rig rig;
// SHAPE: ListModelsResponse / GetModelResponse built from the schema (no published example).
const std::string page1 =
"{\"models\":[{\"name\":\"command-a-03-2025\",\"endpoints\":[\"chat\"],\"finetuned\":false,\"context_length\":256000,"
"\"features\":[\"tools\"],\"is_deprecated\":false}],\"next_page_token\":\"tok\"}";
rig.mock->expect_response(Method::kGet, "/v1/models", reply(page1));
Account api(rig.client());
ModelList l = api.models("chat", true, 50);
ASSERT_EQ(l.models.size(), 1u);
ASSERT_TRUE(l.find("command-a-03-2025")->supports("chat"));
ASSERT_EQ(l.page.next_page_token, "tok");
ASSERT_EQ(rig.mock->seen()[0].target, "/v1/models?endpoint=chat&default_only=true&page_size=50");
ASSERT_THROW(api.models("", true), ApiError); // default_only needs endpoint
ASSERT_THROW(api.models("nonsense"), ApiError);
ASSERT_THROW(api.models("", false, 0), ApiError);
ASSERT_THROW(api.models("", false, 1001), ApiError);
// The walk follows next_page_token and stops at the page cap, saying so.
Rig walk;
MockExpectation e;
e.method = Method::kGet;
e.path = "/v1/models";
e.response = reply(page1);
e.times = 100;
walk.mock->expect(e);
ModelList all = Account(walk.client()).all_models();
ASSERT_EQ(walk.mock->send_count(), 20u);
ASSERT_TRUE(all.page.truncated);
ASSERT_EQ(all.models.size(), 20u);
}
TEST(cohere_account, get_model_validates_the_name) {
Rig rig;
rig.mock->expect_response(Method::kGet, "/v1/models/command-a-03-2025",
reply("{\"name\":\"command-a-03-2025\",\"endpoints\":[\"chat\",\"generate\"],\"context_length\":256000,\"default_endpoints\":[\"chat\"]}"));
ModelInfo m = Account(rig.client()).model("command-a-03-2025");
ASSERT_EQ(m.context_length, 256000.0);
ASSERT_TRUE(m.supports("generate"));
ASSERT_THROW(Account(rig.client()).model("../x"), ApiError);
ASSERT_THROW(Account(rig.client()).model(""), ApiError);
}
// ---- datasets, batches, embed jobs -------------------------------------------
TEST(cohere_datasets, upload_is_multipart_with_query_and_validated) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v1/datasets", reply("{\"id\":\"ds-1\"}")); // SHAPE: create-dataset
Datasets api(rig.client());
DatasetUpload u;
u.name = "my data";
u.type = "embed-input";
u.filename = "d.jsonl";
u.data = "{\"text\":\"a\"}\n";
u.keep_fields = {"a", "b"};
u.skip_malformed_input = true;
ASSERT_EQ(api.create(u), "ds-1");
auto s = rig.mock->seen()[0];
ASSERT_TRUE(has(s.target, "name=my%20data"));
ASSERT_TRUE(has(s.target, "type=embed-input"));
ASSERT_TRUE(has(s.target, "keep_fields=a&keep_fields=b"));
ASSERT_TRUE(has(s.target, "skip_malformed_input=true"));
ASSERT_TRUE(has(s.headers.get_or("Content-Type", ""), "multipart/form-data"));
ASSERT_TRUE(has(s.body, "name=\"data\"; filename=\"d.jsonl\""));
DatasetUpload bad = u;
bad.type = "nope";
ASSERT_THROW(api.create(bad), ApiError);
bad = u;
bad.data.clear();
ASSERT_THROW(api.create(bad), ApiError);
bad = u;
bad.filename = "a/b";
ASSERT_THROW(api.create(bad), ApiError);
bad = u;
bad.eval_data = "x"; // needs eval_filename
ASSERT_THROW(api.create(bad), ApiError);
bad = u;
bad.name = "";
ASSERT_THROW(api.create(bad), ApiError);
ASSERT_EQ(rig.mock->send_count(), 1u);
}
TEST(cohere_datasets, list_get_usage_remove) {
Rig rig;
// SHAPE documents from the Dataset schema.
const std::string ds =
"{\"id\":\"ds-1\",\"name\":\"n\",\"created_at\":\"2026-01-01T00:00:00Z\",\"updated_at\":\"2026-01-01T00:00:00Z\","
"\"dataset_type\":\"embed-input\",\"validation_status\":\"validated\",\"dataset_parts\":[{\"id\":\"p1\",\"name\":\"d.jsonl\",\"num_rows\":3}]}";
rig.mock->expect_response(Method::kGet, "/v1/datasets", reply("{\"datasets\":[" + ds + "]}"));
rig.mock->expect_response(Method::kGet, "/v1/datasets/ds-1", reply("{\"dataset\":" + ds + "}"));
rig.mock->expect_response(Method::kGet, "/v1/datasets/usage", reply("{\"organization_usage\":\"1024\"}"));
rig.mock->expect_response(Method::kDelete, "/v1/datasets/ds-1", reply("{}"));
Datasets api(rig.client());
DatasetFilter f;
f.dataset_type = "embed-input";
f.validation_status = "validated";
f.limit = 10;
auto list = api.list(f);
ASSERT_EQ(list.size(), 1u);
ASSERT_TRUE(list[0].validated());
ASSERT_EQ(list[0].parts[0].num_rows, 3L);
ASSERT_TRUE(has(rig.mock->seen()[0].target, "datasetType=embed-input"));
ASSERT_TRUE(has(rig.mock->seen()[0].target, "validationStatus=validated"));
ASSERT_EQ(api.get("ds-1").name, "n");
ASSERT_EQ(api.usage().as_string(), "1024");
api.remove("ds-1");
ASSERT_EQ(rig.mock->seen()[3].method, "DELETE");
DatasetFilter bad;
bad.limit = 0;
ASSERT_THROW(api.list(bad), ApiError);
bad = DatasetFilter();
bad.validation_status = "weird";
ASSERT_THROW(api.list(bad), ApiError);
ASSERT_THROW(api.get("a/b"), ApiError);
ASSERT_THROW(api.remove(".."), ApiError);
}
TEST(cohere_datasets, await_validation_polls_with_a_caller_supplied_wait_and_a_cap) {
Rig rig;
MockExpectation e;
e.method = Method::kGet;
e.path = "/v1/datasets/ds-1";
e.response = reply("{\"dataset\":{\"id\":\"ds-1\",\"name\":\"n\",\"validation_status\":\"processing\"}}");
e.times = 10;
rig.mock->expect(e);
int waits = 0;
Dataset d = Datasets(rig.client()).await_validation("ds-1", 3, [&] { ++waits; });
ASSERT_EQ(d.validation_status, "processing");
ASSERT_EQ(rig.mock->send_count(), 3u);
ASSERT_EQ(waits, 3);
ASSERT_THROW(Datasets(rig.client()).await_validation("ds-1", 0, nullptr), ApiError);
ASSERT_THROW(Datasets(rig.client()).await_validation("ds-1", 61, nullptr), ApiError);
}
TEST(cohere_batches, create_get_list_cancel) {
Rig rig;
// SHAPE documents from the Batch schema.
const std::string b =
"{\"id\":\"b1\",\"name\":\"nightly\",\"status\":\"BATCH_STATUS_IN_PROGRESS\",\"input_dataset_id\":\"ds-1\",\"model\":\"command-a-03-2025\",\"num_records\":10,\"input_tokens\":\"42\"}";
rig.mock->expect_response(Method::kPost, "/v2/batches", reply("{\"batch\":" + b + "}"));
rig.mock->expect_response(Method::kGet, "/v2/batches/b1", reply("{\"batch\":" + b + "}"));
rig.mock->expect_response(Method::kGet, "/v2/batches", reply("{\"batches\":[" + b + "],\"next_page_token\":\"n\"}"));
rig.mock->expect_response(Method::kPost, "/v2/batches/b1:cancel", reply("{}"));
Batches api(rig.client());
Batch made = api.create("nightly", "ds-1", "command-a-03-2025");
ASSERT_EQ(made.id, "b1");
ASSERT_FALSE(made.terminal());
ASSERT_EQ(made.input_tokens, "42");
ASSERT_TRUE(has(rig.mock->seen()[0].body, "\"input_dataset_id\":\"ds-1\""));
ASSERT_EQ(api.get("b1").num_records, 10L);
BatchList l = api.list(20, "", "updated_at");
ASSERT_EQ(l.batches.size(), 1u);
ASSERT_EQ(l.page.next_page_token, "n");
ASSERT_EQ(rig.mock->seen()[2].target, "/v2/batches?page_size=20&order_by=updated_at");
api.cancel("b1");
ASSERT_EQ(rig.mock->seen()[3].method, "POST");
ASSERT_EQ(rig.mock->seen()[3].body, "");
ASSERT_THROW(api.create("", "ds-1", "m"), ApiError);
ASSERT_THROW(api.create("n", "a/b", "m"), ApiError);
ASSERT_THROW(api.list(20, "", "name"), ApiError);
ASSERT_THROW(api.cancel("x?y"), ApiError);
Batch done;
done.status = "BATCH_STATUS_COMPLETED";
ASSERT_TRUE(done.terminal());
}
TEST(cohere_embed_jobs, create_list_get_cancel) {
Rig rig;
// SHAPE documents from CreateEmbedJobResponse / EmbedJob.
rig.mock->expect_response(Method::kPost, "/v1/embed-jobs", reply("{\"job_id\":\"j1\"}"));
rig.mock->expect_response(Method::kGet, "/v1/embed-jobs", reply("{\"embed_jobs\":[{\"job_id\":\"j1\",\"status\":\"processing\",\"model\":\"embed-v4.0\"}]}"));
rig.mock->expect_response(Method::kGet, "/v1/embed-jobs/j1", reply("{\"job_id\":\"j1\",\"status\":\"complete\",\"output_dataset_id\":\"o1\"}"));
rig.mock->expect_response(Method::kPost, "/v1/embed-jobs/j1/cancel", reply("{}"));
EmbedJobs api(rig.client());
EmbedJobParams p;
p.dataset_id = "ds-1";
p.name = "job";
p.embedding_types = {"float"};
p.truncate = "END";
ASSERT_EQ(api.create(p), "j1");
ASSERT_TRUE(has(rig.mock->seen()[0].body, "\"dataset_id\":\"ds-1\""));
ASSERT_EQ(api.list().size(), 1u);
EmbedJob j = api.get("j1");
ASSERT_TRUE(j.terminal());
ASSERT_EQ(j.output_dataset_id, "o1");
api.cancel("j1");
EmbedJobParams bad = p;
bad.dataset_id = "";
ASSERT_THROW(api.create(bad), ApiError);
bad = p;
bad.truncate = "NONE"; // jobs accept START or END only
ASSERT_THROW(api.create(bad), ApiError);
bad = p;
bad.input_type = "x";
ASSERT_THROW(api.create(bad), ApiError);
ASSERT_THROW(api.get("../x"), ApiError);
}
// ---- connectors and fine-tuning ---------------------------------------------
TEST(cohere_connectors, crud_and_oauth_authorize) {
Rig rig;
// SHAPE documents from the Connector schema.
const std::string c = "{\"connector\":{\"id\":\"c1\",\"name\":\"wiki\",\"url\":\"https://example.com/search\",\"active\":true,\"excludes\":[\"a\"]}}";
rig.mock->expect_response(Method::kPost, "/v1/connectors", reply(c));
rig.mock->expect_response(Method::kGet, "/v1/connectors/c1", reply(c));
rig.mock->expect_response(Method::kPatch, "/v1/connectors/c1", reply(c));
rig.mock->expect_response(Method::kGet, "/v1/connectors", reply("{\"connectors\":[" + c.substr(13, c.size() - 14) + "],\"total_count\":1}"));
rig.mock->expect_response(Method::kDelete, "/v1/connectors/c1", reply("{}"));
rig.mock->expect_response(Method::kPost, "/v1/connectors/c1/oauth/authorize", reply("{\"redirect_url\":\"https://auth.example.com/x\"}"));
Connectors api(rig.client());
ConnectorParams p;
p.name = "wiki";
p.url = "https://example.com/search";
p.excludes = {"a"};
p.active = true;
Connector made = api.create(p);
ASSERT_EQ(made.id, "c1");
ASSERT_TRUE(made.active);
ASSERT_TRUE(has(rig.mock->seen()[0].body, "\"url\":\"https://example.com/search\""));
ASSERT_EQ(api.get("c1").name, "wiki");
ConnectorParams patch;
patch.active = false;
api.update("c1", patch);
ASSERT_EQ(rig.mock->seen()[2].body, "{\"active\":false}");
ASSERT_EQ(api.list(10).size(), 1u);
api.remove("c1");
ASSERT_EQ(api.oauth_authorize("c1", "https://app.example.com/done"), "https://auth.example.com/x");
ASSERT_TRUE(has(rig.mock->seen()[5].target, "after_token_redirect=https%3A%2F%2Fapp.example.com%2Fdone"));
ConnectorParams bad;
ASSERT_THROW(api.create(bad), ApiError); // needs name and url
bad.name = "n";
bad.url = "ftp://x";
ASSERT_THROW(api.create(bad), ApiError);
ASSERT_THROW(api.update("c1", ConnectorParams()), ApiError); // nothing to send
ConnectorParams oauth;
oauth.name = "n";
oauth.url = "https://x.example.com";
oauth.oauth = Json::parse("{\"client_id\":\"i\"}");
ASSERT_THROW(api.create(oauth), ApiError); // authorize_url and token_url missing
ASSERT_THROW(api.list(0), ApiError);
}
TEST(cohere_finetuning, lifecycle_calls_and_settings_rules) {
Rig rig;
// SHAPE documents from the FinetunedModel schema.
const std::string m = "{\"finetuned_model\":{\"id\":\"ft1\",\"name\":\"mine\",\"status\":\"STATUS_READY\",\"settings\":{\"dataset_id\":\"ds-1\"}}}";
rig.mock->expect_response(Method::kPost, "/v1/finetuning/finetuned-models", reply(m));
rig.mock->expect_response(Method::kGet, "/v1/finetuning/finetuned-models/ft1", reply(m));
rig.mock->expect_response(Method::kPatch, "/v1/finetuning/finetuned-models/ft1", reply(m));
rig.mock->expect_response(Method::kGet, "/v1/finetuning/finetuned-models",
reply("{\"finetuned_models\":[{\"id\":\"ft1\",\"name\":\"mine\"}],\"next_page_token\":\"t\",\"total_size\":7}"));
rig.mock->expect_response(Method::kGet, "/v1/finetuning/finetuned-models/ft1/events", reply("{\"events\":[{\"status\":\"STATUS_QUEUED\"}]}"));
rig.mock->expect_response(Method::kGet, "/v1/finetuning/finetuned-models/ft1/training-step-metrics", reply("{\"step_metrics\":[]}"));
rig.mock->expect_response(Method::kDelete, "/v1/finetuning/finetuned-models/ft1", reply("{}"));
FineTuning api(rig.client());
Json settings = Json::parse("{\"base_model\":{\"base_type\":\"BASE_TYPE_CHAT\"},\"dataset_id\":\"ds-1\"}");
FinetunedModel made = api.create("mine", settings);
ASSERT_TRUE(made.ready());
ASSERT_EQ(made.id, "ft1");
ASSERT_TRUE(has(rig.mock->seen()[0].body, "\"base_type\":\"BASE_TYPE_CHAT\""));
ASSERT_EQ(api.get("ft1").name, "mine");
api.update("ft1", "renamed", Json());
ASSERT_EQ(rig.mock->seen()[2].body, "{\"name\":\"renamed\"}");
auto page = api.list(5);
ASSERT_EQ(page.models.size(), 1u);
ASSERT_EQ(page.page.total, 7L);
ASSERT_EQ(page.page.next_page_token, "t");
ASSERT_TRUE(api.events("ft1").find("events") != nullptr);
ASSERT_TRUE(api.step_metrics("ft1", 10).find("step_metrics") != nullptr);
api.remove("ft1");
ASSERT_THROW(api.create("", settings), ApiError);
ASSERT_THROW(api.create("n", Json::parse("{\"dataset_id\":\"d\"}")), ApiError); // no base_model
ASSERT_THROW(api.create("n", Json::parse("{\"base_model\":{\"base_type\":\"x\"}}")), ApiError); // no dataset_id
ASSERT_THROW(api.update("ft1", "", Json()), ApiError);
ASSERT_THROW(api.events("a/b"), ApiError);
}
// ---- parse and transcription -------------------------------------------------
TEST(cohere_documents, parse_and_transcribe_validate_and_send) {
Rig rig;
rig.mock->expect_response(Method::kPost, "/v2/parse", reply("{\"id\":\"p1\",\"pages\":[]}")); // SHAPE: ParseResponse
rig.mock->expect_response(Method::kPost, "/v2/audio/transcriptions", reply("{\"text\":\"hello\"}")); // SHAPE
Documents api(rig.client());
Json out = api.parse("some-parse-model", "https://example.com/page.png", "markdown");
ASSERT_EQ(out.find("id")->as_string(), "p1");
ASSERT_TRUE(has(rig.mock->seen()[0].body, "\"document\":{\"image_url\":\"https://example.com/page.png\",\"type\":\"image_url\"}"));
ASSERT_EQ(api.transcribe("m", "en", "a.wav", "audio/wav", "RIFFxxxx", 0.0), "hello");
auto s = rig.mock->seen()[1];
ASSERT_TRUE(has(s.headers.get_or("Content-Type", ""), "multipart/form-data"));
ASSERT_TRUE(has(s.body, "name=\"language\""));
ASSERT_TRUE(has(s.body, "filename=\"a.wav\""));
ASSERT_THROW(api.parse("m", "ftp://x"), ApiError);
ASSERT_THROW(api.parse("m", "https://x.example.com/a.png", "html"), ApiError);
ASSERT_THROW(api.transcribe("m", "", "a.wav", "audio/wav", "x"), ApiError);
ASSERT_THROW(api.transcribe("m", "en", "a.wav", "audio/wav", ""), ApiError);
ASSERT_THROW(api.transcribe("m", "en", "a/b.wav", "audio/wav", "x"), ApiError);
ASSERT_THROW(api.transcribe("m", "en", "a.wav", "audio/wav", "x", 3.0), ApiError);
ASSERT_EQ(rig.mock->send_count(), 2u);
}
TEST(cohere_resources, nothing_is_sent_without_a_key) {
Rig rig("");
ASSERT_THROW(Account(rig.client()).check_api_key(), molo::llm::NotConfiguredError);
ASSERT_THROW(Datasets(rig.client()).list(), molo::llm::NotConfiguredError);
ASSERT_THROW(Batches(rig.client()).cancel("b1"), molo::llm::NotConfiguredError);
ASSERT_EQ(rig.mock->send_count(), 0u);
}
+90
View File
@@ -0,0 +1,90 @@
// Opt-in live checks. MOLO_LIVE_NET=1 enables the network; COHERE_API_KEY (via
// llm::Config) enables the read-only calls plus ONE tiny chat; embed and rerank
// additionally need MOLO_LIVE_PAID=1. Without them each test prints a loud SKIP.
// Nothing is created, updated or deleted.
#include <cstdlib>
#include <iostream>
#include "clients/transports/http_transport.h"
#include "support.h"
#include "vendors/cohere/chat.h"
#include "vendors/cohere/inference.h"
#include "vendors/cohere/resources.h"
using namespace molo::cohere;
using molo::llm::ApiError;
namespace {
bool net() { return std::getenv("MOLO_LIVE_NET") != nullptr; }
bool paid() { return std::getenv("MOLO_LIVE_PAID") != nullptr; }
std::shared_ptr<molo::llm::ApiClient> invalid_key_client() {
return std::make_shared<molo::llm::ApiClient>(std::make_unique<molo::clients::transports::HttpTransport>(),
Platform::config(), "invalid-molo-probe");
}
} // namespace
// No key needed. 2026-10-25 observations: an invalid key on GET /v1/models answers 401 with
// {"id","message"}; POST /v1/check-api-key with an invalid key answers 200 {"valid":false}.
TEST(cohere_live, invalid_key_probes) {
if (!net()) {
std::cout << "SKIP cohere_live.invalid_key_probes (set MOLO_LIVE_NET=1)\n";
return;
}
auto client = invalid_key_client();
try {
Account(client).models();
FAIL("expected 401");
} catch (const ApiError& e) {
std::cout << " GET /v1/models with an invalid key: " << e.http_status() << " " << e.message().substr(0, 40) << "\n";
ASSERT_EQ(e.http_status(), 401);
}
ApiKeyCheck c = Account(client).check_api_key();
std::cout << " POST /v1/check-api-key with an invalid key: valid=" << c.valid << "\n";
ASSERT_FALSE(c.valid);
try {
Chat(client).create(ChatParams::prompt("hi"));
FAIL("expected 401");
} catch (const ApiError& e) {
ASSERT_EQ(e.http_status(), 401);
}
}
TEST(cohere_live, key_check_models_and_one_tiny_chat) {
Platform p = Platform::from_env();
if (!net() || !p.configured()) {
std::cout << "SKIP cohere_live.key_check_models_and_one_tiny_chat (set MOLO_LIVE_NET=1 and COHERE_API_KEY)\n";
return;
}
Account acct(p.client);
ApiKeyCheck c = acct.check_api_key();
std::cout << " key valid=" << c.valid << "\n";
ASSERT_TRUE(c.valid);
ModelList models = acct.all_models("chat");
std::cout << " chat models: " << models.models.size() << "\n";
ASSERT_TRUE(!models.models.empty());
ChatParams params = ChatParams::prompt("Reply with the single word: ok");
params.max_tokens = 16;
ChatResult r = Chat(p.client).create(params);
std::cout << " chat finish=" << r.finish_reason << " tokens=" << r.usage.input_tokens + r.usage.output_tokens << "\n";
ASSERT_TRUE(r.usage.present);
}
TEST(cohere_live, paid_embed_and_rerank_one_smallest_request_each) {
Platform p = Platform::from_env();
if (!net() || !paid() || !p.configured()) {
std::cout << "SKIP cohere_live.paid_embed_and_rerank_one_smallest_request_each (set MOLO_LIVE_NET=1, MOLO_LIVE_PAID=1 and COHERE_API_KEY)\n";
return;
}
EmbedParams e;
e.texts = {"hello"};
e.embedding_types = {"float"};
EmbedResult er = Embeddings(p.client).embed(e);
ASSERT_EQ(er.floats().size(), 1u);
RerankParams rp;
rp.query = "capital of France";
rp.documents = {"Paris is the capital of France.", "Bananas are yellow."};
RerankResult rr = Rerank(p.client).rerank(rp);
ASSERT_EQ(rr.hits.size(), 2u);
ASSERT_EQ(rr.hits[0].index, 0);
}