feat(clients): mistral: La Plateforme package, 347-op call table, typed services, ChatModel, demo
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Vendored
+214
@@ -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": "<text>"}` 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 |
|
||||
Vendored
+152
@@ -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
|
||||
Vendored
+62
@@ -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 <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<std::string> 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<long> 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<bool> deployment_chat;
|
||||
void validate() const;
|
||||
};
|
||||
|
||||
class Agents : public MistralService {
|
||||
public:
|
||||
explicit Agents(std::shared_ptr<llm::ApiClient> 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
|
||||
Vendored
+168
@@ -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
|
||||
Vendored
+86
@@ -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 <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<double> temperature;
|
||||
bool diarize = false;
|
||||
std::vector<std::string> context_bias; // <= 100 words
|
||||
std::vector<std::string> 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<TranscriptionSegment> 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<std::string> languages;
|
||||
std::string gender, description;
|
||||
std::optional<int> age;
|
||||
std::vector<std::string> 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<llm::ApiClient> 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
|
||||
Vendored
+122
@@ -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<const char*> 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<std::size_t>(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
|
||||
Vendored
+60
@@ -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 <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<std::string> input_files; // file ids, <= 100
|
||||
std::vector<BatchRequestItem> 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<bool> 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<llm::ApiClient> 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
|
||||
Vendored
+107
@@ -0,0 +1,107 @@
|
||||
#include "vendors/mistral/calls.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#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<std::string> Calls::path_params(const CallSpec& spec) {
|
||||
std::vector<std::string> 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<std::string>& 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<std::string>& ids) {
|
||||
return render(find(op), ids);
|
||||
}
|
||||
|
||||
} // namespace molo::mistral
|
||||
Vendored
+48
@@ -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 <cstddef>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
#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<std::string> 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<std::string>& ids = {});
|
||||
static std::string render(const CallSpec& spec, const std::vector<std::string>& ids = {});
|
||||
};
|
||||
|
||||
} // namespace molo::mistral
|
||||
Vendored
+351
@@ -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
|
||||
Vendored
+48
@@ -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 <cstddef>
|
||||
|
||||
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
|
||||
Vendored
+90
@@ -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
|
||||
Vendored
+54
@@ -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 <functional>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<llm::ApiClient> 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<bool(const StreamDelta&)>; // 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<double> temperature; // 0..1.5
|
||||
std::optional<double> top_p; // (0,1]
|
||||
std::optional<int> max_tokens;
|
||||
std::optional<int> min_tokens;
|
||||
std::optional<int> random_seed;
|
||||
std::vector<std::string> 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<llm::ApiClient> client) : MistralService(std::move(client)) {}
|
||||
ChatResult create(const FimParams& params);
|
||||
ChatResult stream(const FimParams& params, const Chat::DeltaSink& on_delta);
|
||||
};
|
||||
|
||||
} // namespace molo::mistral
|
||||
Vendored
+171
@@ -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> chat, std::string default_model)
|
||||
: chat_(std::move(chat)), default_model_(std::move(default_model)) {}
|
||||
|
||||
std::shared_ptr<MistralChatModel> MistralChatModel::from_env(const std::string& default_model) {
|
||||
Platform p = Platform::from_env();
|
||||
return std::make_shared<MistralChatModel>(std::make_shared<Chat>(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<Json> 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<bool(const llm::ChatChunk&)>& 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
|
||||
Vendored
+46
@@ -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 <memory>
|
||||
|
||||
#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> chat, std::string default_model = ChatParams::kDefaultModel);
|
||||
static std::shared_ptr<MistralChatModel> 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<bool(const llm::ChatChunk&)>& 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> chat_;
|
||||
std::string default_model_;
|
||||
};
|
||||
|
||||
} // namespace molo::mistral
|
||||
Vendored
+223
@@ -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<Json>& 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
|
||||
Vendored
+90
@@ -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 <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<Json>& 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<Json> messages;
|
||||
std::optional<double> temperature; // 0..1.5
|
||||
std::optional<double> top_p; // (0,1]
|
||||
std::optional<double> presence_penalty; // -2..2
|
||||
std::optional<double> frequency_penalty; // -2..2
|
||||
std::optional<double> repetition_penalty; // (0,2]
|
||||
std::optional<int> max_tokens; // >= 0
|
||||
std::optional<int> min_tokens; // >= 0
|
||||
std::optional<int> random_seed; // >= 0
|
||||
std::optional<int> top_k; // >= -1
|
||||
std::optional<int> n; // 1..16
|
||||
std::optional<int> top_logprobs;
|
||||
std::optional<bool> logprobs;
|
||||
std::optional<bool> safe_prompt;
|
||||
std::optional<bool> parallel_tool_calls;
|
||||
std::vector<std::string> stop;
|
||||
std::vector<ToolDef> 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
|
||||
Vendored
+174
@@ -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<std::size_t>(MistralCaps::kMaxChoices)) break;
|
||||
ChatChoice ch;
|
||||
ch.index = static_cast<int>(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<int>(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<int>(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
|
||||
Vendored
+74
@@ -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 <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<ToolCallRecord> 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<ChatChoice> 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<int, ToolCallRecord> calls;
|
||||
};
|
||||
std::string id_, model_, object_;
|
||||
long created_ = 0;
|
||||
TokenUsage usage_;
|
||||
std::map<int, Slot> slots_;
|
||||
bool finished_ = false;
|
||||
std::size_t events_ = 0;
|
||||
};
|
||||
|
||||
} // namespace molo::mistral
|
||||
Vendored
+72
@@ -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<std::string, std::map<std::string, double>> 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<std::string>& 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<Json>& 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
|
||||
Vendored
+33
@@ -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 <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "vendors/mistral/types.h"
|
||||
|
||||
namespace molo::mistral {
|
||||
|
||||
struct ClassificationResult {
|
||||
std::string id, model;
|
||||
// per input: target name -> label -> score
|
||||
std::vector<std::map<std::string, std::map<std::string, double>>> 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<llm::ApiClient> client) : MistralService(std::move(client)) {}
|
||||
ClassificationResult classify(const std::string& model, const std::vector<std::string>& texts);
|
||||
ClassificationResult classify_chat(const std::string& model, const std::vector<Json>& messages);
|
||||
};
|
||||
|
||||
} // namespace molo::mistral
|
||||
+243
@@ -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<Json>& 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<std::string>& 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
|
||||
Vendored
+110
@@ -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 <functional>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<Json>& 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<bool> 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<bool> 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<bool> 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<llm::ApiClient> 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<bool(const ConversationEvent&)>; // 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<std::string>& ids, const Json& body,
|
||||
const EventSink& sink);
|
||||
};
|
||||
|
||||
} // namespace molo::mistral
|
||||
Vendored
+63
@@ -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<double> 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
|
||||
Vendored
+39
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
// Embeddings: POST /v1/embeddings. input is one string or an array of strings.
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "vendors/mistral/types.h"
|
||||
|
||||
namespace molo::mistral {
|
||||
|
||||
struct EmbeddingParams {
|
||||
static constexpr const char* kDefaultModel = "mistral-embed";
|
||||
std::string model = kDefaultModel;
|
||||
std::vector<std::string> inputs; // 1..512 inputs, each 1..1 MiB
|
||||
std::optional<int> 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<std::vector<double>> vectors; // float / integer outputs; empty for base64
|
||||
std::vector<std::string> encoded; // base64 outputs
|
||||
TokenUsage usage;
|
||||
Json raw;
|
||||
static EmbeddingResult parse(const Json& doc);
|
||||
};
|
||||
|
||||
class Embeddings : public MistralService {
|
||||
public:
|
||||
explicit Embeddings(std::shared_ptr<llm::ApiClient> client) : MistralService(std::move(client)) {}
|
||||
EmbeddingResult create(const EmbeddingParams& params);
|
||||
};
|
||||
|
||||
} // namespace molo::mistral
|
||||
Vendored
+122
@@ -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<int>(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<std::size_t>(params.page_size)) return all;
|
||||
}
|
||||
all.truncated = all.total < 0 || static_cast<long>(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
|
||||
Vendored
+66
@@ -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 <string>
|
||||
|
||||
#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<FileInfo> 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<llm::ApiClient> 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
|
||||
Vendored
+141
@@ -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
|
||||
Vendored
+71
@@ -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 <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<bool> 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<TrainingFileRef> training_files; // 1..64
|
||||
std::vector<std::string> 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<llm::ApiClient> 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
|
||||
Vendored
+214
@@ -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
|
||||
Vendored
+86
@@ -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 <optional>
|
||||
#include <string>
|
||||
|
||||
#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<int> 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<bool> 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<llm::ApiClient> 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
|
||||
Vendored
+86
@@ -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
|
||||
Vendored
+52
@@ -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 <string>
|
||||
#include <vector>
|
||||
|
||||
#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<std::string> aliases;
|
||||
ModelCapabilities capabilities;
|
||||
bool fine_tuned() const { return type == "fine-tuned"; }
|
||||
static ModelCard parse(const Json& doc);
|
||||
};
|
||||
|
||||
struct ModelList {
|
||||
std::vector<ModelCard> 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<llm::ApiClient> 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
|
||||
Vendored
+70
@@ -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<std::string> ModerationItem::flagged_categories() const {
|
||||
std::vector<std::string> 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<std::string>& 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<Json>& 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
|
||||
Vendored
+38
@@ -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 <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "vendors/mistral/types.h"
|
||||
|
||||
namespace molo::mistral {
|
||||
|
||||
struct ModerationItem {
|
||||
std::map<std::string, bool> categories;
|
||||
std::map<std::string, double> scores;
|
||||
bool flagged() const; // any category true
|
||||
std::vector<std::string> flagged_categories() const;
|
||||
};
|
||||
|
||||
struct ModerationResult {
|
||||
std::string id, model;
|
||||
std::vector<ModerationItem> 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<llm::ApiClient> client) : MistralService(std::move(client)) {}
|
||||
// 1..256 texts, each 1..1 MiB.
|
||||
ModerationResult moderate(const std::vector<std::string>& texts, const std::string& model = kDefaultModel);
|
||||
// One conversation (messages built with Messages::*), 1..4096 messages.
|
||||
ModerationResult moderate_chat(const std::vector<Json>& messages, const std::string& model = kDefaultModel);
|
||||
};
|
||||
|
||||
} // namespace molo::mistral
|
||||
Vendored
+108
@@ -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<int>(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
|
||||
Vendored
+59
@@ -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 <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<int> pages; // 0-based page indexes; empty: all (max 1000)
|
||||
std::optional<bool> include_image_base64;
|
||||
std::optional<int> image_limit; // >= 0
|
||||
std::optional<int> image_min_size; // >= 0
|
||||
std::string table_format; // "", markdown, html
|
||||
bool extract_header = false;
|
||||
bool extract_footer = false;
|
||||
std::optional<bool> 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<OcrPage> 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<llm::ApiClient> client) : MistralService(std::move(client)) {}
|
||||
OcrResult process(const OcrParams& params);
|
||||
};
|
||||
|
||||
} // namespace molo::mistral
|
||||
Vendored
+61
@@ -0,0 +1,61 @@
|
||||
#include "vendors/mistral/operations.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
#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<std::string> Operations::list(const std::string& area) {
|
||||
std::vector<std::string> 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<std::string> Operations::areas() {
|
||||
std::vector<std::string> 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
|
||||
Vendored
+39
@@ -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 <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "vendors/mistral/calls.h"
|
||||
#include "vendors/mistral/types.h"
|
||||
|
||||
namespace molo::mistral {
|
||||
|
||||
struct Invocation {
|
||||
std::string op; // operationId
|
||||
std::vector<std::string> ids; // path parameters in order
|
||||
std::map<std::string, std::string> query;
|
||||
Json body; // null: no body
|
||||
bool allow_destructive = false;
|
||||
};
|
||||
|
||||
class Operations : public MistralService {
|
||||
public:
|
||||
explicit Operations(std::shared_ptr<llm::ApiClient> 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<std::string> list(const std::string& area = "");
|
||||
static std::vector<std::string> areas();
|
||||
};
|
||||
|
||||
} // namespace molo::mistral
|
||||
Vendored
+101
@@ -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
|
||||
Vendored
+26
@@ -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<clients::Transport> transport, const std::string& key) {
|
||||
Platform p;
|
||||
p.client = std::make_shared<llm::ApiClient>(std::move(transport), config(), key);
|
||||
return p;
|
||||
}
|
||||
|
||||
} // namespace molo::mistral
|
||||
Vendored
+26
@@ -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 <memory>
|
||||
|
||||
#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<llm::ApiClient> 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<clients::Transport> transport, const std::string& key);
|
||||
bool configured() const { return client && client->configured(); }
|
||||
};
|
||||
|
||||
} // namespace molo::mistral
|
||||
Vendored
+103
@@ -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<std::string>& 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<unsigned char>(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<unsigned char>(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<const char*> 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
|
||||
Vendored
+53
@@ -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 <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<std::string>& 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:<mime>;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<const char*> 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
|
||||
Binary file not shown.
Binary file not shown.
+162
@@ -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())
|
||||
+56
@@ -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()
|
||||
Vendored
+282
@@ -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
|
||||
Vendored
+108
@@ -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
|
||||
Vendored
+94
@@ -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<std::string> J::strings(const Json& doc, const char* key) {
|
||||
std::vector<std::string> out;
|
||||
if (const Json* a = arr(doc, key))
|
||||
for (const Json& 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<int>(J::num(*u, "prompt_tokens"));
|
||||
t.completion_tokens = static_cast<int>(J::num(*u, "completion_tokens"));
|
||||
t.total_tokens = static_cast<int>(J::num(*u, "total_tokens"));
|
||||
t.prompt_audio_seconds = static_cast<int>(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<std::string>& 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<std::string>& ids) {
|
||||
const CallSpec& spec = Calls::find(op);
|
||||
return client_->post_multipart(Calls::render(spec, ids), form);
|
||||
}
|
||||
|
||||
} // namespace molo::mistral
|
||||
Vendored
+61
@@ -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 <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<std::string> 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<llm::ApiClient> client) : llm::Service(std::move(client)) {}
|
||||
|
||||
protected:
|
||||
Json call(const char* op, const std::vector<std::string>& ids = {}, const Json* body = nullptr,
|
||||
const std::string& query = "");
|
||||
Json call_multipart(const char* op, const clients::body::MultipartBody& form,
|
||||
const std::vector<std::string>& ids = {});
|
||||
};
|
||||
|
||||
} // namespace molo::mistral
|
||||
Reference in New Issue
Block a user