Files
moloapp/src/meta/chat_completions.cpp
T

294 lines
10 KiB
C++
Raw Normal View History

#include "meta/chat_completions.h"
#include "meta/calls.h"
#include "meta/caps.h"
#include "meta/check.h"
#include "meta/schema_check.h"
namespace molo::meta {
namespace {
Json msg(const char* role, const Json& content) {
Json o = Json::object();
o.set("role", Json(role));
o.set("content", content);
return o;
}
Json part(const char* type) {
Json o = Json::object();
o.set("type", Json(type));
return o;
}
} // namespace
Json ChatMessage::developer(const std::string& t) { return msg("developer", Json(t)); }
Json ChatMessage::system(const std::string& t) { return msg("system", Json(t)); }
Json ChatMessage::user(const std::string& t) { return msg("user", Json(t)); }
Json ChatMessage::user_parts(const std::vector<Json>& parts) {
Json a = Json::array();
for (const Json& p : parts) a.push(p);
return msg("user", a);
}
Json ChatMessage::assistant(const std::string& t) { return msg("assistant", Json(t)); }
Json ChatMessage::assistant_tool_calls(const std::string& t, const std::vector<Json>& calls) {
Json m = msg("assistant", t.empty() ? Json() : Json(t));
Json a = Json::array();
for (const Json& c : calls) a.push(c);
m.set("tool_calls", a);
return m;
}
Json ChatMessage::tool_result(const std::string& id, const std::string& content) {
Check::call_id(id);
Json m = msg("tool", Json(content));
m.set("tool_call_id", Json(id));
return m;
}
Json ChatMessage::text_part(const std::string& t) {
Json p = part("text");
p.set("text", Json(t));
return p;
}
Json ChatMessage::image_part(const std::string& url, const std::string& detail) {
Check::non_empty("image url", url);
if (!detail.empty()) Check::one_of("detail", detail, {"auto", "low", "high"});
Json p = part("image_url");
Json i = Json::object();
i.set("url", Json(url));
JsonX::set_if(i, "detail", detail);
p.set("image_url", i);
return p;
}
Json ChatMessage::video_part(const std::string& url, double fps) {
Check::non_empty("video url", url);
Json p = part("video_url");
Json v = Json::object();
v.set("url", Json(url));
if (fps > 0) v.set("fps", Json(fps));
p.set("video_url", v);
return p;
}
Json ChatMessage::file_part(const std::string& filename, const std::string& data_url,
const std::string& file_id) {
if (data_url.empty() && file_id.empty()) Check::fail("file part needs file_data or file_id");
Json p = part("file");
Json f = Json::object();
JsonX::set_if(f, "filename", filename);
JsonX::set_if(f, "file_data", data_url);
if (!file_id.empty()) {
Check::segment("file_id", file_id);
f.set("file_id", Json(file_id));
}
p.set("file", f);
return p;
}
Json ChatMessage::audio_part(const std::string& format, const std::string& b64) {
Check::one_of("format", format, {"wav", "mp3"});
Check::non_empty("audio data", b64);
Json p = part("input_audio");
Json a = Json::object();
a.set("data", Json(b64));
a.set("format", Json(format));
p.set("input_audio", a);
return p;
}
Json ChatMessage::tool_call(const std::string& id, const std::string& name,
const std::string& arguments) {
Check::call_id(id);
Check::function_name(name);
Json c = Json::object();
c.set("id", Json(id));
c.set("type", Json("function"));
Json f = Json::object();
f.set("name", Json(name));
f.set("arguments", Json(arguments.empty() ? "{}" : arguments));
c.set("function", f);
return c;
}
Json ChatMessage::function_tool(const std::string& name, const std::string& description,
const Json& parameters, bool strict) {
Check::function_name(name);
if (parameters.is_object()) SchemaCheck::check(parameters, strict);
Json f = Json::object();
f.set("name", Json(name));
JsonX::set_if(f, "description", description);
f.set("parameters", parameters.is_object() ? parameters : Json::object());
if (strict) f.set("strict", Json(true));
Json t = Json::object();
t.set("type", Json("function"));
t.set("function", f);
return t;
}
Json ChatCompletionRequest::format_json_object() {
Json f = Json::object();
f.set("type", Json("json_object"));
return f;
}
Json ChatCompletionRequest::format_json_schema(const std::string& name, const Json& schema,
bool strict, const std::string& description) {
Check::non_empty("name", name);
SchemaCheck::check(schema, strict);
Json js = Json::object();
js.set("name", Json(name));
js.set("schema", schema);
if (strict) js.set("strict", Json(true));
JsonX::set_if(js, "description", description);
Json f = Json::object();
f.set("type", Json("json_schema"));
f.set("json_schema", js);
return f;
}
void ChatCompletionRequest::validate() const {
Check::model_id(model);
if (messages.empty() || messages.size() > MetaCaps::kMaxInputItems)
Check::fail("messages needs 1.." + std::to_string(MetaCaps::kMaxInputItems) + " entries");
for (const Json& m : messages) {
Check::one_of("message role", JsonX::str(m, "role"),
{"developer", "system", "user", "assistant", "tool"});
}
if (tools.size() > MetaCaps::kMaxTools) Check::fail("too many tools");
for (const Json& t : tools) {
if (JsonX::str(t, "type") != "function")
Check::fail("chat completions accepts function tools only (custom tools are Responses-only)");
if (const Json* f = JsonX::obj(t, "function")) Check::function_name(JsonX::str(*f, "name"));
}
if (max_completion_tokens && *max_completion_tokens < 1)
Check::fail("max_completion_tokens must be positive");
if (temperature) Check::temperature(*temperature);
if (top_p) Check::top_p(*top_p);
if (frequency_penalty) Check::penalty("frequency_penalty", *frequency_penalty);
if (presence_penalty) Check::penalty("presence_penalty", *presence_penalty);
Check::reasoning_effort(model, reasoning_effort);
Check::safety_identifier(safety_identifier);
Check::prompt_cache_retention(prompt_cache_retention);
if (!service_tier.empty())
Check::one_of("service_tier", service_tier, {"auto", "default", "flex", "scale", "priority"});
if (metadata.size() > MetaCaps::kMaxMetadataPairs) Check::fail("too many metadata pairs");
if (response_format.is_object()) {
std::string type = JsonX::str(response_format, "type");
Check::one_of("response_format.type", type, {"text", "json_object", "json_schema"});
if (type == "json_schema") {
const Json* js = JsonX::obj(response_format, "json_schema");
const Json* s = js ? JsonX::obj(*js, "schema") : nullptr;
if (!s) Check::fail("response_format json_schema needs json_schema.schema");
SchemaCheck::check(*s, JsonX::flag(*js, "strict"));
}
}
// Fields the docs reject on this endpoint must not arrive through `extra`.
if (extra.is_object())
for (const char* banned : {"n", "stop", "logit_bias", "prediction", "verbosity",
"web_search_options", "modalities", "audio", "logprobs", "text",
"max_output_tokens"})
if (extra.has(banned)) Check::fail(std::string(banned) + " is rejected by chat completions");
}
Json ChatCompletionRequest::to_json(bool stream) const {
validate();
Json b = Json::object();
b.set("model", Json(model));
Json m = Json::array();
for (const Json& x : messages) m.push(x);
b.set("messages", m);
if (!tools.empty()) {
Json t = Json::array();
for (const Json& x : tools) t.push(x);
b.set("tools", t);
}
if (response_format.is_object()) b.set("response_format", response_format);
JsonX::set_if(b, "reasoning_effort", reasoning_effort);
if (max_completion_tokens) b.set("max_completion_tokens", Json(*max_completion_tokens));
if (temperature) b.set("temperature", Json(*temperature));
if (top_p) b.set("top_p", Json(*top_p));
if (frequency_penalty) b.set("frequency_penalty", Json(*frequency_penalty));
if (presence_penalty) b.set("presence_penalty", Json(*presence_penalty));
if (seed) b.set("seed", Json(*seed));
if (parallel_tool_calls) b.set("parallel_tool_calls", Json(*parallel_tool_calls));
JsonX::set_if(b, "safety_identifier", safety_identifier);
JsonX::set_if(b, "prompt_cache_key", prompt_cache_key);
JsonX::set_if(b, "prompt_cache_retention", prompt_cache_retention);
JsonX::set_if(b, "service_tier", service_tier);
if (!metadata.empty()) {
Json md = Json::object();
for (const auto& [k, v] : metadata) md.set(k, Json(v));
b.set("metadata", md);
}
if (stream) {
b.set("stream", Json(true));
if (include_usage) {
Json so = Json::object();
so.set("include_usage", Json(true));
b.set("stream_options", so);
}
}
JsonX::merge(b, extra);
return b;
}
void ChatChunkAccumulator::feed(const Json& chunk) {
id_ = JsonX::str(chunk, "id", id_);
model_ = JsonX::str(chunk, "model", model_);
if (const Json* u = JsonX::obj(chunk, "usage")) {
usage_ = MetaUsage::from_chat(*u);
has_usage_ = true;
}
const Json* choices = JsonX::arr(chunk, "choices");
if (!choices || choices->as_array().empty()) return;
const Json& c = choices->as_array()[0];
std::string fr = JsonX::str(c, "finish_reason");
if (!fr.empty()) finish_reason_ = fr;
const Json* d = JsonX::obj(c, "delta");
if (!d) return;
text_ += JsonX::str(*d, "content");
reasoning_ += JsonX::str(*d, "reasoning_content");
if (const Json* tcs = JsonX::arr(*d, "tool_calls"))
for (const Json& tc : tcs->as_array()) {
long idx = JsonX::num(tc, "index", 0);
ChatToolCallState* st = nullptr;
for (ChatToolCallState& s : calls_)
if (s.index == idx) st = &s;
if (!st) {
calls_.push_back({});
st = &calls_.back();
st->index = idx;
}
st->id = JsonX::str(tc, "id", st->id);
if (const Json* f = JsonX::obj(tc, "function")) {
st->name = JsonX::str(*f, "name", st->name);
st->arguments += JsonX::str(*f, "arguments");
}
}
}
Json ChatCompletionsService::create(const ChatCompletionRequest& request) {
Json body = request.to_json(false);
return client_->post(path_for(Call::kChatCompletionsCreate), body);
}
ChatChunkAccumulator ChatCompletionsService::stream(const ChatCompletionRequest& request,
const ChunkSink& sink) {
Json body = request.to_json(true);
ChatChunkAccumulator acc;
std::size_t seen = 0;
client_->stream(path_for(Call::kChatCompletionsCreate), body, [&](const llm::SseEvent& e) {
if (++seen > MetaCaps::kMaxStreamEvents) return false;
acc.feed(e.data);
return sink ? sink(e.data) : true;
});
return acc;
}
} // namespace molo::meta