Package src/vendors/cohere (molo::cohere): Chat v2 with streaming, tools, thinking and citations; embed, rerank, classify, tokenize, detokenize, parse, transcription; datasets, embed jobs, batches, connectors, fine-tuning, models, check-api-key; legacy v1 chat/generate/summarize; CohereChatModel : ChatModel. All 42 operations of cohere-openapi.yaml are in one call table, verified by tools/check_spec.py. Demo routes under /ai/cohere, 51 unit tests and 2 route tests; live tests are opt-in. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
115 lines
8.9 KiB
C++
115 lines
8.9 KiB
C++
// 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");
|
|
}
|