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
+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
|
||||
Reference in New Issue
Block a user