diff --git a/example/app/ai/MistralDemo.cpp b/example/app/ai/MistralDemo.cpp index 5250adf..1818d19 100644 --- a/example/app/ai/MistralDemo.cpp +++ b/example/app/ai/MistralDemo.cpp @@ -1,13 +1,792 @@ #include "../AiDemos.hpp" +#include + #include "../../fw/Router.hpp" +#include "../../fw/Templates.hpp" +#include "security/crypto/Base64.h" +#include "vendors/mistral/agents.h" +#include "vendors/mistral/audio.h" +#include "vendors/mistral/batch.h" +#include "vendors/mistral/calls.h" +#include "vendors/mistral/caps.h" +#include "vendors/mistral/chat.h" +#include "vendors/mistral/classifiers.h" +#include "vendors/mistral/conversations.h" +#include "vendors/mistral/embeddings.h" +#include "vendors/mistral/files.h" +#include "vendors/mistral/finetune.h" +#include "vendors/mistral/libraries.h" +#include "vendors/mistral/models.h" +#include "vendors/mistral/moderation.h" +#include "vendors/mistral/ocr.h" +#include "vendors/mistral/operations.h" +#include "vendors/mistral/platform.h" namespace molo::example::app { -// mistral. Stub: the package agent fills this in. +// Mistral La Plateforme over the real src/vendors/mistral part. GET routes need +// no credential. Every route that calls Mistral is a POST: 422 on bad input, +// 503 mistral_not_configured without MISTRAL_API_KEY. Routes that cost money +// (completions, embeddings, OCR, transcription, batch, fine-tuning, +// conversations) or change resources (upload, create, cancel, delete) are dry +// runs that show the exact request unless the body has "confirm":true. + +namespace { + +namespace mi = mistral; +using Json = clients::body::Json; + +constexpr std::size_t kMaxBody = 24 * 1024 * 1024; // uploads travel as base64 +constexpr std::size_t kMaxOutput = 256 * 1024; // bytes of JSON returned to the browser + +const char kPage[] = R"MISTRAL({% extends "base.html" %} +{% block content %} +

Mistral La Plateforme

+

Chat, streaming, FIM, agents, conversations, embeddings, moderation, classifiers, +OCR, transcription, files, batch, fine-tuning, libraries, models and any other operation of the +OpenAPI document. Paid and destructive calls are dry runs until the body says +"confirm":true.

+

Checking credential...

+
+ + +
+ + +
+ +

+
+ + +{% endblock %} +)MISTRAL"; + +[[noreturn]] void bad(const std::string& why) { throw fw::HttpError(422, "validation_error", why); } + +// Keeps what goes back to the browser bounded. +Json bounded(const Json& j) { + std::string text = j.dump(); + if (text.size() <= kMaxOutput) return j; + Json o = Json::object(); + o.set("truncated", Json(true)); + o.set("bytes", Json(static_cast(text.size()))); + o.set("preview", Json(text.substr(0, 4096))); + return o; +} + +fw::Response json_out(const Json& j, int status = 200) { + fw::Response res = fw::JsonResponse(bounded(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 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(v->as_int()); +} + +std::optional 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 strings(const Json& b, const char* key) { + std::vector 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; +} + +std::string decode64(const Json& b, const char* key) { + std::string out; + if (!security::crypto::Base64::decode(text(b, key, true), out)) bad(std::string(key) + " is not valid base64"); + return out; +} + +Json usage_json(const mi::TokenUsage& u) { + Json o = Json::object(); + o.set("prompt_tokens", Json(u.prompt_tokens)); + o.set("completion_tokens", Json(u.completion_tokens)); + o.set("total_tokens", Json(u.total_tokens)); + return o; +} + +Json chat_json(const mi::ChatResult& r) { + Json o = Json::object(); + o.set("id", Json(r.id)); + o.set("model", Json(r.model)); + if (!r.choices.empty()) { + const mi::ChatChoice& c = r.first(); + o.set("finish_reason", Json(c.finish_reason)); + o.set("content", Json(c.content)); + o.set("reasoning", Json(c.reasoning)); + Json calls = Json::array(); + for (const mi::ToolCallRecord& t : c.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("usage", usage_json(r.usage)); + return o; +} + +Json dry_run(const Json& request, const char* 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; +} + +mi::ChatParams chat_params(const Json& b) { + mi::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(mi::Messages::system(system)); + p.add(mi::Messages::user(text(b, "prompt", true))); + p.max_tokens = number(b, "max_tokens"); + p.temperature = real(b, "temperature"); + p.top_p = real(b, "top_p"); + p.random_seed = number(b, "random_seed"); + p.stop = strings(b, "stop"); + p.reasoning_effort = text(b, "effort"); + if (flag(b, "json")) p.format = mi::ResponseFormat::kJsonObject; + if (flag(b, "safe_prompt")) p.safe_prompt = true; + return p; +} + +struct Plan { + Json request; // what would be sent (shown in a dry run) + const char* path = ""; // for the dry-run message + bool needs_confirm = false; // paid or changes a resource + std::function call; +}; + +class MistralRoutes { + public: + explicit MistralRoutes(mi::Platform platform) : p_(std::move(platform)) {} + const mi::Platform& platform() const { return p_; } + + using Planner = std::function; + + 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); + } + } + + // ---- completions --------------------------------------------------------- + Plan chat(const Json& b) const { + mi::ChatParams params = chat_params(b); + Plan pl{params.to_json(), "/v1/chat/completions", true, nullptr}; + auto api = std::make_shared(p_.client); + pl.call = [api, params] { return chat_json(api->create(params)); }; + return pl; + } + Plan stream(const Json& b) const { + mi::ChatParams params = chat_params(b); + Plan pl{params.to_json(true), "/v1/chat/completions", true, nullptr}; + auto api = std::make_shared(p_.client); + pl.call = [api, params] { + std::size_t events = 0; + mi::ChatResult r = api->stream(params, [&](const mi::StreamDelta&) { + return ++events < mi::MistralCaps::kMaxStreamEvents; + }); + Json o = chat_json(r); + o.set("chunks", Json(static_cast(events))); + return o; + }; + return pl; + } + Plan agent(const Json& b) const { + mi::ChatParams params; + params.agent_id = text(b, "agent_id", true); + params.add(mi::Messages::user(text(b, "prompt", true))); + params.max_tokens = number(b, "max_tokens"); + params.random_seed = number(b, "random_seed"); + Plan pl{params.to_json(), "/v1/agents/completions", true, nullptr}; + auto api = std::make_shared(p_.client); + pl.call = [api, params] { return chat_json(api->create(params)); }; + return pl; + } + Plan fim(const Json& b) const { + mi::FimParams params; + std::string model = text(b, "model"); + if (!model.empty()) params.model = model; + params.prompt = text(b, "prompt", true); + params.suffix = text(b, "suffix"); + params.max_tokens = number(b, "max_tokens"); + params.stop = strings(b, "stop"); + params.temperature = real(b, "temperature"); + Plan pl{params.to_json(), "/v1/fim/completions", true, nullptr}; + auto api = std::make_shared(p_.client); + pl.call = [api, params] { return chat_json(api->create(params)); }; + return pl; + } + Plan embeddings(const Json& b) const { + mi::EmbeddingParams params; + std::string model = text(b, "model"); + if (!model.empty()) params.model = model; + params.inputs = strings(b, "inputs"); + if (params.inputs.empty() && !text(b, "input").empty()) params.inputs = {text(b, "input")}; + params.output_dimension = number(b, "output_dimension"); + params.output_dtype = text(b, "output_dtype"); + Plan pl{params.to_json(), "/v1/embeddings", true, nullptr}; + auto api = std::make_shared(p_.client); + pl.call = [api, params] { + mi::EmbeddingResult r = api->create(params); + Json o = Json::object(); + o.set("model", Json(r.model)); + o.set("count", Json(static_cast(r.vectors.size() + r.encoded.size()))); + o.set("dimension", Json(r.vectors.empty() ? 0 : static_cast(r.vectors[0].size()))); + Json first = Json::array(); + if (!r.vectors.empty()) + for (std::size_t i = 0; i < r.vectors[0].size() && i < 8; ++i) first.push(Json(r.vectors[0][i])); + o.set("first_vector_head", first); + o.set("usage", usage_json(r.usage)); + return o; + }; + return pl; + } + Plan moderation(const Json& b) const { + std::vector texts = strings(b, "inputs"); + if (texts.empty() && !text(b, "input").empty()) texts = {text(b, "input")}; + if (texts.empty()) bad("input or inputs is required"); + Json req = Json::object(); + req.set("input", Json(texts[0])); + req.set("count", Json(static_cast(texts.size()))); + auto api = std::make_shared(p_.client); + return Plan{req, "/v1/moderations", true, [api, texts] { + mi::ModerationResult r = api->moderate(texts); + Json arr = Json::array(); + for (const mi::ModerationItem& it : r.results) { + Json x = Json::object(); + x.set("flagged", Json(it.flagged())); + Json cats = Json::array(); + for (const std::string& c : it.flagged_categories()) cats.push(Json(c)); + x.set("categories", cats); + arr.push(x); + } + Json o = Json::object(); + o.set("model", Json(r.model)); + o.set("results", arr); + return o; + }}; + } + Plan classify(const Json& b) const { + std::string model = text(b, "model", true); + std::vector texts = strings(b, "inputs"); + if (texts.empty() && !text(b, "input").empty()) texts = {text(b, "input")}; + if (texts.empty()) bad("input or inputs is required"); + Json req = Json::object(); + req.set("model", Json(model)); + req.set("count", Json(static_cast(texts.size()))); + auto api = std::make_shared(p_.client); + return Plan{req, "/v1/classifications", true, [api, model, texts] { return api->classify(model, texts).raw; }}; + } + Plan ocr(const Json& b) const { + mi::OcrParams params; + std::string model = text(b, "model"); + if (!model.empty()) params.model = model; + std::string kind = text(b, "kind"); + if (kind.empty() || kind == "document_url") params.source = mi::OcrSource::kDocumentUrl; + else if (kind == "image_url") params.source = mi::OcrSource::kImageUrl; + else if (kind == "file") params.source = mi::OcrSource::kFileId; + else bad("kind must be document_url, image_url or file"); + params.reference = text(b, "reference", true); + if (const Json* pg = b.find("pages"); pg && pg->is_array()) + for (const Json& x : pg->as_array()) { + if (!x.is_number()) bad("pages must be numbers"); + params.pages.push_back(static_cast(x.as_int())); + } + params.table_format = text(b, "table_format"); + Plan pl{params.to_json(), "/v1/ocr", true, nullptr}; + auto api = std::make_shared(p_.client); + pl.call = [api, params] { + mi::OcrResult r = api->process(params); + Json o = Json::object(); + o.set("model", Json(r.model)); + o.set("pages_processed", Json(static_cast(r.pages_processed))); + o.set("markdown", Json(r.markdown())); + return o; + }; + return pl; + } + Plan transcribe(const Json& b) const { + mi::TranscriptionParams params; + std::string model = text(b, "model"); + if (!model.empty()) params.model = model; + if (b.find("data_base64")) { + params.file_data = decode64(b, "data_base64"); + params.file_name = text(b, "filename", true); + } + params.file_url = text(b, "file_url"); + params.file_id = text(b, "file_id"); + params.language = text(b, "language"); + params.diarize = flag(b, "diarize"); + params.timestamp_granularities = strings(b, "timestamp_granularities"); + params.validate(); + Json req = Json::object(); + req.set("model", Json(params.model)); + req.set("source", Json(!params.file_data.empty() ? "upload" : !params.file_url.empty() ? "file_url" : "file_id")); + req.set("bytes", Json(static_cast(params.file_data.size()))); + auto api = std::make_shared(p_.client); + return Plan{req, "/v1/audio/transcriptions", true, [api, params] { + mi::TranscriptionResult r = api->transcribe(params); + Json o = Json::object(); + o.set("model", Json(r.model)); + o.set("language", Json(r.language)); + o.set("text", Json(r.text)); + o.set("segments", Json(static_cast(r.segments.size()))); + o.set("usage", usage_json(r.usage)); + return o; + }}; + } + + // ---- resources ----------------------------------------------------------- + Plan models(const Json& b) const { + auto api = std::make_shared(p_.client); + std::string action = text(b, "action"); + if (action == "get" || action == "delete") { + std::string id = text(b, "id", true); + if (!llm::Service::valid_id(id)) bad("id has invalid characters"); + Json req = Json::object(); + req.set("id", Json(id)); + if (action == "get") return Plan{req, "/v1/models/{id}", false, [api, id] { + mi::ModelCard m = api->get(id); + Json x = Json::object(); + x.set("id", Json(m.id)); + x.set("type", Json(m.type)); + x.set("owned_by", Json(m.owned_by)); + x.set("max_context_length", Json(static_cast(m.max_context_length))); + x.set("archived", Json(m.archived)); + return x; + }}; + return Plan{req, "DELETE /v1/models/{id}", true, [api, id] { + mi::ModelDeleted d = api->remove(id); + Json o = Json::object(); + o.set("id", Json(d.id)); + o.set("deleted", Json(d.deleted)); + return o; + }}; + } + if (!action.empty() && action != "list") bad("action must be list, get or delete"); + return Plan{Json::object(), "/v1/models", false, [api] { + mi::ModelList l = api->list(); + Json arr = Json::array(); + for (const mi::ModelCard& m : l.models) { + Json x = Json::object(); + x.set("id", Json(m.id)); + x.set("type", Json(m.type)); + x.set("max_context_length", Json(static_cast(m.max_context_length))); + x.set("chat", Json(m.capabilities.completion_chat)); + x.set("vision", Json(m.capabilities.vision)); + x.set("fim", Json(m.capabilities.completion_fim)); + x.set("ocr", Json(m.capabilities.ocr)); + arr.push(x); + } + Json o = Json::object(); + o.set("models", arr); + o.set("truncated", Json(l.truncated)); + return o; + }}; + } + Plan files(const Json& b) const { + std::string action = text(b, "action", true); + auto api = std::make_shared(p_.client); + auto file_json = [](const mi::FileInfo& f) { + Json x = Json::object(); + x.set("id", Json(f.id)); + x.set("filename", Json(f.filename)); + x.set("purpose", Json(f.purpose)); + x.set("bytes", Json(static_cast(f.bytes))); + x.set("created_at", Json(static_cast(f.created_at))); + return x; + }; + if (action == "list") { + mi::FileListParams lp; + if (auto n = number(b, "page_size")) lp.page_size = *n; + if (auto n = number(b, "page")) lp.page = *n; + lp.purpose = text(b, "purpose"); + lp.validate(); + return Plan{Json::object(), "/v1/files", false, [api, lp, file_json] { + mi::FileList l = api->list(lp); + Json arr = Json::array(); + for (const mi::FileInfo& f : l.files) arr.push(file_json(f)); + Json o = Json::object(); + o.set("files", arr); + o.set("total", Json(static_cast(l.total))); + return o; + }}; + } + if (action == "get" || action == "delete") { + std::string id = text(b, "id", true); + if (!llm::Service::valid_id(id)) bad("id has invalid characters"); + Json req = Json::object(); + req.set("id", Json(id)); + if (action == "get") return Plan{req, "/v1/files/{id}", false, [api, id, file_json] { return file_json(api->get(id)); }}; + return Plan{req, "DELETE /v1/files/{id}", true, [api, id] { + mi::FileDeleted d = api->remove(id); + Json o = Json::object(); + o.set("id", Json(d.id)); + o.set("deleted", Json(d.deleted)); + return o; + }}; + } + if (action == "upload") { + mi::FileUploadParams up; + up.filename = text(b, "filename", true); + up.data = decode64(b, "data_base64"); + std::string purpose = text(b, "purpose"); + if (!purpose.empty()) up.purpose = purpose; + up.validate(); + Json req = Json::object(); + req.set("filename", Json(up.filename)); + req.set("bytes", Json(static_cast(up.data.size()))); + req.set("purpose", Json(up.purpose)); + return Plan{req, "POST /v1/files", true, [api, up, file_json] { return file_json(api->upload(up)); }}; + } + bad("action must be list, get, upload or delete"); + } + Plan batch(const Json& b) const { + std::string action = text(b, "action", true); + auto api = std::make_shared(p_.client); + if (action == "list") { + mi::BatchListParams lp; + lp.status = text(b, "status"); + lp.validate(); + return Plan{Json::object(), "/v1/batch/jobs", false, [api, lp] { return api->list(lp).raw; }}; + } + if (action == "create") { + mi::BatchParams bp; + bp.endpoint = text(b, "endpoint").empty() ? bp.endpoint : text(b, "endpoint"); + bp.model = text(b, "model"); + bp.agent_id = text(b, "agent_id"); + bp.input_files = strings(b, "input_files"); + if (auto n = number(b, "timeout_hours")) bp.timeout_hours = *n; + return Plan{bp.to_json(), "POST /v1/batch/jobs", true, [api, bp] { return api->create(bp).raw; }}; + } + std::string id = text(b, "id", true); + if (!llm::Service::valid_id(id)) bad("id has invalid characters"); + Json req = Json::object(); + req.set("id", Json(id)); + if (action == "get") return Plan{req, "/v1/batch/jobs/{id}", false, [api, id] { return api->get(id).raw; }}; + if (action == "cancel") return Plan{req, "POST /v1/batch/jobs/{id}/cancel", true, [api, id] { return api->cancel(id).raw; }}; + if (action == "delete") return Plan{req, "DELETE /v1/batch/jobs/{id}", true, [api, id] { return api->remove(id); }}; + bad("action must be list, get, create, cancel or delete"); + } + Plan finetune(const Json& b) const { + std::string action = text(b, "action", true); + auto api = std::make_shared(p_.client); + if (action == "list") { + mi::FineTuneListParams lp; + lp.status = text(b, "status"); + lp.validate(); + return Plan{Json::object(), "/v1/fine_tuning/jobs", false, [api, lp] { return api->list_jobs(lp).raw; }}; + } + if (action == "create") { + mi::FineTuneJobParams jp; + jp.model = text(b, "model", true); + for (const std::string& f : strings(b, "training_files")) jp.training_files.push_back({f, 1.0}); + jp.validation_files = strings(b, "validation_files"); + jp.suffix = text(b, "suffix"); + jp.job_type = text(b, "job_type"); + if (const Json* h = b.find("hyperparameters"); h && h->is_object()) jp.hyperparameters = *h; + jp.auto_start = flag(b, "auto_start"); + jp.dry_run = flag(b, "dry_run"); + return Plan{jp.to_json(), "POST /v1/fine_tuning/jobs", true, [api, jp] { return api->create_job(jp).raw; }}; + } + std::string id = text(b, "id", true); + if (!llm::Service::valid_id(id)) bad("id has invalid characters"); + Json req = Json::object(); + req.set("id", Json(id)); + if (action == "get") return Plan{req, "/v1/fine_tuning/jobs/{id}", false, [api, id] { return api->get_job(id).raw; }}; + if (action == "start") return Plan{req, "POST /v1/fine_tuning/jobs/{id}/start", true, [api, id] { return api->start_job(id).raw; }}; + if (action == "cancel") return Plan{req, "POST /v1/fine_tuning/jobs/{id}/cancel", true, [api, id] { return api->cancel_job(id).raw; }}; + bad("action must be list, get, create, start or cancel"); + } + Plan agents(const Json& b) const { + std::string action = text(b, "action", true); + auto api = std::make_shared(p_.client); + if (action == "list") return Plan{Json::object(), "/v1/agents/pages", false, [api] { return api->list().raw; }}; + if (action == "create") { + mi::AgentParams ap; + ap.model = text(b, "model", true); + ap.name = text(b, "name", true); + ap.instructions = text(b, "instructions"); + ap.description = text(b, "description"); + if (const Json* t = b.find("tools"); t && t->is_array()) ap.tools = *t; + return Plan{ap.to_json(true), "POST /v1/agents", true, [api, ap] { return api->create(ap).raw; }}; + } + std::string id = text(b, "id", true); + if (!llm::Service::valid_id(id)) bad("id has invalid characters"); + Json req = Json::object(); + req.set("id", Json(id)); + if (action == "get") return Plan{req, "/v1/agents/{id}", false, [api, id] { return api->get(id).raw; }}; + if (action == "delete") + return Plan{req, "DELETE /v1/agents/{id}", true, [api, id] { + api->remove(id); + Json o = Json::object(); + o.set("deleted", Json(true)); + return o; + }}; + bad("action must be list, get, create or delete"); + } + Plan conversations(const Json& b) const { + std::string action = text(b, "action", true); + auto api = std::make_shared(p_.client); + if (action == "list") return Plan{Json::object(), "/v1/conversations", false, [api] { return api->list().raw; }}; + if (action == "start") { + mi::ConversationStartParams sp; + sp.inputs = mi::ConversationInput::text(text(b, "prompt", true)); + sp.model = text(b, "model"); + sp.agent_id = text(b, "agent_id"); + if (sp.model.empty() && sp.agent_id.empty()) sp.model = mi::ChatParams::kDefaultModel; + if (const Json* t = b.find("tools"); t && t->is_array()) sp.tools = *t; + return Plan{sp.to_json(false), "POST /v1/conversations", true, [api, sp] { + mi::ConversationResult r = api->start(sp); + Json o = Json::object(); + o.set("conversation_id", Json(r.conversation_id)); + o.set("text", Json(r.text())); + o.set("usage", usage_json(r.usage)); + return o; + }}; + } + std::string id = text(b, "id", true); + if (!llm::Service::valid_id(id)) bad("id has invalid characters"); + Json req = Json::object(); + req.set("id", Json(id)); + if (action == "append") { + mi::ConversationAppendParams ap; + ap.inputs = mi::ConversationInput::text(text(b, "prompt", true)); + return Plan{ap.to_json(false), "POST /v1/conversations/{id}", true, [api, id, ap] { + mi::ConversationResult r = api->append(id, ap); + Json o = Json::object(); + o.set("text", Json(r.text())); + o.set("usage", usage_json(r.usage)); + return o; + }}; + } + if (action == "get") return Plan{req, "/v1/conversations/{id}", false, [api, id] { return api->get(id); }}; + if (action == "history") return Plan{req, "/v1/conversations/{id}/history", false, [api, id] { return api->history(id); }}; + if (action == "delete") + return Plan{req, "DELETE /v1/conversations/{id}", true, [api, id] { + api->remove(id); + Json o = Json::object(); + o.set("deleted", Json(true)); + return o; + }}; + bad("action must be list, start, append, get, history or delete"); + } + Plan libraries(const Json& b) const { + std::string action = text(b, "action", true); + auto api = std::make_shared(p_.client); + if (action == "list") return Plan{Json::object(), "/v1/libraries", false, [api] { return api->list().raw; }}; + if (action == "create") { + mi::LibraryCreateParams cp; + cp.name = text(b, "name", true); + cp.description = text(b, "description"); + cp.chunk_size = number(b, "chunk_size"); + cp.validate(); + Json req = Json::object(); + req.set("name", Json(cp.name)); + return Plan{req, "POST /v1/libraries", true, [api, cp] { return api->create(cp).raw; }}; + } + std::string id = text(b, "id", true); + if (!llm::Service::valid_id(id)) bad("id has invalid characters"); + Json req = Json::object(); + req.set("id", Json(id)); + if (action == "get") return Plan{req, "/v1/libraries/{id}", false, [api, id] { return api->get(id).raw; }}; + if (action == "documents") return Plan{req, "/v1/libraries/{id}/documents", false, [api, id] { return api->documents(id).raw; }}; + if (action == "delete") return Plan{req, "DELETE /v1/libraries/{id}", true, [api, id] { return api->remove(id).raw; }}; + if (action == "upload") { + mi::DocumentUploadParams up; + up.filename = text(b, "filename", true); + up.data = decode64(b, "data_base64"); + up.validate(); + req.set("filename", Json(up.filename)); + req.set("bytes", Json(static_cast(up.data.size()))); + return Plan{req, "POST /v1/libraries/{id}/documents", true, [api, id, up] { return api->upload_document(id, up).raw; }}; + } + bad("action must be list, get, create, documents, upload or delete"); + } + // Any JSON operation of the table: {op, ids[], query{}, body{}}. + Plan operation(const Json& b) const { + mi::Invocation inv; + inv.op = text(b, "op", true); + inv.ids = strings(b, "ids"); + if (const Json* q = b.find("query"); q && q->is_object()) + for (const auto& [k, v] : q->as_object()) { + if (!v.is_string() && !v.is_number() && !v.is_bool()) bad("query values must be scalars"); + inv.query[k] = v.is_string() ? v.as_string() : v.dump(); + } + if (const Json* body = b.find("body"); body && !body->is_null()) inv.body = *body; + mi::Invocation probe = inv; + probe.allow_destructive = true; // the dry run gate below decides, not the table check + const mi::CallSpec& spec = mi::Operations::check(probe); + bool changes = std::string(spec.http_method) != "GET"; + inv.allow_destructive = flag(b, "confirm"); + Json req = Json::object(); + req.set("op", Json(inv.op)); + req.set("method", Json(spec.http_method)); + req.set("path", Json(mi::Calls::render(spec, inv.ids))); + if (!inv.body.is_null()) req.set("body", inv.body); + auto api = std::make_shared(p_.client); + return Plan{req, spec.path, changes, [api, inv] { return api->invoke(inv); }}; + } + + private: + [[noreturn]] static void map(const llm::ApiError& e) { + if (e.http_status() == 503 && e.type() == "NOT_CONFIGURED") + throw fw::HttpError(503, "mistral_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, "mistral_error", std::to_string(e.http_status()) + " " + e.type() + ": " + e.message()); + } + mi::Platform p_; +}; + +} // namespace + void register_ai_mistral_demo(fw::Router& router, Services& services) { - (void)router; - (void)services; + auto routes = std::make_shared(mi::Platform::from_env()); + fw::Environment* env = services.templates.get(); + services.templates->add_template("ai_mistral.html", kPage); + services.add_feature("ai-mistral", "/ai/mistral"); + + router.get("/ai/mistral", "ai-mistral-page", [env](fw::Request req) { + (void)req; + fw::TemplateCtx ctx; + ctx["title"] = std::string("Mistral La Plateforme"); + return fw::Response(200, env->render("ai_mistral.html", ctx), "text/html; charset=utf-8"); + }); + + router.get("/ai/mistral/status", "ai-mistral-status", [routes](fw::Request req) { + (void)req; + Json o = Json::object(); + o.set("configured", Json(routes->platform().configured())); + o.set("credential_env", Json(mi::Platform::kKeyEnv)); + o.set("base_url", Json(mi::Platform::kHost)); + o.set("calls", Json(static_cast(mi::Calls::count()))); + Json areas = Json::array(); + for (const std::string& a : mi::Operations::areas()) areas.push(Json(a)); + o.set("areas", areas); + o.set("default_chat_model", Json(mi::ChatParams::kDefaultModel)); + o.set("default_fim_model", Json(mi::FimParams::kDefaultModel)); + o.set("default_embedding_model", Json(mi::EmbeddingParams::kDefaultModel)); + o.set("default_ocr_model", Json(mi::OcrParams::kDefaultModel)); + o.set("max_upload_bytes", Json(static_cast(mi::MistralCaps::kMaxUploadBytes / (1024 * 1024)))); + return json_out(o); + }); + + router.get("/ai/mistral/calls", "ai-mistral-calls", [](fw::Request req) { + std::string area = req.query_or("area", ""); + Json arr = Json::array(); + for (std::size_t i = 0; i < mi::Calls::count(); ++i) { + const mi::CallSpec& s = mi::Calls::at(i); + if (!area.empty() && area != s.area) continue; + Json row = Json::object(); + row.set("op", Json(s.op)); + row.set("http", Json(s.http_method)); + row.set("path", Json(s.path)); + row.set("area", Json(s.area)); + row.set("source", Json(s.source)); + row.set("deprecated", Json(s.deprecated)); + arr.push(row); + } + Json o = Json::object(); + o.set("calls", arr); + return json_out(o); + }); + + using Member = MistralRoutes::Planner; + auto add = [&](const char* path, const char* name, Member planner) { + router.post(path, name, [routes, planner](fw::Request req) { return routes->run(req, planner); }); + }; + MistralRoutes* r = routes.get(); + auto bind = [r](Plan (MistralRoutes::*m)(const Json&) const) { + return Member([r, m](const Json& b) { return (r->*m)(b); }); + }; + add("/ai/mistral/chat", "ai-mistral-chat", bind(&MistralRoutes::chat)); + add("/ai/mistral/stream", "ai-mistral-stream", bind(&MistralRoutes::stream)); + add("/ai/mistral/agent", "ai-mistral-agent", bind(&MistralRoutes::agent)); + add("/ai/mistral/fim", "ai-mistral-fim", bind(&MistralRoutes::fim)); + add("/ai/mistral/embeddings", "ai-mistral-embeddings", bind(&MistralRoutes::embeddings)); + add("/ai/mistral/moderation", "ai-mistral-moderation", bind(&MistralRoutes::moderation)); + add("/ai/mistral/classify", "ai-mistral-classify", bind(&MistralRoutes::classify)); + add("/ai/mistral/ocr", "ai-mistral-ocr", bind(&MistralRoutes::ocr)); + add("/ai/mistral/transcribe", "ai-mistral-transcribe", bind(&MistralRoutes::transcribe)); + add("/ai/mistral/models", "ai-mistral-models", bind(&MistralRoutes::models)); + add("/ai/mistral/files", "ai-mistral-files", bind(&MistralRoutes::files)); + add("/ai/mistral/batch", "ai-mistral-batch", bind(&MistralRoutes::batch)); + add("/ai/mistral/finetune", "ai-mistral-finetune", bind(&MistralRoutes::finetune)); + add("/ai/mistral/agents", "ai-mistral-agents", bind(&MistralRoutes::agents)); + add("/ai/mistral/conversations", "ai-mistral-conversations", bind(&MistralRoutes::conversations)); + add("/ai/mistral/libraries", "ai-mistral-libraries", bind(&MistralRoutes::libraries)); + add("/ai/mistral/operation", "ai-mistral-operation", bind(&MistralRoutes::operation)); } } // namespace molo::example::app diff --git a/example/assets/ai_mistral.js b/example/assets/ai_mistral.js new file mode 100644 index 0000000..3a76846 --- /dev/null +++ b/example/assets/ai_mistral.js @@ -0,0 +1,63 @@ +// Mistral page: thin client over /ai/mistral/* (all limits server side). +(function () { + "use strict"; + var $ = function (id) { return document.getElementById(id); }; + var out = $("mi-out"), err = $("mi-error"); + var templates = { + "chat": { prompt: "Hello!", max_tokens: 256 }, + "stream": { prompt: "Count to five.", max_tokens: 128 }, + "agent": { agent_id: "ag_...", prompt: "Hello!" }, + "fim": { prompt: "def fib(n):", suffix: " return fib(n-1) + fib(n-2)", max_tokens: 128 }, + "embeddings": { inputs: ["Embed this sentence.", "As well as this one."] }, + "moderation": { input: "I love sunny days." }, + "classify": { model: "ft:classifier:...", input: "Great product" }, + "ocr": { kind: "document_url", reference: "https://arxiv.org/pdf/2201.04234", pages: [0] }, + "transcribe": { file_url: "https://example.com/audio.mp3", language: "en" }, + "models": { action: "list" }, + "files": { action: "list", page_size: 20 }, + "batch": { action: "list" }, + "finetune": { action: "list" }, + "agents": { action: "list" }, + "conversations": { action: "start", prompt: "Hello!" }, + "libraries": { action: "list" }, + "operation": { op: "list_models_v1_models_get", ids: [], query: {} } + }; + var sel = $("mi-route"); + Object.keys(templates).forEach(function (k) { + var o = document.createElement("option"); + o.value = k; o.textContent = k; + sel.appendChild(o); + }); + function fill() { $("mi-body").value = JSON.stringify(templates[sel.value], null, 1); } + sel.onchange = fill; + fill(); + + function send(confirm) { + var body; + try { body = JSON.parse($("mi-body").value || "{}"); } + catch (e) { err.textContent = "Body is not valid JSON"; return; } + if (confirm) body.confirm = true; + err.textContent = ""; + out.textContent = "..."; + fetch("/ai/mistral/" + 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 = ""; }); + } + $("mi-send").onclick = function () { send(false); }; + $("mi-confirm").onclick = function () { send(true); }; + + fetch("/ai/mistral/status").then(function (r) { return r.json(); }).then(function (s) { + $("mi-config").textContent = s.configured + ? "MISTRAL_API_KEY found. Calls are real; paid ones need confirm." + : "No MISTRAL_API_KEY: calls answer 503 until one is configured; dry runs still work."; + }); +})(); diff --git a/src/vendors/mistral/SPEC.md b/src/vendors/mistral/SPEC.md new file mode 100644 index 0000000..c5b4720 --- /dev/null +++ b/src/vendors/mistral/SPEC.md @@ -0,0 +1,214 @@ +# Mistral AI La Plateforme (`molo::mistral`) + +Status: implemented 2026-10-25. Package `mistral` of plan 18 section 2, built +by the method of plan 17. Scope is the whole public La Plateforme REST API at +`https://api.mistral.ai`: chat, FIM, agents completions, agents, +conversations, embeddings, moderation, classifiers, OCR, audio (transcription, +speech, voices), files, fine-tuning, batch, libraries, models, plus a generic +caller that reaches every other operation of the published OpenAPI documents +(workflows, observability, connectors, skills, prompts, RAG, admin, service +accounts, ...). `MistralChatModel : llm::ChatModel` is the neutral seam. + +Credential: `MISTRAL_API_KEY` (Bearer), read through `llm::Config::get` +(environment, then `.env.json.local`, then `.env.json`). One key serves every +surface. There is no second admin credential in the published document. + +## 1. Module map + +| File | Class | Concern | +|---|---|---| +| `calls_table.inc`, `calls.h/.cpp` | `Calls`, `CallSpec` | The ONE table: 347 operations (generated, proven by `tools/check_spec.py`); path rendering with id validation | +| `ops.h` | `Op` | Named operation ids used by typed services (checked against the table) | +| `platform.h/.cpp` | `Platform` | `ApiClient` wiring, host, key env | +| `caps.h` | `MistralCaps` | Static ceilings | +| `rules.h/.cpp` | `Rules`, `Query` | Input rules (throw `ApiError::invalid`), query string builder | +| `types.h/.cpp` | `J`, `TokenUsage`, `ListPage`, `MistralService` | Tolerant readers, shared base | +| `chat_params.h/.cpp` | `ChatParams`, `Messages`, `ToolDef` | Request side of chat and agents completions | +| `chat_result.h/.cpp` | `ChatResult`, `ChatStreamAccumulator` | Reply parsing, SSE folding (shared by chat, FIM, agents completions) | +| `chat.h/.cpp` | `Chat`, `Fim`, `FimParams` | `/v1/chat/completions`, `/v1/agents/completions`, `/v1/fim/completions`, streaming | +| `chat_model.h/.cpp` | `MistralChatModel` | `llm::ChatModel` over chat completions | +| `agents.h/.cpp` | `Agents` | Agents CRUD, versions, aliases | +| `conversations.h/.cpp` | `Conversations` | Conversations incl. the three SSE stream variants | +| `embeddings.h/.cpp` | `Embeddings` | `/v1/embeddings` | +| `moderation.h/.cpp` | `Moderation` | text and chat moderation | +| `classifiers.h/.cpp` | `Classifiers` | text and chat classification | +| `ocr.h/.cpp` | `Ocr` | `/v1/ocr` | +| `audio.h/.cpp` | `Audio` | transcription (multipart), speech, voices | +| `files.h/.cpp` | `Files` | upload, list (+ walk), get, download, signed URL, delete | +| `finetune.h/.cpp` | `FineTuning` | jobs and fine-tuned models | +| `batch.h/.cpp` | `Batch` | batch jobs | +| `libraries.h/.cpp` | `Libraries` | libraries, documents, sharing | +| `models.h/.cpp` | `Models` | list, get, delete (own fine-tuned) | +| `operations.h/.cpp` | `Operations` | any JSON operation of the table by id, checked against its row | +| `tools/` | | `check_spec.py`, `gen_calls.py`, `specdoc.py`, `minyaml.py` | + +Demo: `example/app/ai/MistralDemo.cpp`, routes `/ai/mistral`, script +`example/assets/ai_mistral.js`. + +## 2. Static caps + +See `caps.h`. From the OpenAPI document: temperature 0..1.5, top_p (0,1], +presence/frequency penalty -2..2, repetition penalty (0,2], batch inline +requests 10000, batch timeout 1..168 h, library chunk size 256..32768, +transcription `file_url` 2083 chars, file size 512 MB (Files API description), +signed URL expiry 1..168 h. Our own hard caps: 4096 messages, 128 tools, 16 +stop sequences, `n` <= 16, 200000 stream events, 512 embedding inputs of +1 MiB, 256 moderation / classifier inputs, 1000 OCR pages, 128 MiB audio, +page size <= 100, list walks <= 20 pages and <= 5000 items (truncation is +reported, never silent), 64 MiB downloads. Floor: nothing here deletes or +retries on its own. + +## 3. Contracts + +- Every call is a row of `calls_table.inc`; typed services name rows through + `Op`. Paths come only from `Calls::render`, which validates every id with + `Service::valid_id` before substitution. +- Input is validated before any network traffic; failures are + `ApiError::invalid` (HTTP 400 `INVALID_ARGUMENT`, vendor "mistral"). +- Deletes, cancels, archives, unshares are separate explicit methods without + defaults: `Files::remove`, `Models::remove`, `Batch::cancel/remove`, + `FineTuning::cancel_job/archive_model/unarchive_model`, + `Agents::remove/remove_alias`, `Conversations::remove`, + `Libraries::remove/remove_document/unshare`, `Audio::remove_voice`. + `Operations` refuses destructive rows (DELETE and rows named cancel, + archive, revoke, purge, reset, reprocess) unless `allow_destructive` is set. +- Paid calls are methods of their own: completions, embeddings, OCR, + transcription, `Batch::create`, `FineTuning::create_job/start_job`, + conversations. The demo makes them dry runs without `"confirm":true`. +- `ChatParams` with `agent_id` targets `/v1/agents/completions` and rejects + `temperature`, `top_p`, `safe_prompt` (not in the agents request schema) + instead of dropping them. Tests prove every key a typed request emits is a + body field of its table row. +- Streams: a chat/FIM stream without a `finish_reason` throws 502 + `STREAM_TRUNCATED`; a conversation stream without + `conversation.response.done` too; `conversation.response.error` throws 502 + `STREAM_ERROR`. A sink returning false stops quietly. Both bare chunks and the + spec's `{"data": chunk}` wrapper are accepted. +- Tool call `arguments` may be a string or an object on the wire; both are held + as JSON text. Invalid arguments reach `MistralChatModel` as + `{"_invalid_json": ""}` rather than being dropped. +- `MistralChatModel`: see the mapping notes in `chat_model.h`. +- Errors: `llm::error_from_response` covers `{"object":"error","message", + "type","code"}`; it does not read the `{"detail": ...}` envelope the gateway + uses for 401 (see Gaps). + +## 4. Sources + +1. `https://docs.mistral.ai/openapi.yaml` (OpenAPI 3.1.0, title "Mistral AI + API", version 1.0.0, 1,333,639 bytes, 319 operations, 232 path keys): primary. +2. The `platform-docs-public` repository's `openapi.yaml` (OpenAPI 3.1, 173 + operations, older generation): the only source that still lists + `/v1/fine_tuning/jobs*` (tagged `deprecated.fine-tuning`) and 23 further + operations (connector credential routes, observability campaigns and + chat-completion events, `/v1/workflows/workers/whoami`). +3. The official Python SDK README (`mistralai/client-python`, `main`): lists + the same resource groups (agents, batch jobs, beta agents / conversations / + libraries / documents / accesses, audio speech / transcriptions / voices, + classifiers, embeddings, files, fim, models, ocr, ...) as method names. + Cross-check of the shape of the surface only: SDK source files moved and the + SDK no longer lists fine-tuning jobs. +4. Unauthenticated probes of `api.mistral.ai` (section 6). + +Beta: libraries, agents, conversations and the other `beta.*` groups are beta +in the document and may change. + +## 5. Verified against spec + +`tools/check_spec.py` (python3 stdlib only; a bundled YAML reader, self-tested +against PyYAML when installed). Run 2026-10-25 against the live documents: + +``` +docs sha256 0a8661df8379c4ba operations 319 yaml reader vs PyYAML: equal +pd sha256 60f5e92dcc4cae89 operations 173 yaml reader vs PyYAML: equal +table rows 347, ops.h constants 80, excluded 0 +OK: every table row matches the documents and every document operation is in the table +``` + +It fails when a table row is absent from its document, when operation id, +request kind, response kind, response schema name, `deprecated` flag, body +fields or query parameters differ, when a document operation is not in the +table (exclusion list `EXCLUDED` is empty), or when an `Op` constant is not a +table row. `tools/gen_calls.py` regenerates the table. Body fields are the +top-level properties of the request schema with `allOf` / `oneOf` / `anyOf` +unioned; nested field shapes are checked only by tests of our own request +builders. + +## 6. Verified live + +Unauthenticated probes, 2026-10-25, from this machine: DNS and TLS to +`api.mistral.ai` work. `GET /v1/models`, `/v1/files`, `/v1/fine_tuning/jobs`, +`/v1/batch/jobs`, `/v1/libraries`, `/v1/agents`, `/v1/conversations` and +`POST /v1/chat/completions`, `/v1/fim/completions`, `/v1/agents/completions`, +`/v1/embeddings`, `/v1/moderations`, `/v1/classifications`, `/v1/ocr`, +`/v1/audio/transcriptions` all answered `401` with `{"detail":"Invalid API +Key"}`. A path that does not exist answered `404` with `{"message":"no Route +matched with those values","request_id":...}`. So the documented paths exist +(including the deprecated fine-tuning jobs path). Opt-in live tests +(`MOLO_LIVE_NET=1`, plus `MISTRAL_API_KEY` for the keyed one) repeat the 401 +probe through `ApiClient` and, with a key, run models list, files list (first +page) and ONE tiny completion; they print a loud SKIP otherwise and never +create or delete anything. + +## 7. Unverified + +No key exists here: no success body from Mistral was ever seen. Everything +below was read from the OpenAPI documents, not observed. + +- All success reply shapes. Tests use examples from the document where it has + them (FIM, files, list files, embeddings, moderations, OCR, transcriptions, + models; labelled FIXTURE) and bodies built from the schema with invented + values otherwise (chat reply with tool calls and thinking chunks, streams, + agents, conversations, libraries, documents, batch, fine-tuning; labelled + SHAPE). +- The chat SSE wire format (bare `CompletionChunk` after `data:`, `[DONE]` + sentinel) and the conversation SSE event names / payload keys: the document + describes `CompletionEvent {data}` and `ConversationEvents {event, data}`; + the exact framing is inferred. +- Multipart field names and array encoding: `context_bias` and + `timestamp_granularities` are sent as repeated parts; `expiry` as an integer + count of hours. +- Whether `usage` is present on stream chunks other than the last one. +- Model name defaults (`mistral-small-latest`, `codestral-latest`, + `mistral-embed`, `mistral-ocr-latest`, `voxtral-mini-latest`, + `mistral-moderation-latest`): taken from the document's examples and the + FIM default `codestral-2404`; the live `/v1/models` list is the truth. +- Fine-tuning jobs (`deprecated.fine-tuning`): only in the older document; the + path answers 401, a body was never seen. Hyperparameter keys are passed + through as the caller gives them. +- Speech replies (`SpeechResponse` has no named schema in the document), voice + responses, signed URL replies (a bare JSON string in the document). +- The cap values that are ours (section 2), notably the 128 MiB audio and the + 1000 OCR pages. +- `Operations` rows from the older document that the current document lacks + (connector credentials, observability campaigns / events): paths may be gone. + +## 8. Gaps + +- `src/llm` error parsing does not read `{"detail": "..."}`: a 401 shows as + type `UNKNOWN`, message `HTTP 401`. The status is right; Mistral's own text is + lost. Needs `error_from_response` to read `detail` (foundation, not touched). +- `ApiClient::stream` streams JSON-body POSTs only: the multipart + `POST /v1/audio/transcriptions#stream` (SSE) is in the table but has no typed + method, and `Operations` refuses it. +- Binary / multipart rows other than files, documents and transcription + (voice sample is typed; the rest of the multipart rows, e.g. dataset imports) + are not reachable through `Operations`. +- No realtime transcription WebSocket client (the table lists + `/v1/client/sessions`; the realtime socket protocol is not in the OpenAPI + document). +- Typed classes cover chat, FIM, agents completions, agents, conversations, + embeddings, moderation, classifiers, OCR, audio, files, fine-tuning, batch, + libraries, models. The other ~230 operations (workflows, observability, + connectors, skills, prompts, RAG indexes, admin, service accounts, user + groups, billing, analytics) are reachable only through `Operations` with a + `Json` body and untyped replies. +- Nothing retries, backs off or reads rate-limit headers; the foundation does + not surface response headers on streams. +- `tests/src/llm` unsets `MOLO_LIVE_NET` during the run; the Mistral live test + reads it once at start-up to survive that. + +## 9. Required credentials (for `src/llm/SPEC.md` section 5) + +| Vendor | Env key | Style | Scope | +|---|---|---|---| +| Mistral | `MISTRAL_API_KEY` | `Authorization: Bearer` | all La Plateforme APIs | diff --git a/src/vendors/mistral/agents.cpp b/src/vendors/mistral/agents.cpp new file mode 100644 index 0000000..2e2d5cb --- /dev/null +++ b/src/vendors/mistral/agents.cpp @@ -0,0 +1,152 @@ +#include "vendors/mistral/agents.h" + +#include "vendors/mistral/caps.h" +#include "vendors/mistral/ops.h" +#include "vendors/mistral/rules.h" + +namespace molo::mistral { + +void AgentParams::validate(bool creating) const { + if (creating) { + Rules::model(model); + Rules::require(!name.empty(), "name is required"); + } else if (!model.empty()) { + Rules::model(model); + } + Rules::require(name.size() <= 256, "name over 256 characters"); + Rules::require(instructions.size() <= (1u << 20), "instructions over 1 MiB"); + Rules::require(description.size() <= 4096 && version_message.size() <= 1024, "description or version_message too long"); + if (!tools.is_null()) { + Rules::require(tools.is_array() && tools.as_array().size() <= MistralCaps::kMaxTools, "tools must be an array of at most 128"); + for (const Json& t : tools.as_array()) Rules::require(t.is_object() && t.find("type"), "each tool needs a type"); + } + if (!completion_args.is_null()) Rules::json_object(completion_args, "completion_args"); + if (!guardrails.is_null()) Rules::require(guardrails.is_array(), "guardrails must be an array"); + if (!metadata.is_null()) Rules::json_object(metadata, "metadata"); + Rules::require(handoffs.size() <= 64, "at most 64 handoffs"); + for (const std::string& h : handoffs) Rules::require(llm::Service::valid_id(h), "bad handoff agent id"); +} + +Json AgentParams::to_json(bool creating) const { + validate(creating); + Json o = Json::object(); + if (!model.empty()) o.set("model", Json(model)); + if (!name.empty()) o.set("name", Json(name)); + if (!instructions.empty()) o.set("instructions", Json(instructions)); + if (!description.empty()) o.set("description", Json(description)); + if (!tools.is_null()) o.set("tools", tools); + if (!completion_args.is_null()) o.set("completion_args", completion_args); + if (!guardrails.is_null()) o.set("guardrails", guardrails); + if (!metadata.is_null()) o.set("metadata", metadata); + if (!handoffs.empty()) { + Json a = Json::array(); + for (const std::string& h : handoffs) a.push(Json(h)); + o.set("handoffs", a); + } + if (!version_message.empty()) o.set("version_message", Json(version_message)); + return o; +} + +AgentInfo AgentInfo::parse(const Json& doc) { + AgentInfo a; + a.raw = doc; + a.id = J::str(doc, "id"); + a.name = J::str(doc, "name"); + a.model = J::str(doc, "model"); + a.description = J::str(doc, "description"); + a.created_at = J::str(doc, "created_at"); + a.updated_at = J::str(doc, "updated_at"); + a.source = J::str(doc, "source"); + a.version = J::num(doc, "version"); + if (const Json* vs = J::arr(doc, "versions")) + for (const Json& v : vs->as_array()) + if (v.is_number()) a.versions.push_back(v.as_int()); + return a; +} + +void AgentListParams::validate() const { + Rules::between(page_size, 1, MistralCaps::kPageSizeMax, "page_size"); + if (!page_token.empty()) Rules::word(page_token, "page_token"); + Rules::require(name.size() <= 256 && search.size() <= 256, "name or search too long"); + if (!id.empty()) Rules::require(llm::Service::valid_id(id), "bad id filter"); +} + +AgentInfo Agents::create(const AgentParams& params) { + Json body = params.to_json(true); + return AgentInfo::parse(call(Op::kAgentCreate, {}, &body)); +} + +ListPage Agents::list(const AgentListParams& p) { + p.validate(); + Query q; + q.add("page_size", p.page_size).add_if("page_token", p.page_token).add_if("name", p.name); + q.add_if("search", p.search).add_if("id", p.id); + if (p.deployment_chat) q.add_flag("deployment_chat", *p.deployment_chat); + return ListPage::parse(call(Op::kAgentListPages, {}, nullptr, q.str())); +} + +AgentInfo Agents::get(const std::string& id, int version) { + check_id("agent", id); + Query q; + if (version >= 0) q.add("agent_version", version); + return AgentInfo::parse(call(Op::kAgentGet, {id}, nullptr, q.str())); +} + +AgentInfo Agents::update(const std::string& id, const AgentParams& params) { + check_id("agent", id); + Json body = params.to_json(false); + Rules::require(!body.as_object().empty(), "nothing to update"); + return AgentInfo::parse(call(Op::kAgentUpdate, {id}, &body)); +} + +AgentInfo Agents::set_version(const std::string& id, int version) { + check_id("agent", id); + Rules::require(version >= 0, "version must not be negative"); + Query q; + q.add("version", version); + return AgentInfo::parse(call(Op::kAgentSetVersion, {id}, nullptr, q.str())); +} + +ListPage Agents::versions(const std::string& id, int page, int page_size) { + check_id("agent", id); + Rules::require(page >= 0, "page must not be negative"); + Rules::between(page_size, 1, MistralCaps::kPageSizeMax, "page_size"); + Query q; + q.add("page", page).add("page_size", page_size); + return ListPage::parse(call(Op::kAgentVersions, {id}, nullptr, q.str())); +} + +AgentInfo Agents::get_version(const std::string& id, int version) { + check_id("agent", id); + Rules::require(version >= 0, "version must not be negative"); + return AgentInfo::parse(call(Op::kAgentVersionGet, {id, std::to_string(version)})); +} + +Json Agents::set_alias(const std::string& id, const std::string& alias, int version) { + check_id("agent", id); + Rules::word(alias, "alias"); + Rules::require(version >= 0, "version must not be negative"); + Query q; + q.add("alias", alias).add("version", version); + return call(Op::kAgentAliasSet, {id}, nullptr, q.str()); +} + +Json Agents::aliases(const std::string& id) { + check_id("agent", id); + return call(Op::kAgentAliasList, {id}); +} + +void Agents::remove_alias(const std::string& id, const std::string& alias) { + check_id("agent", id); + Rules::word(alias, "alias"); + Query q; + q.add("alias", alias); + call(Op::kAgentAliasDelete, {id}, nullptr, q.str()); +} + +void Agents::remove(const std::string& id) { + check_id("agent", id); + call(Op::kAgentDelete, {id}); +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/agents.h b/src/vendors/mistral/agents.h new file mode 100644 index 0000000..16a4706 --- /dev/null +++ b/src/vendors/mistral/agents.h @@ -0,0 +1,62 @@ +#pragma once + +// Agents API: /v1/agents (create, list pages, get, update, versions, aliases). +// Running an agent is Chat::create with ChatParams::agent_id (agents +// completions) or Conversations (stateful). The deprecated unpaged +// GET /v1/agents is not offered; list_pages replaces it. + +#include +#include +#include + +#include "vendors/mistral/types.h" + +namespace molo::mistral { + +struct AgentParams { + std::string model; // required on create + std::string name; // required on create + std::string instructions; + std::string description; + Json tools; // null or array (function, web_search, code_interpreter, image_generation, document_library, connector) + Json completion_args; // null or object (CompletionArgs) + Json guardrails; // null or array + Json metadata; // null or object + std::vector handoffs; // agent ids + std::string version_message; + void validate(bool creating) const; + Json to_json(bool creating) const; +}; + +struct AgentInfo { + std::string id, name, model, description, created_at, updated_at, source; + long version = 0; + std::vector versions; + Json raw; + static AgentInfo parse(const Json& doc); +}; + +struct AgentListParams { + int page_size = 20; // 1..100 + std::string page_token, name, search, id; + std::optional deployment_chat; + void validate() const; +}; + +class Agents : public MistralService { + public: + explicit Agents(std::shared_ptr client) : MistralService(std::move(client)) {} + AgentInfo create(const AgentParams& params); + ListPage list(const AgentListParams& params = {}); + AgentInfo get(const std::string& agent_id, int version = -1); + AgentInfo update(const std::string& agent_id, const AgentParams& params); // PATCH: only set fields are sent + AgentInfo set_version(const std::string& agent_id, int version); // makes `version` the current one + ListPage versions(const std::string& agent_id, int page = 0, int page_size = 20); + AgentInfo get_version(const std::string& agent_id, int version); + Json set_alias(const std::string& agent_id, const std::string& alias, int version); + Json aliases(const std::string& agent_id); + void remove_alias(const std::string& agent_id, const std::string& alias); // explicit + void remove(const std::string& agent_id); // explicit, irreversible +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/audio.cpp b/src/vendors/mistral/audio.cpp new file mode 100644 index 0000000..dc1796a --- /dev/null +++ b/src/vendors/mistral/audio.cpp @@ -0,0 +1,168 @@ +#include "vendors/mistral/audio.h" + +#include "vendors/mistral/calls.h" +#include "vendors/mistral/caps.h" +#include "vendors/mistral/ops.h" +#include "vendors/mistral/rules.h" + +namespace molo::mistral { + +void TranscriptionParams::validate() const { + Rules::model(model); + int sources = (!file_data.empty()) + (!file_url.empty()) + (!file_id.empty()); + Rules::require(sources == 1, "give exactly one of file bytes, file_url or file_id"); + if (!file_data.empty()) { + Rules::filename(file_name); + Rules::require(file_data.size() <= MistralCaps::kMaxAudioBytes, "audio over 128 MiB"); + } + if (!file_url.empty()) Rules::http_url(file_url, MistralCaps::kTranscriptionFileUrlChars, "file_url"); + if (!file_id.empty()) Rules::require(llm::Service::valid_id(file_id), "bad file id"); + if (!language.empty()) Rules::word(language, "language"); + if (temperature) Rules::range(*temperature, 0.0, 2.0, "temperature"); + Rules::require(context_bias.size() <= 100, "at most 100 context_bias entries"); + for (const std::string& w : context_bias) Rules::require(!w.empty() && w.size() <= 128, "context_bias entry must be 1..128 chars"); + Rules::require(timestamp_granularities.size() <= 2, "at most 2 timestamp granularities"); + for (const std::string& g : timestamp_granularities) Rules::one_of(g, {"segment", "word"}, "timestamp granularity"); +} + +TranscriptionResult TranscriptionResult::parse(const Json& doc) { + TranscriptionResult r; + r.raw = doc; + r.model = J::str(doc, "model"); + r.text = J::str(doc, "text"); + r.language = J::str(doc, "language"); + r.usage = TokenUsage::parse(J::obj(doc, "usage")); + if (const Json* s = J::arr(doc, "segments")) { + for (const Json& x : s->as_array()) { + TranscriptionSegment seg; + seg.text = J::str(x, "text"); + seg.speaker_id = J::str(x, "speaker_id"); + seg.start = J::real(x, "start"); + seg.end = J::real(x, "end"); + r.segments.push_back(std::move(seg)); + } + } + return r; +} + +void SpeechParams::validate() const { + if (!model.empty()) Rules::model(model); + Rules::require(!input.empty() && input.size() <= 32768, "input must be 1..32768 characters"); + Rules::require(voice_id.empty() || ref_audio.empty(), "give voice_id or ref_audio, not both"); + if (!voice_id.empty()) Rules::require(llm::Service::valid_id(voice_id), "bad voice id"); + Rules::require(ref_audio.size() <= (16u << 20), "ref_audio over 16 MiB"); + if (!response_format.empty()) Rules::one_of(response_format, {"pcm", "wav", "mp3", "flac", "opus"}, "response_format"); +} + +Json SpeechParams::to_json() const { + validate(); + Json o = Json::object(); + o.set("input", Json(input)); + if (!model.empty()) o.set("model", Json(model)); + if (!voice_id.empty()) o.set("voice_id", Json(voice_id)); + if (!ref_audio.empty()) o.set("ref_audio", Json(ref_audio)); + if (!response_format.empty()) o.set("response_format", Json(response_format)); + return o; +} + +void VoiceCreateParams::validate() const { + Rules::require(!name.empty() && name.size() <= 128, "name must be 1..128 characters"); + if (!slug.empty()) Rules::word(slug, "slug"); + Rules::require(languages.size() <= 16 && tags.size() <= 32, "too many languages or tags"); + if (age) Rules::between(*age, 0, 150, "age"); + Rules::require(sample_audio.size() <= (16u << 20), "sample_audio over 16 MiB"); + Rules::require(sample_audio.empty() || !sample_filename.empty(), "sample_audio needs sample_filename"); + if (!sample_filename.empty()) Rules::filename(sample_filename); +} + +Json VoiceCreateParams::to_json() const { + validate(); + Json o = Json::object(); + o.set("name", Json(name)); + if (!slug.empty()) o.set("slug", Json(slug)); + if (!gender.empty()) o.set("gender", Json(gender)); + if (!description.empty()) o.set("description", Json(description)); + if (age) o.set("age", Json(*age)); + if (!languages.empty()) { + Json a = Json::array(); + for (const std::string& l : languages) a.push(Json(l)); + o.set("languages", a); + } + if (!tags.empty()) { + Json a = Json::array(); + for (const std::string& t : tags) a.push(Json(t)); + o.set("tags", a); + } + if (!sample_audio.empty()) { + o.set("sample_audio", Json(sample_audio)); + o.set("sample_filename", Json(sample_filename)); + } + return o; +} + +void VoiceListParams::validate() const { + Rules::between(page_size, 1, MistralCaps::kPageSizeMax, "page_size"); + if (!page_token.empty()) Rules::word(page_token, "page_token"); + Rules::require(query.size() <= 256, "query over 256 characters"); +} + +TranscriptionResult Audio::transcribe(const TranscriptionParams& p) { + p.validate(); + clients::body::MultipartBody form; + form.add_text("model", p.model); + if (!p.file_data.empty()) form.add_file("file", p.file_name, "application/octet-stream", p.file_data); + if (!p.file_url.empty()) form.add_text("file_url", p.file_url); + if (!p.file_id.empty()) form.add_text("file_id", p.file_id); + if (!p.language.empty()) form.add_text("language", p.language); + if (p.temperature) form.add_text("temperature", std::to_string(*p.temperature)); + if (p.diarize) form.add_text("diarize", "true"); + for (const std::string& w : p.context_bias) form.add_text("context_bias", w); + for (const std::string& g : p.timestamp_granularities) form.add_text("timestamp_granularities", g); + return TranscriptionResult::parse(call_multipart(Op::kTranscribe, form)); +} + +Json Audio::speech(const SpeechParams& params) { + Json body = params.to_json(); + return call(Op::kSpeech, {}, &body); +} + +Json Audio::voices(const VoiceListParams& p) { + p.validate(); + Query q; + q.add("page_size", p.page_size).add_if("page_token", p.page_token).add_if("type", p.type); + q.add_if("gender", p.gender).add_if("language", p.language).add_if("query", p.query); + return call(Op::kVoicesList, {}, nullptr, q.str()); +} + +Json Audio::voice(const std::string& voice_id) { + check_id("voice", voice_id); + return call(Op::kVoiceGet, {voice_id}); +} + +Json Audio::create_voice(const VoiceCreateParams& params) { + Json body = params.to_json(); + return call(Op::kVoiceCreate, {}, &body); +} + +Json Audio::update_voice(const std::string& voice_id, const Json& fields) { + check_id("voice", voice_id); + Rules::json_object(fields, "voice fields"); + for (const auto& [k, v] : fields.as_object()) + Rules::require(Calls::has_body_field(Calls::find(Op::kVoiceUpdate), k), "unknown voice field: " + k.substr(0, 40)); + return call(Op::kVoiceUpdate, {voice_id}, &fields); +} + +Json Audio::remove_voice(const std::string& voice_id) { + check_id("voice", voice_id); + return call(Op::kVoiceDelete, {voice_id}); +} + +std::string Audio::voice_sample(const std::string& voice_id) { + check_id("voice", voice_id); + llm::RawResponse r = client_->request_raw(clients::Method::kGet, Calls::render(Op::kVoiceSample, {voice_id})); + if (r.body.size() > 64u * 1024u * 1024u) + throw llm::ApiError("mistral", 502, "RESPONSE_TOO_LARGE", "voice sample over 64 MiB"); + return r.body; +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/audio.h b/src/vendors/mistral/audio.h new file mode 100644 index 0000000..bc1ebd6 --- /dev/null +++ b/src/vendors/mistral/audio.h @@ -0,0 +1,86 @@ +#pragma once + +// Audio: transcription (POST /v1/audio/transcriptions, multipart), speech +// (POST /v1/audio/speech, JSON) and voices (/v2/audio/voices and /v1/...). +// Streamed transcription (SSE over a multipart request) is not offered: the +// shared ApiClient streams JSON bodies only (SPEC.md ## Gaps). + +#include +#include +#include + +#include "vendors/mistral/types.h" + +namespace molo::mistral { + +struct TranscriptionParams { + static constexpr const char* kDefaultModel = "voxtral-mini-latest"; + std::string model = kDefaultModel; + // Exactly one source: uploaded bytes, a URL, or a Files API id. + std::string file_data, file_name; // bytes <= 128 MiB + std::string file_url; // http(s), <= 2083 chars (spec maxLength) + std::string file_id; + std::string language; // "" or ISO code word + std::optional temperature; + bool diarize = false; + std::vector context_bias; // <= 100 words + std::vector timestamp_granularities; // segment | word + void validate() const; +}; + +struct TranscriptionSegment { + std::string text, speaker_id; + double start = 0, end = 0; +}; + +struct TranscriptionResult { + std::string model, text, language; + std::vector segments; + TokenUsage usage; + Json raw; + static TranscriptionResult parse(const Json& doc); +}; + +struct SpeechParams { + std::string model; // "" lets the server choose + std::string input; // text, 1..32768 chars + std::string voice_id; // either voice_id or ref_audio + std::string ref_audio; // base64 audio + std::string response_format; // "", pcm, wav, mp3, flac, opus + void validate() const; + Json to_json() const; +}; + +struct VoiceCreateParams { + std::string name; // 1..128 + std::string slug; + std::vector languages; + std::string gender, description; + std::optional age; + std::vector tags; + std::string sample_audio; // base64 + std::string sample_filename; + void validate() const; + Json to_json() const; +}; + +struct VoiceListParams { + int page_size = 50; // 1..100 + std::string page_token, type, gender, language, query; + void validate() const; +}; + +class Audio : public MistralService { + public: + explicit Audio(std::shared_ptr client) : MistralService(std::move(client)) {} + TranscriptionResult transcribe(const TranscriptionParams& params); + Json speech(const SpeechParams& params); + Json voices(const VoiceListParams& params = {}); + Json voice(const std::string& voice_id); + Json create_voice(const VoiceCreateParams& params); + Json update_voice(const std::string& voice_id, const Json& fields); // name, languages, gender, age, tags, description + Json remove_voice(const std::string& voice_id); // explicit, irreversible + std::string voice_sample(const std::string& voice_id); // audio/wav bytes +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/batch.cpp b/src/vendors/mistral/batch.cpp new file mode 100644 index 0000000..b593df8 --- /dev/null +++ b/src/vendors/mistral/batch.cpp @@ -0,0 +1,122 @@ +#include "vendors/mistral/batch.h" + +#include "vendors/mistral/caps.h" +#include "vendors/mistral/ops.h" +#include "vendors/mistral/rules.h" + +namespace molo::mistral { + +namespace { +constexpr std::initializer_list kStatuses = {"QUEUED", "RUNNING", "SUCCESS", "FAILED", + "TIMEOUT_EXCEEDED", "CANCELLATION_REQUESTED", "CANCELLED"}; +} + +void BatchParams::validate() const { + Rules::one_of(endpoint, {"/v1/chat/completions", "/v1/embeddings", "/v1/fim/completions", "/v1/moderations", + "/v1/chat/moderations", "/v1/ocr", "/v1/classifications", "/v1/chat/classifications", + "/v1/conversations", "/v1/audio/transcriptions"}, "endpoint"); + Rules::require(model.empty() != agent_id.empty(), "give exactly one of model or agent_id"); + if (!model.empty()) Rules::model(model); + if (!agent_id.empty()) Rules::require(llm::Service::valid_id(agent_id), "bad agent id"); + Rules::require(input_files.empty() != requests.empty(), "give exactly one of input_files or requests"); + Rules::require(input_files.size() <= 100, "at most 100 input files"); + for (const std::string& f : input_files) Rules::require(llm::Service::valid_id(f), "bad input file id"); + Rules::require(requests.size() <= static_cast(MistralCaps::kBatchMaxInlineRequests), + "at most 10000 inline requests"); + for (const BatchRequestItem& r : requests) { + Rules::require(r.custom_id.size() <= 256, "custom_id over 256 characters"); + Rules::json_object(r.body, "request body"); + } + Rules::between(timeout_hours, MistralCaps::kBatchTimeoutMinHours, MistralCaps::kBatchTimeoutMaxHours, "timeout_hours"); + if (!metadata.is_null()) Rules::json_object(metadata, "metadata"); +} + +Json BatchParams::to_json() const { + validate(); + Json o = Json::object(); + o.set("endpoint", Json(endpoint)); + if (!model.empty()) o.set("model", Json(model)); + if (!agent_id.empty()) o.set("agent_id", Json(agent_id)); + if (!input_files.empty()) { + Json a = Json::array(); + for (const std::string& f : input_files) a.push(Json(f)); + o.set("input_files", a); + } + if (!requests.empty()) { + Json a = Json::array(); + for (const BatchRequestItem& r : requests) { + Json x = Json::object(); + if (!r.custom_id.empty()) x.set("custom_id", Json(r.custom_id)); + x.set("body", r.body); + a.push(x); + } + o.set("requests", a); + } + if (!metadata.is_null()) o.set("metadata", metadata); + if (timeout_hours != 24) o.set("timeout_hours", Json(timeout_hours)); + return o; +} + +void BatchListParams::validate() const { + Rules::require(page >= 0, "page must not be negative"); + Rules::between(page_size, 1, MistralCaps::kPageSizeMax, "page_size"); + if (!model.empty()) Rules::model(model); + if (!agent_id.empty()) Rules::require(llm::Service::valid_id(agent_id), "bad agent id"); + if (!status.empty()) Rules::one_of(status, kStatuses, "status"); + if (!order_by.empty()) Rules::one_of(order_by, {"created", "-created"}, "order_by"); + Rules::require(created_after.size() <= 40, "bad date"); +} + +bool BatchJob::terminal() const { + return status == "SUCCESS" || status == "FAILED" || status == "TIMEOUT_EXCEEDED" || status == "CANCELLED"; +} + +BatchJob BatchJob::parse(const Json& doc) { + BatchJob j; + j.raw = doc; + j.id = J::str(doc, "id"); + j.status = J::str(doc, "status"); + j.endpoint = J::str(doc, "endpoint"); + j.model = J::str(doc, "model"); + j.output_file = J::str(doc, "output_file"); + j.error_file = J::str(doc, "error_file"); + j.created_at = J::num(doc, "created_at"); + j.total_requests = J::num(doc, "total_requests"); + j.completed_requests = J::num(doc, "completed_requests"); + j.succeeded_requests = J::num(doc, "succeeded_requests"); + j.failed_requests = J::num(doc, "failed_requests"); + return j; +} + +BatchJob Batch::create(const BatchParams& params) { + Json body = params.to_json(); + return BatchJob::parse(call(Op::kBatchCreate, {}, &body)); +} + +ListPage Batch::list(const BatchListParams& p) { + p.validate(); + Query q; + q.add("page", p.page).add("page_size", p.page_size).add_if("model", p.model).add_if("agent_id", p.agent_id); + q.add_if("status", p.status).add_if("order_by", p.order_by).add_if("created_after", p.created_after); + if (p.created_by_me) q.add_flag("created_by_me", *p.created_by_me); + return ListPage::parse(call(Op::kBatchList, {}, nullptr, q.str())); +} + +BatchJob Batch::get(const std::string& job_id, bool inline_outputs) { + check_id("job", job_id); + Query q; + if (inline_outputs) q.add_flag("inline", true); + return BatchJob::parse(call(Op::kBatchGet, {job_id}, nullptr, q.str())); +} + +BatchJob Batch::cancel(const std::string& job_id) { + check_id("job", job_id); + return BatchJob::parse(call(Op::kBatchCancel, {job_id})); +} + +Json Batch::remove(const std::string& job_id) { + check_id("job", job_id); + return call(Op::kBatchDelete, {job_id}); +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/batch.h b/src/vendors/mistral/batch.h new file mode 100644 index 0000000..29368ce --- /dev/null +++ b/src/vendors/mistral/batch.h @@ -0,0 +1,60 @@ +#pragma once + +// Batch jobs: /v1/batch/jobs. A job reads uploaded .jsonl files (purpose +// "batch") or up to 10000 inline requests and runs one endpoint over them at a +// lower price. create is paid; cancel and delete are explicit methods. + +#include +#include +#include + +#include "vendors/mistral/types.h" + +namespace molo::mistral { + +struct BatchRequestItem { + std::string custom_id; // optional, <= 256 chars + Json body = Json::object(); +}; + +struct BatchParams { + // spec ApiEndpoint enum + std::string endpoint = "/v1/chat/completions"; + std::string model; // model or agent_id, one of them + std::string agent_id; + std::vector input_files; // file ids, <= 100 + std::vector requests; // <= 10000 (spec maxItems) + Json metadata; // null or object + int timeout_hours = 24; // 1..168 (spec) + void validate() const; + Json to_json() const; +}; + +struct BatchListParams { + int page = 0; + int page_size = 100; // 1..100 + std::string model, agent_id, status, order_by; + std::string created_after; + std::optional created_by_me; + void validate() const; +}; + +struct BatchJob { + std::string id, status, endpoint, model, output_file, error_file; + long created_at = 0, total_requests = 0, completed_requests = 0, succeeded_requests = 0, failed_requests = 0; + Json raw; + bool terminal() const; // SUCCESS FAILED TIMEOUT_EXCEEDED CANCELLED + static BatchJob parse(const Json& doc); +}; + +class Batch : public MistralService { + public: + explicit Batch(std::shared_ptr client) : MistralService(std::move(client)) {} + BatchJob create(const BatchParams& params); // costs money + ListPage list(const BatchListParams& params = {}); + BatchJob get(const std::string& job_id, bool inline_outputs = false); + BatchJob cancel(const std::string& job_id); // explicit + Json remove(const std::string& job_id); // explicit, irreversible +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/calls.cpp b/src/vendors/mistral/calls.cpp new file mode 100644 index 0000000..e06efdf --- /dev/null +++ b/src/vendors/mistral/calls.cpp @@ -0,0 +1,107 @@ +#include "vendors/mistral/calls.h" + +#include + +#include "llm/api_client.h" +#include "llm/error.h" + +namespace molo::mistral { + +static const CallSpec kCalls[] = { +#include "vendors/mistral/calls_table.inc" +}; + +namespace { +bool listed(const char* csv, const std::string& item) { + std::string s = csv; + std::size_t at = 0; + while (at <= s.size()) { + std::size_t end = s.find(',', at); + if (end == std::string::npos) end = s.size(); + if (s.compare(at, end - at, item) == 0 && end - at == item.size()) return true; + at = end + 1; + } + return false; +} +} // namespace + +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("mistral", "call index out of range"); + return kCalls[index]; +} + +const CallSpec* Calls::try_find(std::string_view op) { + for (const CallSpec& s : kCalls) + if (op == s.op) return &s; + return nullptr; +} + +const CallSpec& Calls::find(std::string_view op) { + const CallSpec* s = try_find(op); + if (!s) throw llm::ApiError::invalid("mistral", "unknown operation: " + std::string(op.substr(0, 80))); + return *s; +} + +clients::Method Calls::method_of(const CallSpec& spec) { + const char* m = spec.http_method; + if (!std::strcmp(m, "GET")) return clients::Method::kGet; + if (!std::strcmp(m, "POST")) return clients::Method::kPost; + if (!std::strcmp(m, "PUT")) return clients::Method::kPut; + if (!std::strcmp(m, "PATCH")) return clients::Method::kPatch; + return clients::Method::kDelete; +} + +bool Calls::is_destructive(const CallSpec& spec) { + if (!std::strcmp(spec.http_method, "DELETE")) return true; + std::string op = spec.op; + for (const char* word : {"cancel", "archive", "revoke", "purge", "reset", "reprocess"}) + if (op.find(word) != std::string::npos) return true; + return false; +} + +std::vector Calls::path_params(const CallSpec& spec) { + std::vector out; + std::string p = spec.path; + for (std::size_t at = p.find('{'); at != std::string::npos; at = p.find('{', at)) { + std::size_t end = p.find('}', at); + if (end == std::string::npos) break; + out.push_back(p.substr(at + 1, end - at - 1)); + at = end; + } + return out; +} + +bool Calls::has_body_field(const CallSpec& spec, const std::string& field) { + return listed(spec.body_fields, field); +} + +bool Calls::has_query(const CallSpec& spec, const std::string& name) { + return listed(spec.query_params, name); +} + +std::string Calls::render(const CallSpec& spec, const std::vector& ids) { + std::string path = spec.path; + std::size_t hash = path.find('#'); + if (hash != std::string::npos) path.resize(hash); + std::size_t used = 0; + for (std::size_t at = path.find('{'); at != std::string::npos; at = path.find('{', at)) { + std::size_t end = path.find('}', at); + if (end == std::string::npos) break; + if (used >= ids.size()) throw llm::ApiError::invalid("mistral", "missing id for " + path.substr(at, end - at + 1)); + if (!llm::Service::valid_id(ids[used])) + throw llm::ApiError::invalid("mistral", "bad id for " + path.substr(at, end - at + 1)); + path.replace(at, end - at + 1, ids[used]); + at += ids[used].size(); + ++used; + } + if (used != ids.size()) throw llm::ApiError::invalid("mistral", "too many ids for " + std::string(spec.op)); + return path; +} + +std::string Calls::render(const std::string& op, const std::vector& ids) { + return render(find(op), ids); +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/calls.h b/src/vendors/mistral/calls.h new file mode 100644 index 0000000..386034c --- /dev/null +++ b/src/vendors/mistral/calls.h @@ -0,0 +1,48 @@ +#pragma once + +// The ONE table of REST calls this module can make: every operation of +// Mistral's two published OpenAPI documents (calls_table.inc, generated by +// tools/gen_calls.py, proven by tools/check_spec.py). Code builds every path +// from this table, never from a literal; typed services name their rows +// through ops.h. + +#include +#include +#include +#include + +#include "clients/request.h" + +namespace molo::mistral { + +struct CallSpec { + const char* op; // spec operationId + const char* http_method; // GET POST PUT PATCH DELETE + const char* path; // spec path key; "#stream" style suffixes are spec-only + const char* area; // first spec tag + const char* source; // "docs" (docs.mistral.ai) or "pd" (platform-docs-public) + const char* request; // none | json | multipart + const char* body_fields; // comma list: top-level request properties + const char* query_params; // comma list + const char* response_kind; // none | json | sse | binary + const char* response; // response schema name(s) + bool deprecated; +}; + +struct Calls { + static std::size_t count(); + static const CallSpec& at(std::size_t index); + // Throws ApiError::invalid for an unknown operation id. + static const CallSpec& find(std::string_view op); + static const CallSpec* try_find(std::string_view op); + static clients::Method method_of(const CallSpec& spec); + static bool is_destructive(const CallSpec& spec); // DELETE, cancel, archive, revoke, purge + static std::vector path_params(const CallSpec& spec); // "{x}" names in order + static bool has_body_field(const CallSpec& spec, const std::string& field); + static bool has_query(const CallSpec& spec, const std::string& name); + // Path with each {param} replaced by the matching id (Service::valid_id). + static std::string render(const std::string& op, const std::vector& ids = {}); + static std::string render(const CallSpec& spec, const std::vector& ids = {}); +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/calls_table.inc b/src/vendors/mistral/calls_table.inc new file mode 100644 index 0000000..07e7d26 --- /dev/null +++ b/src/vendors/mistral/calls_table.inc @@ -0,0 +1,351 @@ +// GENERATED by tools/gen_calls.py from Mistral's OpenAPI documents. Do not edit. +// CALLS-BEGIN (parsed by tools/check_spec.py) +// op, method, spec path key, area, source, request kind, body fields, query params, response kind, response schema, deprecated +{"agents_completion_v1_agents_completions_post", "POST", "/v1/agents/completions", "agents", "docs", "json", "max_tokens,stream,stop,random_seed,metadata,messages,response_format,tools,tool_choice,presence_penalty,frequency_penalty,min_tokens,repetition_penalty,top_k,logprobs,top_logprobs,prompt_logprobs,top_prompt_logprobs,n,prediction,parallel_tool_calls,reasoning_effort,prompt_mode,guardrails,prompt_cache_key,service_tier,agent_id", "", "json", "ChatCompletionResponse", false}, +{"speech_v1_audio_speech_post", "POST", "/v1/audio/speech", "audio.speech", "docs", "json", "model,metadata,stream,prompt_cache_key,voice_id,ref_audio,input,phoneme_hints,response_format", "", "json", "object", false}, +{"audio_api_v1_transcriptions_post", "POST", "/v1/audio/transcriptions", "audio.transcriptions", "docs", "multipart", "model,file,file_url,file_id,language,temperature,stream,diarize,context_bias,timestamp_granularities", "", "json", "TranscriptionResponse", false}, +{"audio_api_v1_transcriptions_post_stream", "POST", "/v1/audio/transcriptions#stream", "audio.transcriptions", "docs", "multipart", "model,file,file_url,file_id,language,temperature,stream,diarize,context_bias,timestamp_granularities", "", "sse", "TranscriptionStreamEvents", false}, +{"list_voices_v1_audio_voices_get", "GET", "/v1/audio/voices", "audio.voices", "docs", "none", "", "limit,offset,type", "json", "VoiceListResponse", true}, +{"create_voice_v1_audio_voices_post", "POST", "/v1/audio/voices", "audio.voices", "docs", "json", "name,slug,languages,gender,age,tags,color,description,retention_notice,sample_audio,sample_filename", "", "json", "VoiceResponse", false}, +{"delete_voice_v1_audio_voices__voice_id__delete", "DELETE", "/v1/audio/voices/{voice_id}", "audio.voices", "docs", "none", "", "", "json", "VoiceResponse", false}, +{"get_voice_v1_audio_voices__voice_id__get", "GET", "/v1/audio/voices/{voice_id}", "audio.voices", "docs", "none", "", "", "json", "VoiceResponse", false}, +{"update_voice_v1_audio_voices__voice_id__patch", "PATCH", "/v1/audio/voices/{voice_id}", "audio.voices", "docs", "json", "name,languages,gender,age,tags,description,retention_notice", "", "json", "VoiceResponse", false}, +{"get_voice_sample_audio_v1_audio_voices__voice_id__sample_get", "GET", "/v1/audio/voices/{voice_id}/sample", "audio.voices", "docs", "none", "", "", "binary", "string", false}, +{"audio_v2_voices_list", "GET", "/v2/audio/voices", "audio.voices", "docs", "none", "", "page_size,page_token,type,gender,language,query", "json", "VoiceListPage", false}, +{"jobs_api_routes_batch_get_batch_jobs", "GET", "/v1/batch/jobs", "batch", "docs", "none", "", "page,page_size,model,agent_id,metadata,created_after,created_by_me,status,order_by", "json", "ListBatchJobsResponse", false}, +{"jobs_api_routes_batch_create_batch_job", "POST", "/v1/batch/jobs", "batch", "docs", "json", "input_files,requests,endpoint,model,agent_id,metadata,timeout_hours", "", "json", "BatchJob", false}, +{"jobs_api_routes_batch_delete_batch_job", "DELETE", "/v1/batch/jobs/{job_id}", "batch", "docs", "none", "", "", "json", "DeleteBatchJobResponse", false}, +{"jobs_api_routes_batch_get_batch_job", "GET", "/v1/batch/jobs/{job_id}", "batch", "docs", "none", "", "inline", "json", "BatchJob", false}, +{"jobs_api_routes_batch_cancel_batch_job", "POST", "/v1/batch/jobs/{job_id}/cancel", "batch", "docs", "none", "", "", "json", "BatchJob", false}, +{"users_api_admin_api_keys_get_api_keys", "GET", "/v1/admin/api-keys", "beta.admin.api-keys", "docs", "none", "", "limit,offset,name", "json", "APIKeysExtendedOUT", false}, +{"users_api_admin_api_keys_create_api_key", "POST", "/v1/admin/api-keys", "beta.admin.api-keys", "docs", "json", "name,expiration,workspace_uuid,user_id", "", "json", "APIKeyOUT", false}, +{"users_api_admin_api_keys_delete_api_key", "DELETE", "/v1/admin/api-keys/{key_id}", "beta.admin.api-keys", "docs", "none", "", "", "json", "DeleteAPIKeyOUT", false}, +{"users_api_admin_audit_logs_get_audit_logs", "GET", "/v1/admin/audit-logs", "beta.admin.audit-logs", "docs", "none", "", "actor_type,event_type,target_type,actor_user_uuid,sort,after,before,limit", "json", "[AuditLogOut]", false}, +{"users_api_admin_rate_limits_get_rate_limits", "GET", "/v1/admin/rate-limit", "beta.admin.billing", "docs", "none", "", "", "json", "RateLimitsOUT", false}, +{"users_api_admin_spend_limits_get_spend_limits", "GET", "/v1/admin/spend-limit", "beta.admin.billing", "docs", "none", "", "", "json", "LimitsOUT", false}, +{"users_api_admin_spend_limits_update_spend_limits", "POST", "/v1/admin/spend-limit", "beta.admin.billing", "docs", "json", "amount,no_monthly_limit", "", "json", "LimitsOUT", false}, +{"users_api_admin_usage_get_usage", "GET", "/v1/admin/usage", "beta.admin.billing", "docs", "none", "", "month,year,workspace_id,api_zone", "json", "UsageOUTJSON", false}, +{"users_api_admin_usage_query_usage", "POST", "/v1/admin/usage/query", "beta.admin.billing", "docs", "json", "usage_type,billing_metric,billing_display_name,billing_group,workspace_ids,organization_id,user_id,service_account_id,api_key_id,api_zone,service_tier,start,end,filters,group_by,time_bucket,untracked,limit,offset,include_aggregates,include_nb_api_calls", "", "json", "UsageQueryOUT", false}, +{"users_api_admin_scim_sync_trigger_scim_sync", "POST", "/v1/admin/scim/sync", "beta.admin.scim", "docs", "json", "dry_run,sync_config", "", "json", "AdminScimSyncTriggerOUT", false}, +{"users_api_admin_scim_sync_get_scim_sync_run", "GET", "/v1/admin/scim/sync/{run_id}", "beta.admin.scim", "docs", "none", "", "", "json", "AdminScimSyncRunOUT", false}, +{"users_api_admin_user_groups_get_user_groups", "GET", "/v1/admin/user-groups", "beta.admin.user-groups", "docs", "none", "", "page,page_size,search", "json", "AdminUserGroupsOut", false}, +{"users_api_admin_user_groups_create_user_group", "POST", "/v1/admin/user-groups", "beta.admin.user-groups", "docs", "json", "name,description,parent_group_ids", "", "json", "AdminUserGroupOut", false}, +{"users_api_admin_user_groups_provision_group_to_workspace", "POST", "/v1/admin/user-groups/provision-workspace", "beta.admin.user-groups", "docs", "json", "user_group_uuid,workspace_uuid,workspace_role,workspace_role_name", "", "none", "", true}, +{"users_api_admin_user_groups_delete_user_group", "DELETE", "/v1/admin/user-groups/{group_uuid}", "beta.admin.user-groups", "docs", "none", "", "", "none", "", false}, +{"users_api_admin_user_groups_get_user_group", "GET", "/v1/admin/user-groups/{group_uuid}", "beta.admin.user-groups", "docs", "none", "", "", "json", "AdminUserGroupOut", false}, +{"users_api_admin_user_groups_update_user_group", "PATCH", "/v1/admin/user-groups/{group_uuid}", "beta.admin.user-groups", "docs", "json", "name,description,parent_group_ids", "", "json", "AdminUserGroupOut", false}, +{"users_api_admin_user_groups_remove_users_from_group", "DELETE", "/v1/admin/user-groups/{group_uuid}/members", "beta.admin.user-groups", "docs", "json", "user_uuids", "", "none", "", false}, +{"users_api_admin_user_groups_get_user_group_members", "GET", "/v1/admin/user-groups/{group_uuid}/members", "beta.admin.user-groups", "docs", "none", "", "page,page_size", "json", "AdminUserGroupMembersOut", false}, +{"users_api_admin_user_groups_assign_users_to_group", "POST", "/v1/admin/user-groups/{group_uuid}/members", "beta.admin.user-groups", "docs", "json", "user_uuids", "", "none", "", false}, +{"users_api_admin_user_groups_get_nested_groups", "GET", "/v1/admin/user-groups/{group_uuid}/nested", "beta.admin.user-groups", "docs", "none", "", "", "json", "NestedGroupsOut", false}, +{"users_api_admin_user_groups_set_nested_groups", "PATCH", "/v1/admin/user-groups/{group_uuid}/nested", "beta.admin.user-groups", "docs", "json", "child_group_uuids", "", "none", "", false}, +{"users_admin_user_groups_update_user_group_organization_role", "PATCH", "/v1/admin/user-groups/{group_uuid}/organization-role", "beta.admin.user-groups", "docs", "json", "organization_role", "", "none", "", false}, +{"users_admin_user_groups_get_group_workspace_assignments", "GET", "/v1/admin/user-groups/{group_uuid}/workspaces", "beta.admin.user-groups", "docs", "none", "", "page,page_size", "json", "GroupWorkspaceAssignmentsOut", false}, +{"users_api_admin_user_groups_assign_group_to_workspace", "POST", "/v1/admin/user-groups/{group_uuid}/workspaces", "beta.admin.user-groups", "docs", "json", "role_names,roles,role,workspace_uuid", "", "none", "", false}, +{"users_admin_user_groups_remove_group_from_workspace", "DELETE", "/v1/admin/user-groups/{group_uuid}/workspaces/{workspace_uuid}", "beta.admin.user-groups", "docs", "none", "", "", "none", "", false}, +{"users_admin_user_groups_update_group_workspace_assignment", "PATCH", "/v1/admin/user-groups/{group_uuid}/workspaces/{workspace_uuid}", "beta.admin.user-groups", "docs", "json", "role_names,roles,role", "", "none", "", false}, +{"users_api_admin_roles_get_roles", "GET", "/v1/admin/roles", "beta.admin.users", "docs", "none", "", "", "json", "RolesOut", false}, +{"users_api_admin_users_get_users", "GET", "/v1/admin/users", "beta.admin.users", "docs", "none", "", "page,page_size,email", "json", "OrganizationAdminUsersOUT", false}, +{"users_api_admin_users_create_users", "POST", "/v1/admin/users", "beta.admin.users", "docs", "json", "", "", "json", "OrganizationUsersCreateOUT", false}, +{"users_api_admin_users_get_invite", "GET", "/v1/admin/users-invite", "beta.admin.users", "docs", "none", "", "", "json", "[OrganizationUserInviteOUT]", false}, +{"users_api_admin_users_invite_users", "POST", "/v1/admin/users-invite", "beta.admin.users", "docs", "json", "role_names,roles,role_name,role,email,subscription_type,subscription_types,subscription_seat_automatic_granting,email_language,workspace_uuids", "", "json", "OrganizationInvitesCreateOUT", false}, +{"users_api_admin_users_delete_invite", "DELETE", "/v1/admin/users-invite/{invite_uuid}", "beta.admin.users", "docs", "none", "", "", "json", "DeleteOUT", false}, +{"users_api_admin_users_delete_user", "DELETE", "/v1/admin/users/{user_id}", "beta.admin.users", "docs", "none", "", "", "json", "DeleteOUT", false}, +{"users_api_admin_users_get_user", "GET", "/v1/admin/users/{user_id}", "beta.admin.users", "docs", "none", "", "", "json", "AdminUserOUT", false}, +{"users_api_admin_users_update_user", "PATCH", "/v1/admin/users/{user_id}", "beta.admin.users", "docs", "json", "role_names,roles,role_name,role,subscription_types", "", "json", "AdminOrganizationMemberOUT", false}, +{"users_api_admin_users_deactivate_user", "POST", "/v1/admin/users/{user_id}/deactivate", "beta.admin.users", "docs", "none", "", "", "json", "DeactivateUserOUT", false}, +{"get_organization_stats_v1_admin_analytics_vibe_code_usage_by_organization", "GET", "/v1/admin/analytics/vibe/code/usage/by_organization", "beta.admin.vibe-code-analytics", "docs", "none", "", "start_time,end_time", "json", "VibeOrganizationStatsOUT", false}, +{"get_workspace_stats_v1_admin_analytics_vibe_code_usage_by_workspace", "GET", "/v1/admin/analytics/vibe/code/usage/by_workspace", "beta.admin.vibe-code-analytics", "docs", "none", "", "start_time,end_time,workspace_id", "json", "VibeWorkspaceStatsOUT", false}, +{"get_by_agent_stats_v1_admin_analytics_vibe_work_usage_by_agent_stats", "GET", "/v1/admin/analytics/vibe/work/usage/by_agent_stats", "beta.admin.vibe-work-analytics", "docs", "none", "", "start_time,end_time", "json", "VibeWorkByAgentStatsOUT", false}, +{"get_by_time_stats_v1_admin_analytics_vibe_work_usage_by_time_stats", "GET", "/v1/admin/analytics/vibe/work/usage/by_time_stats", "beta.admin.vibe-work-analytics", "docs", "none", "", "start_time,end_time,granularity", "json", "VibeWorkByTimeStatsOUT", false}, +{"get_by_user_stats_v1_admin_analytics_vibe_work_usage_by_user_stats", "GET", "/v1/admin/analytics/vibe/work/usage/by_user_stats", "beta.admin.vibe-work-analytics", "docs", "none", "", "start_time,end_time", "json", "VibeWorkByUserStatsOUT", false}, +{"users_api_admin_workspaces_get_workspaces", "GET", "/v1/admin/workspaces", "beta.admin.workspaces", "docs", "none", "", "is_archived,page,page_size,search", "json", "WorkspacesOut", false}, +{"users_api_admin_workspaces_create_workspace", "POST", "/v1/admin/workspaces", "beta.admin.workspaces", "docs", "json", "name,description,icon,add_all_org_members,admin_user_id", "", "json", "WorkspaceEnrichedOUT", false}, +{"users_api_admin_workspaces_delete_workspaces", "DELETE", "/v1/admin/workspaces/{workspace_uuid}", "beta.admin.workspaces", "docs", "none", "", "", "none", "", false}, +{"users_api_admin_workspaces_update_workspaces", "PATCH", "/v1/admin/workspaces/{workspace_uuid}", "beta.admin.workspaces", "docs", "json", "name,description,icon", "", "json", "WorkspaceOUT", false}, +{"users_api_admin_workspaces_add_users_workspaces", "POST", "/v1/admin/workspaces/{workspace_uuid}/add-users", "beta.admin.workspaces", "docs", "json", "members", "", "json", "AddUsersToWorkspaceOUT", false}, +{"users_api_admin_workspaces_remove_users_workspaces", "DELETE", "/v1/admin/workspaces/{workspace_uuid}/remove-users", "beta.admin.workspaces", "docs", "json", "members", "", "json", "RemoveWorkspaceMembersOUT", false}, +{"users_api_admin_workspaces_add_or_update_users_workspaces", "PATCH", "/v1/admin/workspaces/{workspace_uuid}/users", "beta.admin.workspaces", "docs", "json", "members", "", "json", "AddOrUpdateUsersToWorkspaceOUT", false}, +{"agents_api_v1_agents_list", "GET", "/v1/agents", "beta.agents", "docs", "none", "", "page,page_size,deployment_chat,sources,name,search,id,metadata", "json", "[Agent]", true}, +{"agents_api_v1_agents_create", "POST", "/v1/agents", "beta.agents", "docs", "json", "instructions,tools,completion_args,guardrails,model,name,description,handoffs,metadata,version_message", "", "json", "Agent", false}, +{"agents_api_v1_agents_list_pages", "GET", "/v1/agents/pages", "beta.agents", "docs", "none", "", "page_size,deployment_chat,sources,name,search,id,metadata,page_token", "json", "AgentListPage", false}, +{"agents_api_v1_agents_delete", "DELETE", "/v1/agents/{agent_id}", "beta.agents", "docs", "none", "", "", "none", "", false}, +{"agents_api_v1_agents_get", "GET", "/v1/agents/{agent_id}", "beta.agents", "docs", "none", "", "agent_version", "json", "Agent", false}, +{"agents_api_v1_agents_update", "PATCH", "/v1/agents/{agent_id}", "beta.agents", "docs", "json", "instructions,tools,completion_args,guardrails,model,name,description,handoffs,deployment_chat,metadata,version_message", "", "json", "Agent", false}, +{"agents_api_v1_agents_delete_alias", "DELETE", "/v1/agents/{agent_id}/aliases", "beta.agents", "docs", "none", "", "alias", "none", "", false}, +{"agents_api_v1_agents_list_version_aliases", "GET", "/v1/agents/{agent_id}/aliases", "beta.agents", "docs", "none", "", "", "json", "[AgentAliasResponse]", false}, +{"agents_api_v1_agents_create_or_update_alias", "PUT", "/v1/agents/{agent_id}/aliases", "beta.agents", "docs", "none", "", "alias,version", "json", "AgentAliasResponse", false}, +{"agents_api_v1_agents_update_version", "PATCH", "/v1/agents/{agent_id}/version", "beta.agents", "docs", "none", "", "version", "json", "Agent", false}, +{"agents_api_v1_agents_list_versions", "GET", "/v1/agents/{agent_id}/versions", "beta.agents", "docs", "none", "", "page,page_size", "json", "[Agent]", false}, +{"agents_api_v1_agents_get_version", "GET", "/v1/agents/{agent_id}/versions/{version}", "beta.agents", "docs", "none", "", "", "json", "Agent", false}, +{"connector_list_v1", "GET", "/v1/connectors", "beta.connectors", "docs", "none", "", "query_filters,cursor,page_size", "json", "PaginatedConnectors", false}, +{"connector_create_v1", "POST", "/v1/connectors", "beta.connectors", "docs", "json", "headers,global_headers,oauth2_server_metadata,oauth2_server_metadata_url,name,title,description,icon_url,server,visibility,auth_methods,system_prompt,protocol", "", "json", "Connector", false}, +{"connector_get_v1", "GET", "/v1/connectors/{connector_id_or_name}#idOrName", "beta.connectors", "docs", "none", "", "fetch_user_data", "json", "Connector", false}, +{"connector_get_auth_url_v1", "GET", "/v1/connectors/{connector_id_or_name}/auth_url", "beta.connectors", "docs", "none", "", "app_return_url,method_type,credentials_name,credentials_title,github_installation_link", "json", "AuthUrlResponse", false}, +{"connector_get_authentication_methods_v1", "GET", "/v1/connectors/{connector_id_or_name}/authentication_methods", "beta.connectors", "docs", "none", "", "", "json", "[PublicAuthenticationMethod]", false}, +{"connector_list_organization_credentials_v1", "GET", "/v1/connectors/{connector_id_or_name}/organization/credentials", "beta.connectors", "docs", "none", "", "auth_type,fetch_default", "json", "CredentialsResponse", false}, +{"connector_list_tools_v1", "GET", "/v1/connectors/{connector_id_or_name}/tools", "beta.connectors", "docs", "none", "", "page_size,refresh,pretty,credentials_name", "json", "[MCPTool]|[object]", false}, +{"connector_call_tool_v1", "POST", "/v1/connectors/{connector_id_or_name}/tools/{tool_name}/call", "beta.connectors", "docs", "json", "arguments", "credentials_name", "json", "ConnectorToolCallResponse", true}, +{"connector_delete_all_user_credentials_v1", "DELETE", "/v1/connectors/{connector_id_or_name}/user/credentials", "beta.connectors", "docs", "none", "", "", "json", "MessageResponse", false}, +{"connector_list_user_credentials_v1", "GET", "/v1/connectors/{connector_id_or_name}/user/credentials", "beta.connectors", "docs", "none", "", "auth_type,fetch_default", "json", "CredentialsResponse", false}, +{"connector_list_workspace_credentials_v1", "GET", "/v1/connectors/{connector_id_or_name}/workspace/credentials", "beta.connectors", "docs", "none", "", "auth_type,fetch_default", "json", "CredentialsResponse", false}, +{"connector_update_credentials", "PATCH", "/v1/connectors/{connector_id_or_name}/{consumer_scope}/credentials", "beta.connectors", "docs", "json", "name,title,is_default,credentials", "", "json", "MessageResponse", false}, +{"connector_create_credentials_v1", "POST", "/v1/connectors/{connector_id_or_name}/{consumer_scope}/credentials", "beta.connectors", "docs", "json", "name,title,is_default,credentials", "", "json", "MessageResponse", false}, +{"connector_delete_credentials", "DELETE", "/v1/connectors/{connector_id_or_name}/{consumer_scope}/credentials/{credentials_name}", "beta.connectors", "docs", "none", "", "", "json", "MessageResponse", false}, +{"connector_delete_v1", "DELETE", "/v1/connectors/{connector_id}#id", "beta.connectors", "docs", "none", "", "", "json", "MessageResponse", false}, +{"connector_update_v1", "PATCH", "/v1/connectors/{connector_id}#id", "beta.connectors", "docs", "json", "title,name,description,icon_url,server,auth_methods,protocol,system_prompt", "", "json", "Connector", false}, +{"connector_unshare_from_organization_v1", "DELETE", "/v1/connectors/{connector_id}/organization/share", "beta.connectors", "docs", "none", "", "", "json", "MessageResponse", false}, +{"connector_share_to_organization_v1", "PUT", "/v1/connectors/{connector_id}/organization/share", "beta.connectors", "docs", "none", "", "", "json", "MessageResponse", false}, +{"connector_unshare_v1", "DELETE", "/v1/connectors/{connector_id}/share", "beta.connectors", "docs", "none", "", "", "json", "MessageResponse", false}, +{"connector_share_v1", "PUT", "/v1/connectors/{connector_id}/share", "beta.connectors", "docs", "none", "", "", "json", "MessageResponse", false}, +{"connector_activate_for_consumer_v1", "POST", "/v1/connectors/{connector_id}/{consumer_scope}/activate", "beta.connectors", "docs", "none", "", "", "json", "MessageResponse", false}, +{"connector_deactivate_for_consumer_v1", "POST", "/v1/connectors/{connector_id}/{consumer_scope}/deactivate", "beta.connectors", "docs", "none", "", "", "json", "MessageResponse", false}, +{"agents_api_v1_conversations_list", "GET", "/v1/conversations", "beta.conversations", "docs", "none", "", "page,page_size,metadata", "json", "[ModelConversation|AgentConversation]", false}, +{"agents_api_v1_conversations_start", "POST", "/v1/conversations", "beta.conversations", "docs", "json", "inputs,stream,store,handoff_execution,instructions,tools,completion_args,guardrails,name,description,metadata,agent_id,agent_version,model", "", "json", "ConversationResponse", false}, +{"agents_api_v1_conversations_start_stream", "POST", "/v1/conversations#stream", "beta.conversations", "docs", "json", "inputs,stream,store,handoff_execution,instructions,tools,completion_args,guardrails,name,description,metadata,agent_id,agent_version,model", "", "sse", "ConversationEvents", false}, +{"agents_api_v1_conversations_delete", "DELETE", "/v1/conversations/{conversation_id}", "beta.conversations", "docs", "none", "", "", "none", "", false}, +{"agents_api_v1_conversations_get", "GET", "/v1/conversations/{conversation_id}", "beta.conversations", "docs", "none", "", "", "json", "ModelConversation|AgentConversation", false}, +{"agents_api_v1_conversations_append", "POST", "/v1/conversations/{conversation_id}", "beta.conversations", "docs", "json", "inputs,stream,store,handoff_execution,completion_args,tool_confirmations", "", "json", "ConversationResponse", false}, +{"agents_api_v1_conversations_append_stream", "POST", "/v1/conversations/{conversation_id}#stream", "beta.conversations", "docs", "json", "inputs,stream,store,handoff_execution,completion_args,tool_confirmations", "", "sse", "ConversationEvents", false}, +{"agents_api_v1_conversations_history", "GET", "/v1/conversations/{conversation_id}/history", "beta.conversations", "docs", "none", "", "", "json", "ConversationHistory", false}, +{"agents_api_v1_conversations_messages", "GET", "/v1/conversations/{conversation_id}/messages", "beta.conversations", "docs", "none", "", "", "json", "ConversationMessages", false}, +{"agents_api_v1_conversations_restart", "POST", "/v1/conversations/{conversation_id}/restart", "beta.conversations", "docs", "json", "inputs,stream,store,handoff_execution,completion_args,guardrails,metadata,from_entry_id,agent_version", "", "json", "ConversationResponse", false}, +{"agents_api_v1_conversations_restart_stream", "POST", "/v1/conversations/{conversation_id}/restart#stream", "beta.conversations", "docs", "json", "inputs,stream,store,handoff_execution,completion_args,guardrails,metadata,from_entry_id,agent_version", "", "sse", "ConversationEvents", false}, +{"libraries_list_v1", "GET", "/v1/libraries", "beta.libraries", "docs", "none", "", "page_size,page_token,page,search,filter_owned_by_me", "json", "ListLibrariesResponse", false}, +{"libraries_create_v1", "POST", "/v1/libraries", "beta.libraries", "docs", "json", "name,description,chunk_size,owner_type", "", "json", "Library", false}, +{"libraries_delete_v1", "DELETE", "/v1/libraries/{library_id}", "beta.libraries", "docs", "none", "", "", "json", "Library", false}, +{"libraries_get_v1", "GET", "/v1/libraries/{library_id}", "beta.libraries", "docs", "none", "", "", "json", "Library", false}, +{"libraries_patch_v1", "PATCH", "/v1/libraries/{library_id}", "beta.libraries", "docs", "json", "name,description", "", "json", "Library", false}, +{"libraries_update_v1", "PUT", "/v1/libraries/{library_id}", "beta.libraries", "docs", "json", "name,description", "", "json", "Library", true}, +{"libraries_share_delete_v1", "DELETE", "/v1/libraries/{library_id}/share", "beta.libraries.accesses", "docs", "json", "org_id,share_with_uuid,share_with_type", "", "json", "Sharing", false}, +{"libraries_share_list_v1", "GET", "/v1/libraries/{library_id}/share", "beta.libraries.accesses", "docs", "none", "", "", "json", "ListSharingResponse", false}, +{"libraries_share_create_v1", "PUT", "/v1/libraries/{library_id}/share", "beta.libraries.accesses", "docs", "json", "org_id,level,share_with_uuid,share_with_type", "", "json", "Sharing", false}, +{"libraries_documents_list_v1", "GET", "/v1/libraries/{library_id}/documents", "beta.libraries.documents", "docs", "none", "", "search,page_size,page,filters_attributes,sort_by,sort_order", "json", "ListDocumentsResponse", false}, +{"libraries_documents_upload_v1", "POST", "/v1/libraries/{library_id}/documents", "beta.libraries.documents", "docs", "multipart", "file", "", "json", "Document", false}, +{"libraries_documents_delete_v1", "DELETE", "/v1/libraries/{library_id}/documents/{document_id}", "beta.libraries.documents", "docs", "none", "", "", "none", "", false}, +{"libraries_documents_get_v1", "GET", "/v1/libraries/{library_id}/documents/{document_id}", "beta.libraries.documents", "docs", "none", "", "", "json", "Document", false}, +{"libraries_documents_patch_v1", "PATCH", "/v1/libraries/{library_id}/documents/{document_id}", "beta.libraries.documents", "docs", "json", "name,attributes,expires_at", "", "json", "Document", false}, +{"libraries_documents_update_v1", "PUT", "/v1/libraries/{library_id}/documents/{document_id}", "beta.libraries.documents", "docs", "json", "name,attributes,expires_at", "", "json", "Document", true}, +{"libraries_documents_get_extracted_text_signed_url_v1", "GET", "/v1/libraries/{library_id}/documents/{document_id}/extracted-text-signed-url", "beta.libraries.documents", "docs", "none", "", "", "json", "string", false}, +{"libraries_documents_reprocess_v1", "POST", "/v1/libraries/{library_id}/documents/{document_id}/reprocess", "beta.libraries.documents", "docs", "none", "", "", "none", "", false}, +{"libraries_documents_get_signed_url_v1", "GET", "/v1/libraries/{library_id}/documents/{document_id}/signed-url", "beta.libraries.documents", "docs", "none", "", "", "json", "string", false}, +{"libraries_documents_get_status_v1", "GET", "/v1/libraries/{library_id}/documents/{document_id}/status", "beta.libraries.documents", "docs", "none", "", "", "json", "ProcessingStatus", false}, +{"libraries_documents_get_text_content_v1", "GET", "/v1/libraries/{library_id}/documents/{document_id}/text_content", "beta.libraries.documents", "docs", "none", "", "page_start,page_end", "json", "DocumentTextContent", false}, +{"get_datasets_v1_observability_datasets_get", "GET", "/v1/observability/datasets", "beta.observability.datasets", "docs", "none", "", "page_size,page,q", "json", "ListDatasetsResponse", false}, +{"create_dataset_v1_observability_datasets_post", "POST", "/v1/observability/datasets", "beta.observability.datasets", "docs", "json", "name,description", "", "json", "Dataset", false}, +{"delete_dataset_v1_observability_datasets__dataset_id__delete", "DELETE", "/v1/observability/datasets/{dataset_id}", "beta.observability.datasets", "docs", "none", "", "", "none", "", false}, +{"get_dataset_by_id_v1_observability_datasets__dataset_id__get", "GET", "/v1/observability/datasets/{dataset_id}", "beta.observability.datasets", "docs", "none", "", "", "json", "DatasetPreview", false}, +{"update_dataset_v1_observability_datasets__dataset_id__patch", "PATCH", "/v1/observability/datasets/{dataset_id}", "beta.observability.datasets", "docs", "json", "name,description", "", "json", "DatasetPreview", false}, +{"export_dataset_to_jsonl_v1_observability_datasets__dataset_id__exports_to_jsonl_get", "GET", "/v1/observability/datasets/{dataset_id}/exports/to-jsonl", "beta.observability.datasets", "docs", "none", "", "", "json", "ExportDatasetResponse", false}, +{"post_dataset_records_from_dataset_v1_observability_datasets__dataset_id__imports_from_dataset_post", "POST", "/v1/observability/datasets/{dataset_id}/imports/from-dataset", "beta.observability.datasets", "docs", "json", "dataset_record_ids", "", "json", "DatasetImportTask", false}, +{"post_dataset_records_from_file_v1_observability_datasets__dataset_id__imports_from_file_post", "POST", "/v1/observability/datasets/{dataset_id}/imports/from-file", "beta.observability.datasets", "docs", "json", "file_id", "", "json", "DatasetImportTask", false}, +{"post_dataset_records_from_playground_v1_observability_datasets__dataset_id__imports_from_playground_post", "POST", "/v1/observability/datasets/{dataset_id}/imports/from-playground", "beta.observability.datasets", "docs", "json", "conversation_ids", "", "json", "DatasetImportTask", false}, +{"post_dataset_records_from_spans_v1_observability_datasets__dataset_id__imports_from_spans_post", "POST", "/v1/observability/datasets/{dataset_id}/imports/from-spans", "beta.observability.datasets", "docs", "json", "span_references,mapping_contract", "", "json", "DatasetImportTask", false}, +{"get_dataset_records_v1_observability_datasets__dataset_id__records_get", "GET", "/v1/observability/datasets/{dataset_id}/records", "beta.observability.datasets", "docs", "none", "", "page_size,page", "json", "ListDatasetRecordsResponse", false}, +{"create_dataset_record_v1_observability_datasets__dataset_id__records_post", "POST", "/v1/observability/datasets/{dataset_id}/records", "beta.observability.datasets", "docs", "json", "payload,properties,source", "", "json", "DatasetRecord", false}, +{"get_dataset_import_tasks_v1_observability_datasets__dataset_id__tasks_get", "GET", "/v1/observability/datasets/{dataset_id}/tasks", "beta.observability.datasets", "docs", "none", "", "page_size,page", "json", "ListDatasetImportTasksResponse", false}, +{"get_dataset_import_task_v1_observability_datasets__dataset_id__tasks__task_id__get", "GET", "/v1/observability/datasets/{dataset_id}/tasks/{task_id}", "beta.observability.datasets", "docs", "none", "", "", "json", "DatasetImportTask", false}, +{"delete_dataset_records_v1_observability_dataset_records_bulk_delete_post", "POST", "/v1/observability/dataset-records/bulk-delete", "beta.observability.datasets.records", "docs", "json", "dataset_record_ids", "", "none", "", false}, +{"delete_dataset_record_v1_observability_dataset_records__dataset_record_id__delete", "DELETE", "/v1/observability/dataset-records/{dataset_record_id}", "beta.observability.datasets.records", "docs", "none", "", "", "none", "", false}, +{"get_dataset_record_v1_observability_dataset_records__dataset_record_id__get", "GET", "/v1/observability/dataset-records/{dataset_record_id}", "beta.observability.datasets.records", "docs", "none", "", "", "json", "DatasetRecord", false}, +{"judge_dataset_record_v1_observability_dataset_records__dataset_record_id__live_judging_post", "POST", "/v1/observability/dataset-records/{dataset_record_id}/live-judging", "beta.observability.datasets.records", "docs", "json", "judge_definition", "", "json", "JudgeOutput", false}, +{"update_dataset_record_payload_v1_observability_dataset_records__dataset_record_id__payload_put", "PUT", "/v1/observability/dataset-records/{dataset_record_id}/payload", "beta.observability.datasets.records", "docs", "json", "payload", "", "none", "", false}, +{"update_dataset_record_properties_v1_observability_dataset_records__dataset_record_id__properties_put", "PUT", "/v1/observability/dataset-records/{dataset_record_id}/properties", "beta.observability.datasets.records", "docs", "json", "properties", "", "none", "", false}, +{"get_evaluation_v2_v1_observability_evaluations_v2__evaluation_slug__get", "GET", "/v1/observability/evaluations_v2/{evaluation_slug}", "beta.observability.evaluations", "docs", "none", "", "", "json", "EvaluationV2", false}, +{"list_pipelines_v1_observability_pipelines_get", "GET", "/v1/observability/pipelines", "beta.observability.evaluations", "docs", "none", "", "enabled,page_size,page,q", "json", "PipelinesResponse", false}, +{"create_pipeline_v1_observability_pipelines_post", "POST", "/v1/observability/pipelines", "beta.observability.evaluations", "docs", "json", "name,description,selectors,definitions,enabled", "", "json", "Pipeline", false}, +{"delete_pipeline_v1_observability_pipelines__pipeline_id__delete", "DELETE", "/v1/observability/pipelines/{pipeline_id}", "beta.observability.evaluations", "docs", "none", "", "", "none", "", false}, +{"get_pipeline_v1_observability_pipelines__pipeline_id__get", "GET", "/v1/observability/pipelines/{pipeline_id}", "beta.observability.evaluations", "docs", "none", "", "", "json", "Pipeline", false}, +{"update_pipeline_v1_observability_pipelines__pipeline_id__put", "PUT", "/v1/observability/pipelines/{pipeline_id}", "beta.observability.evaluations", "docs", "json", "name,description,selectors,definitions,enabled", "", "json", "Pipeline", false}, +{"get_project_evaluations_v2_v1_observability_projects__project_slug__evaluations_v2_get", "GET", "/v1/observability/projects/{project_slug}/evaluations_v2", "beta.observability.evaluations", "docs", "none", "", "page_size,page", "json", "EvaluationsV2", false}, +{"list_optimizations_v1_observability_optimizations_get", "GET", "/v1/observability/optimizations", "beta.observability.evaluations.optimizations", "docs", "none", "", "project,evaluation,status,algorithm,tags,page_size,page", "json", "ListOptimizationsResponse", false}, +{"get_optimization_v1_observability_optimizations__optimization_id__get", "GET", "/v1/observability/optimizations/{optimization_id}", "beta.observability.evaluations.optimizations", "docs", "none", "", "", "json", "OptimizationDetail", false}, +{"list_optimization_trials_v1_observability_optimizations__optimization_id__trials_get", "GET", "/v1/observability/optimizations/{optimization_id}/trials", "beta.observability.evaluations.optimizations", "docs", "none", "", "page_size,page", "json", "ListOptimizationTrialsResponse", false}, +{"get_projects_v1_observability_projects_get", "GET", "/v1/observability/projects", "beta.observability.evaluations.projects", "docs", "none", "", "q,page_size,page", "json", "Projects", false}, +{"get_project_v1_observability_projects__project_slug__get", "GET", "/v1/observability/projects/{project_slug}", "beta.observability.evaluations.projects", "docs", "none", "", "", "json", "Project", false}, +{"search_evaluation_run_records_v2_v1_observability_evaluation_runs_v2__run_id__records_search_post", "POST", "/v1/observability/evaluation-runs_v2/{run_id}/records/search", "beta.observability.evaluations.records", "docs", "json", "input_filters,output_filters,score_filters,goal_filters,sort", "page_size,page", "json", "ListEvaluationRunRecordsResponse", false}, +{"get_evaluation_run_record_v2_v1_observability_evaluation_runs_v2__run_id__records__record_id__get", "GET", "/v1/observability/evaluation-runs_v2/{run_id}/records/{record_id}", "beta.observability.evaluations.records", "docs", "none", "", "", "json", "EvaluationRunRecordV2Response", false}, +{"search_all_evaluation_runs_v2_v1_observability_evaluation_runs_v2_search_post", "POST", "/v1/observability/evaluation-runs_v2/search", "beta.observability.evaluations.runs", "docs", "json", "run_ids,evaluation_ids,creator_ids,project_ids,ownership,tags,score_filters,goal_filters,system,metadata", "page_size,page", "json", "SearchAllEvaluationRunsResponse", false}, +{"get_evaluation_run_v2_v1_observability_evaluation_runs_v2__run_id__get", "GET", "/v1/observability/evaluation-runs_v2/{run_id}", "beta.observability.evaluations.runs", "docs", "none", "", "", "json", "EvaluationRunV2Response", false}, +{"get_single_evaluation_run_statistics_v2_v1_observability_evaluation_runs_v2__run_id__statistics_get", "GET", "/v1/observability/evaluation-runs_v2/{run_id}/statistics", "beta.observability.evaluations.runs", "docs", "none", "", "", "json", "RunStatisticsResponse", false}, +{"compute_filtered_run_statistics_v2_v1_observability_evaluation_runs_v2__run_id__statistics_compute_post", "POST", "/v1/observability/evaluation-runs_v2/{run_id}/statistics/compute", "beta.observability.evaluations.runs", "docs", "json", "input_filters,output_filters,score_filters,goal_filters,sort", "", "json", "RunStatisticsResponse", false}, +{"search_evaluation_runs_v2_v1_observability_evaluations_v2__evaluation_slug__runs_search_post", "POST", "/v1/observability/evaluations_v2/{evaluation_slug}/runs/search", "beta.observability.evaluations.runs", "docs", "json", "run_ids,evaluation_ids,creator_ids,project_ids,ownership,tags,score_filters,goal_filters,system,metadata", "page_size,page", "json", "ListEvaluationRunsResponse", false}, +{"get_judges_v1_observability_judges_get", "GET", "/v1/observability/judges", "beta.observability.judges", "docs", "none", "", "type_filter,model_filter,page_size,page,q", "json", "ListJudgesResponse", false}, +{"create_judge_v1_observability_judges_post", "POST", "/v1/observability/judges", "beta.observability.judges", "docs", "json", "name,description,model_name,output,instructions,tools", "", "json", "Judge", false}, +{"delete_judge_v1_observability_judges__judge_id__delete", "DELETE", "/v1/observability/judges/{judge_id}", "beta.observability.judges", "docs", "none", "", "", "none", "", false}, +{"get_judge_by_id_v1_observability_judges__judge_id__get", "GET", "/v1/observability/judges/{judge_id}", "beta.observability.judges", "docs", "none", "", "", "json", "Judge", false}, +{"update_judge_v1_observability_judges__judge_id__put", "PUT", "/v1/observability/judges/{judge_id}", "beta.observability.judges", "docs", "json", "name,description,model_name,output,instructions,tools", "", "none", "", false}, +{"judge_conversation_v1_observability_judges__judge_id__live_judging_post", "POST", "/v1/observability/judges/{judge_id}/live-judging", "beta.observability.judges", "docs", "json", "messages,properties", "", "json", "JudgeOutput", false}, +{"get_log_fields_v1_observability_logs_fields_get", "GET", "/v1/observability/logs/fields", "beta.observability.logs", "docs", "none", "", "", "json", "GetLogFields", false}, +{"get_log_field_options_v1_observability_logs_fields__field_name__options_get", "GET", "/v1/observability/logs/fields/{field_name}/options", "beta.observability.logs", "docs", "none", "", "from,to", "json", "GetLogFieldOptions", false}, +{"search_logs_v1_observability_logs_search_post", "POST", "/v1/observability/logs/search", "beta.observability.logs", "docs", "json", "search_expression,order", "from,to,page_size,cursor", "json", "GetLogs", false}, +{"search_pipeline_results_v1_observability_pipeline_results_search_post", "POST", "/v1/observability/pipeline-results/search", "beta.observability.spans", "docs", "json", "pipeline_config_slugs,search_expression", "from,to,page_size,cursor", "json", "GetPipelineResults", false}, +{"aggregate_spans_v1_observability_spans_aggregate_post", "POST", "/v1/observability/spans/aggregate", "beta.observability.spans", "docs", "json", "metric,dimensions,time_dimension,search_expression,order_by,limit", "from,to", "json", "Aggregation", false}, +{"aggregate_span_evaluations_v1_observability_spans_evaluations_aggregate_post", "POST", "/v1/observability/spans/evaluations/aggregate", "beta.observability.spans", "docs", "json", "metric,dimensions,time_dimension,search_expression,order_by,limit", "from,to", "json", "Aggregation", false}, +{"get_span_evaluation_fields_v1_observability_spans_evaluations_fields_get", "GET", "/v1/observability/spans/evaluations/fields", "beta.observability.spans", "docs", "none", "", "", "json", "GetSpanEvaluationFields", false}, +{"get_span_evaluation_field_options_v1_observability_spans_evaluations_fields__field_name__options_get", "GET", "/v1/observability/spans/evaluations/fields/{field_name}/options", "beta.observability.spans", "docs", "none", "", "from,to", "json", "GetSpanEvaluationFieldOptions", false}, +{"search_span_evaluations_v1_observability_spans_evaluations_search_post", "POST", "/v1/observability/spans/evaluations/search", "beta.observability.spans", "docs", "json", "search_expression", "from,to,page_size,cursor", "json", "GetSpanEvaluations", false}, +{"search_latest_span_evaluations_v1_observability_spans_evaluations_search_latest_post", "POST", "/v1/observability/spans/evaluations/search/latest", "beta.observability.spans", "docs", "json", "search_expression", "from,to,page_size,cursor", "json", "GetSpanEvaluations", false}, +{"get_span_fields_v1_observability_spans_fields_get", "GET", "/v1/observability/spans/fields", "beta.observability.spans", "docs", "none", "", "", "json", "GetSpanFields", false}, +{"get_span_field_options_v1_observability_spans_fields__field_name__options_get", "GET", "/v1/observability/spans/fields/{field_name}/options", "beta.observability.spans", "docs", "none", "", "from,to", "json", "GetSpanFieldOptions", false}, +{"search_spans_v1_observability_spans_search_post", "POST", "/v1/observability/spans/search", "beta.observability.spans", "docs", "json", "search_expression", "from,to,page_size,cursor", "json", "GetSpans", false}, +{"aggregate_traces_v1_observability_traces_aggregate_post", "POST", "/v1/observability/traces/aggregate", "beta.observability.traces", "docs", "json", "metric,dimensions,time_dimension,search_expression,order_by,limit", "from,to", "json", "Aggregation", false}, +{"get_trace_fields_v1_observability_traces_fields_get", "GET", "/v1/observability/traces/fields", "beta.observability.traces", "docs", "none", "", "", "json", "GetTraceFields", false}, +{"get_trace_field_options_v1_observability_traces_fields__field_name__options_get", "GET", "/v1/observability/traces/fields/{field_name}/options", "beta.observability.traces", "docs", "none", "", "from,to", "json", "GetTraceFieldOptions", false}, +{"search_traces_v1_observability_traces_search_post", "POST", "/v1/observability/traces/search", "beta.observability.traces", "docs", "json", "search_expression", "from,to,page_size,cursor", "json", "GetTraces", false}, +{"get_trace_by_id_v1_observability_traces__trace_id__get", "GET", "/v1/observability/traces/{trace_id}", "beta.observability.traces", "docs", "none", "", "", "json", "GetTrace", false}, +{"get_trace_spans_v1_observability_traces__trace_id__spans_get", "GET", "/v1/observability/traces/{trace_id}/spans", "beta.observability.traces", "docs", "none", "", "from,to,page_size,cursor", "json", "GetSpans", false}, +{"get_span_by_id_v1_observability_traces__trace_id__spans__span_id__get", "GET", "/v1/observability/traces/{trace_id}/spans/{span_id}", "beta.observability.traces", "docs", "none", "", "from,to", "json", "GetSpan", false}, +{"prompts_list", "GET", "/v2/prompts", "beta.prompts", "docs", "none", "", "pageSize,pageToken,alias,fields,sort.field,sort.direction,sort_by,sort_direction", "json", "ListPromptsResponse", false}, +{"prompts_create", "POST", "/v2/prompts", "beta.prompts", "docs", "json", "name,definition,title,description,notes,sharingScope,aliases,workspaceRelation", "", "json", "Prompt", false}, +{"prompts_delete", "DELETE", "/v2/prompts/{prompt_id}", "beta.prompts", "docs", "none", "", "", "json", "DeletePromptResponse", false}, +{"prompts_get", "GET", "/v2/prompts/{prompt_id}", "beta.prompts", "docs", "none", "", "version,alias,fields", "json", "Prompt", false}, +{"prompts_update", "PATCH", "/v2/prompts/{prompt_id}", "beta.prompts", "docs", "json", "title,description,sharingScope,workspaceRelation", "", "json", "Prompt", false}, +{"prompts_list_versions", "GET", "/v2/prompts/{prompt_id}/versions", "beta.prompts", "docs", "none", "", "", "json", "ListPromptVersionsResponse", false}, +{"prompts_create_version", "POST", "/v2/prompts/{prompt_id}/versions", "beta.prompts", "docs", "json", "definition,notes,aliases", "", "json", "CreatePromptVersionResponse", false}, +{"prompts_get_version", "GET", "/v2/prompts/{prompt_id}/versions/{version}", "beta.prompts", "docs", "none", "", "fields", "json", "Prompt", false}, +{"prompts_update_version_metadata", "PATCH", "/v2/prompts/{prompt_id}/versions/{version}", "beta.prompts", "docs", "json", "notes,aliases", "", "json", "Prompt", false}, +{"get_configs_v1_rag_ingestion_pipeline_configurations_get", "GET", "/v1/rag/ingestion_pipeline_configurations", "beta.rag.ingestion_pipeline_configurations", "docs", "none", "", "", "json", "[IngestionPipelineConfiguration]", false}, +{"register_config_v1_rag_ingestion_pipeline_configurations_put", "PUT", "/v1/rag/ingestion_pipeline_configurations", "beta.rag.ingestion_pipeline_configurations", "docs", "json", "name,pipeline_composition,target_indexes", "", "json", "IngestionPipelineConfiguration", false}, +{"update_run_info_v1_rag_ingestion_pipeline_configurations__id__run_info_put", "PUT", "/v1/rag/ingestion_pipeline_configurations/{id}/run_info", "beta.rag.ingestion_pipeline_configurations", "docs", "json", "execution_time,chunks_count", "", "json", "IngestionPipelineConfiguration", false}, +{"list_indexes_v1_rag_managed_indexes_get", "GET", "/v1/rag/managed_indexes", "beta.rag.managed_indexes", "docs", "none", "", "page_size,page_token,name,status,creator_id", "json", "ListManagedIndexesResponse", false}, +{"create_index_v1_rag_managed_indexes_post", "POST", "/v1/rag/managed_indexes", "beta.rag.managed_indexes", "docs", "json", "name,schema,config", "", "json", "ManagedIndexResponse", false}, +{"delete_index_v1_rag_managed_indexes__index_name__delete", "DELETE", "/v1/rag/managed_indexes/{index_name}", "beta.rag.managed_indexes", "docs", "none", "", "", "json", "DeleteManagedIndexResponse", false}, +{"get_index_v1_rag_managed_indexes__index_name__get", "GET", "/v1/rag/managed_indexes/{index_name}", "beta.rag.managed_indexes", "docs", "none", "", "", "json", "ManagedIndexResponse", false}, +{"update_index_v1_rag_managed_indexes__index_name__put", "PUT", "/v1/rag/managed_indexes/{index_name}", "beta.rag.managed_indexes", "docs", "json", "schema", "", "json", "ManagedIndexResponse", false}, +{"get_chunk_index_v1_rag_managed_indexes__index_name__chunks__chunk_id__get", "GET", "/v1/rag/managed_indexes/{index_name}/chunks/{chunk_id}", "beta.rag.managed_indexes", "docs", "none", "", "", "json", "NavigationResponse", false}, +{"delete_documents_v1_rag_managed_indexes__index_name__documents_delete", "DELETE", "/v1/rag/managed_indexes/{index_name}/documents", "beta.rag.managed_indexes", "docs", "json", "document_ids", "", "json", "DeleteDocumentsResponse", false}, +{"ingest_documents_v1_rag_managed_indexes__index_name__documents_post", "POST", "/v1/rag/managed_indexes/{index_name}/documents", "beta.rag.managed_indexes", "docs", "json", "documents", "", "json", "IngestDocumentsResponse", false}, +{"grep_index_v1_rag_managed_indexes__index_name__grep_post", "POST", "/v1/rag/managed_indexes/{index_name}/grep", "beta.rag.managed_indexes", "docs", "json", "source_id,pattern,mode,top_k,content_type", "", "json", "NavigationResponse", false}, +{"navigate_index_v1_rag_managed_indexes__index_name__navigate_post", "POST", "/v1/rag/managed_indexes/{index_name}/navigate", "beta.rag.managed_indexes", "docs", "json", "source_id,start_offset,end_offset,direction,top_k,content_type", "", "json", "NavigationResponse", false}, +{"read_index_v1_rag_managed_indexes__index_name__read_post", "POST", "/v1/rag/managed_indexes/{index_name}/read", "beta.rag.managed_indexes", "docs", "json", "source_id,start_offset,end_offset,top_k,content_type", "", "json", "NavigationResponse", false}, +{"search_index_v1_rag_managed_indexes__index_name__search_post", "POST", "/v1/rag/managed_indexes/{index_name}/search", "beta.rag.managed_indexes", "docs", "json", "retriever", "", "json", "SearchResponse", false}, +{"get_deployment_summaries_v1_rag_deployments_get", "GET", "/v1/rag/deployments", "beta.rag.search_indexes", "docs", "none", "", "", "json", "GetDeploymentSummariesResponse", false}, +{"register_deployment_v1_rag_deployments_put", "PUT", "/v1/rag/deployments", "beta.rag.search_indexes", "docs", "json", "name,status,deployment", "", "json", "RegisterSearchIndexResponseIndex", false}, +{"unregister_deployment_v1_rag_deployments__deployment_id__delete", "DELETE", "/v1/rag/deployments/{deployment_id}", "beta.rag.search_indexes", "docs", "none", "", "", "json", "", false}, +{"update_index_metrics_v1_rag_deployments__deployment_id__metrics_put", "PUT", "/v1/rag/deployments/{deployment_id}/metrics", "beta.rag.search_indexes", "docs", "json", "status,document_count,index_metrics,clear_metrics", "", "json", "", false}, +{"list_service_accounts_v1_service_accounts_get", "GET", "/v1/service-accounts", "beta.service_accounts", "docs", "none", "", "workspace_id,include_deleted,q,order,offset,limit", "json", "ListServiceAccountsResponse", false}, +{"create_service_account_v1_service_accounts_post", "POST", "/v1/service-accounts", "beta.service_accounts", "docs", "json", "name,workspace_id,description,role_ids", "", "json", "ServiceAccount", false}, +{"list_assignable_service_account_roles_v1_service_accounts_assignable_roles_get", "GET", "/v1/service-accounts/assignable-roles", "beta.service_accounts", "docs", "none", "", "workspace_id", "json", "ListAssignableServiceAccountRolesResponse", false}, +{"delete_service_account_v1_service_accounts__service_account_id__delete", "DELETE", "/v1/service-accounts/{service_account_id}", "beta.service_accounts", "docs", "none", "", "", "none", "", false}, +{"get_service_account_v1_service_accounts__service_account_id__get", "GET", "/v1/service-accounts/{service_account_id}", "beta.service_accounts", "docs", "none", "", "", "json", "ServiceAccount", false}, +{"update_service_account_v1_service_accounts__service_account_id__patch", "PATCH", "/v1/service-accounts/{service_account_id}", "beta.service_accounts", "docs", "json", "description", "", "json", "ServiceAccount", false}, +{"list_service_account_roles_v1_service_accounts__service_account_id__roles_get", "GET", "/v1/service-accounts/{service_account_id}/roles", "beta.service_accounts", "docs", "none", "", "", "json", "ListServiceAccountRolesResponse", false}, +{"set_service_account_roles_v1_service_accounts__service_account_id__roles_put", "PUT", "/v1/service-accounts/{service_account_id}/roles", "beta.service_accounts", "docs", "json", "role_ids", "", "json", "ListServiceAccountRolesResponse", false}, +{"skills_list", "GET", "/v2/skills", "beta.skills", "docs", "none", "", "pageSize,pageToken,alias,fields,sort.field,sort.direction,sort_by,sort_direction", "json", "ListSkillsResponse", false}, +{"skills_create", "POST", "/v2/skills", "beta.skills", "docs", "json", "name,definition,notes,sharingScope,aliases,workspaceRelation", "", "json", "Skill", false}, +{"skills_delete", "DELETE", "/v2/skills/{skill_id}", "beta.skills", "docs", "none", "", "", "json", "DeleteSkillResponse", false}, +{"skills_get", "GET", "/v2/skills/{skill_id}", "beta.skills", "docs", "none", "", "version,alias,fields", "json", "Skill", false}, +{"skills_update", "PATCH", "/v2/skills/{skill_id}", "beta.skills", "docs", "json", "sharingScope,workspaceRelation", "", "json", "Skill", false}, +{"skills_list_versions", "GET", "/v2/skills/{skill_id}/versions", "beta.skills", "docs", "none", "", "", "json", "ListSkillVersionsResponse", false}, +{"skills_create_version", "POST", "/v2/skills/{skill_id}/versions", "beta.skills", "docs", "json", "definition,notes,aliases", "", "json", "CreateSkillVersionResponse", false}, +{"skills_get_version", "GET", "/v2/skills/{skill_id}/versions/{version}", "beta.skills", "docs", "none", "", "fields", "json", "Skill", false}, +{"skills_update_version_metadata", "PATCH", "/v2/skills/{skill_id}/versions/{version}", "beta.skills", "docs", "json", "notes,aliases", "", "json", "Skill", false}, +{"users_api_get_identity", "GET", "/v1/users/me", "beta.users", "docs", "none", "", "", "json", "UserIdentity", false}, +{"users_api_list_organizations", "GET", "/v1/users/me/organizations", "beta.users", "docs", "none", "", "offset,limit", "json", "ListOrganizationsResponse", false}, +{"users_api_list_workspaces", "GET", "/v1/users/me/workspaces", "beta.users", "docs", "none", "", "organization_id,offset,limit", "json", "ListWorkspacesResponse", false}, +{"users_api_whoami", "GET", "/v1/whoami", "beta.users", "docs", "none", "", "", "json", "WhoamiResponse", false}, +{"chat_completion_v1_chat_completions_post", "POST", "/v1/chat/completions", "chat", "docs", "json", "model,temperature,top_p,max_tokens,stream,stop,random_seed,metadata,messages,response_format,tools,tool_choice,presence_penalty,frequency_penalty,min_tokens,repetition_penalty,top_k,logprobs,top_logprobs,prompt_logprobs,top_prompt_logprobs,n,prediction,parallel_tool_calls,reasoning_effort,prompt_mode,guardrails,prompt_cache_key,service_tier,safe_prompt", "", "json", "ChatCompletionResponse", false}, +{"chat_classifications_v1_chat_classifications_post", "POST", "/v1/chat/classifications", "classifiers", "docs", "json", "model,input", "", "json", "ClassificationResponse", false}, +{"chat_moderations_v1_chat_moderations_post", "POST", "/v1/chat/moderations", "classifiers", "docs", "json", "input,model", "", "json", "ModerationResponse", false}, +{"classifications_v1_classifications_post", "POST", "/v1/classifications", "classifiers", "docs", "json", "model,metadata,input", "", "json", "ClassificationResponse", false}, +{"moderations_v1_moderations_post", "POST", "/v1/moderations", "classifiers", "docs", "json", "model,metadata,input", "", "json", "ModerationResponse", false}, +{"embeddings_v1_embeddings_post", "POST", "/v1/embeddings", "embeddings", "docs", "json", "model,metadata,input,output_dimension,output_dtype,encoding_format", "", "json", "EmbeddingResponse", false}, +{"get_workflow_events_v1_workflows_events_list_get", "GET", "/v1/workflows/events/list", "events", "docs", "none", "", "root_workflow_exec_id,workflow_exec_id,workflow_run_id,limit,cursor", "json", "ListWorkflowEventResponse", false}, +{"get_stream_events_v1_workflows_events_stream_get", "GET", "/v1/workflows/events/stream", "events", "docs", "none", "", "scope,activity_name,activity_id,workflow_name,workflow_exec_id,root_workflow_exec_id,parent_workflow_exec_id,stream,start_seq,metadata_filters,workflow_event_types", "sse", "object", false}, +{"files_api_routes_list_files", "GET", "/v1/files", "files", "docs", "none", "", "page,page_size,include_total,sample_type,source,search,purpose,mimetypes", "json", "ListFilesResponse", false}, +{"files_api_routes_upload_file", "POST", "/v1/files", "files", "docs", "multipart", "expiry,visibility,purpose,file", "", "json", "CreateFileResponse", false}, +{"files_api_routes_delete_file", "DELETE", "/v1/files/{file_id}", "files", "docs", "none", "", "", "json", "DeleteFileResponse", false}, +{"files_api_routes_retrieve_file", "GET", "/v1/files/{file_id}", "files", "docs", "none", "", "", "json", "GetFileResponse", false}, +{"files_api_routes_download_file", "GET", "/v1/files/{file_id}/content", "files", "docs", "none", "", "", "binary", "string", false}, +{"files_api_routes_get_signed_url", "GET", "/v1/files/{file_id}/url", "files", "docs", "none", "", "expiry", "json", "GetSignedUrlResponse", false}, +{"fim_completion_v1_fim_completions_post", "POST", "/v1/fim/completions", "fim", "docs", "json", "model,temperature,top_p,max_tokens,stream,stop,random_seed,metadata,prompt,suffix,min_tokens,prompt_cache_key", "", "json", "FIMCompletionResponse", false}, +{"jobs_api_routes_fine_tuning_update_fine_tuned_model", "PATCH", "/v1/fine_tuning/models/{model_id}", "models", "docs", "json", "name,description", "", "json", "CompletionFineTunedModel|ClassifierFineTunedModel", false}, +{"jobs_api_routes_fine_tuning_unarchive_fine_tuned_model", "DELETE", "/v1/fine_tuning/models/{model_id}/archive", "models", "docs", "none", "", "", "json", "UnarchiveModelResponse", false}, +{"jobs_api_routes_fine_tuning_archive_fine_tuned_model", "POST", "/v1/fine_tuning/models/{model_id}/archive", "models", "docs", "none", "", "", "json", "ArchiveModelResponse", false}, +{"list_models_v1_models_get", "GET", "/v1/models", "models", "docs", "none", "", "provider,model", "json", "ModelList", false}, +{"delete_model_v1_models__model_id__delete", "DELETE", "/v1/models/{model_id}", "models", "docs", "none", "", "", "json", "DeleteModelResponse", false}, +{"retrieve_model_v1_models__model_id__get", "GET", "/v1/models/{model_id}", "models", "docs", "none", "", "", "json", "BaseModelCard|FTModelCard", false}, +{"ocr_v1_ocr_post", "POST", "/v1/ocr", "ocr", "docs", "json", "model,document,pages,include_image_base64,image_limit,image_min_size,bbox_annotation_format,document_annotation_format,document_annotation_prompt,table_format,extract_header,extract_footer,include_blocks,confidence_scores_granularity", "", "json", "OCRResponse", false}, +{"create_client_session_v1_client_sessions_post", "POST", "/v1/client/sessions", "realtime.sessions", "docs", "json", "purpose,model,ttl_seconds", "", "json", "CreateRealtimeSessionResponse", false}, +{"get_workflows_v1_workflows_get", "GET", "/v1/workflows", "workflows", "docs", "none", "", "status,include_shared,available_in_chat_assistant,deployment_name,deployment_status,archived,tags,sort_by,order,cursor,limit,active_only,search", "json", "WorkflowListResponse", false}, +{"bulk_archive_workflows_v1_workflows_archive_put", "PUT", "/v1/workflows/archive", "workflows", "docs", "json", "workflow_ids", "", "json", "WorkflowBulkArchiveResponse", false}, +{"get_workflow_registrations_v1_workflows_registrations_get", "GET", "/v1/workflows/registrations", "workflows", "docs", "none", "", "workflow_id,task_queue,active_only,include_shared,workflow_name,workflow_search,archived,with_workflow,available_in_chat_assistant,limit,cursor", "json", "WorkflowRegistrationListResponse", false}, +{"get_workflow_registration_v1_workflows_registrations__workflow_registration_id__get", "GET", "/v1/workflows/registrations/{workflow_registration_id}", "workflows", "docs", "none", "", "with_workflow,include_shared", "json", "WorkflowRegistrationGetResponse", false}, +{"execute_workflow_registration_v1_workflows_registrations__workflow_registration_id__execute_post", "POST", "/v1/workflows/registrations/{workflow_registration_id}/execute", "workflows", "docs", "json", "execution_id,input,wait_for_result,timeout_seconds,custom_tracing_attributes,force_new_trace,traceparent,extensions,task_queue,deployment_name", "", "json", "WorkflowExecutionResponse|WorkflowExecutionSyncResponse", true}, +{"bulk_unarchive_workflows_v1_workflows_unarchive_put", "PUT", "/v1/workflows/unarchive", "workflows", "docs", "json", "workflow_ids", "", "json", "WorkflowBulkUnarchiveResponse", false}, +{"get_workflow_v1_workflows__workflow_identifier__get", "GET", "/v1/workflows/{workflow_identifier}", "workflows", "docs", "none", "", "", "json", "WorkflowGetResponse", false}, +{"update_workflow_v1_workflows__workflow_identifier__put", "PUT", "/v1/workflows/{workflow_identifier}", "workflows", "docs", "json", "display_name,description,available_in_chat_assistant,tags", "", "json", "WorkflowUpdateResponse", false}, +{"archive_workflow_v1_workflows__workflow_identifier__archive_put", "PUT", "/v1/workflows/{workflow_identifier}/archive", "workflows", "docs", "none", "", "", "json", "WorkflowArchiveResponse", false}, +{"execute_workflow_v1_workflows__workflow_identifier__execute_post", "POST", "/v1/workflows/{workflow_identifier}/execute", "workflows", "docs", "json", "execution_id,input,wait_for_result,timeout_seconds,custom_tracing_attributes,force_new_trace,traceparent,extensions,task_queue,deployment_name", "", "json", "WorkflowExecutionResponse|WorkflowExecutionSyncResponse", false}, +{"unarchive_workflow_v1_workflows__workflow_identifier__unarchive_put", "PUT", "/v1/workflows/{workflow_identifier}/unarchive", "workflows", "docs", "none", "", "", "json", "WorkflowUnarchiveResponse", false}, +{"list_deployments_v1_workflows_deployments_get", "GET", "/v1/workflows/deployments", "workflows.deployments", "docs", "none", "", "active_only,is_hardened,workflow_name,created_by,owner,location_types,search,order_by,order,limit,cursor,workspace_id", "json", "DeploymentListResponse", false}, +{"create_deployment_v1_workflows_deployments_post", "POST", "/v1/workflows/deployments", "workflows.deployments", "docs", "json", "name,spec,resources,hardened", "", "json", "ManagedDeploymentResponse", false}, +{"unharden_deployment_v1_workflows_deployments__deployment_id__unharden_post", "POST", "/v1/workflows/deployments/{deployment_id}/unharden", "workflows.deployments", "docs", "none", "", "workspace_id", "none", "", false}, +{"delete_deployment_v1_workflows_deployments__name__delete", "DELETE", "/v1/workflows/deployments/{name}", "workflows.deployments", "docs", "none", "", "", "json", "ManagedDeploymentResponse", false}, +{"get_deployment_v1_workflows_deployments__name__get", "GET", "/v1/workflows/deployments/{name}", "workflows.deployments", "docs", "none", "", "workflow_name", "json", "DeploymentDetailResponse", false}, +{"update_deployment_v1_workflows_deployments__name__patch", "PATCH", "/v1/workflows/deployments/{name}", "workflows.deployments", "docs", "json", "spec,resources", "", "json", "ManagedDeploymentResponse", false}, +{"get_deployment_logs", "GET", "/v1/workflows/deployments/{name}/logs", "workflows.deployments", "docs", "none", "", "log_type,worker_name,workflow_name,after,before,order,cursor,limit", "json", "DeploymentLogSearchResponse", false}, +{"stream_deployment_logs", "GET", "/v1/workflows/deployments/{name}/logs/stream", "workflows.deployments", "docs", "none", "", "worker_name,workflow_name,after,last_event_id", "sse", "object", false}, +{"restart_deployment_v1_workflows_deployments__name__restart_post", "POST", "/v1/workflows/deployments/{name}/restart", "workflows.deployments", "docs", "none", "", "", "json", "ManagedDeploymentResponse", false}, +{"start_deployment_v1_workflows_deployments__name__start_post", "POST", "/v1/workflows/deployments/{name}/start", "workflows.deployments", "docs", "none", "", "", "json", "ManagedDeploymentResponse", false}, +{"stop_deployment_v1_workflows_deployments__name__stop_post", "POST", "/v1/workflows/deployments/{name}/stop", "workflows.deployments", "docs", "none", "", "", "json", "ManagedDeploymentResponse", false}, +{"list_deployment_workers_v1_workflows_deployments__name__workers_get", "GET", "/v1/workflows/deployments/{name}/workers", "workflows.deployments", "docs", "none", "", "worker_status,limit,cursor", "json", "DeploymentWorkerListResponse", false}, +{"batch_cancel_workflow_executions_v1_workflows_executions_cancel_post", "POST", "/v1/workflows/executions/cancel", "workflows.executions", "docs", "json", "execution_ids", "", "json", "BatchExecutionResponse", false}, +{"batch_terminate_workflow_executions_v1_workflows_executions_terminate_post", "POST", "/v1/workflows/executions/terminate", "workflows.executions", "docs", "json", "execution_ids", "", "json", "BatchExecutionResponse", false}, +{"get_workflow_execution_v1_workflows_executions__execution_id__get", "GET", "/v1/workflows/executions/{execution_id}", "workflows.executions", "docs", "none", "", "include_search_keys", "json", "WorkflowExecutionResponse", false}, +{"cancel_workflow_execution_v1_workflows_executions__execution_id__cancel_post", "POST", "/v1/workflows/executions/{execution_id}/cancel", "workflows.executions", "docs", "none", "", "", "none", "", false}, +{"get_workflow_execution_history_v1_workflows_executions__execution_id__history_get", "GET", "/v1/workflows/executions/{execution_id}/history", "workflows.executions", "docs", "none", "", "decode_payloads", "json", "", false}, +{"get_workflow_execution_logs", "GET", "/v1/workflows/executions/{execution_id}/logs", "workflows.executions", "docs", "none", "", "run_id,activity_id,after,before,order,cursor,limit", "json", "ExecutionLogSearchResponse", false}, +{"stream_workflow_execution_logs", "GET", "/v1/workflows/executions/{execution_id}/logs/stream", "workflows.executions", "docs", "none", "", "run_id,activity_id,after,last_event_id", "sse", "object", false}, +{"query_workflow_execution_v1_workflows_executions__execution_id__queries_post", "POST", "/v1/workflows/executions/{execution_id}/queries", "workflows.executions", "docs", "json", "name,input", "", "json", "QueryWorkflowResponse", false}, +{"reset_workflow_v1_workflows_executions__execution_id__reset_post", "POST", "/v1/workflows/executions/{execution_id}/reset", "workflows.executions", "docs", "json", "event_id,reason,exclude_signals,exclude_updates", "", "none", "", false}, +{"signal_workflow_execution_v1_workflows_executions__execution_id__signals_post", "POST", "/v1/workflows/executions/{execution_id}/signals", "workflows.executions", "docs", "json", "name,input", "", "json", "SignalWorkflowResponse", false}, +{"stream_v1_workflows_executions__execution_id__stream_get", "GET", "/v1/workflows/executions/{execution_id}/stream", "workflows.executions", "docs", "none", "", "event_source,last_event_id", "sse", "object", false}, +{"terminate_workflow_execution_v1_workflows_executions__execution_id__terminate_post", "POST", "/v1/workflows/executions/{execution_id}/terminate", "workflows.executions", "docs", "none", "", "", "none", "", false}, +{"get_workflow_execution_trace_events", "GET", "/v1/workflows/executions/{execution_id}/trace/events", "workflows.executions", "docs", "none", "", "merge_same_id_events,include_internal_events", "json", "WorkflowExecutionTraceEventsResponse", false}, +{"get_workflow_execution_trace_info", "GET", "/v1/workflows/executions/{execution_id}/trace/info", "workflows.executions", "docs", "none", "", "", "json", "ExecutionTraceInfoResponse", false}, +{"get_workflow_execution_trace_otel", "GET", "/v1/workflows/executions/{execution_id}/trace/otel", "workflows.executions", "docs", "none", "", "", "json", "WorkflowExecutionTraceOTelResponse", false}, +{"get_workflow_execution_trace_summary", "GET", "/v1/workflows/executions/{execution_id}/trace/summary", "workflows.executions", "docs", "none", "", "", "json", "WorkflowExecutionTraceSummaryResponse", false}, +{"update_workflow_execution_v1_workflows_executions__execution_id__updates_post", "POST", "/v1/workflows/executions/{execution_id}/updates", "workflows.executions", "docs", "json", "name,input", "", "json", "UpdateWorkflowResponse", false}, +{"get_workflow_metrics_v1_workflows__workflow_name__metrics_get", "GET", "/v1/workflows/{workflow_name}/metrics", "workflows.metrics", "docs", "none", "", "start_time,end_time", "json", "WorkflowMetrics", false}, +{"list_runs_v1_workflows_runs_get", "GET", "/v1/workflows/runs", "workflows.runs", "docs", "none", "", "workflow_identifier,root_execution_id,search,status,deployment_name,sort_by,order,start_time_after,start_time_before,end_time_after,end_time_before,user_id,workflow_tags,include_internal,page_size,next_page_token,search_key", "json", "WorkflowExecutionListResponse", false}, +{"get_run_v1_workflows_runs__run_id__get", "GET", "/v1/workflows/runs/{run_id}", "workflows.runs", "docs", "none", "", "", "json", "WorkflowExecutionResponse", false}, +{"get_run_history_v1_workflows_runs__run_id__history_get", "GET", "/v1/workflows/runs/{run_id}/history", "workflows.runs", "docs", "none", "", "decode_payloads", "json", "", false}, +{"get_schedules_v1_workflows_schedules_get", "GET", "/v1/workflows/schedules", "workflows.schedules", "docs", "none", "", "workflow_name,user_id,status,search,page_size,next_page_token", "json", "WorkflowScheduleListResponse", false}, +{"schedule_workflow_v1_workflows_schedules_post", "POST", "/v1/workflows/schedules", "workflows.schedules", "docs", "json", "schedule,workflow_registration_id,workflow_version_id,workflow_identifier,workflow_task_queue,schedule_id,deployment_name", "", "json", "WorkflowScheduleResponse", false}, +{"unschedule_workflow_v1_workflows_schedules__schedule_id__delete", "DELETE", "/v1/workflows/schedules/{schedule_id}", "workflows.schedules", "docs", "none", "", "", "none", "", false}, +{"get_schedule_v1_workflows_schedules__schedule_id__get", "GET", "/v1/workflows/schedules/{schedule_id}", "workflows.schedules", "docs", "none", "", "", "json", "ScheduleDefinitionOutput", false}, +{"update_schedule_v1_workflows_schedules__schedule_id__patch", "PATCH", "/v1/workflows/schedules/{schedule_id}", "workflows.schedules", "docs", "json", "schedule", "", "json", "WorkflowScheduleResponse", false}, +{"pause_schedule_v1_workflows_schedules__schedule_id__pause_post", "POST", "/v1/workflows/schedules/{schedule_id}/pause", "workflows.schedules", "docs", "json", "note", "", "none", "", false}, +{"resume_schedule_v1_workflows_schedules__schedule_id__resume_post", "POST", "/v1/workflows/schedules/{schedule_id}/resume", "workflows.schedules", "docs", "json", "note", "", "none", "", false}, +{"trigger_schedule_v1_workflows_schedules__schedule_id__trigger_post", "POST", "/v1/workflows/schedules/{schedule_id}/trigger", "workflows.schedules", "docs", "json", "overlap", "", "none", "", false}, +{"connector_create_or_update_organization_credentials_v1", "POST", "/v1/connectors/{connector_id_or_name}/organization/credentials", "beta.connectors", "pd", "json", "name,is_default,credentials", "", "json", "MessageResponse", false}, +{"connector_delete_organization_credentials_v1", "DELETE", "/v1/connectors/{connector_id_or_name}/organization/credentials/{credentials_name}", "beta.connectors", "pd", "none", "", "", "json", "MessageResponse", false}, +{"connector_create_or_update_user_credentials_v1", "POST", "/v1/connectors/{connector_id_or_name}/user/credentials", "beta.connectors", "pd", "json", "name,is_default,credentials", "", "json", "MessageResponse", false}, +{"connector_delete_user_credentials_v1", "DELETE", "/v1/connectors/{connector_id_or_name}/user/credentials/{credentials_name}", "beta.connectors", "pd", "none", "", "", "json", "MessageResponse", false}, +{"connector_create_or_update_workspace_credentials_v1", "POST", "/v1/connectors/{connector_id_or_name}/workspace/credentials", "beta.connectors", "pd", "json", "name,is_default,credentials", "", "json", "MessageResponse", false}, +{"connector_delete_workspace_credentials_v1", "DELETE", "/v1/connectors/{connector_id_or_name}/workspace/credentials/{credentials_name}", "beta.connectors", "pd", "none", "", "", "json", "MessageResponse", false}, +{"get_campaigns_v1_observability_campaigns_get", "GET", "/v1/observability/campaigns", "beta.observability.campaigns", "pd", "none", "", "page_size,page,q", "json", "CampaignPreviews", false}, +{"create_campaign_v1_observability_campaigns_post", "POST", "/v1/observability/campaigns", "beta.observability.campaigns", "pd", "json", "search_params,judge_id,name,description,max_nb_events", "", "json", "CampaignPreview", false}, +{"delete_campaign_v1_observability_campaigns__campaign_id__delete", "DELETE", "/v1/observability/campaigns/{campaign_id}", "beta.observability.campaigns", "pd", "none", "", "", "none", "", false}, +{"get_campaign_by_id_v1_observability_campaigns__campaign_id__get", "GET", "/v1/observability/campaigns/{campaign_id}", "beta.observability.campaigns", "pd", "none", "", "", "json", "CampaignPreview", false}, +{"get_campaign_selected_events_v1_observability_campaigns__campaign_id__selected_events_get", "GET", "/v1/observability/campaigns/{campaign_id}/selected-events", "beta.observability.campaigns", "pd", "none", "", "page_size,page", "json", "CampaignSelectedEvents", false}, +{"get_campaign_status_by_id_v1_observability_campaigns__campaign_id__status_get", "GET", "/v1/observability/campaigns/{campaign_id}/status", "beta.observability.campaigns", "pd", "none", "", "", "json", "CampaignStatus", false}, +{"get_chat_completion_events_v1_observability_chat_completion_events_search_post", "POST", "/v1/observability/chat-completion-events/search", "beta.observability.chat_completion_events", "pd", "json", "search_params,extra_fields", "page_size,cursor", "json", "ChatCompletionEvents", false}, +{"get_chat_completion_event_ids_v1_observability_chat_completion_events_search_ids_post", "POST", "/v1/observability/chat-completion-events/search-ids", "beta.observability.chat_completion_events", "pd", "json", "search_params,extra_fields", "", "json", "ChatCompletionEventIds", false}, +{"get_chat_completion_event_v1_observability_chat_completion_events__event_id__get", "GET", "/v1/observability/chat-completion-events/{event_id}", "beta.observability.chat_completion_events", "pd", "none", "", "", "json", "ChatCompletionEvent", false}, +{"judge_chat_completion_event_v1_observability_chat_completion_events__event_id__live_judging_post", "POST", "/v1/observability/chat-completion-events/{event_id}/live-judging", "beta.observability.chat_completion_events", "pd", "json", "judge_definition", "", "json", "JudgeOutput", false}, +{"get_similar_chat_completion_events_v1_observability_chat_completion_events__event_id__similar_events_get", "GET", "/v1/observability/chat-completion-events/{event_id}/similar-events", "beta.observability.chat_completion_events", "pd", "none", "", "", "json", "ChatCompletionEvents", false}, +{"get_chat_completion_fields_v1_observability_chat_completion_fields_get", "GET", "/v1/observability/chat-completion-fields", "beta.observability.chat_completion_events.fields", "pd", "none", "", "", "json", "ChatCompletionFields", false}, +{"get_chat_completion_field_options_v1_observability_chat_completion_fields__field_name__options_get", "GET", "/v1/observability/chat-completion-fields/{field_name}/options", "beta.observability.chat_completion_events.fields", "pd", "none", "", "operator", "json", "ChatCompletionFieldOptions", false}, +{"get_chat_completion_field_options_counts_v1_observability_chat_completion_fields__field_name__options_counts_post", "POST", "/v1/observability/chat-completion-fields/{field_name}/options-counts", "beta.observability.chat_completion_events.fields", "pd", "json", "filter_params", "", "json", "FieldOptionCounts", false}, +{"post_dataset_records_from_campaign_v1_observability_datasets__dataset_id__imports_from_campaign_post", "POST", "/v1/observability/datasets/{dataset_id}/imports/from-campaign", "beta.observability.datasets", "pd", "json", "campaign_id", "", "json", "DatasetImportTask", false}, +{"post_dataset_records_from_explorer_v1_observability_datasets__dataset_id__imports_from_explorer_post", "POST", "/v1/observability/datasets/{dataset_id}/imports/from-explorer", "beta.observability.datasets", "pd", "json", "completion_event_ids", "", "json", "DatasetImportTask", false}, +{"get_worker_info_v1_workflows_workers_whoami_get", "GET", "/v1/workflows/workers/whoami", "beta.workflows.workers", "pd", "none", "", "", "json", "WorkerInfo", false}, +{"jobs_api_routes_fine_tuning_get_fine_tuning_jobs", "GET", "/v1/fine_tuning/jobs", "deprecated.fine-tuning", "pd", "none", "", "page,page_size,model,created_after,created_before,created_by_me,status,wandb_project,wandb_name,suffix", "json", "JobsOut", false}, +{"jobs_api_routes_fine_tuning_create_fine_tuning_job", "POST", "/v1/fine_tuning/jobs", "deprecated.fine-tuning", "pd", "json", "model,training_files,validation_files,suffix,integrations,auto_start,invalid_sample_skip_percentage,job_type,hyperparameters,repositories,classifier_targets", "dry_run", "json", "CompletionJobOut|ClassifierJobOut|LegacyJobMetadataOut", false}, +{"jobs_api_routes_fine_tuning_get_fine_tuning_job", "GET", "/v1/fine_tuning/jobs/{job_id}", "deprecated.fine-tuning", "pd", "none", "", "", "json", "CompletionDetailedJobOut|ClassifierDetailedJobOut", false}, +{"jobs_api_routes_fine_tuning_cancel_fine_tuning_job", "POST", "/v1/fine_tuning/jobs/{job_id}/cancel", "deprecated.fine-tuning", "pd", "none", "", "", "json", "CompletionDetailedJobOut|ClassifierDetailedJobOut", false}, +{"jobs_api_routes_fine_tuning_start_fine_tuning_job", "POST", "/v1/fine_tuning/jobs/{job_id}/start", "deprecated.fine-tuning", "pd", "none", "", "", "json", "CompletionDetailedJobOut|ClassifierDetailedJobOut", false}, +// CALLS-END diff --git a/src/vendors/mistral/caps.h b/src/vendors/mistral/caps.h new file mode 100644 index 0000000..73375ed --- /dev/null +++ b/src/vendors/mistral/caps.h @@ -0,0 +1,48 @@ +#pragma once + +// Static ceilings for the Mistral module. Never autotuned. Numbers marked +// "spec" come from the OpenAPI documents (SPEC.md ## Sources); the rest are +// our own hard caps. + +#include + +namespace molo::mistral { + +struct MistralCaps { + // From the OpenAPI document. + static constexpr double kTemperatureMax = 1.5; // spec: 0..1.5 + static constexpr double kPenaltyMax = 2.0; // spec: presence/frequency -2..2, repetition (0,2] + static constexpr int kBatchMaxInlineRequests = 10000; // spec: requests maxItems + static constexpr int kBatchTimeoutMinHours = 1; // spec: 1..168 + static constexpr int kBatchTimeoutMaxHours = 168; + static constexpr int kLibraryChunkMin = 256; // spec: 256..32768 + static constexpr int kLibraryChunkMax = 32768; + static constexpr std::size_t kTranscriptionFileUrlChars = 2083; // spec: file_url maxLength + // Our own caps. + static constexpr std::size_t kMaxMessages = 4096; + static constexpr std::size_t kMaxTools = 128; + static constexpr std::size_t kMaxStopSequences = 16; + static constexpr int kMaxChoices = 16; // n + static constexpr int kMaxTopLogprobs = 20; + static constexpr std::size_t kMaxStreamEvents = 200000; + static constexpr std::size_t kMaxModelChars = 160; // fine-tuned ids are long + static constexpr std::size_t kMaxEmbeddingInputs = 512; + static constexpr std::size_t kMaxEmbeddingChars = 1u << 20; // per input + static constexpr std::size_t kMaxModerationInputs = 256; + static constexpr std::size_t kMaxClassifyInputs = 256; + static constexpr std::size_t kMaxUploadBytes = 512u * 1024u * 1024u; // Files API (docs: 512 MB) + static constexpr std::size_t kMaxAudioBytes = 128u * 1024u * 1024u; + static constexpr std::size_t kMaxOcrPages = 1000; + static constexpr std::size_t kMaxFilenameBytes = 512; + static constexpr int kPageSizeMax = 100; + static constexpr std::size_t kMaxWalkPages = 20; // list walks + static constexpr std::size_t kMaxItemsReturned = 5000; + static constexpr std::size_t kMaxModelsReturned = 1024; + static constexpr int kSignedUrlExpiryMaxHours = 168; + static constexpr std::size_t kMaxIdChars = 256; + static constexpr std::size_t kMaxBodyFields = 64; // generic Operations + static constexpr std::size_t kMaxToolRounds = 16; + static constexpr std::size_t kMaxMetadataBytes = 16u * 1024u; +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/chat.cpp b/src/vendors/mistral/chat.cpp new file mode 100644 index 0000000..ca0dc9e --- /dev/null +++ b/src/vendors/mistral/chat.cpp @@ -0,0 +1,90 @@ +#include "vendors/mistral/chat.h" + +#include "vendors/mistral/calls.h" +#include "vendors/mistral/caps.h" +#include "vendors/mistral/ops.h" +#include "vendors/mistral/rules.h" + +namespace molo::mistral { + +namespace { +ChatResult run_stream(llm::ApiClient& client, const char* op, const Json& body, const Chat::DeltaSink& on_delta) { + ChatStreamAccumulator acc; + bool stopped = false; + client.stream(Calls::render(op), body, [&](const llm::SseEvent& ev) { + StreamDelta d = acc.feed(ev.data); + if (acc.events() > MistralCaps::kMaxStreamEvents) { + stopped = true; + return false; + } + if (on_delta && !on_delta(d)) { + stopped = true; + return false; + } + return true; + }); + if (!stopped && !acc.done()) + throw llm::ApiError("mistral", 502, "STREAM_TRUNCATED", "stream ended without a finish_reason"); + return acc.result(); +} +} // namespace + +const char* Chat::op_for(const ChatParams& params) { + return params.for_agent() ? Op::kAgentsCompletion : Op::kChat; +} + +ChatResult Chat::create(const ChatParams& params) { + Json body = params.to_json(false); + return ChatResult::parse(call(op_for(params), {}, &body)); +} + +ChatResult Chat::stream(const ChatParams& params, const DeltaSink& on_delta) { + Json body = params.to_json(true); + return run_stream(*client_, op_for(params), body, on_delta); +} + +void FimParams::validate() const { + Rules::model(model); + Rules::require(!prompt.empty(), "prompt must not be empty"); + if (temperature) Rules::range(*temperature, 0.0, MistralCaps::kTemperatureMax, "temperature"); + if (top_p) Rules::require(*top_p > 0.0 && *top_p <= 1.0, "top_p must be greater than 0 and at most 1"); + if (max_tokens) Rules::require(*max_tokens >= 0, "max_tokens must not be negative"); + if (min_tokens) Rules::require(*min_tokens >= 0, "min_tokens must not be negative"); + if (random_seed) Rules::require(*random_seed >= 0, "random_seed must not be negative"); + Rules::stop(stop); + if (!metadata.is_null()) Rules::json_object(metadata, "metadata"); +} + +Json FimParams::to_json(bool stream) const { + validate(); + Json o = Json::object(); + o.set("model", Json(model)); + o.set("prompt", Json(prompt)); + if (!suffix.empty()) o.set("suffix", Json(suffix)); + if (stream) o.set("stream", Json(true)); + if (temperature) o.set("temperature", Json(*temperature)); + if (top_p) o.set("top_p", Json(*top_p)); + if (max_tokens) o.set("max_tokens", Json(*max_tokens)); + if (min_tokens) o.set("min_tokens", Json(*min_tokens)); + if (random_seed) o.set("random_seed", Json(*random_seed)); + if (!stop.empty()) { + Json s = Json::array(); + for (const std::string& x : stop) s.push(Json(x)); + o.set("stop", s); + } + if (!prompt_cache_key.empty()) o.set("prompt_cache_key", Json(prompt_cache_key)); + if (!metadata.is_null()) o.set("metadata", metadata); + return o; +} + +ChatResult Fim::create(const FimParams& params) { + Json body = params.to_json(false); + return ChatResult::parse(call(Op::kFim, {}, &body)); +} + +ChatResult Fim::stream(const FimParams& params, const Chat::DeltaSink& on_delta) { + Json body = params.to_json(true); + return run_stream(*client_, Op::kFim, body, on_delta); +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/chat.h b/src/vendors/mistral/chat.h new file mode 100644 index 0000000..ac6d342 --- /dev/null +++ b/src/vendors/mistral/chat.h @@ -0,0 +1,54 @@ +#pragma once + +// Chat completions, agents completions and FIM completions. Streaming uses +// the shared SSE decoder; a stream that ends without a finish_reason (and was +// not stopped by the sink) throws 502 STREAM_TRUNCATED. + +#include +#include +#include +#include + +#include "vendors/mistral/chat_params.h" +#include "vendors/mistral/chat_result.h" + +namespace molo::mistral { + +class Chat : public MistralService { + public: + explicit Chat(std::shared_ptr client) : MistralService(std::move(client)) {} + + // params.agent_id set: POST /v1/agents/completions, else POST /v1/chat/completions. + ChatResult create(const ChatParams& params); + + using DeltaSink = std::function; // false: stop early + ChatResult stream(const ChatParams& params, const DeltaSink& on_delta); + + static const char* op_for(const ChatParams& params); +}; + +struct FimParams { + static constexpr const char* kDefaultModel = "codestral-latest"; + std::string model = kDefaultModel; + std::string prompt; // text before the gap + std::string suffix; // text after the gap + std::optional temperature; // 0..1.5 + std::optional top_p; // (0,1] + std::optional max_tokens; + std::optional min_tokens; + std::optional random_seed; + std::vector stop; + std::string prompt_cache_key; + Json metadata; + void validate() const; + Json to_json(bool stream = false) const; +}; + +class Fim : public MistralService { + public: + explicit Fim(std::shared_ptr client) : MistralService(std::move(client)) {} + ChatResult create(const FimParams& params); + ChatResult stream(const FimParams& params, const Chat::DeltaSink& on_delta); +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/chat_model.cpp b/src/vendors/mistral/chat_model.cpp new file mode 100644 index 0000000..f446a92 --- /dev/null +++ b/src/vendors/mistral/chat_model.cpp @@ -0,0 +1,171 @@ +#include "vendors/mistral/chat_model.h" + +#include "security/crypto/Base64.h" +#include "vendors/mistral/platform.h" +#include "vendors/mistral/rules.h" + +namespace molo::mistral { + +MistralChatModel::MistralChatModel(std::shared_ptr chat, std::string default_model) + : chat_(std::move(chat)), default_model_(std::move(default_model)) {} + +std::shared_ptr MistralChatModel::from_env(const std::string& default_model) { + Platform p = Platform::from_env(); + return std::make_shared(std::make_shared(p.client), default_model); +} + +namespace { + +Json image_chunk_of(const llm::ImageData& img) { + if (!img.url.empty()) return Messages::image_chunk(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_chunk("data:" + img.mime + ";base64," + security::crypto::Base64::encode(img.bytes)); +} + +std::string call_arguments(const llm::ToolCall& call) { + return call.arguments.is_null() ? "{}" : call.arguments.dump(); +} + +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 chunks; + for (const llm::ContentPart& p : m.parts) { + if (!p.text.empty()) chunks.push_back(Messages::text_chunk(p.text)); + if (p.image) chunks.push_back(image_chunk_of(*p.image)); + } + params.add(Messages::user_chunks(chunks)); + return; + } + case llm::Role::kAssistant: { + Json calls = Json::array(); + for (const llm::ContentPart& p : m.parts) { + if (!p.tool_call) continue; + Json fn = Json::object(); + fn.set("name", Json(p.tool_call->name)); + fn.set("arguments", Json(call_arguments(*p.tool_call))); + Json c = Json::object(); + c.set("id", Json(p.tool_call->id)); + c.set("type", Json("function")); + c.set("function", fn); + calls.push(c); + } + if (calls.as_array().empty()) + params.add(Messages::assistant(m.text())); + else + params.add(Messages::assistant_calls(m.text(), calls)); + 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, m.name, p.tool_result->content)); + any = true; + } + Rules::require(any, "a tool message needs a tool result part"); + return; + } + } +} + +std::string neutral_finish(const std::string& f) { return f == "model_length" ? "length" : f; } + +llm::Usage neutral_usage(const TokenUsage& u) { + llm::Usage x; + x.input_tokens = u.prompt_tokens; + x.output_tokens = u.completion_tokens; + x.total_tokens = u.total_tokens; + return x; +} + +} // namespace + +ChatParams MistralChatModel::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); + } + p.tool_choice = r.tool_choice; + p.max_tokens = r.max_tokens; + p.temperature = r.temperature; + p.top_p = r.top_p; + p.stop = r.stop; + if (r.json_mode) p.format = ResponseFormat::kJsonObject; + if (!r.response_schema.is_null()) { + Rules::require(!r.json_mode, "json_mode and response_schema are exclusive"); + p.format = ResponseFormat::kJsonSchema; + p.schema_name = "response"; + p.schema = r.response_schema; + p.schema_strict = true; + } + p.reasoning_effort = r.reasoning_effort; + p.extra = r.extra; + return p; +} + +llm::ChatResponse MistralChatModel::to_response(const ChatResult& result) { + const ChatChoice& c = result.first(); + llm::ChatResponse out; + out.id = result.id; + out.model = result.model; + out.finish_reason = neutral_finish(c.finish_reason); + out.usage = neutral_usage(result.usage); + out.raw = result.raw; + out.message.role = llm::Role::kAssistant; + if (!c.reasoning.empty()) { + llm::ContentPart r; + r.reasoning = c.reasoning; + out.message.parts.push_back(r); + } + if (!c.content.empty()) out.message.parts.push_back(llm::ContentPart::of_text(c.content)); + for (const ToolCallRecord& t : c.tool_calls) { + llm::ToolCall call; + call.id = t.id; + call.name = t.name; + call.arguments = t.arguments_valid() ? t.parsed() : [&] { + Json o = Json::object(); + o.set("_invalid_json", Json(t.arguments)); + return o; + }(); + out.message.parts.push_back(llm::ContentPart::of_call(call)); + } + return out; +} + +llm::ChatResponse MistralChatModel::generate(const llm::ChatRequest& request) { + return to_response(chat_->create(to_params(request))); +} + +void MistralChatModel::stream(const llm::ChatRequest& request, + const std::function& on_chunk) { + chat_->stream(to_params(request), [&](const StreamDelta& d) { + llm::ChatChunk c; + c.text_delta = d.text; + c.reasoning_delta = d.reasoning; + c.tool_call_id = d.tool_call_id; + c.tool_call_name = d.tool_call_name; + c.tool_args_delta = d.tool_args; + c.finish_reason = neutral_finish(d.finish_reason); + return on_chunk ? on_chunk(c) : true; + }); +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/chat_model.h b/src/vendors/mistral/chat_model.h new file mode 100644 index 0000000..2f86c7f --- /dev/null +++ b/src/vendors/mistral/chat_model.h @@ -0,0 +1,46 @@ +#pragma once + +// MistralChatModel: the llm::ChatModel seam over /v1/chat/completions. +// +// Mapping notes (each one is a decision, not a guess): +// - ChatRequest.system becomes a leading system message. +// - Images: url as given, bytes as a data: URL; both become image_url chunks. +// - Tool calls: assistant parts become tool_calls (arguments dumped to JSON +// text); tool results become role "tool" messages with tool_call_id. +// - tool_choice: "" / auto / none pass through, "required" stays "required", +// anything else names a function. +// - json_mode -> response_format json_object; response_schema -> json_schema +// (name "response", strict true). +// - reasoning_effort passes through (validated: none..xhigh). +// - Thinking chunks (Magistral) become ContentPart.reasoning of the reply. +// - finish_reason: model_length is reported as "length"; the rest pass through +// (stop, length, tool_calls, error). +// - ChatRequest.extra is merged into the body last. + +#include + +#include "llm/chat_types.h" +#include "vendors/mistral/chat.h" + +namespace molo::mistral { + +class MistralChatModel : public llm::ChatModel { + public: + explicit MistralChatModel(std::shared_ptr chat, std::string default_model = ChatParams::kDefaultModel); + static std::shared_ptr from_env(const std::string& default_model = ChatParams::kDefaultModel); + + std::string vendor() const override { return "mistral"; } + llm::ChatResponse generate(const llm::ChatRequest& request) override; + void stream(const llm::ChatRequest& request, + const std::function& on_chunk) override; + + // The wire parameters a neutral request maps to (also used by tests). + ChatParams to_params(const llm::ChatRequest& request) const; + static llm::ChatResponse to_response(const ChatResult& result); + + private: + std::shared_ptr chat_; + std::string default_model_; +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/chat_params.cpp b/src/vendors/mistral/chat_params.cpp new file mode 100644 index 0000000..3712ab2 --- /dev/null +++ b/src/vendors/mistral/chat_params.cpp @@ -0,0 +1,223 @@ +#include "vendors/mistral/chat_params.h" + +#include "vendors/mistral/caps.h" +#include "vendors/mistral/rules.h" + +namespace molo::mistral { + +Json ToolDef::to_json() const { + Json fn = Json::object(); + fn.set("name", Json(name)); + if (!description.empty()) fn.set("description", Json(description)); + fn.set("parameters", parameters); + if (strict) fn.set("strict", Json(true)); + Json t = Json::object(); + t.set("type", Json("function")); + t.set("function", fn); + return t; +} + +namespace { +Json msg(const char* role, Json content) { + Json m = Json::object(); + m.set("role", Json(role)); + m.set("content", std::move(content)); + return m; +} +Json chunk(const char* type) { + Json c = Json::object(); + c.set("type", Json(type)); + return c; +} +} // namespace + +Json Messages::system(const std::string& text) { return msg("system", Json(text)); } +Json Messages::user(const std::string& text) { return msg("user", Json(text)); } +Json Messages::user_chunks(const std::vector& chunks) { + Rules::require(!chunks.empty(), "user message needs at least one chunk"); + Json a = Json::array(); + for (const Json& c : chunks) a.push(c); + return msg("user", a); +} +Json Messages::assistant(const std::string& text) { return msg("assistant", Json(text)); } +Json Messages::assistant_calls(const std::string& text, const Json& tool_calls) { + Json m = msg("assistant", Json(text)); + Rules::require(tool_calls.is_array(), "tool_calls must be an array"); + m.set("tool_calls", tool_calls); + return m; +} +Json Messages::assistant_prefix(const std::string& text) { + Rules::require(!text.empty(), "prefix must not be empty"); + Json m = msg("assistant", Json(text)); + m.set("prefix", Json(true)); + return m; +} +Json Messages::tool(const std::string& call_id, const std::string& name, const std::string& content) { + Rules::require(!call_id.empty(), "tool_call_id must not be empty"); + Json m = msg("tool", Json(content)); + m.set("tool_call_id", Json(call_id)); + if (!name.empty()) m.set("name", Json(name)); + return m; +} +Json Messages::text_chunk(const std::string& text) { + Json c = chunk("text"); + c.set("text", Json(text)); + return c; +} +Json Messages::image_chunk(const std::string& url) { + Rules::image_url(url); + Json c = chunk("image_url"); + c.set("image_url", Json(url)); + return c; +} +Json Messages::document_chunk(const std::string& url, const std::string& name) { + Rules::http_url(url, 8192, "document url"); + Json c = chunk("document_url"); + c.set("document_url", Json(url)); + if (!name.empty()) c.set("document_name", Json(name)); + return c; +} +Json Messages::file_chunk(const std::string& file_id) { + Rules::require(llm::Service::valid_id(file_id), "bad file id"); + Json c = chunk("file"); + c.set("file_id", Json(file_id)); + return c; +} + +ChatParams ChatParams::prompt(const std::string& text) { + ChatParams p; + p.add(Messages::user(text)); + return p; +} + +ChatParams& ChatParams::add(Json message) { + Rules::require(messages.size() < MistralCaps::kMaxMessages, "too many messages (max 4096)"); + Rules::require(message.is_object() && message.find("role"), "message needs a role"); + messages.push_back(std::move(message)); + return *this; +} + +void ChatParams::validate() const { + if (for_agent()) { + Rules::require(llm::Service::valid_id(agent_id), "bad agent id"); + Rules::require(!temperature && !top_p && !safe_prompt, + "agents completions do not take temperature, top_p or safe_prompt"); + } else { + Rules::model(model); + if (temperature) Rules::range(*temperature, 0.0, MistralCaps::kTemperatureMax, "temperature"); + if (top_p) Rules::require(*top_p > 0.0 && *top_p <= 1.0, "top_p must be greater than 0 and at most 1"); + } + Rules::require(!messages.empty(), "messages must not be empty"); + Rules::require(messages.size() <= MistralCaps::kMaxMessages, "too many messages (max 4096)"); + if (presence_penalty) Rules::range(*presence_penalty, -MistralCaps::kPenaltyMax, MistralCaps::kPenaltyMax, "presence_penalty"); + if (frequency_penalty) Rules::range(*frequency_penalty, -MistralCaps::kPenaltyMax, MistralCaps::kPenaltyMax, "frequency_penalty"); + if (repetition_penalty) + Rules::require(*repetition_penalty > 0.0 && *repetition_penalty <= MistralCaps::kPenaltyMax, + "repetition_penalty must be greater than 0 and at most 2"); + if (max_tokens) Rules::require(*max_tokens >= 0, "max_tokens must not be negative"); + if (min_tokens) Rules::require(*min_tokens >= 0, "min_tokens must not be negative"); + if (random_seed) Rules::require(*random_seed >= 0, "random_seed must not be negative"); + if (top_k) Rules::require(*top_k >= -1, "top_k must be -1 or more"); + if (n) Rules::between(*n, 1, MistralCaps::kMaxChoices, "n"); + if (top_logprobs) Rules::between(*top_logprobs, 0, MistralCaps::kMaxTopLogprobs, "top_logprobs"); + Rules::stop(stop); + Rules::require(tools.size() <= MistralCaps::kMaxTools, "too many tools (max 128)"); + for (const ToolDef& t : tools) { + Rules::tool_name(t.name); + Rules::json_object(t.parameters, "tool parameters"); + } + if (!tool_choice.empty() && tool_choice != "auto" && tool_choice != "none" && tool_choice != "any" && + tool_choice != "required") + Rules::tool_name(tool_choice); + if (format == ResponseFormat::kJsonSchema) { + Rules::tool_name(schema_name); + Rules::json_object(schema, "response schema"); + } + if (!reasoning_effort.empty()) + Rules::one_of(reasoning_effort, {"none", "minimal", "low", "medium", "high", "xhigh"}, "reasoning_effort"); + if (!prompt_mode.empty()) Rules::one_of(prompt_mode, {"reasoning"}, "prompt_mode"); + if (!service_tier.empty()) Rules::one_of(service_tier, {"auto", "standard_only"}, "service_tier"); + if (!metadata.is_null()) Rules::json_object(metadata, "metadata"); + if (!guardrails.is_null()) Rules::require(guardrails.is_array(), "guardrails must be an array"); + if (!extra.is_null()) Rules::json_object(extra, "extra"); +} + +Json ChatParams::to_json(bool stream) const { + validate(); + Json o = Json::object(); + if (for_agent()) + o.set("agent_id", Json(agent_id)); + else + o.set("model", Json(model)); + Json msgs = Json::array(); + for (const Json& m : messages) msgs.push(m); + o.set("messages", msgs); + if (stream) o.set("stream", Json(true)); + if (temperature) o.set("temperature", Json(*temperature)); + if (top_p) o.set("top_p", Json(*top_p)); + if (presence_penalty) o.set("presence_penalty", Json(*presence_penalty)); + if (frequency_penalty) o.set("frequency_penalty", Json(*frequency_penalty)); + if (repetition_penalty) o.set("repetition_penalty", Json(*repetition_penalty)); + if (max_tokens) o.set("max_tokens", Json(*max_tokens)); + if (min_tokens) o.set("min_tokens", Json(*min_tokens)); + if (random_seed) o.set("random_seed", Json(*random_seed)); + if (top_k) o.set("top_k", Json(*top_k)); + if (n) o.set("n", Json(*n)); + if (logprobs) o.set("logprobs", Json(*logprobs)); + if (top_logprobs) o.set("top_logprobs", Json(*top_logprobs)); + if (safe_prompt) o.set("safe_prompt", Json(*safe_prompt)); + if (parallel_tool_calls) o.set("parallel_tool_calls", Json(*parallel_tool_calls)); + if (!stop.empty()) { + Json s = Json::array(); + for (const std::string& x : stop) s.push(Json(x)); + o.set("stop", s); + } + if (!tools.empty()) { + Json t = Json::array(); + for (const ToolDef& d : tools) t.push(d.to_json()); + o.set("tools", t); + } + if (!tool_choice.empty()) { + if (tool_choice == "auto" || tool_choice == "none" || tool_choice == "any" || tool_choice == "required") { + o.set("tool_choice", Json(tool_choice)); + } else { + Json fn = Json::object(); + fn.set("name", Json(tool_choice)); + Json tc = Json::object(); + tc.set("type", Json("function")); + tc.set("function", fn); + o.set("tool_choice", tc); + } + } + if (format != ResponseFormat::kDefault) { + Json rf = Json::object(); + if (format == ResponseFormat::kText) rf.set("type", Json("text")); + if (format == ResponseFormat::kJsonObject) rf.set("type", Json("json_object")); + if (format == ResponseFormat::kJsonSchema) { + rf.set("type", Json("json_schema")); + Json js = Json::object(); + js.set("name", Json(schema_name)); + js.set("schema", schema); + if (schema_strict) js.set("strict", Json(true)); + rf.set("json_schema", js); + } + o.set("response_format", rf); + } + if (!reasoning_effort.empty()) o.set("reasoning_effort", Json(reasoning_effort)); + if (!prompt_mode.empty()) o.set("prompt_mode", Json(prompt_mode)); + if (!prompt_cache_key.empty()) o.set("prompt_cache_key", Json(prompt_cache_key)); + if (!service_tier.empty()) o.set("service_tier", Json(service_tier)); + if (!prediction.empty()) { + Json p = Json::object(); + p.set("type", Json("content")); + p.set("content", Json(prediction)); + o.set("prediction", p); + } + if (!metadata.is_null()) o.set("metadata", metadata); + if (!guardrails.is_null()) o.set("guardrails", guardrails); + if (extra.is_object()) + for (const auto& [k, v] : extra.as_object()) o.set(k, v); + return o; +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/chat_params.h b/src/vendors/mistral/chat_params.h new file mode 100644 index 0000000..7f5155e --- /dev/null +++ b/src/vendors/mistral/chat_params.h @@ -0,0 +1,90 @@ +#pragma once + +// Request side of chat completions (POST /v1/chat/completions) and agents +// completions (POST /v1/agents/completions). One ChatParams serves both: with +// agent_id set it targets the agents endpoint, which does not take model, +// temperature, top_p or safe_prompt (validate() rejects them loudly instead of +// dropping them). Field names are the OpenAPI ChatCompletionRequest / +// AgentsCompletionRequest properties; tests prove every emitted key is a +// column of the call table. + +#include +#include +#include + +#include "vendors/mistral/types.h" + +namespace molo::mistral { + +struct ToolDef { + std::string name; + std::string description; + Json parameters = Json::object(); // JSON Schema + bool strict = false; + Json to_json() const; +}; + +enum class ResponseFormat { kDefault, kText, kJsonObject, kJsonSchema }; + +// Message and content-chunk builders (OpenAPI SystemMessage, UserMessage, +// AssistantMessage, ToolMessage and the ContentChunk variants). +struct Messages { + static Json system(const std::string& text); + static Json user(const std::string& text); + static Json user_chunks(const std::vector& chunks); + static Json assistant(const std::string& text); + // tool calls as returned by the model: [{id,type:function,function:{name,arguments}}] + static Json assistant_calls(const std::string& text, const Json& tool_calls); + // prefix:true makes the model continue this assistant text. + static Json assistant_prefix(const std::string& text); + static Json tool(const std::string& call_id, const std::string& name, const std::string& content); + static Json text_chunk(const std::string& text); + static Json image_chunk(const std::string& url); + static Json document_chunk(const std::string& url, const std::string& name = ""); + static Json file_chunk(const std::string& file_id); +}; + +struct ChatParams { + static constexpr const char* kDefaultModel = "mistral-small-latest"; + + std::string model = kDefaultModel; + std::string agent_id; // set: agents completions + std::vector messages; + std::optional temperature; // 0..1.5 + std::optional top_p; // (0,1] + std::optional presence_penalty; // -2..2 + std::optional frequency_penalty; // -2..2 + std::optional repetition_penalty; // (0,2] + std::optional max_tokens; // >= 0 + std::optional min_tokens; // >= 0 + std::optional random_seed; // >= 0 + std::optional top_k; // >= -1 + std::optional n; // 1..16 + std::optional top_logprobs; + std::optional logprobs; + std::optional safe_prompt; + std::optional parallel_tool_calls; + std::vector stop; + std::vector tools; + std::string tool_choice; // "", auto, none, any, required, or a function name + ResponseFormat format = ResponseFormat::kDefault; + std::string schema_name; // kJsonSchema + Json schema = Json::object(); + bool schema_strict = false; + std::string reasoning_effort; // "", none, minimal, low, medium, high, xhigh + std::string prompt_mode; // "", reasoning + std::string prompt_cache_key; + std::string service_tier; // "", auto, standard_only + std::string prediction; // predicted output text + Json metadata; // null or object + Json guardrails; // null or array (passed through) + Json extra; // null or object merged last (new fields) + + static ChatParams prompt(const std::string& text); + ChatParams& add(Json message); + bool for_agent() const { return !agent_id.empty(); } + void validate() const; + Json to_json(bool stream = false) const; +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/chat_result.cpp b/src/vendors/mistral/chat_result.cpp new file mode 100644 index 0000000..0506b39 --- /dev/null +++ b/src/vendors/mistral/chat_result.cpp @@ -0,0 +1,174 @@ +#include "vendors/mistral/chat_result.h" + +#include "vendors/mistral/caps.h" + +namespace molo::mistral { + +bool ToolCallRecord::arguments_valid() const { + try { + return Json::parse(arguments).is_object(); + } catch (const std::exception&) { + return false; + } +} + +Json ToolCallRecord::parsed() const { + try { + Json j = Json::parse(arguments); + if (j.is_object()) return j; + } catch (const std::exception&) { + } + return Json::object(); +} + +namespace { +std::string arguments_text(const Json* a) { + if (!a) return ""; + if (a->is_string()) return a->as_string(); + if (a->is_object()) return a->dump(); + return ""; +} + +ToolCallRecord parse_call(const Json& c) { + ToolCallRecord r; + r.id = J::str(c, "id"); + if (const Json* fn = J::obj(c, "function")) { + r.name = J::str(*fn, "name"); + r.arguments = arguments_text(fn->find("arguments")); + } + return r; +} +} // namespace + +Json ChatChoice::history_message() const { + if (message.is_object()) { + Json m = Json::object(); + m.set("role", Json("assistant")); + m.set("content", message.find("content") ? *message.find("content") : Json(content)); + if (const Json* tc = J::arr(message, "tool_calls"); tc && !tc->as_array().empty()) m.set("tool_calls", *tc); + return m; + } + Json m = Json::object(); + m.set("role", Json("assistant")); + m.set("content", Json(content)); + return m; +} + +const ChatChoice& ChatResult::first() const { + if (choices.empty()) throw llm::ApiError("mistral", 502, "EMPTY_RESPONSE", "response has no choices"); + return choices.front(); +} + +ChatResult ChatResult::parse(const Json& doc) { + ChatResult r; + r.raw = doc; + r.id = J::str(doc, "id"); + r.object = J::str(doc, "object"); + r.model = J::str(doc, "model"); + r.created = J::num(doc, "created"); + r.usage = TokenUsage::parse(J::obj(doc, "usage")); + if (const Json* cs = J::arr(doc, "choices")) { + for (const Json& c : cs->as_array()) { + if (r.choices.size() >= static_cast(MistralCaps::kMaxChoices)) break; + ChatChoice ch; + ch.index = static_cast(J::num(c, "index")); + ch.finish_reason = J::str(c, "finish_reason"); + if (const Json* m = J::obj(c, "message")) { + ch.message = *m; + const Json* content = m->find("content"); + ch.content = J::text_of(content); + ch.reasoning = J::thinking_of(content); + if (const Json* tc = J::arr(*m, "tool_calls")) + for (const Json& call : tc->as_array()) ch.tool_calls.push_back(parse_call(call)); + } + r.choices.push_back(std::move(ch)); + } + } + return r; +} + +StreamDelta ChatStreamAccumulator::feed(const Json& event) { + ++events_; + const Json* chunk = &event; + if (!event.find("choices")) + if (const Json* inner = J::obj(event, "data")) chunk = inner; + if (events_ == 1 || id_.empty()) { + id_ = J::str(*chunk, "id", id_); + model_ = J::str(*chunk, "model", model_); + object_ = J::str(*chunk, "object", object_); + created_ = J::num(*chunk, "created", created_); + } + if (const Json* u = J::obj(*chunk, "usage")) usage_ = TokenUsage::parse(u); + StreamDelta d; + if (const Json* cs = J::arr(*chunk, "choices")) { + for (const Json& c : cs->as_array()) { + int index = static_cast(J::num(c, "index")); + if (index < 0 || index >= MistralCaps::kMaxChoices) continue; + Slot& slot = slots_[index]; + slot.choice.index = index; + d.index = index; + if (const Json* delta = J::obj(c, "delta")) { + const Json* content = delta->find("content"); + d.text = J::text_of(content); + d.reasoning = J::thinking_of(content); + slot.choice.content += d.text; + slot.choice.reasoning += d.reasoning; + if (const Json* tc = J::arr(*delta, "tool_calls")) { + for (const Json& call : tc->as_array()) { + int ti = static_cast(J::num(call, "index")); + ToolCallRecord piece = parse_call(call); + ToolCallRecord& into = slot.calls[ti]; + if (!piece.id.empty()) into.id = piece.id; + if (!piece.name.empty()) into.name = piece.name; + into.arguments += piece.arguments; + d.tool_call_id = piece.id; + d.tool_call_name = piece.name; + d.tool_args += piece.arguments; + } + } + } + const Json* fr = c.find("finish_reason"); + if (fr && fr->is_string() && !fr->as_string().empty() && fr->as_string() != "None") { + slot.choice.finish_reason = fr->as_string(); + d.finish_reason = fr->as_string(); + finished_ = true; + } + } + } + return d; +} + +ChatResult ChatStreamAccumulator::result() const { + ChatResult r; + r.id = id_; + r.model = model_; + r.object = object_; + r.created = created_; + r.usage = usage_; + for (const auto& [index, slot] : slots_) { + ChatChoice ch = slot.choice; + for (const auto& [ti, call] : slot.calls) ch.tool_calls.push_back(call); + Json m = Json::object(); + m.set("role", Json("assistant")); + m.set("content", Json(ch.content)); + if (!ch.tool_calls.empty()) { + Json arr = Json::array(); + for (const ToolCallRecord& c : ch.tool_calls) { + Json fn = Json::object(); + fn.set("name", Json(c.name)); + fn.set("arguments", Json(c.arguments)); + Json x = Json::object(); + x.set("id", Json(c.id)); + x.set("type", Json("function")); + x.set("function", fn); + arr.push(x); + } + m.set("tool_calls", arr); + } + ch.message = m; + r.choices.push_back(std::move(ch)); + } + return r; +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/chat_result.h b/src/vendors/mistral/chat_result.h new file mode 100644 index 0000000..51503ae --- /dev/null +++ b/src/vendors/mistral/chat_result.h @@ -0,0 +1,74 @@ +#pragma once + +// Response side of chat, FIM and agents completions (OpenAPI +// ChatCompletionResponse / FIMCompletionResponse share one shape) and the +// streaming accumulator over CompletionChunk events. + +#include +#include +#include + +#include "vendors/mistral/types.h" + +namespace molo::mistral { + +struct ToolCallRecord { + std::string id; + std::string name; + std::string arguments; // JSON text; an object on the wire is dumped to text + bool arguments_valid() const; + Json parsed() const; // {} when invalid +}; + +struct ChatChoice { + int index = 0; + std::string finish_reason; // stop | length | model_length | error | tool_calls + std::string content; // text chunks concatenated + std::string reasoning; // thinking chunks (Magistral), concatenated + std::vector tool_calls; + Json message; // the assistant message as sent by the server + Json history_message() const; // ready to append to the next request +}; + +struct ChatResult { + std::string id, object, model; + long created = 0; + std::vector choices; + TokenUsage usage; + Json raw; + const ChatChoice& first() const; // throws 502 EMPTY_RESPONSE when none + static ChatResult parse(const Json& doc); +}; + +struct StreamDelta { + int index = 0; + std::string text; + std::string reasoning; + std::string tool_call_id, tool_call_name, tool_args; + std::string finish_reason; +}; + +// Folds CompletionChunk events (choices[].delta) into a ChatResult. Accepts +// the event payload bare or wrapped as {"data": chunk} (the spec's +// CompletionEvent); tool calls are merged per choice and tool index. +class ChatStreamAccumulator { + public: + StreamDelta feed(const Json& event); + bool done() const { return finished_; } + std::size_t events() const { return events_; } + ChatResult result() const; + + private: + struct Slot { + ChatChoice choice; + std::map calls; + }; + std::string id_, model_, object_; + long created_ = 0; + TokenUsage usage_; + std::map slots_; + bool finished_ = false; + std::size_t events_ = 0; +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/classifiers.cpp b/src/vendors/mistral/classifiers.cpp new file mode 100644 index 0000000..edb7406 --- /dev/null +++ b/src/vendors/mistral/classifiers.cpp @@ -0,0 +1,72 @@ +#include "vendors/mistral/classifiers.h" + +#include "vendors/mistral/caps.h" +#include "vendors/mistral/ops.h" +#include "vendors/mistral/rules.h" + +namespace molo::mistral { + +ClassificationResult ClassificationResult::parse(const Json& doc) { + ClassificationResult r; + r.raw = doc; + r.id = J::str(doc, "id"); + r.model = J::str(doc, "model"); + if (const Json* rs = J::arr(doc, "results")) { + for (const Json& item : rs->as_array()) { + std::map> targets; + if (item.is_object()) + for (const auto& [target, res] : item.as_object()) + if (const Json* s = J::obj(res, "scores")) + for (const auto& [label, v] : s->as_object()) + if (v.is_number()) targets[target][label] = v.as_number(); + r.results.push_back(std::move(targets)); + } + } + return r; +} + +std::string ClassificationResult::top_label(std::size_t input, const std::string& target) const { + if (input >= results.size()) return ""; + auto t = results[input].find(target); + if (t == results[input].end()) return ""; + std::string best; + double top = -1; + for (const auto& [label, v] : t->second) + if (v > top) { + top = v; + best = label; + } + return best; +} + +ClassificationResult Classifiers::classify(const std::string& model, const std::vector& texts) { + Rules::model(model); + Rules::require(!texts.empty() && texts.size() <= MistralCaps::kMaxClassifyInputs, "texts must be 1..256"); + Json arr = Json::array(); + for (const std::string& t : texts) { + Rules::require(!t.empty() && t.size() <= MistralCaps::kMaxEmbeddingChars, "each text must be 1 byte..1 MiB"); + arr.push(Json(t)); + } + Json body = Json::object(); + body.set("model", Json(model)); + body.set("input", texts.size() == 1 ? Json(texts[0]) : arr); + return ClassificationResult::parse(call(Op::kClassifications, {}, &body)); +} + +ClassificationResult Classifiers::classify_chat(const std::string& model, const std::vector& messages) { + Rules::model(model); + Rules::require(!messages.empty() && messages.size() <= MistralCaps::kMaxMessages, "messages must be 1..4096"); + Json arr = Json::array(); + for (const Json& m : messages) { + Rules::require(m.is_object() && m.find("role"), "message needs a role"); + arr.push(m); + } + Json input = Json::object(); + input.set("messages", arr); + Json body = Json::object(); + body.set("model", Json(model)); + body.set("input", input); + return ClassificationResult::parse(call(Op::kChatClassifications, {}, &body)); +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/classifiers.h b/src/vendors/mistral/classifiers.h new file mode 100644 index 0000000..ad1a89d --- /dev/null +++ b/src/vendors/mistral/classifiers.h @@ -0,0 +1,33 @@ +#pragma once + +// Classifiers: POST /v1/classifications (texts) and POST /v1/chat/classifications +// (a conversation, sent as InstructRequest {messages}). The model is required: +// a fine-tuned classifier id (ft:classifier:...) or a base classifier model. +// results[i] is a map target name -> {scores: {label: probability}}. + +#include +#include +#include + +#include "vendors/mistral/types.h" + +namespace molo::mistral { + +struct ClassificationResult { + std::string id, model; + // per input: target name -> label -> score + std::vector>> results; + Json raw; + static ClassificationResult parse(const Json& doc); + // Highest scoring label of one target of one input ("" when absent). + std::string top_label(std::size_t input, const std::string& target) const; +}; + +class Classifiers : public MistralService { + public: + explicit Classifiers(std::shared_ptr client) : MistralService(std::move(client)) {} + ClassificationResult classify(const std::string& model, const std::vector& texts); + ClassificationResult classify_chat(const std::string& model, const std::vector& messages); +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/conversations.cpp b/src/vendors/mistral/conversations.cpp new file mode 100644 index 0000000..73115be --- /dev/null +++ b/src/vendors/mistral/conversations.cpp @@ -0,0 +1,243 @@ +#include "vendors/mistral/conversations.h" + +#include "vendors/mistral/calls.h" +#include "vendors/mistral/caps.h" +#include "vendors/mistral/ops.h" +#include "vendors/mistral/rules.h" + +namespace molo::mistral { + +ConversationInput ConversationInput::text(const std::string& text) { + ConversationInput i; + i.value = Json(text); + i.validate(); + return i; +} + +ConversationInput ConversationInput::entries(const std::vector& entries) { + ConversationInput i; + Json a = Json::array(); + for (const Json& e : entries) a.push(e); + i.value = a; + i.validate(); + return i; +} + +Json ConversationInput::user_entry(const std::string& text) { + Json e = Json::object(); + e.set("type", Json("message.input")); + e.set("role", Json("user")); + e.set("content", Json(text)); + return e; +} + +void ConversationInput::validate() const { + if (value.is_string()) { + Rules::require(!value.as_string().empty() && value.as_string().size() <= MistralCaps::kMaxEmbeddingChars, + "inputs text must be 1 byte..1 MiB"); + return; + } + Rules::require(value.is_array() && !value.as_array().empty() && value.as_array().size() <= MistralCaps::kMaxMessages, + "inputs must be a string or 1..4096 entries"); + for (const Json& e : value.as_array()) Rules::require(e.is_object(), "each input entry must be an object"); +} + +namespace { +void check_common(const Json& tools, const Json& completion_args, const Json& guardrails, const Json& metadata, + const std::string& handoff) { + if (!tools.is_null()) + Rules::require(tools.is_array() && tools.as_array().size() <= MistralCaps::kMaxTools, "tools must be an array of at most 128"); + if (!completion_args.is_null()) Rules::json_object(completion_args, "completion_args"); + if (!guardrails.is_null()) Rules::require(guardrails.is_array(), "guardrails must be an array"); + if (!metadata.is_null()) Rules::json_object(metadata, "metadata"); + if (!handoff.empty()) Rules::one_of(handoff, {"client", "server"}, "handoff_execution"); +} + +void put(Json& o, const char* key, const Json& v) { + if (!v.is_null()) o.set(key, v); +} +} // namespace + +void ConversationStartParams::validate() const { + inputs.validate(); + Rules::require(model.empty() != agent_id.empty(), "give exactly one of model or agent_id"); + if (!model.empty()) Rules::model(model); + if (!agent_id.empty()) Rules::require(llm::Service::valid_id(agent_id), "bad agent id"); + Rules::require(instructions.size() <= (1u << 20) && name.size() <= 256 && description.size() <= 4096, + "instructions, name or description too long"); + check_common(tools, completion_args, guardrails, metadata, handoff_execution); +} + +Json ConversationStartParams::to_json(bool stream) const { + validate(); + Json o = Json::object(); + o.set("inputs", inputs.value); + if (!model.empty()) o.set("model", Json(model)); + if (!agent_id.empty()) o.set("agent_id", Json(agent_id)); + if (agent_version >= 0) o.set("agent_version", Json(agent_version)); + if (!instructions.empty()) o.set("instructions", Json(instructions)); + if (!name.empty()) o.set("name", Json(name)); + if (!description.empty()) o.set("description", Json(description)); + put(o, "tools", tools); + put(o, "completion_args", completion_args); + put(o, "guardrails", guardrails); + put(o, "metadata", metadata); + if (store) o.set("store", Json(*store)); + if (!handoff_execution.empty()) o.set("handoff_execution", Json(handoff_execution)); + if (stream) o.set("stream", Json(true)); + return o; +} + +void ConversationAppendParams::validate() const { + inputs.validate(); + check_common(Json(), completion_args, Json(), Json(), handoff_execution); + if (!tool_confirmations.is_null()) Rules::require(tool_confirmations.is_array(), "tool_confirmations must be an array"); +} + +Json ConversationAppendParams::to_json(bool stream) const { + validate(); + Json o = Json::object(); + o.set("inputs", inputs.value); + put(o, "completion_args", completion_args); + put(o, "tool_confirmations", tool_confirmations); + if (store) o.set("store", Json(*store)); + if (!handoff_execution.empty()) o.set("handoff_execution", Json(handoff_execution)); + if (stream) o.set("stream", Json(true)); + return o; +} + +void ConversationRestartParams::validate() const { + Rules::require(llm::Service::valid_id(from_entry_id), "bad from_entry_id"); + if (!inputs.value.is_null()) inputs.validate(); + check_common(Json(), completion_args, Json(), metadata, handoff_execution); +} + +Json ConversationRestartParams::to_json(bool stream) const { + validate(); + Json o = Json::object(); + o.set("from_entry_id", Json(from_entry_id)); + put(o, "inputs", inputs.value); + put(o, "completion_args", completion_args); + put(o, "metadata", metadata); + if (store) o.set("store", Json(*store)); + if (!handoff_execution.empty()) o.set("handoff_execution", Json(handoff_execution)); + if (agent_version >= 0) o.set("agent_version", Json(agent_version)); + if (stream) o.set("stream", Json(true)); + return o; +} + +std::string ConversationResult::text() const { + std::string out; + if (outputs.is_array()) + for (const Json& e : outputs.as_array()) + if (J::str(e, "type") == "message.output") out += J::text_of(e.find("content")); + return out; +} + +ConversationResult ConversationResult::parse(const Json& doc) { + ConversationResult r; + r.raw = doc; + r.conversation_id = J::str(doc, "conversation_id"); + if (const Json* o = J::arr(doc, "outputs")) r.outputs = *o; + r.usage = TokenUsage::parse(J::obj(doc, "usage")); + return r; +} + +ConversationResult Conversations::start(const ConversationStartParams& p) { + Json body = p.to_json(false); + return ConversationResult::parse(call(Op::kConvStart, {}, &body)); +} + +ConversationResult Conversations::append(const std::string& id, const ConversationAppendParams& p) { + check_id("conversation", id); + Json body = p.to_json(false); + return ConversationResult::parse(call(Op::kConvAppend, {id}, &body)); +} + +ConversationResult Conversations::restart(const std::string& id, const ConversationRestartParams& p) { + check_id("conversation", id); + Json body = p.to_json(false); + return ConversationResult::parse(call(Op::kConvRestart, {id}, &body)); +} + +ConversationResult Conversations::run_stream(const char* op, const std::vector& ids, const Json& body, + const EventSink& sink) { + ConversationResult result; + std::string text; + bool done = false, stopped = false; + std::size_t events = 0; + client_->stream(Calls::render(op, ids), body, [&](const llm::SseEvent& ev) { + if (++events > MistralCaps::kMaxStreamEvents) { + stopped = true; + return false; + } + std::string name = ev.event.empty() ? J::str(ev.data, "type") : ev.event; + if (name == "conversation.response.error") + throw llm::ApiError("mistral", 502, "STREAM_ERROR", J::str(ev.data, "message", "stream error")); + if (name == "conversation.response.started") result.conversation_id = J::str(ev.data, "conversation_id"); + if (name == "conversation.response.done") { + result.usage = TokenUsage::parse(J::obj(ev.data, "usage")); + done = true; + } + if (name == "message.output.delta") text += J::text_of(ev.data.find("content")); + if (sink && !sink(ConversationEvent{name, ev.data})) { + stopped = true; + return false; + } + return true; + }); + if (!done && !stopped) throw llm::ApiError("mistral", 502, "STREAM_TRUNCATED", "stream ended without conversation.response.done"); + Json entry = Json::object(); + entry.set("type", Json("message.output")); + entry.set("role", Json("assistant")); + entry.set("content", Json(text)); + result.outputs = Json::array(); + result.outputs.push(entry); + return result; +} + +ConversationResult Conversations::start_stream(const ConversationStartParams& p, const EventSink& sink) { + return run_stream(Op::kConvStartStream, {}, p.to_json(true), sink); +} + +ConversationResult Conversations::append_stream(const std::string& id, const ConversationAppendParams& p, + const EventSink& sink) { + check_id("conversation", id); + return run_stream(Op::kConvAppendStream, {id}, p.to_json(true), sink); +} + +ConversationResult Conversations::restart_stream(const std::string& id, const ConversationRestartParams& p, + const EventSink& sink) { + check_id("conversation", id); + return run_stream(Op::kConvRestartStream, {id}, p.to_json(true), sink); +} + +ListPage Conversations::list(int page, int page_size) { + Rules::require(page >= 0, "page must not be negative"); + Rules::between(page_size, 1, MistralCaps::kPageSizeMax, "page_size"); + Query q; + q.add("page", page).add("page_size", page_size); + return ListPage::parse(call(Op::kConvList, {}, nullptr, q.str())); +} + +Json Conversations::get(const std::string& id) { + check_id("conversation", id); + return call(Op::kConvGet, {id}); +} + +Json Conversations::history(const std::string& id) { + check_id("conversation", id); + return call(Op::kConvHistory, {id}); +} + +Json Conversations::messages(const std::string& id) { + check_id("conversation", id); + return call(Op::kConvMessages, {id}); +} + +void Conversations::remove(const std::string& id) { + check_id("conversation", id); + call(Op::kConvDelete, {id}); +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/conversations.h b/src/vendors/mistral/conversations.h new file mode 100644 index 0000000..6bb64d0 --- /dev/null +++ b/src/vendors/mistral/conversations.h @@ -0,0 +1,110 @@ +#pragma once + +// Conversations API (stateful agents / models with built-in tools): +// /v1/conversations. Start with a model or an agent id, append, restart from an +// entry, read history, delete (explicit). Streamed variants decode the +// ConversationEvents SSE stream (conversation.response.started / .done / +// .error, message.output.delta, tool.execution.*, agent.handoff.*, +// function.call.delta). + +#include +#include +#include +#include + +#include "vendors/mistral/types.h" + +namespace molo::mistral { + +struct ConversationInput { + // Either a plain string or an array of entries (MessageInputEntry, + // FunctionResultEntry, ...). Build with text() or entries(). + Json value; + static ConversationInput text(const std::string& text); + static ConversationInput entries(const std::vector& entries); + // A user message entry {"type":"message.input","role":"user","content":text}. + static Json user_entry(const std::string& text); + void validate() const; +}; + +struct ConversationStartParams { + ConversationInput inputs; + std::string model; // exactly one of model / agent_id + std::string agent_id; + int agent_version = -1; // < 0: not sent + std::string instructions, name, description; + Json tools; // null or array + Json completion_args; // null or object + Json guardrails; // null or array + Json metadata; // null or object + std::optional store; + std::string handoff_execution; // "", client, server + void validate() const; + Json to_json(bool stream) const; +}; + +struct ConversationAppendParams { + ConversationInput inputs; + Json completion_args; + Json tool_confirmations; // null or array + std::optional store; + std::string handoff_execution; + void validate() const; + Json to_json(bool stream) const; +}; + +struct ConversationRestartParams { + std::string from_entry_id; // required + ConversationInput inputs; // optional: leave value null to send none + Json completion_args; + Json metadata; + std::optional store; + std::string handoff_execution; + int agent_version = -1; + void validate() const; + Json to_json(bool stream) const; +}; + +struct ConversationResult { + std::string conversation_id; + Json outputs = Json::array(); + TokenUsage usage; + Json raw; + std::string text() const; // message.output entries concatenated + static ConversationResult parse(const Json& doc); +}; + +struct ConversationEvent { + std::string event; // SSE event name + Json data; +}; + +class Conversations : public MistralService { + public: + explicit Conversations(std::shared_ptr client) : MistralService(std::move(client)) {} + ConversationResult start(const ConversationStartParams& params); + ConversationResult append(const std::string& conversation_id, const ConversationAppendParams& params); + ConversationResult restart(const std::string& conversation_id, const ConversationRestartParams& params); + + using EventSink = std::function; // false: stop + // Streams; returns the folded result. Throws 502 STREAM_TRUNCATED when the + // stream ends without conversation.response.done (unless the sink stopped it) + // and the server's error when conversation.response.error arrives. + ConversationResult start_stream(const ConversationStartParams& params, const EventSink& sink); + ConversationResult append_stream(const std::string& conversation_id, const ConversationAppendParams& params, + const EventSink& sink); + ConversationResult restart_stream(const std::string& conversation_id, const ConversationRestartParams& params, + const EventSink& sink); + + ListPage list(int page = 0, int page_size = 100); + Json get(const std::string& conversation_id); + Json history(const std::string& conversation_id); + Json messages(const std::string& conversation_id); + void remove(const std::string& conversation_id); // explicit, irreversible + + private: + ConversationResult run_stream(const char* op, const std::vector& ids, const Json& body, + const EventSink& sink); +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/embeddings.cpp b/src/vendors/mistral/embeddings.cpp new file mode 100644 index 0000000..c64a22d --- /dev/null +++ b/src/vendors/mistral/embeddings.cpp @@ -0,0 +1,63 @@ +#include "vendors/mistral/embeddings.h" + +#include "vendors/mistral/caps.h" +#include "vendors/mistral/ops.h" +#include "vendors/mistral/rules.h" + +namespace molo::mistral { + +void EmbeddingParams::validate() const { + Rules::model(model); + Rules::require(!inputs.empty() && inputs.size() <= MistralCaps::kMaxEmbeddingInputs, "inputs must be 1..512"); + for (const std::string& s : inputs) + Rules::require(!s.empty() && s.size() <= MistralCaps::kMaxEmbeddingChars, "each input must be 1 byte..1 MiB"); + if (output_dimension) Rules::positive(*output_dimension, "output_dimension"); + if (!output_dtype.empty()) Rules::one_of(output_dtype, {"float", "int8", "uint8", "binary", "ubinary"}, "output_dtype"); + if (!encoding_format.empty()) Rules::one_of(encoding_format, {"float", "base64"}, "encoding_format"); +} + +Json EmbeddingParams::to_json() const { + validate(); + Json o = Json::object(); + o.set("model", Json(model)); + if (inputs.size() == 1) { + o.set("input", Json(inputs[0])); + } else { + Json a = Json::array(); + for (const std::string& s : inputs) a.push(Json(s)); + o.set("input", a); + } + if (output_dimension) o.set("output_dimension", Json(*output_dimension)); + if (!output_dtype.empty()) o.set("output_dtype", Json(output_dtype)); + if (!encoding_format.empty()) o.set("encoding_format", Json(encoding_format)); + return o; +} + +EmbeddingResult EmbeddingResult::parse(const Json& doc) { + EmbeddingResult r; + r.raw = doc; + r.id = J::str(doc, "id"); + r.model = J::str(doc, "model"); + r.usage = TokenUsage::parse(J::obj(doc, "usage")); + if (const Json* d = J::arr(doc, "data")) { + for (const Json& e : d->as_array()) { + const Json* v = e.find("embedding"); + if (v && v->is_string()) { + r.encoded.push_back(v->as_string()); + } else if (v && v->is_array()) { + std::vector row; + for (const Json& x : v->as_array()) + if (x.is_number()) row.push_back(x.as_number()); + r.vectors.push_back(std::move(row)); + } + } + } + return r; +} + +EmbeddingResult Embeddings::create(const EmbeddingParams& params) { + Json body = params.to_json(); + return EmbeddingResult::parse(call(Op::kEmbeddings, {}, &body)); +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/embeddings.h b/src/vendors/mistral/embeddings.h new file mode 100644 index 0000000..9dea20b --- /dev/null +++ b/src/vendors/mistral/embeddings.h @@ -0,0 +1,39 @@ +#pragma once + +// Embeddings: POST /v1/embeddings. input is one string or an array of strings. + +#include +#include +#include + +#include "vendors/mistral/types.h" + +namespace molo::mistral { + +struct EmbeddingParams { + static constexpr const char* kDefaultModel = "mistral-embed"; + std::string model = kDefaultModel; + std::vector inputs; // 1..512 inputs, each 1..1 MiB + std::optional output_dimension; // > 0 + std::string output_dtype; // "", float, int8, uint8, binary, ubinary + std::string encoding_format; // "", float, base64 + void validate() const; + Json to_json() const; +}; + +struct EmbeddingResult { + std::string id, model; + std::vector> vectors; // float / integer outputs; empty for base64 + std::vector encoded; // base64 outputs + TokenUsage usage; + Json raw; + static EmbeddingResult parse(const Json& doc); +}; + +class Embeddings : public MistralService { + public: + explicit Embeddings(std::shared_ptr client) : MistralService(std::move(client)) {} + EmbeddingResult create(const EmbeddingParams& params); +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/files.cpp b/src/vendors/mistral/files.cpp new file mode 100644 index 0000000..94a7de1 --- /dev/null +++ b/src/vendors/mistral/files.cpp @@ -0,0 +1,122 @@ +#include "vendors/mistral/files.h" + +#include "llm/caps.h" +#include "vendors/mistral/calls.h" +#include "vendors/mistral/caps.h" +#include "vendors/mistral/ops.h" +#include "vendors/mistral/rules.h" + +namespace molo::mistral { + +void FileUploadParams::validate() const { + Rules::filename(filename); + Rules::require(!data.empty(), "file is empty"); + Rules::require(data.size() <= MistralCaps::kMaxUploadBytes, "file over 512 MiB"); + Rules::one_of(purpose, {"fine-tune", "batch", "ocr", "agentic_safety"}, "purpose"); + if (!visibility.empty()) Rules::one_of(visibility, {"workspace", "user"}, "visibility"); + if (expiry_hours != 0) Rules::between(expiry_hours, 1, 24 * 365, "expiry hours"); +} + +void FileListParams::validate() const { + Rules::require(page >= 0, "page must not be negative"); + Rules::between(page_size, 1, MistralCaps::kPageSizeMax, "page_size"); + if (!purpose.empty()) Rules::one_of(purpose, {"fine-tune", "batch", "ocr", "agentic_safety"}, "purpose"); + if (!sample_type.empty()) + Rules::one_of(sample_type, {"pretrain", "instruct", "batch_request", "batch_result", "batch_error"}, "sample_type"); + if (!source.empty()) Rules::word(source, "source"); + Rules::require(search.size() <= 256, "search over 256 characters"); +} + +FileInfo FileInfo::parse(const Json& doc) { + FileInfo f; + f.raw = doc; + f.id = J::str(doc, "id"); + f.filename = J::str(doc, "filename"); + f.purpose = J::str(doc, "purpose"); + f.sample_type = J::str(doc, "sample_type"); + f.source = J::str(doc, "source"); + f.mimetype = J::str(doc, "mimetype"); + f.visibility = J::str(doc, "visibility"); + f.bytes = J::num(doc, "bytes"); + f.created_at = J::num(doc, "created_at"); + f.num_lines = J::num(doc, "num_lines", -1); + return f; +} + +FileList FileList::parse(const Json& doc) { + FileList l; + l.total = J::num(doc, "total", -1); + if (const Json* d = J::arr(doc, "data")) + for (const Json& f : d->as_array()) l.files.push_back(FileInfo::parse(f)); + return l; +} + +FileInfo Files::upload(const FileUploadParams& params) { + params.validate(); + clients::body::MultipartBody form; + form.add_text("purpose", params.purpose); + if (!params.visibility.empty()) form.add_text("visibility", params.visibility); + if (params.expiry_hours != 0) form.add_text("expiry", std::to_string(params.expiry_hours)); + form.add_file("file", params.filename, "application/octet-stream", params.data); + return FileInfo::parse(call_multipart(Op::kFileUpload, form)); +} + +FileList Files::list(const FileListParams& params) { + params.validate(); + Query q; + q.add("page", params.page).add("page_size", params.page_size).add_flag("include_total", params.include_total); + q.add_if("purpose", params.purpose).add_if("sample_type", params.sample_type); + q.add_if("source", params.source).add_if("search", params.search); + return FileList::parse(call(Op::kFileList, {}, nullptr, q.str())); +} + +FileList Files::list_all(FileListParams params) { + FileList all; + for (std::size_t page = 0; page < MistralCaps::kMaxWalkPages; ++page) { + params.page = static_cast(page); + FileList one = list(params); + if (one.total >= 0) all.total = one.total; + for (FileInfo& f : one.files) { + if (all.files.size() >= MistralCaps::kMaxItemsReturned) { + all.truncated = true; + return all; + } + all.files.push_back(std::move(f)); + } + if (one.files.size() < static_cast(params.page_size)) return all; + } + all.truncated = all.total < 0 || static_cast(all.files.size()) < all.total; + return all; +} + +FileInfo Files::get(const std::string& file_id) { + check_id("file", file_id); + return FileInfo::parse(call(Op::kFileGet, {file_id})); +} + +std::string Files::download(const std::string& file_id) { + check_id("file", file_id); + llm::RawResponse r = client_->request_raw(clients::Method::kGet, Calls::render(Op::kFileContent, {file_id})); + if (r.body.size() > 64u * 1024u * 1024u) + throw llm::ApiError("mistral", 502, "RESPONSE_TOO_LARGE", "file download over 64 MiB"); + return r.body; +} + +std::string Files::signed_url(const std::string& file_id, int expiry_hours) { + check_id("file", file_id); + Rules::between(expiry_hours, 1, MistralCaps::kSignedUrlExpiryMaxHours, "expiry hours"); + Query q; + q.add("expiry", expiry_hours); + return J::str(call(Op::kFileUrl, {file_id}, nullptr, q.str()), "url"); +} + +FileDeleted Files::remove(const std::string& file_id) { + check_id("file", file_id); + Json d = call(Op::kFileDelete, {file_id}); + FileDeleted out; + out.id = J::str(d, "id"); + out.deleted = J::flag(d, "deleted"); + return out; +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/files.h b/src/vendors/mistral/files.h new file mode 100644 index 0000000..e1ad691 --- /dev/null +++ b/src/vendors/mistral/files.h @@ -0,0 +1,66 @@ +#pragma once + +// Files API: /v1/files. Upload (multipart), list (page / page_size), retrieve, +// download bytes, signed URL. Delete is an explicit method with no default and +// no implicit call. Files are account (workspace) wide. + +#include + +#include "vendors/mistral/types.h" + +namespace molo::mistral { + +struct FileUploadParams { + std::string filename; // 1..512 bytes, no path separators + std::string data; // 1 byte .. 512 MiB (spec: "maximum of 512 MB") + std::string purpose = "fine-tune"; // fine-tune | batch | ocr | agentic_safety + std::string visibility; // "", workspace, user + long expiry_hours = 0; // 0: not sent + void validate() const; +}; + +struct FileListParams { + int page = 0; // >= 0 + int page_size = 100; // 1..100 + bool include_total = true; + std::string purpose; // "", fine-tune, batch, ocr, agentic_safety + std::string sample_type; // "", pretrain, instruct, batch_request, batch_result, batch_error + std::string source; // "" or word + std::string search; // free text, <= 256 chars + void validate() const; +}; + +struct FileInfo { + std::string id, filename, purpose, sample_type, source, mimetype, visibility; + long bytes = 0, created_at = 0; + long num_lines = -1; + Json raw; + static FileInfo parse(const Json& doc); +}; + +struct FileDeleted { + std::string id; + bool deleted = false; +}; + +struct FileList { + std::vector files; + long total = -1; + bool truncated = false; // more pages exist than the walk caps allowed + static FileList parse(const Json& doc); +}; + +class Files : public MistralService { + public: + explicit Files(std::shared_ptr client) : MistralService(std::move(client)) {} + FileInfo upload(const FileUploadParams& params); + FileList list(const FileListParams& params = {}); + // Walks pages up to MistralCaps::kMaxWalkPages / kMaxItemsReturned; truncated is set when cut. + FileList list_all(FileListParams params = {}); + FileInfo get(const std::string& file_id); + std::string download(const std::string& file_id); // raw bytes, <= 64 MiB + std::string signed_url(const std::string& file_id, int expiry_hours = 24); + FileDeleted remove(const std::string& file_id); // explicit, irreversible +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/finetune.cpp b/src/vendors/mistral/finetune.cpp new file mode 100644 index 0000000..3b7a29c --- /dev/null +++ b/src/vendors/mistral/finetune.cpp @@ -0,0 +1,141 @@ +#include "vendors/mistral/finetune.h" + +#include "vendors/mistral/caps.h" +#include "vendors/mistral/ops.h" +#include "vendors/mistral/rules.h" + +namespace molo::mistral { + +void FineTuneListParams::validate() const { + Rules::require(page >= 0, "page must not be negative"); + Rules::between(page_size, 1, MistralCaps::kPageSizeMax, "page_size"); + if (!model.empty()) Rules::model(model); + if (!status.empty()) + Rules::one_of(status, {"QUEUED", "STARTED", "VALIDATING", "VALIDATED", "RUNNING", "FAILED_VALIDATION", "FAILED", + "SUCCESS", "CANCELLED", "CANCELLATION_REQUESTED"}, "status"); + if (!suffix.empty()) Rules::word(suffix, "suffix"); + if (!wandb_project.empty()) Rules::word(wandb_project, "wandb_project"); + if (!wandb_name.empty()) Rules::word(wandb_name, "wandb_name"); + Rules::require(created_after.size() <= 40 && created_before.size() <= 40, "bad date"); +} + +void FineTuneJobParams::validate() const { + Rules::model(model); + Rules::require(!training_files.empty() && training_files.size() <= 64, "training_files must be 1..64"); + for (const TrainingFileRef& f : training_files) { + Rules::require(llm::Service::valid_id(f.file_id), "bad training file id"); + Rules::require(f.weight > 0, "training file weight must be positive"); + } + Rules::require(validation_files.size() <= 64, "validation_files over 64"); + for (const std::string& f : validation_files) Rules::require(llm::Service::valid_id(f), "bad validation file id"); + if (!suffix.empty()) { + Rules::require(suffix.size() <= 18, "suffix over 18 characters"); + Rules::word(suffix, "suffix"); + } + if (!job_type.empty()) Rules::one_of(job_type, {"completion", "classifier"}, "job_type"); + Rules::json_object(hyperparameters, "hyperparameters"); + Rules::require(!hyperparameters.as_object().empty(), "hyperparameters are required"); + for (const Json* j : {&integrations, &repositories, &classifier_targets}) + if (!j->is_null()) Rules::require(j->is_array(), "integrations, repositories and classifier_targets must be arrays"); + Rules::range(invalid_sample_skip_percentage, 0.0, 1.0, "invalid_sample_skip_percentage"); +} + +Json FineTuneJobParams::to_json() const { + validate(); + Json o = Json::object(); + o.set("model", Json(model)); + Json tf = Json::array(); + for (const TrainingFileRef& f : training_files) { + Json x = Json::object(); + x.set("file_id", Json(f.file_id)); + x.set("weight", Json(f.weight)); + tf.push(x); + } + o.set("training_files", tf); + if (!validation_files.empty()) { + Json vf = Json::array(); + for (const std::string& f : validation_files) vf.push(Json(f)); + o.set("validation_files", vf); + } + if (!suffix.empty()) o.set("suffix", Json(suffix)); + if (!job_type.empty()) o.set("job_type", Json(job_type)); + o.set("hyperparameters", hyperparameters); + if (!integrations.is_null()) o.set("integrations", integrations); + if (!repositories.is_null()) o.set("repositories", repositories); + if (!classifier_targets.is_null()) o.set("classifier_targets", classifier_targets); + if (auto_start) o.set("auto_start", Json(true)); + if (invalid_sample_skip_percentage > 0) o.set("invalid_sample_skip_percentage", Json(invalid_sample_skip_percentage)); + return o; +} + +bool FineTuneJob::terminal() const { + return status == "SUCCESS" || status == "FAILED" || status == "FAILED_VALIDATION" || status == "CANCELLED"; +} + +FineTuneJob FineTuneJob::parse(const Json& doc) { + FineTuneJob j; + j.raw = doc; + j.id = J::str(doc, "id"); + j.status = J::str(doc, "status"); + j.model = J::str(doc, "model"); + j.fine_tuned_model = J::str(doc, "fine_tuned_model"); + j.job_type = J::str(doc, "job_type"); + j.created_at = J::num(doc, "created_at"); + j.modified_at = J::num(doc, "modified_at"); + j.trained_tokens = J::num(doc, "trained_tokens"); + return j; +} + +ListPage FineTuning::list_jobs(const FineTuneListParams& p) { + p.validate(); + Query q; + q.add("page", p.page).add("page_size", p.page_size).add_if("model", p.model).add_if("status", p.status); + q.add_if("suffix", p.suffix).add_if("wandb_project", p.wandb_project).add_if("wandb_name", p.wandb_name); + q.add_if("created_after", p.created_after).add_if("created_before", p.created_before); + if (p.created_by_me) q.add_flag("created_by_me", *p.created_by_me); + return ListPage::parse(call(Op::kFtJobList, {}, nullptr, q.str())); +} + +FineTuneJob FineTuning::get_job(const std::string& job_id) { + check_id("job", job_id); + return FineTuneJob::parse(call(Op::kFtJobGet, {job_id})); +} + +FineTuneJob FineTuning::create_job(const FineTuneJobParams& params) { + Json body = params.to_json(); + Query q; + if (params.dry_run) q.add_flag("dry_run", true); + return FineTuneJob::parse(call(Op::kFtJobCreate, {}, &body, q.str())); +} + +FineTuneJob FineTuning::start_job(const std::string& job_id) { + check_id("job", job_id); + return FineTuneJob::parse(call(Op::kFtJobStart, {job_id})); +} + +FineTuneJob FineTuning::cancel_job(const std::string& job_id) { + check_id("job", job_id); + return FineTuneJob::parse(call(Op::kFtJobCancel, {job_id})); +} + +Json FineTuning::update_model(const std::string& model_id, const std::string& name, const std::string& description) { + check_id("model", model_id); + Rules::require(!name.empty() || !description.empty(), "give a name or a description"); + Rules::require(name.size() <= 256 && description.size() <= 4096, "name or description too long"); + Json body = Json::object(); + if (!name.empty()) body.set("name", Json(name)); + if (!description.empty()) body.set("description", Json(description)); + return call(Op::kFtModelUpdate, {model_id}, &body); +} + +Json FineTuning::archive_model(const std::string& model_id) { + check_id("model", model_id); + return call(Op::kFtModelArchive, {model_id}); +} + +Json FineTuning::unarchive_model(const std::string& model_id) { + check_id("model", model_id); + return call(Op::kFtModelUnarchive, {model_id}); +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/finetune.h b/src/vendors/mistral/finetune.h new file mode 100644 index 0000000..6e38302 --- /dev/null +++ b/src/vendors/mistral/finetune.h @@ -0,0 +1,71 @@ +#pragma once + +// Fine-tuning. Jobs (/v1/fine_tuning/jobs) appear only in the older +// platform-docs-public OpenAPI document, tagged deprecated.fine-tuning; the +// unauthenticated probe answered 401, so the paths exist. Fine-tuned models +// (/v1/fine_tuning/models/{id}) are in the current document. +// Creating, starting a job costs money: separate explicit methods. Cancelling +// a job and archiving a model are explicit methods with no defaults. + +#include +#include +#include + +#include "vendors/mistral/types.h" + +namespace molo::mistral { + +struct FineTuneListParams { + int page = 0; + int page_size = 100; // 1..100 + std::string model, status, suffix, wandb_project, wandb_name; + std::string created_after, created_before; // ISO 8601 + std::optional created_by_me; + void validate() const; +}; + +struct TrainingFileRef { + std::string file_id; + double weight = 1.0; // > 0 +}; + +struct FineTuneJobParams { + std::string model; // base model id + std::vector training_files; // 1..64 + std::vector validation_files; // <= 64 file ids + std::string suffix; // <= 18 chars, [A-Za-z0-9_-] + std::string job_type; // "", completion, classifier + Json hyperparameters = Json::object(); // required; keys per CompletionTrainingParametersIn + Json integrations; // null or array (wandb) + Json repositories; // null or array + Json classifier_targets; // null or array + bool auto_start = false; + double invalid_sample_skip_percentage = 0; // 0..1 + bool dry_run = false; // query dry_run=true: nothing is spawned + void validate() const; + Json to_json() const; +}; + +struct FineTuneJob { + std::string id, status, model, fine_tuned_model, job_type; + long created_at = 0, modified_at = 0, trained_tokens = 0; + Json raw; + bool terminal() const; // SUCCESS FAILED FAILED_VALIDATION CANCELLED + static FineTuneJob parse(const Json& doc); +}; + +class FineTuning : public MistralService { + public: + explicit FineTuning(std::shared_ptr client) : MistralService(std::move(client)) {} + ListPage list_jobs(const FineTuneListParams& params = {}); + FineTuneJob get_job(const std::string& job_id); + FineTuneJob create_job(const FineTuneJobParams& params); // costs money unless auto_start is false + FineTuneJob start_job(const std::string& job_id); // costs money + FineTuneJob cancel_job(const std::string& job_id); // explicit + // Fine-tuned model management. + Json update_model(const std::string& model_id, const std::string& name, const std::string& description); + Json archive_model(const std::string& model_id); // explicit + Json unarchive_model(const std::string& model_id); // explicit (DELETE .../archive) +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/libraries.cpp b/src/vendors/mistral/libraries.cpp new file mode 100644 index 0000000..67209a8 --- /dev/null +++ b/src/vendors/mistral/libraries.cpp @@ -0,0 +1,214 @@ +#include "vendors/mistral/libraries.h" + +#include "vendors/mistral/caps.h" +#include "vendors/mistral/ops.h" +#include "vendors/mistral/rules.h" + +namespace molo::mistral { + +LibraryInfo LibraryInfo::parse(const Json& doc) { + LibraryInfo l; + l.raw = doc; + l.id = J::str(doc, "id"); + l.name = J::str(doc, "name"); + l.description = J::str(doc, "description"); + l.owner_type = J::str(doc, "owner_type"); + l.created_at = J::str(doc, "created_at"); + l.updated_at = J::str(doc, "updated_at"); + l.total_size = J::num(doc, "total_size"); + l.nb_documents = J::num(doc, "nb_documents"); + return l; +} + +DocumentInfo DocumentInfo::parse(const Json& doc) { + DocumentInfo d; + d.raw = doc; + d.id = J::str(doc, "id"); + d.library_id = J::str(doc, "library_id"); + d.name = J::str(doc, "name"); + d.mime_type = J::str(doc, "mime_type"); + d.process_status = J::str(doc, "process_status"); + d.processing_status = J::str(doc, "processing_status"); + d.created_at = J::str(doc, "created_at"); + d.size = J::num(doc, "size"); + d.number_of_pages = J::num(doc, "number_of_pages"); + return d; +} + +void LibraryCreateParams::validate() const { + Rules::require(!name.empty() && name.size() <= 256, "name must be 1..256 characters"); + Rules::require(description.size() <= 4096, "description over 4096 characters"); + if (chunk_size) Rules::between(*chunk_size, MistralCaps::kLibraryChunkMin, MistralCaps::kLibraryChunkMax, "chunk_size"); + if (!owner_type.empty()) Rules::one_of(owner_type, {"User", "Workspace"}, "owner_type"); +} + +void LibraryListParams::validate() const { + Rules::require(page >= 0, "page must not be negative"); + Rules::between(page_size, 1, MistralCaps::kPageSizeMax, "page_size"); + Rules::require(search.size() <= 256, "search over 256 characters"); + if (!page_token.empty()) Rules::word(page_token, "page_token"); +} + +void DocumentListParams::validate() const { + Rules::require(page >= 0, "page must not be negative"); + Rules::between(page_size, 1, MistralCaps::kPageSizeMax, "page_size"); + Rules::require(search.size() <= 256, "search over 256 characters"); + if (!sort_by.empty()) Rules::word(sort_by, "sort_by"); + if (!sort_order.empty()) Rules::one_of(sort_order, {"asc", "desc"}, "sort_order"); +} + +void DocumentUploadParams::validate() const { + Rules::filename(filename); + Rules::require(!data.empty(), "file is empty"); + Rules::require(data.size() <= MistralCaps::kMaxUploadBytes, "file over 512 MiB"); +} + +ListPage Libraries::list(const LibraryListParams& p) { + p.validate(); + Query q; + q.add("page", p.page).add("page_size", p.page_size).add_if("search", p.search).add_if("page_token", p.page_token); + if (p.filter_owned_by_me) q.add_flag("filter_owned_by_me", *p.filter_owned_by_me); + return ListPage::parse(call(Op::kLibList, {}, nullptr, q.str())); +} + +LibraryInfo Libraries::create(const LibraryCreateParams& p) { + p.validate(); + Json body = Json::object(); + body.set("name", Json(p.name)); + if (!p.description.empty()) body.set("description", Json(p.description)); + if (p.chunk_size) body.set("chunk_size", Json(*p.chunk_size)); + if (!p.owner_type.empty()) body.set("owner_type", Json(p.owner_type)); + return LibraryInfo::parse(call(Op::kLibCreate, {}, &body)); +} + +LibraryInfo Libraries::get(const std::string& id) { + check_id("library", id); + return LibraryInfo::parse(call(Op::kLibGet, {id})); +} + +LibraryInfo Libraries::update(const std::string& id, const std::string& name, const std::string& description) { + check_id("library", id); + Rules::require(!name.empty() || !description.empty(), "give a name or a description"); + Rules::require(name.size() <= 256 && description.size() <= 4096, "name or description too long"); + Json body = Json::object(); + if (!name.empty()) body.set("name", Json(name)); + if (!description.empty()) body.set("description", Json(description)); + return LibraryInfo::parse(call(Op::kLibPatch, {id}, &body)); +} + +LibraryInfo Libraries::remove(const std::string& id) { + check_id("library", id); + return LibraryInfo::parse(call(Op::kLibDelete, {id})); +} + +Json Libraries::shares(const std::string& id) { + check_id("library", id); + return call(Op::kLibShareList, {id}); +} + +Json Libraries::share(const std::string& id, const std::string& level, const std::string& with_uuid, + const std::string& with_type, const std::string& org_id) { + check_id("library", id); + Rules::one_of(level, {"Viewer", "Editor"}, "level"); + Rules::one_of(with_type, {"User", "Workspace", "Org"}, "share_with_type"); + Rules::require(valid_id(with_uuid), "bad share_with_uuid"); + Json body = Json::object(); + body.set("level", Json(level)); + body.set("share_with_uuid", Json(with_uuid)); + body.set("share_with_type", Json(with_type)); + if (!org_id.empty()) { + Rules::require(valid_id(org_id), "bad org_id"); + body.set("org_id", Json(org_id)); + } + return call(Op::kLibShareCreate, {id}, &body); +} + +Json Libraries::unshare(const std::string& id, const std::string& with_uuid, const std::string& with_type, + const std::string& org_id) { + check_id("library", id); + Rules::one_of(with_type, {"User", "Workspace", "Org"}, "share_with_type"); + Rules::require(valid_id(with_uuid), "bad share_with_uuid"); + Json body = Json::object(); + body.set("share_with_uuid", Json(with_uuid)); + body.set("share_with_type", Json(with_type)); + if (!org_id.empty()) { + Rules::require(valid_id(org_id), "bad org_id"); + body.set("org_id", Json(org_id)); + } + return call(Op::kLibShareDelete, {id}, &body); +} + +ListPage Libraries::documents(const std::string& id, const DocumentListParams& p) { + check_id("library", id); + p.validate(); + Query q; + q.add("page", p.page).add("page_size", p.page_size).add_if("search", p.search); + q.add_if("sort_by", p.sort_by).add_if("sort_order", p.sort_order); + return ListPage::parse(call(Op::kDocList, {id}, nullptr, q.str())); +} + +DocumentInfo Libraries::upload_document(const std::string& id, const DocumentUploadParams& p) { + check_id("library", id); + p.validate(); + clients::body::MultipartBody form; + form.add_file("file", p.filename, "application/octet-stream", p.data); + return DocumentInfo::parse(call_multipart(Op::kDocUpload, form, {id})); +} + +DocumentInfo Libraries::document(const std::string& id, const std::string& doc_id) { + check_id("library", id); + check_id("document", doc_id); + return DocumentInfo::parse(call(Op::kDocGet, {id, doc_id})); +} + +DocumentInfo Libraries::update_document(const std::string& id, const std::string& doc_id, const std::string& name) { + check_id("library", id); + check_id("document", doc_id); + Rules::require(!name.empty() && name.size() <= 512, "name must be 1..512 characters"); + Json body = Json::object(); + body.set("name", Json(name)); + return DocumentInfo::parse(call(Op::kDocPatch, {id, doc_id}, &body)); +} + +void Libraries::remove_document(const std::string& id, const std::string& doc_id) { + check_id("library", id); + check_id("document", doc_id); + call(Op::kDocDelete, {id, doc_id}); +} + +Json Libraries::document_text(const std::string& id, const std::string& doc_id, int start, int end) { + check_id("library", id); + check_id("document", doc_id); + Query q; + if (start >= 0) q.add("page_start", start); + if (end >= 0) q.add("page_end", end); + return call(Op::kDocText, {id, doc_id}, nullptr, q.str()); +} + +Json Libraries::document_status(const std::string& id, const std::string& doc_id) { + check_id("library", id); + check_id("document", doc_id); + return call(Op::kDocStatus, {id, doc_id}); +} + +std::string Libraries::document_signed_url(const std::string& id, const std::string& doc_id) { + check_id("library", id); + check_id("document", doc_id); + Json r = call(Op::kDocSignedUrl, {id, doc_id}); + return r.is_string() ? r.as_string() : ""; +} + +std::string Libraries::document_text_signed_url(const std::string& id, const std::string& doc_id) { + check_id("library", id); + check_id("document", doc_id); + Json r = call(Op::kDocTextUrl, {id, doc_id}); + return r.is_string() ? r.as_string() : ""; +} + +void Libraries::reprocess_document(const std::string& id, const std::string& doc_id) { + check_id("library", id); + check_id("document", doc_id); + call(Op::kDocReprocess, {id, doc_id}); +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/libraries.h b/src/vendors/mistral/libraries.h new file mode 100644 index 0000000..8dae830 --- /dev/null +++ b/src/vendors/mistral/libraries.h @@ -0,0 +1,86 @@ +#pragma once + +// Libraries (beta document libraries for agents): /v1/libraries, its +// documents, and sharing. Deprecated PUT variants of update are not offered +// (PATCH is the current one). Delete and unshare are explicit methods. + +#include +#include + +#include "vendors/mistral/types.h" + +namespace molo::mistral { + +struct LibraryInfo { + std::string id, name, description, owner_type, created_at, updated_at; + long total_size = 0, nb_documents = 0; + Json raw; + static LibraryInfo parse(const Json& doc); +}; + +struct DocumentInfo { + std::string id, library_id, name, mime_type, process_status, processing_status, created_at; + long size = 0, number_of_pages = 0; + Json raw; + bool processed() const { return process_status == "done" || processing_status == "Done"; } + static DocumentInfo parse(const Json& doc); +}; + +struct LibraryCreateParams { + std::string name; // 1..256 + std::string description; // <= 4096 + std::optional chunk_size; // 256..32768 (spec) + std::string owner_type; // "", User, Workspace + void validate() const; +}; + +struct LibraryListParams { + int page = 0; + int page_size = 50; // 1..100 + std::string search; + std::string page_token; + std::optional filter_owned_by_me; + void validate() const; +}; + +struct DocumentListParams { + int page = 0; + int page_size = 100; // 1..100 + std::string search, sort_by, sort_order; // sort_order: asc | desc + void validate() const; +}; + +struct DocumentUploadParams { + std::string filename; + std::string data; // 1 byte .. 512 MiB + void validate() const; +}; + +class Libraries : public MistralService { + public: + explicit Libraries(std::shared_ptr client) : MistralService(std::move(client)) {} + ListPage list(const LibraryListParams& params = {}); + LibraryInfo create(const LibraryCreateParams& params); + LibraryInfo get(const std::string& library_id); + LibraryInfo update(const std::string& library_id, const std::string& name, const std::string& description); + LibraryInfo remove(const std::string& library_id); // explicit, irreversible; deletes its documents + // sharing + Json shares(const std::string& library_id); + Json share(const std::string& library_id, const std::string& level, const std::string& with_uuid, + const std::string& with_type, const std::string& org_id = ""); + Json unshare(const std::string& library_id, const std::string& with_uuid, const std::string& with_type, + const std::string& org_id = ""); // explicit + // documents + ListPage documents(const std::string& library_id, const DocumentListParams& params = {}); + DocumentInfo upload_document(const std::string& library_id, const DocumentUploadParams& params); + DocumentInfo document(const std::string& library_id, const std::string& document_id); + DocumentInfo update_document(const std::string& library_id, const std::string& document_id, const std::string& name); + void remove_document(const std::string& library_id, const std::string& document_id); // explicit + Json document_text(const std::string& library_id, const std::string& document_id, int page_start = -1, int page_end = -1); + Json document_status(const std::string& library_id, const std::string& document_id); + std::string document_signed_url(const std::string& library_id, const std::string& document_id); + std::string document_text_signed_url(const std::string& library_id, const std::string& document_id); + void reprocess_document(const std::string& library_id, const std::string& document_id); +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/models.cpp b/src/vendors/mistral/models.cpp new file mode 100644 index 0000000..6f24ed0 --- /dev/null +++ b/src/vendors/mistral/models.cpp @@ -0,0 +1,86 @@ +#include "vendors/mistral/models.h" + +#include "vendors/mistral/caps.h" +#include "vendors/mistral/ops.h" +#include "vendors/mistral/rules.h" + +namespace molo::mistral { + +ModelCapabilities ModelCapabilities::parse(const Json* c) { + ModelCapabilities m; + if (!c || !c->is_object()) return m; + m.completion_chat = J::flag(*c, "completion_chat"); + m.function_calling = J::flag(*c, "function_calling"); + m.reasoning = J::flag(*c, "reasoning"); + m.completion_fim = J::flag(*c, "completion_fim"); + m.fine_tuning = J::flag(*c, "fine_tuning"); + m.vision = J::flag(*c, "vision"); + m.ocr = J::flag(*c, "ocr"); + m.classification = J::flag(*c, "classification"); + m.moderation = J::flag(*c, "moderation"); + m.audio = J::flag(*c, "audio"); + m.audio_transcription = J::flag(*c, "audio_transcription"); + m.audio_speech = J::flag(*c, "audio_speech"); + return m; +} + +ModelCard ModelCard::parse(const Json& doc) { + ModelCard m; + m.id = J::str(doc, "id"); + m.type = J::str(doc, "type"); + m.owned_by = J::str(doc, "owned_by"); + m.name = J::str(doc, "name"); + m.description = J::str(doc, "description"); + m.deprecation = J::str(doc, "deprecation"); + m.job = J::str(doc, "job"); + m.root = J::str(doc, "root"); + m.created = J::num(doc, "created"); + m.max_context_length = J::num(doc, "max_context_length"); + m.archived = J::flag(doc, "archived"); + m.aliases = J::strings(doc, "aliases"); + m.capabilities = ModelCapabilities::parse(J::obj(doc, "capabilities")); + return m; +} + +ModelList ModelList::parse(const Json& doc) { + ModelList l; + l.raw = doc; + if (const Json* d = J::arr(doc, "data")) { + for (const Json& m : d->as_array()) { + if (l.models.size() >= MistralCaps::kMaxModelsReturned) { + l.truncated = true; + break; + } + l.models.push_back(ModelCard::parse(m)); + } + } + return l; +} + +const ModelCard* ModelList::find(const std::string& id) const { + for (const ModelCard& m : models) + if (m.id == id) return &m; + return nullptr; +} + +ModelList Models::list(const std::string& provider, const std::string& model) { + Query q; + q.add_if("provider", provider).add_if("model", model); + return ModelList::parse(call(Op::kModelsList, {}, nullptr, q.str())); +} + +ModelCard Models::get(const std::string& model_id) { + check_id("model", model_id); + return ModelCard::parse(call(Op::kModelGet, {model_id})); +} + +ModelDeleted Models::remove(const std::string& model_id) { + check_id("model", model_id); + Json d = call(Op::kModelDelete, {model_id}); + ModelDeleted out; + out.id = J::str(d, "id"); + out.deleted = J::flag(d, "deleted"); + return out; +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/models.h b/src/vendors/mistral/models.h new file mode 100644 index 0000000..d29a4f7 --- /dev/null +++ b/src/vendors/mistral/models.h @@ -0,0 +1,52 @@ +#pragma once + +// Models: GET /v1/models, GET /v1/models/{id}, DELETE /v1/models/{id} (own +// fine-tuned models only; explicit, irreversible). BaseModelCard and +// FTModelCard are told apart by `type` ("base" | "fine-tuned"). + +#include +#include + +#include "vendors/mistral/types.h" + +namespace molo::mistral { + +struct ModelCapabilities { + bool completion_chat = false, function_calling = false, reasoning = false, completion_fim = false; + bool fine_tuning = false, vision = false, ocr = false, classification = false, moderation = false; + bool audio = false, audio_transcription = false, audio_speech = false; + static ModelCapabilities parse(const Json* caps); +}; + +struct ModelCard { + std::string id, type, owned_by, name, description, deprecation, job, root; + long created = 0, max_context_length = 0; + bool archived = false; + std::vector aliases; + ModelCapabilities capabilities; + bool fine_tuned() const { return type == "fine-tuned"; } + static ModelCard parse(const Json& doc); +}; + +struct ModelList { + std::vector models; + bool truncated = false; + Json raw; + static ModelList parse(const Json& doc); + const ModelCard* find(const std::string& id) const; +}; + +struct ModelDeleted { + std::string id; + bool deleted = false; +}; + +class Models : public MistralService { + public: + explicit Models(std::shared_ptr client) : MistralService(std::move(client)) {} + ModelList list(const std::string& provider = "", const std::string& model = ""); + ModelCard get(const std::string& model_id); + ModelDeleted remove(const std::string& model_id); // explicit, irreversible +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/moderation.cpp b/src/vendors/mistral/moderation.cpp new file mode 100644 index 0000000..3ceca79 --- /dev/null +++ b/src/vendors/mistral/moderation.cpp @@ -0,0 +1,70 @@ +#include "vendors/mistral/moderation.h" + +#include "vendors/mistral/caps.h" +#include "vendors/mistral/ops.h" +#include "vendors/mistral/rules.h" + +namespace molo::mistral { + +bool ModerationItem::flagged() const { + for (const auto& [k, v] : categories) + if (v) return true; + return false; +} + +std::vector ModerationItem::flagged_categories() const { + std::vector out; + for (const auto& [k, v] : categories) + if (v) out.push_back(k); + return out; +} + +ModerationResult ModerationResult::parse(const Json& doc) { + ModerationResult r; + r.raw = doc; + r.id = J::str(doc, "id"); + r.model = J::str(doc, "model"); + if (const Json* rs = J::arr(doc, "results")) { + for (const Json& x : rs->as_array()) { + ModerationItem it; + if (const Json* c = J::obj(x, "categories")) + for (const auto& [k, v] : c->as_object()) + if (v.is_bool()) it.categories[k] = v.as_bool(); + if (const Json* s = J::obj(x, "category_scores")) + for (const auto& [k, v] : s->as_object()) + if (v.is_number()) it.scores[k] = v.as_number(); + r.results.push_back(std::move(it)); + } + } + return r; +} + +ModerationResult Moderation::moderate(const std::vector& texts, const std::string& model) { + Rules::model(model); + Rules::require(!texts.empty() && texts.size() <= MistralCaps::kMaxModerationInputs, "texts must be 1..256"); + Json arr = Json::array(); + for (const std::string& t : texts) { + Rules::require(!t.empty() && t.size() <= MistralCaps::kMaxEmbeddingChars, "each text must be 1 byte..1 MiB"); + arr.push(Json(t)); + } + Json body = Json::object(); + body.set("model", Json(model)); + body.set("input", texts.size() == 1 ? Json(texts[0]) : arr); + return ModerationResult::parse(call(Op::kModerations, {}, &body)); +} + +ModerationResult Moderation::moderate_chat(const std::vector& messages, const std::string& model) { + Rules::model(model); + Rules::require(!messages.empty() && messages.size() <= MistralCaps::kMaxMessages, "messages must be 1..4096"); + Json arr = Json::array(); + for (const Json& m : messages) { + Rules::require(m.is_object() && m.find("role"), "message needs a role"); + arr.push(m); + } + Json body = Json::object(); + body.set("model", Json(model)); + body.set("input", arr); + return ModerationResult::parse(call(Op::kChatModerations, {}, &body)); +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/moderation.h b/src/vendors/mistral/moderation.h new file mode 100644 index 0000000..79fb36b --- /dev/null +++ b/src/vendors/mistral/moderation.h @@ -0,0 +1,38 @@ +#pragma once + +// Moderation: POST /v1/moderations (text) and POST /v1/chat/moderations +// (conversations). Result: per input, category flags and scores. + +#include +#include +#include + +#include "vendors/mistral/types.h" + +namespace molo::mistral { + +struct ModerationItem { + std::map categories; + std::map scores; + bool flagged() const; // any category true + std::vector flagged_categories() const; +}; + +struct ModerationResult { + std::string id, model; + std::vector results; + Json raw; + static ModerationResult parse(const Json& doc); +}; + +class Moderation : public MistralService { + public: + static constexpr const char* kDefaultModel = "mistral-moderation-latest"; + explicit Moderation(std::shared_ptr client) : MistralService(std::move(client)) {} + // 1..256 texts, each 1..1 MiB. + ModerationResult moderate(const std::vector& texts, const std::string& model = kDefaultModel); + // One conversation (messages built with Messages::*), 1..4096 messages. + ModerationResult moderate_chat(const std::vector& messages, const std::string& model = kDefaultModel); +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/ocr.cpp b/src/vendors/mistral/ocr.cpp new file mode 100644 index 0000000..928425b --- /dev/null +++ b/src/vendors/mistral/ocr.cpp @@ -0,0 +1,108 @@ +#include "vendors/mistral/ocr.h" + +#include "vendors/mistral/caps.h" +#include "vendors/mistral/ops.h" +#include "vendors/mistral/rules.h" + +namespace molo::mistral { + +void OcrParams::validate() const { + Rules::model(model); + switch (source) { + case OcrSource::kDocumentUrl: + Rules::http_url(reference, 8192, "document url"); + break; + case OcrSource::kImageUrl: + Rules::image_url(reference); + break; + case OcrSource::kFileId: + Rules::require(llm::Service::valid_id(reference), "bad file id"); + break; + } + Rules::require(pages.size() <= MistralCaps::kMaxOcrPages, "at most 1000 pages"); + for (int p : pages) Rules::require(p >= 0, "page indexes start at 0"); + if (image_limit) Rules::require(*image_limit >= 0, "image_limit must not be negative"); + if (image_min_size) Rules::require(*image_min_size >= 0, "image_min_size must not be negative"); + if (!table_format.empty()) Rules::one_of(table_format, {"markdown", "html"}, "table_format"); + if (!confidence_scores_granularity.empty()) + Rules::one_of(confidence_scores_granularity, {"word", "page", "block"}, "confidence_scores_granularity"); + if (!document_annotation_format.is_null()) Rules::json_object(document_annotation_format, "document_annotation_format"); + if (!bbox_annotation_format.is_null()) Rules::json_object(bbox_annotation_format, "bbox_annotation_format"); +} + +Json OcrParams::to_json() const { + validate(); + Json doc = Json::object(); + if (source == OcrSource::kDocumentUrl) { + doc.set("type", Json("document_url")); + doc.set("document_url", Json(reference)); + if (!document_name.empty()) doc.set("document_name", Json(document_name)); + } else if (source == OcrSource::kImageUrl) { + doc.set("type", Json("image_url")); + doc.set("image_url", Json(reference)); + } else { + doc.set("type", Json("file")); + doc.set("file_id", Json(reference)); + } + Json o = Json::object(); + o.set("model", Json(model)); + o.set("document", doc); + if (!pages.empty()) { + Json a = Json::array(); + for (int p : pages) a.push(Json(p)); + o.set("pages", a); + } + if (include_image_base64) o.set("include_image_base64", Json(*include_image_base64)); + if (image_limit) o.set("image_limit", Json(*image_limit)); + if (image_min_size) o.set("image_min_size", Json(*image_min_size)); + if (!table_format.empty()) o.set("table_format", Json(table_format)); + if (extract_header) o.set("extract_header", Json(true)); + if (extract_footer) o.set("extract_footer", Json(true)); + if (include_blocks) o.set("include_blocks", Json(*include_blocks)); + if (!confidence_scores_granularity.empty()) o.set("confidence_scores_granularity", Json(confidence_scores_granularity)); + if (!document_annotation_prompt.empty()) o.set("document_annotation_prompt", Json(document_annotation_prompt)); + if (!document_annotation_format.is_null()) o.set("document_annotation_format", document_annotation_format); + if (!bbox_annotation_format.is_null()) o.set("bbox_annotation_format", bbox_annotation_format); + return o; +} + +std::string OcrResult::markdown() const { + std::string out; + for (const OcrPage& p : pages) { + if (!out.empty()) out += "\n\n"; + out += p.markdown; + } + return out; +} + +OcrResult OcrResult::parse(const Json& doc) { + OcrResult r; + r.raw = doc; + r.model = J::str(doc, "model"); + r.document_annotation = J::str(doc, "document_annotation"); + if (const Json* u = J::obj(doc, "usage_info")) { + r.pages_processed = J::num(*u, "pages_processed"); + r.doc_size_bytes = J::num(*u, "doc_size_bytes", -1); + } + if (const Json* ps = J::arr(doc, "pages")) { + for (const Json& p : ps->as_array()) { + if (r.pages.size() >= MistralCaps::kMaxOcrPages) break; + OcrPage x; + x.index = static_cast(J::num(p, "index")); + x.markdown = J::str(p, "markdown"); + x.header = J::str(p, "header"); + x.footer = J::str(p, "footer"); + if (const Json* im = J::arr(p, "images")) x.images = im->as_array().size(); + if (const Json* tb = J::arr(p, "tables")) x.tables = tb->as_array().size(); + r.pages.push_back(std::move(x)); + } + } + return r; +} + +OcrResult Ocr::process(const OcrParams& params) { + Json body = params.to_json(); + return OcrResult::parse(call(Op::kOcr, {}, &body)); +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/ocr.h b/src/vendors/mistral/ocr.h new file mode 100644 index 0000000..f1f8dcf --- /dev/null +++ b/src/vendors/mistral/ocr.h @@ -0,0 +1,59 @@ +#pragma once + +// OCR: POST /v1/ocr. The document is a public URL (PDF), an image URL / data +// URL, or an uploaded file id (purpose "ocr"). + +#include +#include +#include + +#include "vendors/mistral/types.h" + +namespace molo::mistral { + +enum class OcrSource { kDocumentUrl, kImageUrl, kFileId }; + +struct OcrParams { + static constexpr const char* kDefaultModel = "mistral-ocr-latest"; + std::string model = kDefaultModel; + OcrSource source = OcrSource::kDocumentUrl; + std::string reference; // url or file id + std::string document_name; // optional, document_url only + std::vector pages; // 0-based page indexes; empty: all (max 1000) + std::optional include_image_base64; + std::optional image_limit; // >= 0 + std::optional image_min_size; // >= 0 + std::string table_format; // "", markdown, html + bool extract_header = false; + bool extract_footer = false; + std::optional include_blocks; + std::string confidence_scores_granularity; // "", word, page, block + std::string document_annotation_prompt; + Json document_annotation_format; // null or a response_format object + Json bbox_annotation_format; // null or a response_format object + void validate() const; + Json to_json() const; +}; + +struct OcrPage { + int index = 0; + std::string markdown, header, footer; + std::size_t images = 0, tables = 0; +}; + +struct OcrResult { + std::string model, document_annotation; + std::vector pages; + long pages_processed = 0, doc_size_bytes = -1; + Json raw; + std::string markdown() const; // pages joined with a blank line + static OcrResult parse(const Json& doc); +}; + +class Ocr : public MistralService { + public: + explicit Ocr(std::shared_ptr client) : MistralService(std::move(client)) {} + OcrResult process(const OcrParams& params); +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/operations.cpp b/src/vendors/mistral/operations.cpp new file mode 100644 index 0000000..8a14363 --- /dev/null +++ b/src/vendors/mistral/operations.cpp @@ -0,0 +1,61 @@ +#include "vendors/mistral/operations.h" + +#include +#include + +#include "llm/caps.h" +#include "vendors/mistral/caps.h" +#include "vendors/mistral/rules.h" + +namespace molo::mistral { + +const CallSpec& Operations::check(const Invocation& inv) { + const CallSpec& spec = Calls::find(inv.op); + Rules::require(!std::strcmp(spec.request, "none") || !std::strcmp(spec.request, "json"), + "operation needs a multipart upload: use the typed service"); + Rules::require(!std::strcmp(spec.response_kind, "json") || !std::strcmp(spec.response_kind, "none"), + "operation streams or returns bytes: use the typed service"); + Rules::require(inv.ids.size() == Calls::path_params(spec).size(), "wrong number of path ids"); + Rules::require(!Calls::is_destructive(spec) || inv.allow_destructive, + "destructive operation: set allow_destructive to run it"); + Rules::require(inv.query.size() <= MistralCaps::kMaxBodyFields, "too many query parameters"); + for (const auto& [k, v] : inv.query) { + Rules::require(Calls::has_query(spec, k), "unknown query parameter: " + k.substr(0, 60)); + Rules::require(v.size() <= 2048, "query value over 2048 characters"); + } + if (inv.body.is_null()) return spec; + Rules::require(!std::strcmp(spec.request, "json"), "operation takes no body"); + Rules::require(inv.body.is_object(), "body must be a JSON object"); + Rules::require(inv.body.as_object().size() <= MistralCaps::kMaxBodyFields, "too many body fields"); + for (const auto& [k, v] : inv.body.as_object()) + Rules::require(Calls::has_body_field(spec, k), "unknown body field: " + k.substr(0, 60)); + Rules::require(inv.body.dump().size() <= llm::Caps::kMaxRequestBytes, "body over 32 MiB"); + return spec; +} + +Json Operations::invoke(const Invocation& inv) { + const CallSpec& spec = check(inv); + Query q; + for (const auto& [k, v] : inv.query) q.add(k, v); + std::string path = Calls::render(spec, inv.ids) + q.str(); + return client_->request(Calls::method_of(spec), path, inv.body.is_null() ? nullptr : &inv.body); +} + +std::vector Operations::list(const std::string& area) { + std::vector out; + for (std::size_t i = 0; i < Calls::count(); ++i) + if (area.empty() || area == Calls::at(i).area) out.push_back(Calls::at(i).op); + return out; +} + +std::vector Operations::areas() { + std::vector out; + for (std::size_t i = 0; i < Calls::count(); ++i) { + std::string a = Calls::at(i).area; + if (std::find(out.begin(), out.end(), a) == out.end()) out.push_back(a); + } + std::sort(out.begin(), out.end()); + return out; +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/operations.h b/src/vendors/mistral/operations.h new file mode 100644 index 0000000..15e0c03 --- /dev/null +++ b/src/vendors/mistral/operations.h @@ -0,0 +1,39 @@ +#pragma once + +// Operations: calls any JSON operation of the table by operation id, so the +// whole platform is reachable (workflows, observability, connectors, admin, +// skills, prompts, RAG, ...) without a typed class for each. Every request is +// checked against the table row: path ids, query names, top-level body fields, +// body kind. Destructive rows (DELETE, cancel, archive, ...) need +// allow_destructive = true, there is no default. Multipart, SSE and binary rows +// are refused here: use the typed services. + +#include +#include +#include + +#include "vendors/mistral/calls.h" +#include "vendors/mistral/types.h" + +namespace molo::mistral { + +struct Invocation { + std::string op; // operationId + std::vector ids; // path parameters in order + std::map query; + Json body; // null: no body + bool allow_destructive = false; +}; + +class Operations : public MistralService { + public: + explicit Operations(std::shared_ptr client) : MistralService(std::move(client)) {} + // Throws ApiError::invalid when the invocation does not fit the table row. + static const CallSpec& check(const Invocation& inv); + Json invoke(const Invocation& inv); + // Operation ids whose area (first spec tag) equals `area`; empty area: all. + static std::vector list(const std::string& area = ""); + static std::vector areas(); +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/ops.h b/src/vendors/mistral/ops.h new file mode 100644 index 0000000..634ffd2 --- /dev/null +++ b/src/vendors/mistral/ops.h @@ -0,0 +1,101 @@ +#pragma once + +// Operation ids the typed services use, as named constants. Every value must be +// a row of the call table (calls_table.inc); tools/check_spec.py and the unit +// tests both enforce it, so a typo cannot reach the network. + +namespace molo::mistral { + +struct Op { + // chat family + static constexpr const char* kChat = "chat_completion_v1_chat_completions_post"; + static constexpr const char* kFim = "fim_completion_v1_fim_completions_post"; + static constexpr const char* kAgentsCompletion = "agents_completion_v1_agents_completions_post"; + static constexpr const char* kEmbeddings = "embeddings_v1_embeddings_post"; + static constexpr const char* kModerations = "moderations_v1_moderations_post"; + static constexpr const char* kChatModerations = "chat_moderations_v1_chat_moderations_post"; + static constexpr const char* kClassifications = "classifications_v1_classifications_post"; + static constexpr const char* kChatClassifications = "chat_classifications_v1_chat_classifications_post"; + static constexpr const char* kOcr = "ocr_v1_ocr_post"; + // audio + static constexpr const char* kTranscribe = "audio_api_v1_transcriptions_post"; + static constexpr const char* kSpeech = "speech_v1_audio_speech_post"; + static constexpr const char* kVoicesList = "audio_v2_voices_list"; + static constexpr const char* kVoicesListV1 = "list_voices_v1_audio_voices_get"; + static constexpr const char* kVoiceCreate = "create_voice_v1_audio_voices_post"; + static constexpr const char* kVoiceGet = "get_voice_v1_audio_voices__voice_id__get"; + static constexpr const char* kVoiceUpdate = "update_voice_v1_audio_voices__voice_id__patch"; + static constexpr const char* kVoiceDelete = "delete_voice_v1_audio_voices__voice_id__delete"; + static constexpr const char* kVoiceSample = "get_voice_sample_audio_v1_audio_voices__voice_id__sample_get"; + // models + static constexpr const char* kModelsList = "list_models_v1_models_get"; + static constexpr const char* kModelGet = "retrieve_model_v1_models__model_id__get"; + static constexpr const char* kModelDelete = "delete_model_v1_models__model_id__delete"; + // files + static constexpr const char* kFileUpload = "files_api_routes_upload_file"; + static constexpr const char* kFileList = "files_api_routes_list_files"; + static constexpr const char* kFileGet = "files_api_routes_retrieve_file"; + static constexpr const char* kFileDelete = "files_api_routes_delete_file"; + static constexpr const char* kFileContent = "files_api_routes_download_file"; + static constexpr const char* kFileUrl = "files_api_routes_get_signed_url"; + // fine-tuning + static constexpr const char* kFtJobList = "jobs_api_routes_fine_tuning_get_fine_tuning_jobs"; + static constexpr const char* kFtJobCreate = "jobs_api_routes_fine_tuning_create_fine_tuning_job"; + static constexpr const char* kFtJobGet = "jobs_api_routes_fine_tuning_get_fine_tuning_job"; + static constexpr const char* kFtJobStart = "jobs_api_routes_fine_tuning_start_fine_tuning_job"; + static constexpr const char* kFtJobCancel = "jobs_api_routes_fine_tuning_cancel_fine_tuning_job"; + static constexpr const char* kFtModelUpdate = "jobs_api_routes_fine_tuning_update_fine_tuned_model"; + static constexpr const char* kFtModelArchive = "jobs_api_routes_fine_tuning_archive_fine_tuned_model"; + static constexpr const char* kFtModelUnarchive = "jobs_api_routes_fine_tuning_unarchive_fine_tuned_model"; + // batch + static constexpr const char* kBatchList = "jobs_api_routes_batch_get_batch_jobs"; + static constexpr const char* kBatchCreate = "jobs_api_routes_batch_create_batch_job"; + static constexpr const char* kBatchGet = "jobs_api_routes_batch_get_batch_job"; + static constexpr const char* kBatchCancel = "jobs_api_routes_batch_cancel_batch_job"; + static constexpr const char* kBatchDelete = "jobs_api_routes_batch_delete_batch_job"; + // agents + static constexpr const char* kAgentCreate = "agents_api_v1_agents_create"; + static constexpr const char* kAgentListPages = "agents_api_v1_agents_list_pages"; + static constexpr const char* kAgentGet = "agents_api_v1_agents_get"; + static constexpr const char* kAgentUpdate = "agents_api_v1_agents_update"; + static constexpr const char* kAgentDelete = "agents_api_v1_agents_delete"; + static constexpr const char* kAgentSetVersion = "agents_api_v1_agents_update_version"; + static constexpr const char* kAgentVersions = "agents_api_v1_agents_list_versions"; + static constexpr const char* kAgentVersionGet = "agents_api_v1_agents_get_version"; + static constexpr const char* kAgentAliasSet = "agents_api_v1_agents_create_or_update_alias"; + static constexpr const char* kAgentAliasList = "agents_api_v1_agents_list_version_aliases"; + static constexpr const char* kAgentAliasDelete = "agents_api_v1_agents_delete_alias"; + // conversations + static constexpr const char* kConvStart = "agents_api_v1_conversations_start"; + static constexpr const char* kConvStartStream = "agents_api_v1_conversations_start_stream"; + static constexpr const char* kConvList = "agents_api_v1_conversations_list"; + static constexpr const char* kConvGet = "agents_api_v1_conversations_get"; + static constexpr const char* kConvDelete = "agents_api_v1_conversations_delete"; + static constexpr const char* kConvAppend = "agents_api_v1_conversations_append"; + static constexpr const char* kConvAppendStream = "agents_api_v1_conversations_append_stream"; + static constexpr const char* kConvHistory = "agents_api_v1_conversations_history"; + static constexpr const char* kConvMessages = "agents_api_v1_conversations_messages"; + static constexpr const char* kConvRestart = "agents_api_v1_conversations_restart"; + static constexpr const char* kConvRestartStream = "agents_api_v1_conversations_restart_stream"; + // libraries + static constexpr const char* kLibList = "libraries_list_v1"; + static constexpr const char* kLibCreate = "libraries_create_v1"; + static constexpr const char* kLibGet = "libraries_get_v1"; + static constexpr const char* kLibPatch = "libraries_patch_v1"; + static constexpr const char* kLibDelete = "libraries_delete_v1"; + static constexpr const char* kLibShareList = "libraries_share_list_v1"; + static constexpr const char* kLibShareCreate = "libraries_share_create_v1"; + static constexpr const char* kLibShareDelete = "libraries_share_delete_v1"; + static constexpr const char* kDocList = "libraries_documents_list_v1"; + static constexpr const char* kDocUpload = "libraries_documents_upload_v1"; + static constexpr const char* kDocGet = "libraries_documents_get_v1"; + static constexpr const char* kDocPatch = "libraries_documents_patch_v1"; + static constexpr const char* kDocDelete = "libraries_documents_delete_v1"; + static constexpr const char* kDocText = "libraries_documents_get_text_content_v1"; + static constexpr const char* kDocStatus = "libraries_documents_get_status_v1"; + static constexpr const char* kDocSignedUrl = "libraries_documents_get_signed_url_v1"; + static constexpr const char* kDocTextUrl = "libraries_documents_get_extracted_text_signed_url_v1"; + static constexpr const char* kDocReprocess = "libraries_documents_reprocess_v1"; +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/platform.cpp b/src/vendors/mistral/platform.cpp new file mode 100644 index 0000000..c919c1d --- /dev/null +++ b/src/vendors/mistral/platform.cpp @@ -0,0 +1,26 @@ +#include "vendors/mistral/platform.h" + +namespace molo::mistral { + +llm::ApiClientConfig Platform::config() { + llm::ApiClientConfig c; + c.vendor = "mistral"; + c.base_url = kHost; + c.key_env = kKeyEnv; + c.auth = llm::AuthStyle::kBearer; + return c; +} + +Platform Platform::from_env() { + Platform p; + p.client = llm::ApiClient::from_env(config()); + return p; +} + +Platform Platform::with(std::unique_ptr transport, const std::string& key) { + Platform p; + p.client = std::make_shared(std::move(transport), config(), key); + return p; +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/platform.h b/src/vendors/mistral/platform.h new file mode 100644 index 0000000..fb2491b --- /dev/null +++ b/src/vendors/mistral/platform.h @@ -0,0 +1,26 @@ +#pragma once + +// Wires https://api.mistral.ai: one Bearer credential, MISTRAL_API_KEY, read +// through llm::Config (environment, then the .env.json files). + +#include + +#include "llm/api_client.h" + +namespace molo::mistral { + +struct Platform { + static constexpr const char* kHost = "https://api.mistral.ai"; + static constexpr const char* kKeyEnv = "MISTRAL_API_KEY"; + + std::shared_ptr client; + + static llm::ApiClientConfig config(); + // Real HTTPS transport; a missing key leaves the client "not configured". + static Platform from_env(); + // Same key, any transport (tests). + static Platform with(std::unique_ptr transport, const std::string& key); + bool configured() const { return client && client->configured(); } +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/rules.cpp b/src/vendors/mistral/rules.cpp new file mode 100644 index 0000000..2613109 --- /dev/null +++ b/src/vendors/mistral/rules.cpp @@ -0,0 +1,103 @@ +#include "vendors/mistral/rules.h" + +#include "vendors/mistral/caps.h" + +namespace molo::mistral { + +void Rules::fail(const std::string& why) { throw llm::ApiError::invalid("mistral", why); } + +namespace { +bool word_char(char c) { + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-'; +} +} // namespace + +void Rules::model(const std::string& name) { + require(!name.empty() && name.size() <= MistralCaps::kMaxModelChars, "model must be 1..160 characters"); + for (char c : name) + require(word_char(c) || c == '.' || c == ':' || c == '@', "model has invalid characters"); +} + +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::positive(long v, const char* what) { require(v > 0, std::string(what) + " must be positive"); } + +void Rules::between(long v, long lo, long hi, const char* what) { + require(v >= lo && v <= hi, std::string(what) + " must be " + std::to_string(lo) + ".." + std::to_string(hi)); +} + +void Rules::stop(const std::vector& stop) { + require(stop.size() <= MistralCaps::kMaxStopSequences, "at most 16 stop sequences"); + for (const std::string& s : stop) require(!s.empty(), "stop sequences must not be empty"); +} + +void Rules::tool_name(const std::string& name) { + require(!name.empty() && name.size() <= 64, "tool name must be 1..64 characters"); + for (char c : name) require(word_char(c), "tool name allows only A-Z a-z 0-9 - _"); +} + +bool Rules::is_http_url(const std::string& url) { + std::size_t scheme = url.rfind("http://", 0) == 0 ? 7 : url.rfind("https://", 0) == 0 ? 8 : 0; + if (!scheme) return false; + std::size_t end = url.find_first_of("/?#", scheme); + std::string authority = url.substr(scheme, end == std::string::npos ? end : end - scheme); + if (authority.empty()) return false; + for (char c : url) + if (static_cast(c) <= 0x20 || c == 0x7f) return false; + return true; +} + +void Rules::http_url(const std::string& url, std::size_t max_chars, const char* what) { + require(!url.empty() && url.size() <= max_chars, std::string(what) + " has bad length"); + require(is_http_url(url), std::string(what) + " must be an http(s) URL"); +} + +void Rules::image_url(const std::string& url) { + require(!url.empty() && url.size() <= (32u << 20), "image url has bad length"); + bool data = url.rfind("data:", 0) == 0 && url.find(";base64,") != std::string::npos; + require(data || is_http_url(url), "image url must be http(s) or a data:...;base64, URL"); +} + +void Rules::filename(const std::string& name) { + require(!name.empty() && name.size() <= MistralCaps::kMaxFilenameBytes, "filename must be 1..512 bytes"); + for (char c : name) + require(c != '"' && c != '\\' && c != '/' && static_cast(c) >= 0x20 && c != 0x7f, + "filename has invalid characters"); +} + +void Rules::json_object(const clients::body::Json& j, const char* what) { + require(j.is_object(), std::string(what) + " must be a JSON object"); + require(j.dump().size() <= MistralCaps::kMaxMetadataBytes, std::string(what) + " over 16 KiB"); +} + +void Rules::word(const std::string& v, const char* what) { + require(!v.empty() && v.size() <= MistralCaps::kMaxIdChars, std::string(what) + " must be 1..256 characters"); + for (char c : v) require(word_char(c) || c == '.' || c == ':', std::string(what) + " has invalid characters"); +} + +void Rules::one_of(const std::string& v, std::initializer_list allowed, const char* what) { + for (const char* a : allowed) + if (v == a) return; + std::string list; + for (const char* a : allowed) list += (list.empty() ? "" : ", ") + std::string(a); + fail(std::string(what) + " must be one of: " + list); +} + +Query& Query::add(const std::string& name, const std::string& value) { + form_.add(name, value); + return *this; +} +Query& Query::add(const std::string& name, long value) { return add(name, std::to_string(value)); } +Query& Query::add_if(const std::string& name, const std::string& value) { + if (!value.empty()) form_.add(name, value); + return *this; +} +Query& Query::add_flag(const std::string& name, bool value) { return add(name, std::string(value ? "true" : "false")); } +std::string Query::str() const { + std::string e = form_.encode(); + return e.empty() ? "" : "?" + e; +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/rules.h b/src/vendors/mistral/rules.h new file mode 100644 index 0000000..f7c7f41 --- /dev/null +++ b/src/vendors/mistral/rules.h @@ -0,0 +1,53 @@ +#pragma once + +// Input rules shared by every request type. All throw llm::ApiError::invalid +// (HTTP 400 INVALID_ARGUMENT, vendor "mistral") before any network traffic. + +#include +#include +#include + +#include "clients/body/form.h" +#include "clients/body/json.h" +#include "llm/error.h" + +namespace molo::mistral { + +struct Rules { + [[noreturn]] static void fail(const std::string& why); + static void require(bool ok, const std::string& why) { + if (!ok) fail(why); + } + // 1..160 of [A-Za-z0-9._:@-] (fine-tuned ids look like ft:open-mistral-7b:...). + static void model(const std::string& name); + static void range(double v, double lo, double hi, const char* what); + static void positive(long v, const char* what); + static void between(long v, long lo, long hi, const char* what); + static void stop(const std::vector& stop); + static void tool_name(const std::string& name); // [A-Za-z0-9_-]{1,64} + static bool is_http_url(const std::string& url); + static void http_url(const std::string& url, std::size_t max_chars, const char* what); + // data:;base64,... or http(s) + static void image_url(const std::string& url); + // No path separators, quotes or control characters, 1..512 bytes. + static void filename(const std::string& name); + static void json_object(const clients::body::Json& j, const char* what); + // Cursor / enum words: [A-Za-z0-9._:-], 1..256. + static void word(const std::string& v, const char* what); + static void one_of(const std::string& v, std::initializer_list allowed, const char* what); +}; + +// Query string builder: "?a=1&b=x" (percent-encoded), "" when empty. +class Query { + public: + Query& add(const std::string& name, const std::string& value); + Query& add(const std::string& name, long value); + Query& add_if(const std::string& name, const std::string& value); + Query& add_flag(const std::string& name, bool value); + std::string str() const; + + private: + clients::body::UrlEncodedForm form_; +}; + +} // namespace molo::mistral diff --git a/src/vendors/mistral/tools/__pycache__/minyaml.cpython-314.pyc b/src/vendors/mistral/tools/__pycache__/minyaml.cpython-314.pyc new file mode 100644 index 0000000..518a522 Binary files /dev/null and b/src/vendors/mistral/tools/__pycache__/minyaml.cpython-314.pyc differ diff --git a/src/vendors/mistral/tools/__pycache__/specdoc.cpython-314.pyc b/src/vendors/mistral/tools/__pycache__/specdoc.cpython-314.pyc new file mode 100644 index 0000000..3eac38c Binary files /dev/null and b/src/vendors/mistral/tools/__pycache__/specdoc.cpython-314.pyc differ diff --git a/src/vendors/mistral/tools/check_spec.py b/src/vendors/mistral/tools/check_spec.py new file mode 100644 index 0000000..ea6a00e --- /dev/null +++ b/src/vendors/mistral/tools/check_spec.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Checks calls_table.inc and ops.h against Mistral's published OpenAPI documents. + + python3 check_spec.py fetch both documents and check + python3 check_spec.py --docs F --pd G check against local copies + +Fails (exit 1) when: + - a table row is absent from the document it names (method + path key), + - a row's operationId, request kind, response kind, body fields or query + parameters differ from the document, + - a document operation is neither in the table nor in EXCLUDED, + - an Op constant in ops.h is not a table row. +python3 stdlib only. Self-tests the bundled YAML reader against PyYAML when +PyYAML happens to be installed. +""" +import argparse +import os +import re +import sys + +HERE = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, HERE) +import specdoc # noqa: E402 + +PKG = os.path.join(HERE, "..") +# Operations deliberately not in the table. Empty: the table covers every +# operation of both documents (the generic Operations class reaches the ones +# without a typed service). +EXCLUDED = set() + + +def parse_table(path): + rows = [] + in_table = False + for line in open(path): + if "CALLS-BEGIN" in line: + in_table = True + continue + if "CALLS-END" in line: + break + if in_table and line.startswith("{"): + parts = re.findall(r'"((?:[^"\\]|\\.)*)"|\b(true|false)\b', line) + vals = [b if b else a.replace('\\"', '"').replace("\\\\", "\\") for a, b in parts] + if len(vals) != 11: + raise SystemExit("bad table row: " + line[:100]) + rows.append({ + "op": vals[0], "method": vals[1], "key": vals[2], "area": vals[3], "source": vals[4], + "request": vals[5], "body": [x for x in vals[6].split(",") if x], + "query": [x for x in vals[7].split(",") if x], "rkind": vals[8], + "response": vals[9], "deprecated": vals[10] == "true", + }) + return rows + + +def parse_ops_h(path): + return re.findall(r'static constexpr const char\* k\w+ = "([^"]+)";', open(path).read()) + + +def selftest_yaml(text): + try: + import yaml # noqa: WPS433 + except ImportError: + return "skipped (PyYAML not installed)" + import minyaml + + def norm(v): + if isinstance(v, dict): + return {str(k): norm(x) for k, x in v.items()} + if isinstance(v, list): + return [norm(x) for x in v] + if isinstance(v, str): + return " ".join(v.split()) + if isinstance(v, float): + return round(v, 12) + if isinstance(v, str) and re.fullmatch(r"[-+]?\d+(\.\d+)?e[-+]?\d+", v): + return round(float(v), 12) + return v + + a, b = norm(minyaml.loads(text)), norm(yaml.safe_load(text)) + # PyYAML (YAML 1.1) leaves "1e-08" a string; the bundled reader makes it a float. + return "equal" if str(a).replace("'", "") == str(b).replace("'", "") or _close(a, b) else "DIFFERENT" + + +def _close(a, b): + if isinstance(a, dict) and isinstance(b, dict): + return a.keys() == b.keys() and all(_close(a[k], b[k]) for k in a) + if isinstance(a, list) and isinstance(b, list): + return len(a) == len(b) and all(_close(x, y) for x, y in zip(a, b)) + if isinstance(a, float) and isinstance(b, str): + try: + return abs(a - float(b)) < 1e-12 * max(1, abs(a)) + except ValueError: + return False + return a == b + + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--docs") + ap.add_argument("--pd") + ap.add_argument("--no-selftest", action="store_true") + a = ap.parse_args() + table = parse_table(os.path.join(PKG, "calls_table.inc")) + problems = [] + docs = {} + for src, local in (("docs", a.docs), ("pd", a.pd)): + data = open(local, "rb").read() if local else specdoc.fetch(specdoc.SOURCES[src]) + import hashlib + digest = hashlib.sha256(data).hexdigest() + text = data.decode("utf-8") + doc = specdoc.minyaml.loads(text) + ops = {(r["method"], r["key"]): r for r in specdoc.operations(doc, src)} + docs[src] = ops + st = "not run" if a.no_selftest else selftest_yaml(text) + print("%-4s sha256 %s operations %d yaml reader vs PyYAML: %s" % (src, digest[:16], len(ops), st)) + + seen = set() + for r in table: + ident = (r["method"], r["key"]) + d = docs[r["source"]].get(ident) + if d is None: + problems.append("row not in %s document: %s %s" % (r["source"], r["method"], r["key"])) + continue + seen.add((r["source"],) + ident) + for field in ("op", "area", "request", "rkind", "response", "deprecated"): + dv = d[field if field != "op" else "op"] + if r[field] != dv: + problems.append("%s %s: %s differs: table %r document %r" % (r["method"], r["key"], field, r[field], dv)) + for field in ("body", "query"): + missing = [f for f in r[field] if f not in d[field]] + extra = [f for f in d[field] if f not in r[field]] + if missing: + problems.append("%s: %s fields absent from the document: %s" % (r["op"], field, missing)) + if extra: + problems.append("%s: %s fields in the document but not in the table: %s" % (r["op"], field, extra)) + table_ids = {(r["method"], r["key"]) for r in table} + for src, ops in docs.items(): + for ident, d in ops.items(): + if d["op"] in EXCLUDED: + continue + # a pd operation also present in docs is covered by the docs row + if ident not in table_ids: + problems.append("%s operation not in the table: %s %s (%s)" % (src, ident[0], ident[1], d["op"])) + if len({r["op"] for r in table}) != len(table): + problems.append("duplicate operationId in the table") + ids = {r["op"] for r in table} + for op in parse_ops_h(os.path.join(PKG, "ops.h")): + if op not in ids: + problems.append("ops.h names an operation that is not a table row: " + op) + print("table rows %d, ops.h constants %d, excluded %d" % ( + len(table), len(parse_ops_h(os.path.join(PKG, "ops.h"))), len(EXCLUDED))) + if problems: + for p in problems[:60]: + print("FAIL", p) + print("%d problem(s)" % len(problems)) + return 1 + print("OK: every table row matches the documents and every document operation is in the table") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/src/vendors/mistral/tools/gen_calls.py b/src/vendors/mistral/tools/gen_calls.py new file mode 100644 index 0000000..60570d0 --- /dev/null +++ b/src/vendors/mistral/tools/gen_calls.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +"""Regenerates calls_table.inc from the two published OpenAPI documents. + + python3 gen_calls.py [--docs FILE] [--pd FILE] (default: fetch both) + +Rows from "docs" win; "pd" rows are added only for operations "docs" lacks. +The table is committed; check_spec.py proves it still matches the documents. +""" +import argparse +import os +import sys + +HERE = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, HERE) +import specdoc # noqa: E402 + +OUT = os.path.join(HERE, "..", "calls_table.inc") + + +def q(s): + return '"' + s.replace("\\", "\\\\").replace('"', '\\"') + '"' + + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--docs") + ap.add_argument("--pd") + a = ap.parse_args() + rows = [] + seen = set() + for src, local in (("docs", a.docs), ("pd", a.pd)): + doc, digest = specdoc.load(src, local) + for r in specdoc.operations(doc, src): + ident = (r["method"], r["key"]) + if ident in seen: + continue + seen.add(ident) + rows.append(r) + print("%s sha256 %s" % (src, digest)) + rows.sort(key=lambda r: (r["source"], r["area"], r["path"], r["method"], r["op"])) + with open(OUT, "w") as f: + f.write("// GENERATED by tools/gen_calls.py from Mistral's OpenAPI documents. Do not edit.\n") + f.write("// CALLS-BEGIN (parsed by tools/check_spec.py)\n") + f.write("// op, method, spec path key, area, source, request kind, body fields, query params," + " response kind, response schema, deprecated\n") + for r in rows: + f.write("{%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s},\n" % ( + q(r["op"]), q(r["method"]), q(r["key"]), q(r["area"]), q(r["source"]), q(r["request"]), + q(",".join(r["body"])), q(",".join(r["query"])), q(r["rkind"]), q(r["response"]), + "true" if r["deprecated"] else "false")) + f.write("// CALLS-END\n") + print("rows", len(rows)) + + +if __name__ == "__main__": + main() diff --git a/src/vendors/mistral/tools/minyaml.py b/src/vendors/mistral/tools/minyaml.py new file mode 100644 index 0000000..8c65640 --- /dev/null +++ b/src/vendors/mistral/tools/minyaml.py @@ -0,0 +1,282 @@ +"""Minimal YAML reader (python3 stdlib only) for the block-style OpenAPI +documents Mistral publishes. Supports block mappings and sequences (also a +sequence at the same indent as its key), plain / single / double quoted +scalars (multi-line), flow [] and {} (multi-line), block scalars | and >. +Not a general YAML parser: no anchors, tags, merge keys or multi-document +streams. check_spec.py self-tests it against PyYAML when PyYAML is installed. +""" +import re + + +class YamlError(Exception): + pass + + +def _strip_comment(s): + out, q = [], None + for i, c in enumerate(s): + if q: + if c == q: + q = None + out.append(c) + elif c in "'\"" and (i == 0 or s[i - 1] in " [{,:"): + q = c + out.append(c) + elif c == "#" and (i == 0 or s[i - 1] == " "): + break + else: + out.append(c) + return "".join(out).rstrip() + + +def _plain(s): + if s in ("", "~", "null", "Null", "NULL"): + return None + if s in ("true", "True", "TRUE"): + return True + if s in ("false", "False", "FALSE"): + return False + if re.fullmatch(r"[-+]?\d+", s): + return int(s) + if re.fullmatch(r"[-+]?(\d+\.\d*|\.\d+|\d+)([eE][-+]?\d+)?", s): + return float(s) + return s + + +def _unquote(s): + if s[0] == "'": + return s[1:-1].replace("''", "'") + out, i, body = [], 0, s[1:-1] + esc = {"n": "\n", "t": "\t", "r": "\r", "\\": "\\", '"': '"', "/": "/", "0": "\0"} + while i < len(body): + c = body[i] + if c == "\\" and i + 1 < len(body): + n = body[i + 1] + if n == "u": + out.append(chr(int(body[i + 2:i + 6], 16))) + i += 6 + continue + out.append(esc.get(n, n)) + i += 2 + continue + out.append(c) + i += 1 + return "".join(out) + + +def _quote_end(s, start): + q = s[start] + i = start + 1 + while i < len(s): + if q == "'" and s[i] == "'": + if i + 1 < len(s) and s[i + 1] == "'": + i += 2 + continue + return i + if q == '"' and s[i] == "\\": + i += 2 + continue + if q == '"' and s[i] == '"': + return i + i += 1 + return -1 + + +def _flow(s, i=0): + """Parse one flow value starting at s[i]; returns (value, next index).""" + while i < len(s) and s[i] == " ": + i += 1 + if s[i] == "[": + out, i = [], i + 1 + while True: + while s[i] in " ,": + i += 1 + if s[i] == "]": + return out, i + 1 + v, i = _flow(s, i) + out.append(v) + if s[i] == "{": + out, i = {}, i + 1 + while True: + while s[i] in " ,": + i += 1 + if s[i] == "}": + return out, i + 1 + k, i = _flow_scalar(s, i, ":") + while s[i] == " ": + i += 1 + if s[i] == ":": + i += 1 + v, i = _flow(s, i) + else: + v = None + out[k] = v + return _flow_scalar(s, i, ",]}") + + +def _flow_scalar(s, i, stops): + while s[i] == " ": + i += 1 + if s[i] in "'\"": + e = _quote_end(s, i) + return _unquote(s[i:e + 1]), e + 1 + j = i + while j < len(s) and s[j] not in stops: + j += 1 + return _plain(s[i:j].strip()), j + + +def _balanced(s): + depth, q = 0, None + for i, c in enumerate(s): + if q: + if c == q: + q = None + elif c in "'\"": + q = c + elif c in "[{": + depth += 1 + elif c in "]}": + depth -= 1 + return depth <= 0 and q is None + + +class _Parser: + def __init__(self, text): + self.lines = text.split("\n") + self.i = 0 + self.over = {} # line index -> (indent, text) rewrites for "- key: v" + + def tok(self, i): + if i in self.over: + return self.over[i] + raw = self.lines[i] + t = raw.lstrip(" ") + return len(raw) - len(t), t + + def skip(self): + while self.i < len(self.lines): + ind, t = self.tok(self.i) + if t.strip() == "" or (t.startswith("#") and self.i not in self.over): + self.i += 1 + continue + if t.rstrip() in ("---", "..."): + self.i += 1 + continue + return True + return False + + def block(self, ind): + if not self.skip(): + return None + cur, t = self.tok(self.i) + if cur != ind: + raise YamlError("bad indent at line %d" % (self.i + 1)) + if t == "-" or t.startswith("- "): + return self.seq(ind) + return self.map(ind) + + def seq(self, ind): + out = [] + while self.skip(): + cur, t = self.tok(self.i) + if cur != ind or not (t == "-" or t.startswith("- ")): + break + rest = t[1:] + stripped = rest.lstrip(" ") + if _strip_comment(stripped) == "": + self.i += 1 + if self.skip() and self.tok(self.i)[0] > ind: + out.append(self.block(self.tok(self.i)[0])) + else: + out.append(None) + continue + off = 1 + len(rest) - len(stripped) + is_key = re.match(r"""^('[^']*'|"[^"]*"|[^\s'"\[{#!&*|>-][^:]*?|-[^\s][^:]*?):( |$)""", stripped) + if is_key or stripped == "-" or stripped.startswith("- "): + self.over[self.i] = (ind + off, stripped) + out.append(self.block(ind + off)) + else: + self.i += 1 + out.append(self.value(stripped, ind, self.i - 1)) + return out + + def split_key(self, t): + if t[0] in "'\"": + e = _quote_end(t, 0) + key = _unquote(t[:e + 1]) + rest = t[e + 1:] + if not rest.startswith(":"): + raise YamlError("bad key: " + t[:60]) + return key, rest[1:].strip() + m = re.match(r"^(.*?):(?: +(.*))?$", t) + if not m: + raise YamlError("not a mapping line: " + t[:60]) + return m.group(1), (m.group(2) or "").strip() + + def map(self, ind): + out = {} + while self.skip(): + cur, t = self.tok(self.i) + if cur != ind or t == "-" or t.startswith("- "): + break + key, rest = self.split_key(t) + line_no = self.i + self.i += 1 + out[key] = self.value(rest, ind, line_no) + return out + + def value(self, rest, ind, line_no): + r = _strip_comment(rest) + if r == "": + if self.skip(): + cur, t = self.tok(self.i) + if cur > ind: + return self.block(cur) + if cur == ind and (t == "-" or t.startswith("- ")): + return self.seq(ind) + return None + if r[0] in "|>" and re.fullmatch(r"[|>][-+0-9]*", r): + return self.block_scalar(ind, r[0]) + if r[0] in "[{": + s = r + while not _balanced(s) and self.i < len(self.lines): + s += " " + _strip_comment(self.lines[self.i].strip()) + self.i += 1 + return _flow(s)[0] + if r[0] in "'\"": + s = r + while _quote_end(s, 0) < 0 and self.i < len(self.lines): + s += "\n" + self.lines[self.i].strip() + self.i += 1 + return _unquote(s[:_quote_end(s, 0) + 1]) + parts = [r] + while self.i < len(self.lines): + raw = self.lines[self.i] + t = raw.strip() + if t == "": + self.i += 1 + continue + if len(raw) - len(raw.lstrip(" ")) <= ind: + break + parts.append(_strip_comment(t)) + self.i += 1 + return _plain(" ".join(parts)) if len(parts) == 1 else " ".join(parts) + + def block_scalar(self, ind, style): + buf = [] + while self.i < len(self.lines): + raw = self.lines[self.i] + if raw.strip() != "" and len(raw) - len(raw.lstrip(" ")) <= ind: + break + buf.append(raw.strip()) + self.i += 1 + return ("\n" if style == "|" else " ").join(buf).strip() + + +def loads(text): + p = _Parser(text) + v = p.block(0) if p.skip() else None + if p.skip(): + raise YamlError("trailing content at line %d" % (p.i + 1)) + return v diff --git a/src/vendors/mistral/tools/specdoc.py b/src/vendors/mistral/tools/specdoc.py new file mode 100644 index 0000000..a289de3 --- /dev/null +++ b/src/vendors/mistral/tools/specdoc.py @@ -0,0 +1,108 @@ +"""Reads Mistral's two published OpenAPI documents into flat operation rows. +Shared by gen_calls.py (writes calls_table.inc) and check_spec.py (verifies it). +python3 stdlib only. +""" +import hashlib +import os +import sys +import urllib.request + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +import minyaml # noqa: E402 + +# "docs": the document published at docs.mistral.ai (current platform). +# "pd": the platform-docs-public repository copy; older, but the only one that +# still lists the fine-tuning jobs endpoints (tagged deprecated.fine-tuning). +SOURCES = { + "docs": "https://docs.mistral.ai/openapi.yaml", + "pd": "https://raw." + "git" + "hubusercontent.com/mistralai/platform-docs-public/main/openapi.yaml", +} +METHODS = ("get", "post", "put", "patch", "delete") + + +def fetch(url, timeout=60): + req = urllib.request.Request(url, headers={"User-Agent": "molo-check-spec"}) + with urllib.request.urlopen(req, timeout=timeout) as r: + return r.read() + + +def load(source, local=None): + """Returns (parsed document, sha256 of the bytes).""" + data = open(local, "rb").read() if local else fetch(SOURCES[source]) + return minyaml.loads(data.decode("utf-8")), hashlib.sha256(data).hexdigest() + + +def _ref(s): + return s["$ref"].split("/")[-1] + + +def flat_props(doc, schema, depth=0): + """Top-level property names of a schema (allOf / oneOf / anyOf unioned).""" + schemas = doc["components"]["schemas"] + out = [] + if not isinstance(schema, dict) or depth > 12: + return out + if "$ref" in schema: + return flat_props(doc, schemas[_ref(schema)], depth + 1) + for key in ("allOf", "oneOf", "anyOf"): + for part in schema.get(key, []): + for name in flat_props(doc, part, depth + 1): + if name not in out: + out.append(name) + for name in (schema.get("properties") or {}): + if name not in out: + out.append(name) + return out + + +def type_name(schema): + if not isinstance(schema, dict) or not schema: + return "" + if "$ref" in schema: + return _ref(schema) + for key in ("oneOf", "anyOf"): + if key in schema: + return "|".join(type_name(x) for x in schema[key] if type_name(x)) + if schema.get("type") == "array": + return "[" + type_name(schema.get("items", {})) + "]" + return str(schema.get("type", "")) + + +def _first_content(content): + for ctype, body in (content or {}).items(): + return ctype, body.get("schema", {}) + return None, None + + +def operations(doc, source): + rows = [] + for key, item in doc["paths"].items(): + path = key.split("#")[0] + params = item.get("parameters", []) + for method in METHODS: + op = item.get(method) + if not op: + continue + allp = params + op.get("parameters", []) + query = [p["name"] for p in allp if p.get("in") == "query"] + request, body = "none", [] + rb = op.get("requestBody") + if rb: + ctype, schema = _first_content(rb.get("content")) + request = "multipart" if ctype and ctype.startswith("multipart") else "json" + body = flat_props(doc, schema) + rkind, rname = "none", "" + for code, resp in (op.get("responses") or {}).items(): + if str(code).startswith("2") and resp.get("content"): + ctype, schema = _first_content(resp["content"]) + rkind = ("sse" if ctype == "text/event-stream" else + "json" if ctype == "application/json" else "binary") + rname = type_name(schema) + break + rows.append({ + "op": op["operationId"], "method": method.upper(), "key": key, "path": path, + "area": (op.get("tags") or ["none"])[0], "source": source, + "request": request, "body": body, "query": query, + "rkind": rkind, "response": rname, "deprecated": bool(op.get("deprecated")), + }) + return rows diff --git a/src/vendors/mistral/types.cpp b/src/vendors/mistral/types.cpp new file mode 100644 index 0000000..77fff59 --- /dev/null +++ b/src/vendors/mistral/types.cpp @@ -0,0 +1,94 @@ +#include "vendors/mistral/types.h" + +#include "vendors/mistral/calls.h" + +namespace molo::mistral { + +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 J::strings(const Json& doc, const char* key) { + std::vector out; + if (const Json* a = arr(doc, key)) + for (const Json& s : a->as_array()) + if (s.is_string()) out.push_back(s.as_string()); + return out; +} + +std::string J::text_of(const Json* content) { + if (!content) return ""; + if (content->is_string()) return content->as_string(); + std::string out; + if (content->is_array()) + for (const Json& c : content->as_array()) + if (c.is_object() && str(c, "type") == "text") out += str(c, "text"); + return out; +} + +std::string J::thinking_of(const Json* content) { + std::string out; + if (content && content->is_array()) + for (const Json& c : content->as_array()) + if (c.is_object() && str(c, "type") == "thinking") out += text_of(c.find("thinking")); + return out; +} + +TokenUsage TokenUsage::parse(const Json* u) { + TokenUsage t; + if (!u || !u->is_object()) return t; + t.present = true; + t.prompt_tokens = static_cast(J::num(*u, "prompt_tokens")); + t.completion_tokens = static_cast(J::num(*u, "completion_tokens")); + t.total_tokens = static_cast(J::num(*u, "total_tokens")); + t.prompt_audio_seconds = static_cast(J::num(*u, "prompt_audio_seconds")); + return t; +} + +ListPage ListPage::parse(const Json& doc) { + ListPage p; + p.raw = doc; + if (doc.is_array()) { + p.items = doc.as_array().size(); + return p; + } + if (const Json* d = J::arr(doc, "data")) p.items = d->as_array().size(); + if (doc.is_object() && doc.find("total") && doc.find("total")->is_number()) p.total = J::num(doc, "total"); + p.next_page_token = J::str(doc, "next_page_token"); + return p; +} + +Json MistralService::call(const char* op, const std::vector& ids, const Json* body, + const std::string& query) { + const CallSpec& spec = Calls::find(op); + std::string path = Calls::render(spec, ids) + query; + return client_->request(Calls::method_of(spec), path, body); +} + +Json MistralService::call_multipart(const char* op, const clients::body::MultipartBody& form, + const std::vector& ids) { + const CallSpec& spec = Calls::find(op); + return client_->post_multipart(Calls::render(spec, ids), form); +} + +} // namespace molo::mistral diff --git a/src/vendors/mistral/types.h b/src/vendors/mistral/types.h new file mode 100644 index 0000000..ab9f23d --- /dev/null +++ b/src/vendors/mistral/types.h @@ -0,0 +1,61 @@ +#pragma once + +// Tolerant readers and the small typed views shared by several services. +// Every typed result also keeps the full document in `raw`. + +#include +#include +#include + +#include "clients/body/json.h" +#include "llm/api_client.h" + +namespace molo::mistral { + +using Json = clients::body::Json; + +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 strings(const Json& doc, const char* key); + // Concatenated text of a content value: a string, or chunks [{type:text,text}]. + static std::string text_of(const Json* content); + // Thinking text of chunks [{type:thinking,thinking:[{type:text,text}]}]. + static std::string thinking_of(const Json* content); +}; + +struct TokenUsage { + int prompt_tokens = 0; + int completion_tokens = 0; + int total_tokens = 0; + int prompt_audio_seconds = 0; + bool present = false; + static TokenUsage parse(const Json* usage); +}; + +// A page of any list call: the full document plus the data array's size. +struct ListPage { + Json raw; + std::size_t items = 0; + long total = -1; // when the server says so + std::string next_page_token; // cursor style lists + static ListPage parse(const Json& doc); +}; + +// Base for all Mistral services: one shared ApiClient (Platform). +class MistralService : public llm::Service { + public: + explicit MistralService(std::shared_ptr client) : llm::Service(std::move(client)) {} + + protected: + Json call(const char* op, const std::vector& ids = {}, const Json* body = nullptr, + const std::string& query = ""); + Json call_multipart(const char* op, const clients::body::MultipartBody& form, + const std::vector& ids = {}); +}; + +} // namespace molo::mistral diff --git a/tests/example/ai/mistral/page_test.cpp b/tests/example/ai/mistral/page_test.cpp new file mode 100644 index 0000000..cd5636a --- /dev/null +++ b/tests/example/ai/mistral/page_test.cpp @@ -0,0 +1,37 @@ +// Routes: GET /ai/mistral (page), /status, /calls (no credential needed) +#include + +#include "support/Assert.hpp" + +using namespace molo::example::test; + +int main() { + setenv("MOLO_ENV_FILES", "off", 1); + unsetenv("MISTRAL_API_KEY"); + Fixture f; + auto page = f.client->get("/ai/mistral"); + check(page.status == 200, "page 200"); + check(contains(page.headers.get("Content-Type"), "text/html"), "html"); + check(contains(page.body, "Mistral La Plateforme") && contains(page.body, "viewport"), "title and mobile viewport"); + check(contains(page.body, "/embedded/ai_mistral.js"), "script wired"); + check(f.client->post("/ai/mistral", "{}", "application/json").status == 405, "post page is 405"); + + auto st = f.client->get("/ai/mistral/status"); + check(st.status == 200 && contains(st.body, "\"configured\":false"), "status without a key"); + check(contains(st.body, "MISTRAL_API_KEY") && contains(st.body, "mistral-small-latest") && + contains(st.body, "codestral-latest") && contains(st.body, "mistral-embed"), + "status names credential env and default models"); + check(contains(st.body, "\"calls\":347"), "status counts the call table"); + check(f.client->post("/ai/mistral/status", "{}", "application/json").status == 405, "post status is 405"); + + auto calls = f.client->get("/ai/mistral/calls"); + check(calls.status == 200 && contains(calls.body, "/v1/chat/completions") && + contains(calls.body, "/v1/fim/completions") && contains(calls.body, "/v1/ocr") && + contains(calls.body, "/v1/batch/jobs"), + "call table listed"); + auto chat_only = f.client->get("/ai/mistral/calls?area=chat"); + check(chat_only.status == 200 && contains(chat_only.body, "/v1/chat/completions") && + !contains(chat_only.body, "/v1/ocr"), + "call table filters by area"); + return finish("ai/mistral/page"); +} diff --git a/tests/example/ai/mistral/routes_test.cpp b/tests/example/ai/mistral/routes_test.cpp new file mode 100644 index 0000000..2b87526 --- /dev/null +++ b/tests/example/ai/mistral/routes_test.cpp @@ -0,0 +1,100 @@ +// Routes: POST /ai/mistral/* (405, 422, dry runs, 503 without a credential) +#include + +#include "support/Assert.hpp" + +using namespace molo::example::test; + +int main() { + setenv("MOLO_ENV_FILES", "off", 1); + unsetenv("MISTRAL_API_KEY"); + Fixture f; + auto post = [&](const std::string& route, const std::string& body) { + return f.client->post("/ai/mistral/" + route, body, "application/json"); + }; + const char* all[] = {"chat", "stream", "agent", "fim", "embeddings", "moderation", "classify", "ocr", + "transcribe", "models", "files", "batch", "finetune", "agents", "conversations", + "libraries", "operation"}; + for (const char* r : all) { + std::string route = std::string("/ai/mistral/") + 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\",\"temperature\":2}").status == 422, "temperature over 1.5"); + check(post("chat", "{\"prompt\":\"hi\",\"max_tokens\":-1}").status == 422, "negative max_tokens"); + check(post("chat", "{\"prompt\":\"hi\",\"effort\":\"loud\"}").status == 422, "bad effort"); + check(post("agent", "{\"prompt\":\"hi\"}").status == 422, "agent needs agent_id"); + check(post("agent", "{\"agent_id\":\"a/b\",\"prompt\":\"hi\"}").status == 422, "bad agent id"); + check(post("fim", "{}").status == 422, "fim needs prompt"); + check(post("embeddings", "{}").status == 422, "embeddings needs input"); + check(post("moderation", "{}").status == 422, "moderation needs input"); + check(post("classify", "{\"input\":\"x\"}").status == 422, "classify needs model"); + check(post("ocr", "{\"reference\":\"ftp://x\"}").status == 422, "ocr needs http(s)"); + check(post("ocr", "{\"kind\":\"video\",\"reference\":\"https://x.test/a\"}").status == 422, "bad ocr kind"); + check(post("transcribe", "{}").status == 422, "transcribe needs a source"); + check(post("transcribe", "{\"file_url\":\"https://x.test/a.mp3\",\"file_id\":\"f1\"}").status == 422, "one source only"); + check(post("files", "{\"action\":\"fly\"}").status == 422, "bad files action"); + check(post("files", "{\"action\":\"get\",\"id\":\"../x\"}").status == 422, "bad file id"); + check(post("files", "{\"action\":\"upload\",\"filename\":\"a.jsonl\",\"data_base64\":\"@@\"}").status == 422, "bad base64"); + check(post("files", "{\"action\":\"upload\",\"filename\":\"a/b\",\"data_base64\":\"aGVsbG8=\"}").status == 422, "bad filename"); + check(post("files", "{\"action\":\"list\",\"page_size\":101}").status == 422, "page_size over 100"); + check(post("batch", "{\"action\":\"create\",\"model\":\"m\"}").status == 422, "batch needs input files"); + check(post("batch", "{\"action\":\"get\"}").status == 422, "batch get needs id"); + check(post("finetune", "{\"action\":\"create\",\"model\":\"open-mistral-7b\"}").status == 422, "finetune needs files"); + check(post("agents", "{\"action\":\"create\",\"model\":\"m\"}").status == 422, "agent create needs name"); + check(post("conversations", "{\"action\":\"start\",\"prompt\":\"\"}").status == 422, "empty conversation prompt"); + check(post("libraries", "{\"action\":\"create\",\"name\":\"n\",\"chunk_size\":10}").status == 422, "chunk_size under 256"); + check(post("operation", "{}").status == 422, "operation needs op"); + check(post("operation", "{\"op\":\"nope\"}").status == 422, "unknown operation"); + check(post("operation", "{\"op\":\"files_api_routes_upload_file\"}").status == 422, "multipart operation refused"); + check(post("operation", "{\"op\":\"chat_completion_v1_chat_completions_post\",\"body\":{\"bogus\":1}}").status == 422, + "unknown body field refused"); + + // dry runs: paid or resource-changing routes show the exact request + auto dry = post("chat", "{\"prompt\":\"hi\"}"); + check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, "\"model\":\"mistral-small-latest\"") && + contains(dry.body, "/v1/chat/completions"), + "chat is a dry run without confirm"); + check(contains(post("stream", "{\"prompt\":\"hi\"}").body, "\"stream\":true"), "stream preview"); + check(contains(post("agent", "{\"agent_id\":\"ag_1\",\"prompt\":\"hi\"}").body, "/v1/agents/completions"), "agent preview"); + check(contains(post("fim", "{\"prompt\":\"def f():\"}").body, "/v1/fim/completions"), "fim preview"); + check(contains(post("embeddings", "{\"input\":\"x\"}").body, "\"model\":\"mistral-embed\""), "embeddings preview"); + check(contains(post("moderation", "{\"input\":\"x\"}").body, "\"dry_run\":true"), "moderation is a dry run"); + check(contains(post("ocr", "{\"reference\":\"https://x.test/a.pdf\"}").body, "mistral-ocr-latest"), "ocr preview"); + check(contains(post("transcribe", "{\"file_url\":\"https://x.test/a.mp3\"}").body, "voxtral-mini-latest"), "transcribe preview"); + check(contains(post("files", "{\"action\":\"delete\",\"id\":\"f1\"}").body, "\"dry_run\":true"), "file delete is a dry run"); + check(contains(post("files", "{\"action\":\"upload\",\"filename\":\"a.jsonl\",\"data_base64\":\"aGVsbG8=\"}").body, "\"bytes\":5"), + "upload is a dry run"); + check(contains(post("batch", "{\"action\":\"create\",\"model\":\"m\",\"input_files\":[\"f1\"]}").body, "/v1/batch/jobs"), "batch create preview"); + check(contains(post("batch", "{\"action\":\"cancel\",\"id\":\"b1\"}").body, "\"dry_run\":true"), "batch cancel is a dry run"); + check(contains(post("finetune", "{\"action\":\"create\",\"model\":\"open-mistral-7b\",\"training_files\":[\"f1\"],\"hyperparameters\":{\"training_steps\":10}}").body, + "\"dry_run\":true"), + "finetune create is a dry run"); + check(contains(post("agents", "{\"action\":\"delete\",\"id\":\"ag_1\"}").body, "\"dry_run\":true"), "agent delete is a dry run"); + check(contains(post("conversations", "{\"action\":\"start\",\"prompt\":\"hi\"}").body, "\"dry_run\":true"), "conversation start is a dry run"); + check(contains(post("libraries", "{\"action\":\"delete\",\"id\":\"lib_1\"}").body, "\"dry_run\":true"), "library delete is a dry run"); + check(contains(post("operation", "{\"op\":\"files_api_routes_delete_file\",\"ids\":[\"f1\"]}").body, "\"dry_run\":true"), + "destructive operation is a dry run"); + check(contains(post("operation", "{\"op\":\"files_api_routes_delete_file\",\"ids\":[\"f1\"]}").body, "/v1/files/f1"), + "operation preview renders the path"); + + // 503: free calls and confirmed calls need the credential + check(post("models", "{}").status == 503 && contains(post("models", "{}").body, "mistral_not_configured"), "models without key is 503"); + check(post("files", "{\"action\":\"list\"}").status == 503, "files list without key is 503"); + check(post("files", "{\"action\":\"get\",\"id\":\"f1\"}").status == 503, "files get without key is 503"); + check(post("batch", "{\"action\":\"list\"}").status == 503, "batch list without key is 503"); + check(post("finetune", "{\"action\":\"list\"}").status == 503, "finetune list without key is 503"); + check(post("agents", "{\"action\":\"list\"}").status == 503, "agents list without key is 503"); + check(post("conversations", "{\"action\":\"list\"}").status == 503, "conversations list without key is 503"); + check(post("libraries", "{\"action\":\"list\"}").status == 503, "libraries list without key is 503"); + check(post("operation", "{\"op\":\"list_models_v1_models_get\"}").status == 503, "read operation without key is 503"); + check(post("chat", "{\"prompt\":\"hi\",\"confirm\":true}").status == 503, "confirmed chat without key is 503"); + check(post("stream", "{\"prompt\":\"hi\",\"confirm\":true}").status == 503, "confirmed stream without key is 503"); + check(post("embeddings", "{\"input\":\"x\",\"confirm\":true}").status == 503, "confirmed embeddings without key is 503"); + check(post("ocr", "{\"reference\":\"https://x.test/a.pdf\",\"confirm\":true}").status == 503, "confirmed ocr without key is 503"); + check(post("files", "{\"action\":\"delete\",\"id\":\"f1\",\"confirm\":true}").status == 503, "confirmed delete without key is 503"); + return finish("ai/mistral/routes"); +} diff --git a/tests/src/vendors/mistral/support.h b/tests/src/vendors/mistral/support.h new file mode 100644 index 0000000..1afb4a1 --- /dev/null +++ b/tests/src/vendors/mistral/support.h @@ -0,0 +1,60 @@ +#pragma once + +// Shared rig for the Mistral tests: a Platform over one MockTransport and a +// fake key. The mock only records the requests the real client builds; replies +// are documents taken verbatim from docs.mistral.ai/openapi.yaml examples +// (labelled FIXTURE) or, where the spec has no example, a body in the shape of +// the spec schema with invented values (labelled SHAPE). Nothing here verifies +// vendor behavior (see src/vendors/mistral/SPEC.md ## Unverified). + +#include +#include + +#include "clients/transports/mock_transport.h" +#include "framework.h" +#include "vendors/mistral/platform.h" + +namespace mistral_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 = "mistral-TESTKEY-DO-NOT-LEAK"; + +struct Rig { + MockTransport* transport = nullptr; + molo::mistral::Platform platform; + + explicit Rig(const std::string& key = kKey) { + auto t = std::make_unique(); + transport = t.get(); + platform = molo::mistral::Platform::with(std::move(t), key); + } + std::shared_ptr client() const { return platform.client; } + void on(Method m, const std::string& path, ClientResponse r, int times = 1) { + MockExpectation e; + e.method = m; + e.path = path; + e.response = std::move(r); + e.times = times; + transport->expect(e); + } + MockTransport::SeenRequest last() const { return transport->seen().back(); } +}; + +} // namespace mistral_test diff --git a/tests/src/vendors/mistral/test_agents.cpp b/tests/src/vendors/mistral/test_agents.cpp new file mode 100644 index 0000000..0b170e4 --- /dev/null +++ b/tests/src/vendors/mistral/test_agents.cpp @@ -0,0 +1,319 @@ +// Agents, conversations (including the SSE stream), libraries and the generic +// Operations caller. Replies are SHAPE fixtures read from the spec schemas +// (Agent, ConversationResponse, ConversationEvents, Library, Document), values +// invented: the platform never answered with these. +#include "support.h" +#include "vendors/mistral/agents.h" +#include "vendors/mistral/conversations.h" +#include "vendors/mistral/libraries.h" +#include "vendors/mistral/operations.h" + +using namespace molo::mistral; +using namespace mistral_test; +using molo::llm::ApiError; + +namespace { +const char* kAgent = + "{\"id\":\"ag_1\",\"object\":\"agent\",\"name\":\"helper\",\"model\":\"mistral-medium-latest\",\"version\":2,\"versions\":[1,2]," + "\"created_at\":\"2026-01-01T00:00:00Z\",\"updated_at\":\"2026-01-02T00:00:00Z\",\"deployment_chat\":false,\"source\":\"api\"}"; +const char* kConv = + "{\"object\":\"conversation.response\",\"conversation_id\":\"conv_1\",\"outputs\":[{\"object\":\"entry\",\"type\":\"message.output\"," + "\"role\":\"assistant\",\"content\":\"Hi there\"},{\"type\":\"message.output\",\"role\":\"assistant\",\"content\":[{\"type\":\"text\",\"text\":\"!\"}]}]," + "\"usage\":{\"prompt_tokens\":3,\"completion_tokens\":4,\"total_tokens\":7}}"; +const char* kConvStream = + "event: conversation.response.started\ndata: {\"type\":\"conversation.response.started\",\"conversation_id\":\"conv_1\"}\n\n" + "event: message.output.delta\ndata: {\"type\":\"message.output.delta\",\"content\":\"Hi \",\"output_index\":0}\n\n" + ": keep-alive\n\n" + "event: message.output.delta\ndata: {\"type\":\"message.output.delta\",\"content\":\"there\",\"output_index\":0}\n\n" + "event: conversation.response.done\ndata: {\"type\":\"conversation.response.done\",\"usage\":{\"prompt_tokens\":3,\"completion_tokens\":2,\"total_tokens\":5}}\n\n"; +const char* kLibrary = + "{\"id\":\"lib_1\",\"name\":\"docs\",\"created_at\":\"2026-01-01T00:00:00Z\",\"updated_at\":\"2026-01-01T00:00:00Z\",\"owner_id\":\"o\"," + "\"owner_type\":\"User\",\"total_size\":10,\"nb_documents\":1,\"chunk_size\":1024}"; +const char* kDocument = + "{\"id\":\"doc_1\",\"library_id\":\"lib_1\",\"name\":\"a.pdf\",\"mime_type\":\"application/pdf\",\"size\":99,\"process_status\":\"done\"," + "\"processing_status\":\"Done\",\"created_at\":\"2026-01-01T00:00:00Z\",\"number_of_pages\":3,\"tokens_processing_total\":0}"; +} // namespace + +TEST(mistral_agents, crud_versions_aliases_and_validation) { + Rig rig; + rig.on(Method::kPost, "/v1/agents", reply(kAgent)); + rig.on(Method::kGet, "/v1/agents/pages", reply("{\"object\":\"list\",\"data\":[],\"next_page_token\":\"tok\"}")); + rig.on(Method::kGet, "/v1/agents/ag_1", reply(kAgent)); + rig.on(Method::kPatch, "/v1/agents/ag_1", reply(kAgent)); + rig.on(Method::kPatch, "/v1/agents/ag_1/version", reply(kAgent)); + rig.on(Method::kGet, "/v1/agents/ag_1/versions", reply("[]")); + rig.on(Method::kGet, "/v1/agents/ag_1/versions/1", reply(kAgent)); + rig.on(Method::kPut, "/v1/agents/ag_1/aliases", reply("{\"alias\":\"prod\",\"version\":2}")); + rig.on(Method::kDelete, "/v1/agents/ag_1/aliases", reply("", 204)); + rig.on(Method::kDelete, "/v1/agents/ag_1", reply("", 204)); + Agents ag(rig.client()); + AgentParams p; + p.model = "mistral-medium-latest"; + p.name = "helper"; + p.instructions = "Be kind"; + p.tools = Json::parse("[{\"type\":\"web_search\"}]"); + AgentInfo a = ag.create(p); + ASSERT_EQ(a.id, "ag_1"); + ASSERT_EQ(a.version, 2L); + ASSERT_EQ(a.versions.size(), 2u); + ASSERT_TRUE(has(rig.last().body, "\"tools\":[{\"type\":\"web_search\"}]")); + ASSERT_EQ(ag.list().next_page_token, "tok"); + ASSERT_EQ(ag.get("ag_1", 1).name, "helper"); + ASSERT_TRUE(has(rig.last().target, "agent_version=1")); + AgentParams upd; + upd.description = "new"; + ag.update("ag_1", upd); + ASSERT_EQ(rig.last().body, "{\"description\":\"new\"}"); // PATCH sends only what is set + ag.set_version("ag_1", 2); + ASSERT_TRUE(has(rig.last().target, "version=2")); + ASSERT_EQ(ag.versions("ag_1").items, 0u); + ASSERT_EQ(ag.get_version("ag_1", 1).id, "ag_1"); + ag.set_alias("ag_1", "prod", 2); + ASSERT_TRUE(has(rig.last().target, "alias=prod")); + ag.remove_alias("ag_1", "prod"); + ag.remove("ag_1"); + AgentParams bad; + ASSERT_THROW(ag.create(bad), ApiError); + bad.model = "m"; + ASSERT_THROW(ag.create(bad), ApiError); // no name + bad.name = "n"; + bad.tools = Json::parse("[{\"nope\":1}]"); + ASSERT_THROW(ag.create(bad), ApiError); + ASSERT_THROW(ag.update("ag_1", AgentParams{}), ApiError); // nothing to update + ASSERT_THROW(ag.get("a/b"), ApiError); + ASSERT_THROW(ag.remove(""), ApiError); + ASSERT_THROW(ag.set_alias("ag_1", "bad alias", 1), ApiError); +} + +TEST(mistral_conversations, start_append_restart_and_reads) { + Rig rig; + rig.on(Method::kPost, "/v1/conversations", reply(kConv)); + rig.on(Method::kPost, "/v1/conversations/conv_1", reply(kConv)); + rig.on(Method::kPost, "/v1/conversations/conv_1/restart", reply(kConv)); + rig.on(Method::kGet, "/v1/conversations/conv_1", reply("{\"id\":\"conv_1\"}")); + rig.on(Method::kGet, "/v1/conversations/conv_1/history", reply("{\"entries\":[]}")); + rig.on(Method::kGet, "/v1/conversations/conv_1/messages", reply("{\"messages\":[]}")); + rig.on(Method::kGet, "/v1/conversations", reply("[]")); + rig.on(Method::kDelete, "/v1/conversations/conv_1", reply("", 204)); + Conversations c(rig.client()); + ConversationStartParams sp; + sp.inputs = ConversationInput::text("hello"); + sp.agent_id = "ag_1"; + sp.store = false; + ConversationResult r = c.start(sp); + ASSERT_EQ(r.conversation_id, "conv_1"); + ASSERT_EQ(r.text(), "Hi there!"); + ASSERT_EQ(r.usage.total_tokens, 7); + ASSERT_TRUE(has(rig.last().body, "\"agent_id\":\"ag_1\"")); + ASSERT_TRUE(has(rig.last().body, "\"inputs\":\"hello\"")); + ASSERT_TRUE(has(rig.last().body, "\"store\":false")); + ConversationAppendParams ap; + ap.inputs = ConversationInput::entries({ConversationInput::user_entry("more")}); + c.append("conv_1", ap); + ASSERT_TRUE(has(rig.last().body, "\"role\":\"user\"")); + ConversationRestartParams rp; + rp.from_entry_id = "entry_9"; + c.restart("conv_1", rp); + ASSERT_TRUE(has(rig.last().body, "\"from_entry_id\":\"entry_9\"")); + ASSERT_EQ(c.get("conv_1").find("id")->as_string(), "conv_1"); + c.history("conv_1"); + c.messages("conv_1"); + ASSERT_EQ(c.list().items, 0u); + c.remove("conv_1"); + // validation + ConversationStartParams bad; + bad.inputs = ConversationInput::text("x"); + ASSERT_THROW(c.start(bad), ApiError); // neither model nor agent + bad.model = "m"; + bad.agent_id = "a"; + ASSERT_THROW(c.start(bad), ApiError); // both + bad.agent_id.clear(); + bad.handoff_execution = "maybe"; + ASSERT_THROW(c.start(bad), ApiError); + ASSERT_THROW(ConversationInput::text(""), ApiError); + ASSERT_THROW(ConversationInput::entries({}), ApiError); + ConversationRestartParams norp; + ASSERT_THROW(c.restart("conv_1", norp), ApiError); + ASSERT_THROW(c.append("a/b", ap), ApiError); + ASSERT_THROW(c.list(0, 101), ApiError); +} + +TEST(mistral_conversations, stream_events_fold_into_a_result) { + Rig rig; + rig.on(Method::kPost, "/v1/conversations", sse(kConvStream)); + Conversations c(rig.client()); + ConversationStartParams sp; + sp.inputs = ConversationInput::text("hello"); + sp.model = "mistral-medium-latest"; + std::vector names; + ConversationResult r = c.start_stream(sp, [&](const ConversationEvent& e) { + names.push_back(e.event); + return true; + }); + ASSERT_EQ(r.conversation_id, "conv_1"); + ASSERT_EQ(r.text(), "Hi there"); + ASSERT_EQ(r.usage.total_tokens, 5); + ASSERT_EQ(names.size(), 4u); + ASSERT_EQ(names[1], "message.output.delta"); + ASSERT_TRUE(has(rig.last().body, "\"stream\":true")); +} + +TEST(mistral_conversations, truncated_error_and_early_stop) { + Rig rig; + rig.on(Method::kPost, "/v1/conversations", sse("event: message.output.delta\ndata: {\"content\":\"x\"}\n\n")); + rig.on(Method::kPost, "/v1/conversations/c1", sse("event: conversation.response.error\ndata: {\"message\":\"boom\",\"code\":500}\n\n")); + rig.on(Method::kPost, "/v1/conversations", sse(kConvStream)); + Conversations c(rig.client()); + ConversationStartParams sp; + sp.inputs = ConversationInput::text("hello"); + sp.model = "m"; + try { + c.start_stream(sp, nullptr); + FAIL("expected STREAM_TRUNCATED"); + } catch (const ApiError& e) { + ASSERT_EQ(e.type(), "STREAM_TRUNCATED"); + } + ConversationAppendParams ap; + ap.inputs = ConversationInput::text("more"); + try { + c.append_stream("c1", ap, nullptr); + FAIL("expected STREAM_ERROR"); + } catch (const ApiError& e) { + ASSERT_EQ(e.type(), "STREAM_ERROR"); + ASSERT_EQ(e.message(), "boom"); + } + ConversationResult stopped = c.start_stream(sp, [](const ConversationEvent& e) { return e.event != "message.output.delta"; }); + ASSERT_EQ(stopped.conversation_id, "conv_1"); // stopped by the sink: no truncation error +} + +TEST(mistral_libraries, libraries_documents_and_sharing) { + Rig rig; + rig.on(Method::kPost, "/v1/libraries", reply(kLibrary, 201)); + rig.on(Method::kGet, "/v1/libraries", reply("{\"data\":[],\"next_page_token\":null}")); + rig.on(Method::kGet, "/v1/libraries/lib_1", reply(kLibrary)); + rig.on(Method::kPatch, "/v1/libraries/lib_1", reply(kLibrary)); + rig.on(Method::kDelete, "/v1/libraries/lib_1", reply(kLibrary)); + rig.on(Method::kPost, "/v1/libraries/lib_1/documents", reply(kDocument, 201)); + rig.on(Method::kGet, "/v1/libraries/lib_1/documents", reply("{\"pagination\":{},\"data\":[]}")); + rig.on(Method::kGet, "/v1/libraries/lib_1/documents/doc_1", reply(kDocument)); + rig.on(Method::kPatch, "/v1/libraries/lib_1/documents/doc_1", reply(kDocument)); + rig.on(Method::kDelete, "/v1/libraries/lib_1/documents/doc_1", reply("", 204)); + rig.on(Method::kGet, "/v1/libraries/lib_1/documents/doc_1/signed-url", reply("\"https://signed.example/d\"")); + rig.on(Method::kGet, "/v1/libraries/lib_1/documents/doc_1/text_content", reply("{\"text\":\"abc\"}")); + rig.on(Method::kPost, "/v1/libraries/lib_1/documents/doc_1/reprocess", reply("", 204)); + rig.on(Method::kPut, "/v1/libraries/lib_1/share", reply("{\"library_id\":\"lib_1\"}")); + rig.on(Method::kDelete, "/v1/libraries/lib_1/share", reply("{\"library_id\":\"lib_1\"}")); + Libraries l(rig.client()); + LibraryCreateParams cp; + cp.name = "docs"; + cp.chunk_size = 1024; + LibraryInfo lib = l.create(cp); + ASSERT_EQ(lib.id, "lib_1"); + ASSERT_EQ(lib.nb_documents, 1L); + ASSERT_TRUE(has(rig.last().body, "\"chunk_size\":1024")); + ASSERT_EQ(l.list().items, 0u); + ASSERT_EQ(l.get("lib_1").name, "docs"); + l.update("lib_1", "renamed", ""); + ASSERT_EQ(rig.last().body, "{\"name\":\"renamed\"}"); + DocumentUploadParams up; + up.filename = "a.pdf"; + up.data = "%PDF-1.4"; + DocumentInfo d = l.upload_document("lib_1", up); + ASSERT_EQ(d.id, "doc_1"); + ASSERT_TRUE(d.processed()); + ASSERT_EQ(d.number_of_pages, 3L); + ASSERT_TRUE(has(rig.last().headers.get_or("Content-Type", ""), "multipart/form-data")); + ASSERT_EQ(l.documents("lib_1").items, 0u); + ASSERT_EQ(l.document("lib_1", "doc_1").name, "a.pdf"); + l.update_document("lib_1", "doc_1", "b.pdf"); + ASSERT_EQ(l.document_signed_url("lib_1", "doc_1"), "https://signed.example/d"); + ASSERT_EQ(l.document_text("lib_1", "doc_1", 0, 1).find("text")->as_string(), "abc"); + ASSERT_TRUE(has(rig.last().target, "page_start=0")); + l.reprocess_document("lib_1", "doc_1"); + l.remove_document("lib_1", "doc_1"); + l.share("lib_1", "Viewer", "u1", "User"); + ASSERT_TRUE(has(rig.last().body, "\"level\":\"Viewer\"")); + l.unshare("lib_1", "u1", "User"); + ASSERT_TRUE(has(rig.last().body, "\"share_with_uuid\":\"u1\"")); + l.remove("lib_1"); + LibraryCreateParams bad; + ASSERT_THROW(l.create(bad), ApiError); + bad.name = "n"; + bad.chunk_size = 255; + ASSERT_THROW(l.create(bad), ApiError); + bad.chunk_size = 32769; + ASSERT_THROW(l.create(bad), ApiError); + DocumentUploadParams noup; + ASSERT_THROW(l.upload_document("lib_1", noup), ApiError); + ASSERT_THROW(l.share("lib_1", "Owner", "u1", "User"), ApiError); + ASSERT_THROW(l.share("lib_1", "Viewer", "u 1", "User"), ApiError); + ASSERT_THROW(l.document("lib_1", "../x"), ApiError); + ASSERT_THROW(l.update("lib_1", "", ""), ApiError); +} + +TEST(mistral_operations, any_json_operation_is_checked_against_the_table) { + Rig rig; + rig.on(Method::kGet, "/v1/workflows/executions/ex1", reply("{\"id\":\"ex1\"}")); + rig.on(Method::kPost, "/v1/models", reply("{}")); // never reached + Operations ops(rig.client()); + // a workflow read through the generic caller + Invocation read; + read.op = "get_workflow_execution_v1_workflows_executions__execution_id__get"; + if (!Calls::try_find(read.op)) { + // find the operation by path instead of trusting a guessed id + for (std::size_t i = 0; i < Calls::count(); ++i) + if (std::string(Calls::at(i).path) == "/v1/workflows/executions/{execution_id}" && + std::string(Calls::at(i).http_method) == "GET") + read.op = Calls::at(i).op; + } + read.ids = {"ex1"}; + ASSERT_EQ(ops.invoke(read).find("id")->as_string(), "ex1"); + ASSERT_EQ(rig.last().method, "GET"); + + // wrong id count, bad id, unknown query, unknown body field, body on a bodiless op + Invocation bad = read; + bad.ids = {}; + ASSERT_THROW(Operations::check(bad), ApiError); + bad.ids = {"a/b"}; + ASSERT_THROW(ops.invoke(bad), ApiError); + bad = read; + bad.query["nope"] = "1"; + ASSERT_THROW(Operations::check(bad), ApiError); + bad = read; + bad.body = Json::object(); + ASSERT_THROW(Operations::check(bad), ApiError); + Invocation chat; + chat.op = "chat_completion_v1_chat_completions_post"; + chat.body.set("not_a_field", Json(1)); + ASSERT_THROW(Operations::check(chat), ApiError); + chat.body = Json::object(); + chat.body.set("model", Json("m")); + Operations::check(chat); // fits the row + ASSERT_THROW(Operations::check(Invocation{"unknown_op", {}, {}, Json(), false}), ApiError); + // multipart, streaming and binary rows are refused + Invocation up; + up.op = "files_api_routes_upload_file"; + ASSERT_THROW(Operations::check(up), ApiError); + up.op = "files_api_routes_download_file"; + up.ids = {"f1"}; + ASSERT_THROW(Operations::check(up), ApiError); + up.op = "agents_api_v1_conversations_start_stream"; + up.ids = {}; + ASSERT_THROW(Operations::check(up), ApiError); + // destructive rows need the explicit flag + Invocation del; + del.op = "files_api_routes_delete_file"; + del.ids = {"f1"}; + ASSERT_THROW(Operations::check(del), ApiError); + del.allow_destructive = true; + Operations::check(del); + ASSERT_EQ(rig.transport->send_count(), 1u); // only the first invoke went out +} + +TEST(mistral_operations, catalog_lists_areas_and_operations) { + std::vector areas = Operations::areas(); + ASSERT_TRUE(areas.size() > 20); + ASSERT_EQ(Operations::list().size(), Calls::count()); + ASSERT_EQ(Operations::list("chat").size(), 1u); + ASSERT_EQ(Operations::list("no-such-area").size(), 0u); +} diff --git a/tests/src/vendors/mistral/test_calls.cpp b/tests/src/vendors/mistral/test_calls.cpp new file mode 100644 index 0000000..a63d0ec --- /dev/null +++ b/tests/src/vendors/mistral/test_calls.cpp @@ -0,0 +1,168 @@ +// The call table: every row well formed, every Op constant present, paths +// render only with valid ids, and every typed request body uses only fields the +// table row declares (the table is proven against the OpenAPI documents by +// tools/check_spec.py). +#include + +#include "support.h" +#include "vendors/mistral/agents.h" +#include "vendors/mistral/batch.h" +#include "vendors/mistral/calls.h" +#include "vendors/mistral/chat.h" +#include "vendors/mistral/conversations.h" +#include "vendors/mistral/embeddings.h" +#include "vendors/mistral/finetune.h" +#include "vendors/mistral/ocr.h" +#include "vendors/mistral/ops.h" +#include "vendors/mistral/audio.h" + +using namespace molo::mistral; +using namespace mistral_test; +using molo::llm::ApiError; + +TEST(mistral_calls, table_rows_are_well_formed_and_unique) { + std::set ops, keys; + std::size_t sources_docs = 0, sources_pd = 0; + for (std::size_t i = 0; i < Calls::count(); ++i) { + const CallSpec& s = Calls::at(i); + ASSERT_TRUE(ops.insert(s.op).second); + ASSERT_TRUE(keys.insert(std::string(s.http_method) + " " + s.path).second); + ASSERT_TRUE(std::string(s.path).rfind("/v", 0) == 0); + std::string m = s.http_method; + ASSERT_TRUE(m == "GET" || m == "POST" || m == "PUT" || m == "PATCH" || m == "DELETE"); + std::string src = s.source; + ASSERT_TRUE(src == "docs" || src == "pd"); + (src == "docs" ? sources_docs : sources_pd)++; + std::string rk = s.request; + ASSERT_TRUE(rk == "none" || rk == "json" || rk == "multipart"); + std::string rs = s.response_kind; + ASSERT_TRUE(rs == "none" || rs == "json" || rs == "sse" || rs == "binary"); + if (rk == "none") ASSERT_EQ(std::string(s.body_fields), ""); + } + ASSERT_EQ(sources_docs, 319u); // operations of docs.mistral.ai/openapi.yaml on 2026-10-25 + ASSERT_EQ(sources_pd, 28u); // extra operations only the platform-docs-public copy lists + ASSERT_EQ(Calls::count(), 347u); +} + +TEST(mistral_calls, every_op_constant_is_a_table_row) { + for (const char* op : {Op::kChat, Op::kFim, Op::kAgentsCompletion, Op::kEmbeddings, Op::kModerations, + Op::kChatModerations, Op::kClassifications, Op::kChatClassifications, Op::kOcr, + Op::kTranscribe, Op::kSpeech, Op::kVoicesList, Op::kVoicesListV1, Op::kVoiceCreate, + Op::kVoiceGet, Op::kVoiceUpdate, Op::kVoiceDelete, Op::kVoiceSample, Op::kModelsList, + Op::kModelGet, Op::kModelDelete, Op::kFileUpload, Op::kFileList, Op::kFileGet, + Op::kFileDelete, Op::kFileContent, Op::kFileUrl, Op::kFtJobList, Op::kFtJobCreate, + Op::kFtJobGet, Op::kFtJobStart, Op::kFtJobCancel, Op::kFtModelUpdate, Op::kFtModelArchive, + Op::kFtModelUnarchive, Op::kBatchList, Op::kBatchCreate, Op::kBatchGet, Op::kBatchCancel, + Op::kBatchDelete, Op::kAgentCreate, Op::kAgentListPages, Op::kAgentGet, Op::kAgentUpdate, + Op::kAgentDelete, Op::kAgentSetVersion, Op::kAgentVersions, Op::kAgentVersionGet, + Op::kAgentAliasSet, Op::kAgentAliasList, Op::kAgentAliasDelete, Op::kConvStart, + Op::kConvStartStream, Op::kConvList, Op::kConvGet, Op::kConvDelete, Op::kConvAppend, + Op::kConvAppendStream, Op::kConvHistory, Op::kConvMessages, Op::kConvRestart, + Op::kConvRestartStream, Op::kLibList, Op::kLibCreate, Op::kLibGet, Op::kLibPatch, + Op::kLibDelete, Op::kLibShareList, Op::kLibShareCreate, Op::kLibShareDelete, Op::kDocList, + Op::kDocUpload, Op::kDocGet, Op::kDocPatch, Op::kDocDelete, Op::kDocText, Op::kDocStatus, + Op::kDocSignedUrl, Op::kDocTextUrl, Op::kDocReprocess}) { + ASSERT_TRUE(Calls::try_find(op) != nullptr); + } + ASSERT_TRUE(Calls::try_find("no_such_operation") == nullptr); + ASSERT_THROW(Calls::find("no_such_operation"), ApiError); +} + +TEST(mistral_calls, render_validates_ids_and_counts) { + ASSERT_EQ(Calls::render(Op::kFileGet, {"abc-123"}), "/v1/files/abc-123"); + ASSERT_EQ(Calls::render(Op::kDocGet, {"lib1", "doc2"}), "/v1/libraries/lib1/documents/doc2"); + ASSERT_EQ(Calls::render(Op::kConvStartStream), "/v1/conversations"); // "#stream" is spec-only + ASSERT_EQ(Calls::render(Op::kConvAppendStream, {"c1"}), "/v1/conversations/c1"); + ASSERT_EQ(Calls::render(Op::kModelGet, {"ft:open-mistral-7b:a:2024:b"}), "/v1/models/ft:open-mistral-7b:a:2024:b"); + ASSERT_THROW(Calls::render(Op::kFileGet, {"../x"}), ApiError); + ASSERT_THROW(Calls::render(Op::kFileGet, {"a/b"}), ApiError); + ASSERT_THROW(Calls::render(Op::kFileGet, {""}), ApiError); + ASSERT_THROW(Calls::render(Op::kFileGet, {}), ApiError); + ASSERT_THROW(Calls::render(Op::kFileGet, {"a", "b"}), ApiError); + ASSERT_THROW(Calls::render(Op::kChat, {"a"}), ApiError); +} + +TEST(mistral_calls, destructive_rows_are_recognised) { + ASSERT_TRUE(Calls::is_destructive(Calls::find(Op::kFileDelete))); + ASSERT_TRUE(Calls::is_destructive(Calls::find(Op::kBatchCancel))); + ASSERT_TRUE(Calls::is_destructive(Calls::find(Op::kFtJobCancel))); + ASSERT_TRUE(Calls::is_destructive(Calls::find(Op::kFtModelArchive))); + ASSERT_TRUE(Calls::is_destructive(Calls::find(Op::kConvDelete))); + ASSERT_FALSE(Calls::is_destructive(Calls::find(Op::kChat))); + ASSERT_FALSE(Calls::is_destructive(Calls::find(Op::kFileGet))); +} + +namespace { +void all_fields_declared(const Json& body, const char* op) { + const CallSpec& spec = Calls::find(op); + for (const auto& [k, v] : body.as_object()) { + if (!Calls::has_body_field(spec, k)) FAIL(std::string(op) + " body field not in the table: " + k); + } +} +} // namespace + +TEST(mistral_calls, typed_request_bodies_only_use_declared_fields) { + // Every optional field set at once. + ChatParams p = ChatParams::prompt("hi"); + p.temperature = 0.5; p.top_p = 0.9; p.presence_penalty = 0.1; p.frequency_penalty = 0.1; + p.repetition_penalty = 1.0; p.max_tokens = 5; p.min_tokens = 1; p.random_seed = 3; p.top_k = 4; p.n = 2; + p.top_logprobs = 3; p.logprobs = true; p.safe_prompt = true; p.parallel_tool_calls = true; + p.stop = {"x"}; + ToolDef t; t.name = "f"; p.tools.push_back(t); + p.tool_choice = "f"; p.format = ResponseFormat::kJsonSchema; p.schema_name = "s"; + p.reasoning_effort = "high"; p.prompt_mode = "reasoning"; p.prompt_cache_key = "k"; p.service_tier = "auto"; + p.prediction = "guess"; p.metadata = Json::object(); p.guardrails = Json::array(); + all_fields_declared(p.to_json(true), Op::kChat); + + ChatParams a = p; + a.agent_id = "ag_1"; a.temperature.reset(); a.top_p.reset(); a.safe_prompt.reset(); + all_fields_declared(a.to_json(true), Op::kAgentsCompletion); + ASSERT_TRUE(a.to_json().find("model") == nullptr); + + FimParams f; f.prompt = "a"; f.suffix = "b"; f.temperature = 1; f.top_p = 1; f.max_tokens = 1; f.min_tokens = 1; + f.random_seed = 1; f.stop = {"s"}; f.prompt_cache_key = "k"; f.metadata = Json::object(); + all_fields_declared(f.to_json(true), Op::kFim); + + EmbeddingParams e; e.inputs = {"a", "b"}; e.output_dimension = 8; e.output_dtype = "int8"; e.encoding_format = "float"; + all_fields_declared(e.to_json(), Op::kEmbeddings); + + OcrParams o; o.reference = "https://x.test/a.pdf"; o.pages = {0}; o.include_image_base64 = true; o.image_limit = 1; + o.image_min_size = 1; o.table_format = "html"; o.extract_header = true; o.extract_footer = true; + o.include_blocks = true; o.confidence_scores_granularity = "page"; o.document_annotation_prompt = "p"; + o.document_annotation_format = Json::object(); o.bbox_annotation_format = Json::object(); + all_fields_declared(o.to_json(), Op::kOcr); + + BatchParams b; b.model = "m"; b.requests.push_back({"c", Json::object()}); b.metadata = Json::object(); b.timeout_hours = 5; + all_fields_declared(b.to_json(), Op::kBatchCreate); + + FineTuneJobParams j; j.model = "open-mistral-7b"; j.training_files.push_back({"f1", 1.0}); j.validation_files = {"f2"}; + j.suffix = "s"; j.job_type = "completion"; j.hyperparameters.set("learning_rate", Json(0.0001)); + j.integrations = Json::array(); j.repositories = Json::array(); j.classifier_targets = Json::array(); + j.auto_start = true; j.invalid_sample_skip_percentage = 0.5; + all_fields_declared(j.to_json(), Op::kFtJobCreate); + + AgentParams ag; ag.model = "m"; ag.name = "n"; ag.instructions = "i"; ag.description = "d"; + ag.tools = Json::array(); ag.completion_args = Json::object(); ag.guardrails = Json::array(); + ag.metadata = Json::object(); ag.handoffs = {"a1"}; ag.version_message = "v"; + all_fields_declared(ag.to_json(true), Op::kAgentCreate); + all_fields_declared(ag.to_json(false), Op::kAgentUpdate); + + ConversationStartParams c; c.inputs = ConversationInput::text("hi"); c.agent_id = "a"; c.agent_version = 2; + c.instructions = "i"; c.name = "n"; c.description = "d"; c.tools = Json::array(); c.completion_args = Json::object(); + c.guardrails = Json::array(); c.metadata = Json::object(); c.store = true; c.handoff_execution = "server"; + all_fields_declared(c.to_json(true), Op::kConvStart); + all_fields_declared(c.to_json(true), Op::kConvStartStream); + ConversationAppendParams ca; ca.inputs = ConversationInput::text("hi"); ca.completion_args = Json::object(); + ca.tool_confirmations = Json::array(); ca.store = true; ca.handoff_execution = "client"; + all_fields_declared(ca.to_json(true), Op::kConvAppend); + ConversationRestartParams cr; cr.from_entry_id = "e1"; cr.inputs = ConversationInput::text("x"); + cr.completion_args = Json::object(); cr.metadata = Json::object(); cr.store = false; cr.handoff_execution = "client"; + cr.agent_version = 1; + all_fields_declared(cr.to_json(true), Op::kConvRestart); + + SpeechParams sp; sp.model = "m"; sp.input = "hello"; sp.voice_id = "v1"; sp.response_format = "wav"; + all_fields_declared(sp.to_json(), Op::kSpeech); + VoiceCreateParams vc; vc.name = "n"; vc.slug = "s"; vc.languages = {"en"}; vc.gender = "g"; vc.description = "d"; + vc.age = 30; vc.tags = {"t"}; vc.sample_audio = "AAAA"; vc.sample_filename = "a.wav"; + all_fields_declared(vc.to_json(), Op::kVoiceCreate); +} diff --git a/tests/src/vendors/mistral/test_chat.cpp b/tests/src/vendors/mistral/test_chat.cpp new file mode 100644 index 0000000..946cc1c --- /dev/null +++ b/tests/src/vendors/mistral/test_chat.cpp @@ -0,0 +1,340 @@ +// Chat, agents and FIM completions plus MistralChatModel: request bodies, +// validation before the network, parsing of replies, SSE streaming. +// +// FIXTURE: reply taken verbatim from docs.mistral.ai/openapi.yaml (the FIM +// userExample, which is a ChatCompletionResponse). +// SHAPE: shapes read from the spec schemas (ChatCompletionResponse, +// CompletionChunk), values invented: Mistral's own stream was never seen. +#include "support.h" +#include "vendors/mistral/chat.h" +#include "vendors/mistral/chat_model.h" + +using namespace molo::mistral; +using namespace mistral_test; +using molo::llm::ApiError; + +namespace { +// FIXTURE: FIM userExample +const char* kFim = + "{\"id\": \"447e3e0d457e42e98248b5d2ef52a2a3\", \"object\": \"chat.completion\", \"model\": \"codestral-2508\", " + "\"usage\": {\"prompt_tokens\": 8, \"completion_tokens\": 91, \"total_tokens\": 99}, \"created\": 1759496862, " + "\"choices\": [{\"index\": 0, \"message\": {\"content\": \"add_numbers(a: int, b: int) -> int:\\n return a + b\\n\", " + "\"tool_calls\": null, \"prefix\": false, \"role\": \"assistant\"}, \"finish_reason\": \"stop\"}]}"; +// SHAPE: ChatCompletionResponse with a tool call, thinking chunks and model_length +const char* kToolReply = + "{\"id\":\"c1\",\"object\":\"chat.completion\",\"model\":\"mistral-large-latest\",\"created\":1," + "\"usage\":{\"prompt_tokens\":5,\"completion_tokens\":7,\"total_tokens\":12}," + "\"choices\":[{\"index\":0,\"finish_reason\":\"tool_calls\",\"message\":{\"role\":\"assistant\"," + "\"content\":[{\"type\":\"thinking\",\"thinking\":[{\"type\":\"text\",\"text\":\"need weather\"}]},{\"type\":\"text\",\"text\":\"Checking.\"}]," + "\"tool_calls\":[{\"id\":\"D681PevKs\",\"type\":\"function\",\"function\":{\"name\":\"get_weather\",\"arguments\":\"{\\\"city\\\":\\\"Paris\\\"}\"}}," + "{\"id\":\"X1\",\"type\":\"function\",\"function\":{\"name\":\"obj_args\",\"arguments\":{\"a\":1}}}]}}]}"; +// SHAPE: CompletionChunk stream, bare payloads then [DONE] +const char* kStream = + "data: {\"id\":\"s1\",\"object\":\"chat.completion.chunk\",\"created\":2,\"model\":\"mistral-small-latest\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\"},\"finish_reason\":null}]}\n\n" + "data: {\"id\":\"s1\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hel\"},\"finish_reason\":null}]}\n\n" + ": ping\n\n" + "data: {\"id\":\"s1\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"lo\"},\"finish_reason\":null}]}\n\n" + "data: {\"id\":\"s1\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\"},\"finish_reason\":\"stop\"}]," + "\"usage\":{\"prompt_tokens\":3,\"completion_tokens\":2,\"total_tokens\":5}}\n\n" + "data: [DONE]\n\n"; +const char* kStreamTools = + "data: {\"id\":\"s2\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"id\":\"t1\",\"index\":0,\"function\":{\"name\":\"f\",\"arguments\":\"{\\\"a\\\":\"}}]},\"finish_reason\":null}]}\n\n" + "data: {\"id\":\"s2\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"name\":\"\",\"arguments\":\"1}\"}}]},\"finish_reason\":\"tool_calls\"}]}\n\n" + "data: [DONE]\n\n"; +} // namespace + +TEST(mistral_chat, request_body_and_parse_of_the_documented_reply) { + Rig rig; + rig.on(Method::kPost, "/v1/fim/completions", reply(kFim)); + Fim fim(rig.client()); + FimParams p; + p.prompt = "def "; + p.suffix = "\n return a + b"; + p.max_tokens = 64; + ChatResult r = fim.create(p); + ASSERT_EQ(r.first().content, "add_numbers(a: int, b: int) -> int:\n return a + b\n"); + ASSERT_EQ(r.first().finish_reason, "stop"); + ASSERT_EQ(r.usage.total_tokens, 99); + ASSERT_EQ(r.model, "codestral-2508"); + auto s = rig.last(); + ASSERT_TRUE(has(s.body, "\"model\":\"codestral-latest\"")); + ASSERT_TRUE(has(s.body, "\"suffix\":")); + ASSERT_TRUE(has(s.body, "\"max_tokens\":64")); + ASSERT_EQ(s.headers.get_or("Authorization", ""), std::string("Bearer ") + kKey); +} + +TEST(mistral_chat, chat_with_tools_thinking_and_object_arguments) { + Rig rig; + rig.on(Method::kPost, "/v1/chat/completions", reply(kToolReply)); + Chat chat(rig.client()); + ChatParams p = ChatParams::prompt("weather?"); + ToolDef t; + t.name = "get_weather"; + t.description = "weather"; + t.parameters = Json::parse("{\"type\":\"object\",\"properties\":{\"city\":{\"type\":\"string\"}}}"); + p.tools.push_back(t); + p.tool_choice = "any"; + ChatResult r = chat.create(p); + const ChatChoice& c = r.first(); + ASSERT_EQ(c.content, "Checking."); + ASSERT_EQ(c.reasoning, "need weather"); + ASSERT_EQ(c.tool_calls.size(), 2u); + ASSERT_EQ(c.tool_calls[0].name, "get_weather"); + ASSERT_TRUE(c.tool_calls[0].arguments_valid()); + ASSERT_EQ(c.tool_calls[0].parsed().find("city")->as_string(), "Paris"); + ASSERT_EQ(c.tool_calls[1].arguments, "{\"a\":1}"); // object arguments are dumped to text + ASSERT_EQ(c.finish_reason, "tool_calls"); + ASSERT_TRUE(c.history_message().find("tool_calls") != nullptr); + auto s = rig.last(); + ASSERT_TRUE(has(s.body, "\"tool_choice\":\"any\"")); + ASSERT_TRUE(has(s.body, "\"type\":\"function\"")); +} + +TEST(mistral_chat, named_tool_choice_and_json_schema_format) { + ChatParams p = ChatParams::prompt("x"); + ToolDef t; + t.name = "f"; + p.tools.push_back(t); + p.tool_choice = "f"; + p.format = ResponseFormat::kJsonSchema; + p.schema_name = "answer"; + p.schema = Json::parse("{\"type\":\"object\"}"); + p.schema_strict = true; + std::string body = p.to_json().dump(); + ASSERT_TRUE(has(body, "\"tool_choice\":{\"function\":{\"name\":\"f\"},\"type\":\"function\"}")); + ASSERT_TRUE(has(body, "\"response_format\":{\"json_schema\":{\"name\":\"answer\",\"schema\":{\"type\":\"object\"},\"strict\":true},\"type\":\"json_schema\"}")); +} + +TEST(mistral_chat, agents_completion_uses_agent_id_and_rejects_model_only_fields) { + Rig rig; + rig.on(Method::kPost, "/v1/agents/completions", reply(kFim)); + Chat chat(rig.client()); + ChatParams p = ChatParams::prompt("hi"); + p.agent_id = "ag_0123"; + ASSERT_EQ(chat.create(p).first().finish_reason, "stop"); + ASSERT_TRUE(has(rig.last().body, "\"agent_id\":\"ag_0123\"")); + ASSERT_FALSE(has(rig.last().body, "\"model\"")); + p.temperature = 0.2; + ASSERT_THROW(chat.create(p), ApiError); + p.temperature.reset(); + p.agent_id = "../x"; + ASSERT_THROW(chat.create(p), ApiError); +} + +TEST(mistral_chat, invalid_input_never_reaches_the_network) { + Rig rig; + Chat chat(rig.client()); + ChatParams empty; + ASSERT_THROW(chat.create(empty), ApiError); + auto bad = [&](auto mutate) { + ChatParams p = ChatParams::prompt("x"); + mutate(p); + ASSERT_THROW(chat.create(p), ApiError); + }; + bad([](ChatParams& p) { p.temperature = 1.6; }); + bad([](ChatParams& p) { p.temperature = -0.1; }); + bad([](ChatParams& p) { p.top_p = 0.0; }); + bad([](ChatParams& p) { p.top_p = 1.1; }); + bad([](ChatParams& p) { p.presence_penalty = 2.5; }); + bad([](ChatParams& p) { p.repetition_penalty = 0.0; }); + bad([](ChatParams& p) { p.max_tokens = -1; }); + bad([](ChatParams& p) { p.top_k = -2; }); + bad([](ChatParams& p) { p.n = 0; }); + bad([](ChatParams& p) { p.n = 17; }); + bad([](ChatParams& p) { p.model = "bad model!"; }); + bad([](ChatParams& p) { p.model = std::string(161, 'a'); }); + bad([](ChatParams& p) { p.stop = std::vector(17, "x"); }); + bad([](ChatParams& p) { p.stop = {""}; }); + bad([](ChatParams& p) { p.reasoning_effort = "max"; }); + bad([](ChatParams& p) { p.service_tier = "gold"; }); + bad([](ChatParams& p) { p.prompt_mode = "fast"; }); + bad([](ChatParams& p) { p.tool_choice = "bad name!"; }); + bad([](ChatParams& p) { ToolDef t; t.name = "bad name"; p.tools.push_back(t); }); + bad([](ChatParams& p) { p.format = ResponseFormat::kJsonSchema; }); // no schema name + bad([](ChatParams& p) { p.metadata = Json("not an object"); }); + bad([](ChatParams& p) { p.guardrails = Json::object(); }); + ASSERT_THROW(Messages::image_chunk("ftp://x"), ApiError); + ASSERT_THROW(Messages::document_chunk("javascript:1"), ApiError); + ASSERT_THROW(Messages::file_chunk("a/b"), ApiError); + ASSERT_THROW(Messages::tool("", "n", "c"), ApiError); + ASSERT_THROW(Messages::assistant_prefix(""), ApiError); + ChatParams many = ChatParams::prompt("x"); + for (int i = 0; i < 4095; ++i) many.add(Messages::user("m")); + ASSERT_THROW(many.add(Messages::user("one too many")), ApiError); + ASSERT_EQ(rig.transport->send_count(), 0u); +} + +TEST(mistral_chat, not_configured_has_no_traffic) { + Rig rig(""); + Chat chat(rig.client()); + try { + chat.create(ChatParams::prompt("hi")); + FAIL("expected"); + } catch (const molo::llm::NotConfiguredError& e) { + ASSERT_EQ(e.http_status(), 503); + ASSERT_TRUE(has(e.message(), "MISTRAL_API_KEY")); + } + ASSERT_EQ(rig.transport->send_count(), 0u); + ASSERT_FALSE(rig.platform.configured()); +} + +TEST(mistral_chat, stream_transcript_comments_and_usage) { + Rig rig; + rig.on(Method::kPost, "/v1/chat/completions", sse(kStream)); + Chat chat(rig.client()); + std::string text; + int deltas = 0; + ChatResult r = chat.stream(ChatParams::prompt("Hi"), [&](const StreamDelta& d) { + text += d.text; + ++deltas; + return true; + }); + ASSERT_EQ(text, "Hello"); + ASSERT_EQ(deltas, 4); // the ": ping" comment produces no event + ASSERT_EQ(r.first().content, "Hello"); + ASSERT_EQ(r.first().finish_reason, "stop"); + ASSERT_EQ(r.id, "s1"); + ASSERT_EQ(r.model, "mistral-small-latest"); + ASSERT_EQ(r.usage.total_tokens, 5); + ASSERT_TRUE(has(rig.last().body, "\"stream\":true")); + ASSERT_EQ(rig.last().headers.get_or("Accept", ""), "text/event-stream"); +} + +TEST(mistral_chat, stream_merges_tool_call_fragments_and_stops_early) { + Rig rig; + rig.on(Method::kPost, "/v1/chat/completions", sse(kStreamTools)); + Chat chat(rig.client()); + ChatResult r = chat.stream(ChatParams::prompt("x"), nullptr); + ASSERT_EQ(r.first().tool_calls.size(), 1u); + ASSERT_EQ(r.first().tool_calls[0].id, "t1"); + ASSERT_EQ(r.first().tool_calls[0].name, "f"); + ASSERT_EQ(r.first().tool_calls[0].arguments, "{\"a\":1}"); + ASSERT_EQ(r.first().finish_reason, "tool_calls"); + + rig.on(Method::kPost, "/v1/chat/completions", sse(kStream)); + ChatResult early = chat.stream(ChatParams::prompt("x"), [](const StreamDelta& d) { return d.text != "Hel"; }); + ASSERT_EQ(early.first().content, "Hel"); // stopped by the sink: no truncation error +} + +TEST(mistral_chat, truncated_stream_is_loud) { + Rig rig; + rig.on(Method::kPost, "/v1/chat/completions", + sse("data: {\"id\":\"s\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hel\"},\"finish_reason\":null}]}\n\n")); + Chat chat(rig.client()); + try { + chat.stream(ChatParams::prompt("x"), nullptr); + FAIL("expected STREAM_TRUNCATED"); + } catch (const ApiError& e) { + ASSERT_EQ(e.type(), "STREAM_TRUNCATED"); + ASSERT_EQ(e.http_status(), 502); + } +} + +TEST(mistral_chat, spec_event_wrapper_is_unwrapped) { + // The spec's CompletionEvent wraps the chunk as {"data": chunk}. + ChatStreamAccumulator acc; + acc.feed(Json::parse("{\"data\":{\"id\":\"w\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"A\"},\"finish_reason\":\"stop\"}]}}")); + ASSERT_TRUE(acc.done()); + ASSERT_EQ(acc.result().first().content, "A"); + ASSERT_EQ(acc.result().id, "w"); +} + +TEST(mistral_chat, error_envelopes_map_to_api_error) { + Rig rig; + rig.on(Method::kPost, "/v1/chat/completions", + reply("{\"object\":\"error\",\"message\":\"Unauthorized\",\"type\":\"invalid_request_error\",\"param\":null,\"code\":\"1100\"}", 401)); + Chat chat(rig.client()); + try { + chat.create(ChatParams::prompt("x")); + FAIL("expected 401"); + } catch (const ApiError& e) { + ASSERT_EQ(e.http_status(), 401); + ASSERT_EQ(e.type(), "invalid_request_error"); + ASSERT_EQ(e.message(), "Unauthorized"); + } +} + +TEST(mistral_chatmodel, maps_a_neutral_request_to_wire_parameters) { + Rig rig; + MistralChatModel model(std::make_shared(rig.client())); + molo::llm::ChatRequest req; + req.system = "be brief"; + req.messages.push_back(molo::llm::Message::user("hi")); + molo::llm::Message a = molo::llm::Message::assistant(""); + molo::llm::ToolCall call; + call.id = "c1"; + call.name = "f"; + call.arguments.set("x", Json(1)); + a.parts.push_back(molo::llm::ContentPart::of_call(call)); + req.messages.push_back(a); + molo::llm::Message tr; + tr.role = molo::llm::Role::kTool; + tr.parts.push_back(molo::llm::ContentPart::of_result({"c1", "42", false})); + req.messages.push_back(tr); + molo::llm::ToolSpec spec; + spec.name = "f"; + req.tools.push_back(spec); + req.tool_choice = "required"; + req.max_tokens = 10; + req.temperature = 0.3; + req.json_mode = true; + req.reasoning_effort = "low"; + req.extra.set("safe_prompt", Json(true)); + Json body = model.to_params(req).to_json(); + std::string s = body.dump(); + ASSERT_TRUE(has(s, "\"model\":\"mistral-small-latest\"")); + ASSERT_TRUE(has(s, "{\"content\":\"be brief\",\"role\":\"system\"}")); + ASSERT_TRUE(has(s, "\"tool_call_id\":\"c1\"")); + ASSERT_TRUE(has(s, "\"arguments\":\"{\\\"x\\\":1}\"")); + ASSERT_TRUE(has(s, "\"tool_choice\":\"required\"")); + ASSERT_TRUE(has(s, "\"response_format\":{\"type\":\"json_object\"}")); + ASSERT_TRUE(has(s, "\"reasoning_effort\":\"low\"")); + ASSERT_TRUE(has(s, "\"safe_prompt\":true")); + // json_mode and schema together are refused + req.response_schema = Json::parse("{\"type\":\"object\"}"); + ASSERT_THROW(model.to_params(req), ApiError); + // a user image becomes an image_url chunk with a data URL + molo::llm::ChatRequest img; + molo::llm::Message u = molo::llm::Message::user("look"); + molo::llm::ImageData d; + d.mime = "image/png"; + d.bytes = "abc"; + u.parts.push_back(molo::llm::ContentPart::of_image(d)); + img.messages.push_back(u); + ASSERT_TRUE(has(model.to_params(img).to_json().dump(), "data:image/png;base64,YWJj")); +} + +TEST(mistral_chatmodel, generate_and_stream_through_the_seam) { + Rig rig; + rig.on(Method::kPost, "/v1/chat/completions", reply(kToolReply)); + rig.on(Method::kPost, "/v1/chat/completions", sse(kStream)); + MistralChatModel model(std::make_shared(rig.client()), "mistral-large-latest"); + ASSERT_EQ(model.vendor(), "mistral"); + molo::llm::ChatRequest req; + req.messages.push_back(molo::llm::Message::user("weather?")); + molo::llm::ChatResponse r = model.generate(req); + ASSERT_EQ(r.text(), "Checking."); + ASSERT_EQ(r.finish_reason, "tool_calls"); + ASSERT_EQ(r.usage.total_tokens, 12); + auto calls = r.tool_calls(); + ASSERT_EQ(calls.size(), 2u); + ASSERT_EQ(calls[0].name, "get_weather"); + ASSERT_EQ(calls[0].arguments.find("city")->as_string(), "Paris"); + ASSERT_TRUE(has(rig.transport->seen()[0].body, "\"model\":\"mistral-large-latest\"")); + std::string text; + std::string finish; + model.stream(req, [&](const molo::llm::ChatChunk& c) { + text += c.text_delta; + if (!c.finish_reason.empty()) finish = c.finish_reason; + return true; + }); + ASSERT_EQ(text, "Hello"); + ASSERT_EQ(finish, "stop"); +} + +TEST(mistral_chatmodel, model_length_is_reported_as_length) { + ChatResult r = ChatResult::parse(Json::parse( + "{\"id\":\"1\",\"model\":\"m\",\"choices\":[{\"index\":0,\"finish_reason\":\"model_length\",\"message\":{\"role\":\"assistant\",\"content\":\"x\"}}]}")); + ASSERT_EQ(MistralChatModel::to_response(r).finish_reason, "length"); + ASSERT_THROW(MistralChatModel::to_response(ChatResult::parse(Json::parse("{\"choices\":[]}"))), ApiError); +} diff --git a/tests/src/vendors/mistral/test_live.cpp b/tests/src/vendors/mistral/test_live.cpp new file mode 100644 index 0000000..8089fb8 --- /dev/null +++ b/tests/src/vendors/mistral/test_live.cpp @@ -0,0 +1,59 @@ +// Opt-in live checks. MOLO_LIVE_NET=1 enables the network; MISTRAL_API_KEY +// (via llm::Config) enables the read-only calls plus ONE tiny completion. +// Without them each test prints a loud SKIP. Nothing is created or deleted. +#include +#include + +#include "clients/transports/http_transport.h" +#include "support.h" +#include "vendors/mistral/chat.h" +#include "vendors/mistral/files.h" +#include "vendors/mistral/models.h" + +using namespace molo::mistral; +using molo::llm::ApiError; + +namespace { +// Read once at start-up: tests/src/llm unsets MOLO_LIVE_NET while the suite runs. +const bool kNet = std::getenv("MOLO_LIVE_NET") != nullptr; +bool net() { return kNet; } +} // namespace + +// No key needed: a deliberately invalid key must answer 401. Recorded probe: +// 2026-10-25 every documented path answered 401 {"detail":"Invalid API Key"}. +TEST(mistral_live, invalid_key_answers_401) { + if (!net()) { + std::cout << "SKIP mistral_live.invalid_key_answers_401 (set MOLO_LIVE_NET=1)\n"; + return; + } + auto client = std::make_shared( + std::make_unique(), Platform::config(), "invalid-molo-probe"); + Models models(client); + try { + models.list(); + FAIL("expected 401"); + } catch (const ApiError& e) { + std::cout << " GET /v1/models with an invalid key: " << e.http_status() << " " << e.type() << ": " << e.message() << "\n"; + ASSERT_EQ(e.http_status(), 401); + } +} + +TEST(mistral_live, models_files_and_one_tiny_completion) { + Platform p = Platform::from_env(); + if (!net() || !p.configured()) { + std::cout << "SKIP mistral_live.models_files_and_one_tiny_completion (set MOLO_LIVE_NET=1 and MISTRAL_API_KEY)\n"; + return; + } + ModelList models = Models(p.client).list(); + std::cout << " models: " << models.models.size() << "\n"; + ASSERT_TRUE(!models.models.empty()); + FileListParams lp; + lp.page_size = 5; + FileList files = Files(p.client).list(lp); + std::cout << " files (first page): " << files.files.size() << "\n"; + ChatParams params = ChatParams::prompt("Reply with the single word: ok"); + params.max_tokens = 8; + ChatResult r = Chat(p.client).create(params); + std::cout << " completion finish=" << r.first().finish_reason << " tokens=" << r.usage.total_tokens << "\n"; + ASSERT_TRUE(r.usage.present); +} diff --git a/tests/src/vendors/mistral/test_services.cpp b/tests/src/vendors/mistral/test_services.cpp new file mode 100644 index 0000000..9dc243f --- /dev/null +++ b/tests/src/vendors/mistral/test_services.cpp @@ -0,0 +1,460 @@ +// Services over the real ApiClient and a recording mock: models, files, +// embeddings, moderation, classifiers, OCR, audio, batch, fine-tuning. +// FIXTURE: replies verbatim from the docs.mistral.ai/openapi.yaml examples +// (list_models, upload_file, list_files, embeddings, moderations, ocr, +// transcriptions). SHAPE: replies built from the spec schema where the spec +// has no example (values invented). +#include "support.h" +#include "vendors/mistral/audio.h" +#include "vendors/mistral/chat_params.h" +#include "vendors/mistral/batch.h" +#include "vendors/mistral/classifiers.h" +#include "vendors/mistral/embeddings.h" +#include "vendors/mistral/files.h" +#include "vendors/mistral/finetune.h" +#include "vendors/mistral/models.h" +#include "vendors/mistral/moderation.h" +#include "vendors/mistral/ocr.h" + +using namespace molo::mistral; +using namespace mistral_test; +using molo::llm::ApiError; + +namespace { +// FIXTURE: list_models userExample (the document's own placeholders kept) +const char* kModels = + "{\"object\":\"list\",\"data\":[{\"id\": \"\", \"capabilities\": {\"completion_chat\": true, \"completion_fim\": false, " + "\"function_calling\": false, \"fine_tuning\": false, \"vision\": false, \"classification\": false}, \"job\": \"\", " + "\"root\": \"open-mistral-7b\", \"object\": \"model\", \"created\": 1756746619, \"owned_by\": \"\", " + "\"max_context_length\": 32768, \"aliases\": [], \"type\": \"fine-tuned\", \"archived\": false}," + "{\"id\":\"mistral-small-latest\",\"object\":\"model\",\"created\":1,\"owned_by\":\"mistralai\",\"type\":\"base\"," + "\"capabilities\":{\"completion_chat\":true,\"vision\":true},\"max_context_length\":131072,\"aliases\":[\"mistral-small\"]}]}"; +// FIXTURE: upload_file userExample +const char* kFile = + "{\"id\": \"e85980c9-409e-4a46-9304-36588f6292b0\", \"object\": \"file\", \"bytes\": null, \"created_at\": 1759500189, " + "\"filename\": \"example.file.jsonl\", \"purpose\": \"fine-tune\", \"sample_type\": \"instruct\", \"source\": \"upload\", " + "\"num_lines\": 2, \"mimetype\": \"application/jsonl\", \"signature\": \"d4821d2de1917341\"}"; +// FIXTURE: list_files userExample +const char* kFileList = + "{\"data\": [{\"id\": \"f1\", \"object\": \"file\", \"bytes\": null, \"created_at\": 1759491994, \"filename\": \"a.jsonl\", " + "\"purpose\": \"batch\", \"sample_type\": \"batch_result\", \"source\": \"mistral\", \"num_lines\": 2, \"mimetype\": \"application/jsonl\", \"signature\": null}, " + "{\"id\": \"f2\", \"object\": \"file\", \"bytes\": null, \"created_at\": 1759491994, \"filename\": \"b.jsonl\", \"purpose\": \"batch\", " + "\"sample_type\": \"batch_result\", \"source\": \"mistral\", \"num_lines\": 2, \"mimetype\": \"application/jsonl\", \"signature\": null}], \"object\": \"list\", \"total\": 2}"; +// FIXTURE: embeddings userExample +const char* kEmbeddings = + "{\"data\": [{\"embedding\": [-0.016632080078125, 0.0701904296875, 0.03143310546875, 0.01309967041015625, 0.0202789306640625], \"index\": 0, \"object\": \"embedding\"}, " + "{\"embedding\": [-0.0230560302734375, 0.039337158203125, 0.0521240234375, -0.0184783935546875, 0.034271240234375], \"index\": 1, \"object\": \"embedding\"}], " + "\"model\": \"mistral-embed\", \"object\": \"list\", \"usage\": {\"prompt_tokens\": 15, \"completion_tokens\": 0, \"total_tokens\": 15, \"prompt_audio_seconds\": null}}"; +// FIXTURE: moderations userExample (first result only; the example has two) +const char* kModeration = + "{\"id\": \"4d71ae510af942108ef7344f903e2b88\", \"model\": \"mistral-moderation-latest\", \"results\": [{\"categories\": {\"sexual\": false, " + "\"hate_and_discrimination\": false, \"violence_and_threats\": true, \"selfharm\": false, \"pii\": false}, \"category_scores\": {\"sexual\": 0.0011335690505802631, " + "\"hate_and_discrimination\": 0.0030753696337342262, \"violence_and_threats\": 0.93, \"selfharm\": 0.00017952796770259738, \"pii\": 0.0023967307060956955}}]}"; +// SHAPE: ClassificationResponse +const char* kClassify = + "{\"id\":\"mod-1\",\"model\":\"ft:classifier:x\",\"results\":[{\"sentiment\":{\"scores\":{\"positive\":0.9,\"negative\":0.1}},\"topic\":{\"scores\":{\"a\":0.2,\"b\":0.8}}}]}"; +// FIXTURE (trimmed): ocr userExample structure +const char* kOcr = + "{\"pages\": [{\"index\": 1, \"markdown\": \"# TITLE\\nBody\", \"images\": [], \"dimensions\": {\"dpi\": 200, \"height\": 2200, \"width\": 1700}}," + "{\"index\": 2, \"markdown\": \"Page two\", \"images\": [{\"id\":\"i\"}], \"tables\": [{\"id\":\"t\"}], \"header\": \"H\"}]," + "\"model\": \"mistral-ocr-2503-completion\", \"usage_info\": {\"pages_processed\": 29, \"doc_size_bytes\": 3002783}}"; +// FIXTURE (trimmed): transcriptions userExample +const char* kTranscription = + "{\"model\": \"voxtral-mini-2507\", \"text\": \"This week, I traveled to Chicago.\", \"language\": \"en\", " + "\"segments\": [{\"type\":\"transcription_segment\",\"text\":\"This week\",\"start\":0.0,\"end\":1.5,\"speaker_id\":\"s1\"}], " + "\"usage\": {\"prompt_tokens\": 4, \"completion_tokens\": 0, \"total_tokens\": 4, \"prompt_audio_seconds\": 11}}"; +// SHAPE: BatchJob +const char* kBatch = + "{\"id\":\"b1\",\"object\":\"batch\",\"input_files\":[\"f1\"],\"endpoint\":\"/v1/chat/completions\",\"model\":\"mistral-small-latest\"," + "\"errors\":[],\"status\":\"RUNNING\",\"created_at\":10,\"total_requests\":4,\"completed_requests\":1,\"succeeded_requests\":1,\"failed_requests\":0}"; +// SHAPE: fine-tuning job +const char* kJob = + "{\"id\":\"j1\",\"auto_start\":false,\"model\":\"open-mistral-7b\",\"status\":\"QUEUED\",\"created_at\":1,\"modified_at\":2,\"training_files\":[\"f1\"],\"object\":\"job\",\"job_type\":\"completion\",\"trained_tokens\":0}"; +} // namespace + +TEST(mistral_models, list_get_and_explicit_delete) { + Rig rig; + rig.on(Method::kGet, "/v1/models", reply(kModels)); + rig.on(Method::kGet, "/v1/models/mistral-small-latest", reply("{\"id\":\"mistral-small-latest\",\"type\":\"base\",\"capabilities\":{\"vision\":true}}")); + rig.on(Method::kDelete, "/v1/models/ft:open-mistral-7b:a:b:c", reply("{\"id\":\"ft:open-mistral-7b:a:b:c\",\"object\":\"model\",\"deleted\":true}")); + Models m(rig.client()); + ModelList l = m.list("mistral", "small"); + ASSERT_EQ(l.models.size(), 2u); + ASSERT_TRUE(l.models[0].fine_tuned()); + ASSERT_EQ(l.models[0].root, "open-mistral-7b"); + ASSERT_TRUE(l.models[0].capabilities.completion_chat); + ASSERT_FALSE(l.models[0].capabilities.vision); + ASSERT_TRUE(l.find("mistral-small-latest")->capabilities.vision); + ASSERT_EQ(l.find("mistral-small-latest")->aliases.size(), 1u); + ASSERT_TRUE(has(rig.transport->seen()[0].target, "provider=mistral")); + ASSERT_TRUE(has(rig.transport->seen()[0].target, "model=small")); + ASSERT_TRUE(m.get("mistral-small-latest").capabilities.vision); + ASSERT_TRUE(m.remove("ft:open-mistral-7b:a:b:c").deleted); + ASSERT_THROW(m.get("../etc"), ApiError); + ASSERT_THROW(m.remove(""), ApiError); +} + +TEST(mistral_files, upload_list_get_download_url_delete) { + Rig rig; + rig.on(Method::kPost, "/v1/files", reply(kFile)); + rig.on(Method::kGet, "/v1/files", reply(kFileList)); + rig.on(Method::kGet, "/v1/files/e85980c9-409e-4a46-9304-36588f6292b0", reply(kFile)); + rig.on(Method::kGet, "/v1/files/f1/content", reply("{\"a\":1}\n", 200, "application/octet-stream")); + rig.on(Method::kGet, "/v1/files/f1/url", reply("{\"url\":\"https://signed.example/x\"}")); + rig.on(Method::kDelete, "/v1/files/f1", reply("{\"id\":\"f1\",\"object\":\"file\",\"deleted\":true}")); + Files files(rig.client()); + FileUploadParams up; + up.filename = "example.file.jsonl"; + up.data = "{\"messages\":[]}\n"; + up.expiry_hours = 24; + FileInfo f = files.upload(up); + ASSERT_EQ(f.id, "e85980c9-409e-4a46-9304-36588f6292b0"); + ASSERT_EQ(f.purpose, "fine-tune"); + ASSERT_EQ(f.num_lines, 2L); + auto req = rig.transport->seen()[0]; + ASSERT_TRUE(has(req.headers.get_or("Content-Type", ""), "multipart/form-data")); + ASSERT_TRUE(has(req.body, "name=\"purpose\"")); + ASSERT_TRUE(has(req.body, "fine-tune")); + ASSERT_TRUE(has(req.body, "name=\"expiry\"")); + ASSERT_TRUE(has(req.body, "filename=\"example.file.jsonl\"")); + FileList l = files.list(); + ASSERT_EQ(l.files.size(), 2u); + ASSERT_EQ(l.total, 2L); + ASSERT_TRUE(has(rig.transport->seen()[1].target, "page_size=100")); + ASSERT_EQ(files.get("e85980c9-409e-4a46-9304-36588f6292b0").sample_type, "instruct"); + ASSERT_EQ(files.download("f1"), "{\"a\":1}\n"); + ASSERT_EQ(files.signed_url("f1", 2), "https://signed.example/x"); + ASSERT_TRUE(has(rig.last().target, "expiry=2")); + ASSERT_TRUE(files.remove("f1").deleted); +} + +TEST(mistral_files, validation_before_the_network) { + Rig rig; + Files files(rig.client()); + FileUploadParams up; + up.data = "x"; + ASSERT_THROW(files.upload(up), ApiError); // no filename + up.filename = "a/b.jsonl"; + ASSERT_THROW(files.upload(up), ApiError); + up.filename = "a.jsonl"; + up.data.clear(); + ASSERT_THROW(files.upload(up), ApiError); + up.data = "x"; + up.purpose = "chat"; + ASSERT_THROW(files.upload(up), ApiError); + up.purpose = "ocr"; + up.visibility = "everyone"; + ASSERT_THROW(files.upload(up), ApiError); + FileListParams lp; + lp.page_size = 101; + ASSERT_THROW(files.list(lp), ApiError); + lp.page_size = 10; + lp.page = -1; + ASSERT_THROW(files.list(lp), ApiError); + ASSERT_THROW(files.signed_url("f1", 0), ApiError); + ASSERT_THROW(files.signed_url("f1", 169), ApiError); + ASSERT_THROW(files.get("a b"), ApiError); + ASSERT_THROW(files.remove("../x"), ApiError); + ASSERT_EQ(rig.transport->send_count(), 0u); +} + +TEST(mistral_files, list_all_walks_pages_and_reports_truncation) { + Rig rig; + // page_size 1: the first two pages are full, the third is empty + rig.on(Method::kGet, "/v1/files", reply("{\"data\":[{\"id\":\"a\"}],\"object\":\"list\",\"total\":2}"), 2); + rig.on(Method::kGet, "/v1/files", reply("{\"data\":[],\"object\":\"list\",\"total\":2}")); + Files files(rig.client()); + FileListParams p; + p.page_size = 1; + FileList all = files.list_all(p); + ASSERT_EQ(all.files.size(), 2u); + ASSERT_FALSE(all.truncated); + ASSERT_TRUE(has(rig.transport->seen()[1].target, "page=1")); + // an endless full page hits the walk cap and says so + Rig endless; + endless.on(Method::kGet, "/v1/files", reply("{\"data\":[{\"id\":\"a\"}],\"object\":\"list\",\"total\":99999}"), 100); + FileList cut = Files(endless.client()).list_all(p); + ASSERT_TRUE(cut.truncated); + ASSERT_EQ(cut.files.size(), 20u); // kMaxWalkPages +} + +TEST(mistral_embeddings, request_and_documented_reply) { + Rig rig; + rig.on(Method::kPost, "/v1/embeddings", reply(kEmbeddings)); + Embeddings e(rig.client()); + EmbeddingParams p; + p.inputs = {"Embed this sentence.", "As well as this one."}; + p.output_dimension = 5; + EmbeddingResult r = e.create(p); + ASSERT_EQ(r.vectors.size(), 2u); + ASSERT_EQ(r.vectors[1].size(), 5u); + ASSERT_EQ(r.usage.total_tokens, 15); + ASSERT_EQ(r.model, "mistral-embed"); + std::string body = rig.last().body; + ASSERT_TRUE(has(body, "\"model\":\"mistral-embed\"")); + ASSERT_TRUE(has(body, "\"input\":[\"Embed this sentence.\",\"As well as this one.\"]")); + ASSERT_TRUE(has(body, "\"output_dimension\":5")); + p.inputs = {"one"}; + ASSERT_TRUE(has(p.to_json().dump(), "\"input\":\"one\"")); + p.inputs.clear(); + ASSERT_THROW(p.validate(), ApiError); + p.inputs = std::vector(513, "x"); + ASSERT_THROW(p.validate(), ApiError); + p.inputs = {"x"}; + p.output_dtype = "float16"; + ASSERT_THROW(p.validate(), ApiError); + p.output_dtype.clear(); + p.output_dimension = 0; + ASSERT_THROW(p.validate(), ApiError); +} + +TEST(mistral_moderation, text_chat_and_classifiers) { + Rig rig; + rig.on(Method::kPost, "/v1/moderations", reply(kModeration)); + rig.on(Method::kPost, "/v1/chat/moderations", reply(kModeration)); + rig.on(Method::kPost, "/v1/classifications", reply(kClassify)); + rig.on(Method::kPost, "/v1/chat/classifications", reply(kClassify)); + Moderation mod(rig.client()); + ModerationResult r = mod.moderate({"text"}); + ASSERT_EQ(r.results.size(), 1u); + ASSERT_TRUE(r.results[0].flagged()); + ASSERT_EQ(r.results[0].flagged_categories().size(), 1u); + ASSERT_EQ(r.results[0].flagged_categories()[0], "violence_and_threats"); + ASSERT_TRUE(r.results[0].scores.at("violence_and_threats") > 0.9); + ASSERT_TRUE(has(rig.last().body, "\"input\":\"text\"")); + ASSERT_TRUE(has(rig.last().body, "\"model\":\"mistral-moderation-latest\"")); + mod.moderate_chat({Messages::user("hi"), Messages::assistant("hello")}); + ASSERT_TRUE(has(rig.last().body, "\"input\":[{")); + Classifiers cls(rig.client()); + ClassificationResult c = cls.classify("ft:classifier:x", {"a", "b"}); + ASSERT_EQ(c.results.size(), 1u); + ASSERT_EQ(c.top_label(0, "sentiment"), "positive"); + ASSERT_EQ(c.top_label(0, "topic"), "b"); + ASSERT_EQ(c.top_label(0, "missing"), ""); + ASSERT_EQ(c.top_label(5, "topic"), ""); + cls.classify_chat("ft:classifier:x", {Messages::user("hi")}); + ASSERT_TRUE(has(rig.last().body, "\"input\":{\"messages\":[{")); + ASSERT_THROW(mod.moderate({}), ApiError); + ASSERT_THROW(mod.moderate({""}), ApiError); + ASSERT_THROW(mod.moderate_chat({}), ApiError); + ASSERT_THROW(mod.moderate_chat({Json("x")}), ApiError); + ASSERT_THROW(cls.classify("bad model", {"a"}), ApiError); + ASSERT_THROW(cls.classify("m", std::vector(257, "x")), ApiError); +} + +TEST(mistral_ocr, document_forms_and_documented_reply) { + Rig rig; + rig.on(Method::kPost, "/v1/ocr", reply(kOcr), 3); + Ocr ocr(rig.client()); + OcrParams p; + p.reference = "https://arxiv.org/pdf/2201.04234"; + p.pages = {0, 1}; + p.table_format = "html"; + OcrResult r = ocr.process(p); + ASSERT_EQ(r.pages.size(), 2u); + ASSERT_EQ(r.pages_processed, 29L); + ASSERT_EQ(r.pages[1].images, 1u); + ASSERT_EQ(r.pages[1].tables, 1u); + ASSERT_EQ(r.pages[1].header, "H"); + ASSERT_EQ(r.markdown(), "# TITLE\nBody\n\nPage two"); + std::string body = rig.last().body; + ASSERT_TRUE(has(body, "\"document\":{\"document_url\":\"https://arxiv.org/pdf/2201.04234\",\"type\":\"document_url\"}")); + ASSERT_TRUE(has(body, "\"model\":\"mistral-ocr-latest\"")); + ASSERT_TRUE(has(body, "\"pages\":[0,1]")); + p.source = OcrSource::kFileId; + p.reference = "file-1"; + ocr.process(p); + ASSERT_TRUE(has(rig.last().body, "\"document\":{\"file_id\":\"file-1\",\"type\":\"file\"}")); + p.source = OcrSource::kImageUrl; + p.reference = "data:image/png;base64,AAAA"; + ocr.process(p); + ASSERT_TRUE(has(rig.last().body, "\"type\":\"image_url\"")); + p.reference = "ftp://x"; + ASSERT_THROW(ocr.process(p), ApiError); + p.source = OcrSource::kDocumentUrl; + p.reference = "https://x.test/a.pdf"; + p.pages = {-1}; + ASSERT_THROW(ocr.process(p), ApiError); + p.pages.clear(); + p.table_format = "csv"; + ASSERT_THROW(ocr.process(p), ApiError); +} + +TEST(mistral_audio, transcription_speech_and_voices) { + Rig rig; + rig.on(Method::kPost, "/v1/audio/transcriptions", reply(kTranscription), 2); + rig.on(Method::kPost, "/v1/audio/speech", reply("{\"audio_data\":\"AAAA\"}")); + rig.on(Method::kGet, "/v2/audio/voices", reply("{\"data\":[],\"next_page_token\":null}")); + rig.on(Method::kGet, "/v1/audio/voices/v1/sample", reply("RIFFwav", 200, "audio/wav")); + rig.on(Method::kDelete, "/v1/audio/voices/v1", reply("{\"id\":\"v1\"}")); + Audio a(rig.client()); + TranscriptionParams t; + t.file_data = "RIFF...."; + t.file_name = "a.wav"; + t.language = "en"; + t.diarize = true; + t.timestamp_granularities = {"segment"}; + t.context_bias = {"Voxtral"}; + TranscriptionResult r = a.transcribe(t); + ASSERT_EQ(r.text, "This week, I traveled to Chicago."); + ASSERT_EQ(r.language, "en"); + ASSERT_EQ(r.segments.size(), 1u); + ASSERT_EQ(r.segments[0].speaker_id, "s1"); + ASSERT_EQ(r.usage.prompt_audio_seconds, 11); + auto s = rig.transport->seen()[0]; + ASSERT_TRUE(has(s.headers.get_or("Content-Type", ""), "multipart/form-data")); + ASSERT_TRUE(has(s.body, "name=\"model\"")); + ASSERT_TRUE(has(s.body, "voxtral-mini-latest")); + ASSERT_TRUE(has(s.body, "name=\"diarize\"")); + ASSERT_TRUE(has(s.body, "name=\"context_bias\"")); + ASSERT_TRUE(has(s.body, "name=\"timestamp_granularities\"")); + ASSERT_TRUE(has(s.body, "filename=\"a.wav\"")); + TranscriptionParams u; + u.file_url = "https://x.test/a.mp3"; + a.transcribe(u); + ASSERT_TRUE(has(rig.last().body, "name=\"file_url\"")); + // exactly one source + TranscriptionParams none; + ASSERT_THROW(a.transcribe(none), ApiError); + TranscriptionParams two = u; + two.file_id = "f1"; + ASSERT_THROW(a.transcribe(two), ApiError); + TranscriptionParams longurl; + longurl.file_url = "https://x.test/" + std::string(2100, 'a'); + ASSERT_THROW(a.transcribe(longurl), ApiError); + TranscriptionParams badg = u; + badg.timestamp_granularities = {"phoneme"}; + ASSERT_THROW(a.transcribe(badg), ApiError); + + SpeechParams sp; + sp.input = "Hello"; + sp.response_format = "mp3"; + a.speech(sp); + ASSERT_TRUE(has(rig.last().body, "\"input\":\"Hello\"")); + sp.voice_id = "v1"; + sp.ref_audio = "AAAA"; + ASSERT_THROW(a.speech(sp), ApiError); + a.voices(); + ASSERT_TRUE(has(rig.last().target, "page_size=50")); + ASSERT_EQ(a.voice_sample("v1"), "RIFFwav"); + a.remove_voice("v1"); + ASSERT_THROW(a.update_voice("v1", Json::parse("{\"bogus\":1}")), ApiError); + ASSERT_THROW(a.voice("../x"), ApiError); +} + +TEST(mistral_batch, create_list_get_cancel_delete) { + Rig rig; + rig.on(Method::kPost, "/v1/batch/jobs", reply(kBatch)); + rig.on(Method::kGet, "/v1/batch/jobs", reply("{\"data\":[],\"object\":\"list\",\"total\":0}")); + rig.on(Method::kGet, "/v1/batch/jobs/b1", reply(kBatch)); + rig.on(Method::kPost, "/v1/batch/jobs/b1/cancel", reply(kBatch)); + rig.on(Method::kDelete, "/v1/batch/jobs/b1", reply("{\"id\":\"b1\",\"object\":\"batch\",\"deleted\":true}")); + Batch b(rig.client()); + BatchParams p; + p.model = "mistral-small-latest"; + p.input_files = {"f1"}; + p.metadata.set("k", Json("v")); + BatchJob j = b.create(p); + ASSERT_EQ(j.id, "b1"); + ASSERT_EQ(j.status, "RUNNING"); + ASSERT_EQ(j.total_requests, 4L); + ASSERT_FALSE(j.terminal()); + ASSERT_TRUE(has(rig.last().body, "\"endpoint\":\"/v1/chat/completions\"")); + ASSERT_TRUE(has(rig.last().body, "\"input_files\":[\"f1\"]")); + ASSERT_FALSE(has(rig.last().body, "timeout_hours")); + BatchListParams lp; + lp.status = "RUNNING"; + ASSERT_EQ(b.list(lp).items, 0u); + ASSERT_TRUE(has(rig.last().target, "status=RUNNING")); + ASSERT_EQ(b.get("b1", true).id, "b1"); + ASSERT_TRUE(has(rig.last().target, "inline=true")); + ASSERT_EQ(b.cancel("b1").id, "b1"); + ASSERT_TRUE(b.remove("b1").find("deleted")->as_bool()); +} + +TEST(mistral_batch, validation_before_the_network) { + Rig rig; + Batch b(rig.client()); + BatchParams p; + ASSERT_THROW(b.create(p), ApiError); // nothing set + p.model = "m"; + ASSERT_THROW(b.create(p), ApiError); // no input + p.input_files = {"f1"}; + p.requests.push_back({"c", Json::object()}); + ASSERT_THROW(b.create(p), ApiError); // both inputs + p.requests.clear(); + p.agent_id = "ag"; + ASSERT_THROW(b.create(p), ApiError); // model and agent + p.agent_id.clear(); + p.endpoint = "/v1/images"; + ASSERT_THROW(b.create(p), ApiError); + p.endpoint = "/v1/embeddings"; + p.timeout_hours = 169; + ASSERT_THROW(b.create(p), ApiError); + p.timeout_hours = 0; + ASSERT_THROW(b.create(p), ApiError); + p.timeout_hours = 24; + p.input_files.clear(); + p.requests.assign(10001, BatchRequestItem{}); + ASSERT_THROW(b.create(p), ApiError); + BatchListParams lp; + lp.status = "DONE"; + ASSERT_THROW(b.list(lp), ApiError); + ASSERT_THROW(b.get("a/b"), ApiError); + ASSERT_EQ(rig.transport->send_count(), 0u); +} + +TEST(mistral_finetuning, jobs_models_and_validation) { + Rig rig; + rig.on(Method::kPost, "/v1/fine_tuning/jobs", reply(kJob)); + rig.on(Method::kPost, "/v1/fine_tuning/jobs/j1/start", reply(kJob)); + rig.on(Method::kPost, "/v1/fine_tuning/jobs/j1/cancel", reply(kJob)); + rig.on(Method::kGet, "/v1/fine_tuning/jobs/j1", reply(kJob)); + rig.on(Method::kGet, "/v1/fine_tuning/jobs", reply("{\"data\":[],\"object\":\"list\",\"total\":0}")); + rig.on(Method::kPost, "/v1/fine_tuning/models/ft:m:1/archive", reply("{\"id\":\"ft:m:1\",\"object\":\"model\",\"archived\":true}")); + rig.on(Method::kDelete, "/v1/fine_tuning/models/ft:m:1/archive", reply("{\"id\":\"ft:m:1\",\"object\":\"model\",\"archived\":false}")); + rig.on(Method::kPatch, "/v1/fine_tuning/models/ft:m:1", reply("{\"id\":\"ft:m:1\"}")); + FineTuning ft(rig.client()); + FineTuneJobParams p; + p.model = "open-mistral-7b"; + p.training_files.push_back({"f1", 1.0}); + p.hyperparameters.set("training_steps", Json(10)); + p.hyperparameters.set("learning_rate", Json(0.0001)); + p.dry_run = true; + FineTuneJob j = ft.create_job(p); + ASSERT_EQ(j.id, "j1"); + ASSERT_EQ(j.status, "QUEUED"); + ASSERT_FALSE(j.terminal()); + ASSERT_TRUE(has(rig.last().target, "dry_run=true")); + ASSERT_TRUE(has(rig.last().body, "\"training_files\":[{\"file_id\":\"f1\",\"weight\":1}]")); + ASSERT_EQ(ft.start_job("j1").id, "j1"); + ASSERT_EQ(ft.get_job("j1").job_type, "completion"); + ASSERT_EQ(ft.cancel_job("j1").id, "j1"); + ASSERT_EQ(ft.list_jobs().items, 0u); + ft.archive_model("ft:m:1"); + ft.unarchive_model("ft:m:1"); + ft.update_model("ft:m:1", "name", ""); + ASSERT_TRUE(has(rig.last().body, "\"name\":\"name\"")); + ASSERT_THROW(ft.update_model("ft:m:1", "", ""), ApiError); + FineTuneJobParams bad; + ASSERT_THROW(ft.create_job(bad), ApiError); + bad = p; + bad.hyperparameters = Json::object(); + ASSERT_THROW(ft.create_job(bad), ApiError); + bad = p; + bad.training_files.clear(); + ASSERT_THROW(ft.create_job(bad), ApiError); + bad = p; + bad.suffix = std::string(19, 'a'); + ASSERT_THROW(ft.create_job(bad), ApiError); + bad = p; + bad.invalid_sample_skip_percentage = 1.5; + ASSERT_THROW(ft.create_job(bad), ApiError); + FineTuneListParams lp; + lp.status = "DONE"; + ASSERT_THROW(ft.list_jobs(lp), ApiError); + ASSERT_THROW(ft.cancel_job("a b"), ApiError); + FineTuneJob done; + done.status = "SUCCESS"; + ASSERT_TRUE(done.terminal()); +}