Files
moloapp/src/meta/response_input.cpp
T

255 lines
8.3 KiB
C++

#include "meta/response_input.h"
#include "meta/caps.h"
#include "meta/check.h"
#include "security/crypto/Base64.h"
namespace molo::meta {
namespace {
Json obj(const char* type) {
Json o = Json::object();
o.set("type", Json(type));
return o;
}
void image_mime(const std::string& mime) {
Check::one_of("mime", mime,
{"image/png", "image/jpeg", "image/jpg", "image/gif", "image/webp", "image/x-icon"});
}
std::string data_url(const std::string& mime, const std::string& bytes) {
if (bytes.empty()) Check::fail("bytes are empty");
if (bytes.size() > MetaCaps::kMaxInlineBytes) Check::fail("inline file is over the 50 MB limit");
return "data:" + mime + ";base64," + security::crypto::Base64::encode(bytes);
}
} // namespace
Json ResponseInput::text(const std::string& t) {
Json o = obj("input_text");
o.set("text", Json(t));
return o;
}
Json ResponseInput::image_url(const std::string& url, const std::string& detail) {
Check::non_empty("image_url", url);
if (!detail.empty()) Check::one_of("detail", detail, {"low", "high", "auto", "original"});
Json o = obj("input_image");
o.set("image_url", Json(url));
JsonX::set_if(o, "detail", detail);
return o;
}
Json ResponseInput::image_file(const std::string& file_id, const std::string& detail) {
Check::segment("file_id", file_id);
if (!detail.empty()) Check::one_of("detail", detail, {"low", "high", "auto", "original"});
Json o = obj("input_image");
o.set("file_id", Json(file_id));
JsonX::set_if(o, "detail", detail);
return o;
}
Json ResponseInput::image_bytes(const std::string& mime, const std::string& bytes) {
image_mime(mime);
return image_url(data_url(mime, bytes));
}
Json ResponseInput::file_id(const std::string& id) {
Check::segment("file_id", id);
Json o = obj("input_file");
o.set("file_id", Json(id));
return o;
}
Json ResponseInput::file_url(const std::string& url) {
if (url.rfind("http://", 0) != 0 && url.rfind("https://", 0) != 0)
Check::fail("file_url must be http or https");
Json o = obj("input_file");
o.set("file_url", Json(url));
return o;
}
Json ResponseInput::file_data(const std::string& filename, const std::string& mime,
const std::string& bytes) {
Check::one_of("mime", mime,
{"application/pdf", "text/plain", "application/json", "application/jsonl",
"image/png", "image/jpeg", "image/gif", "image/webp", "video/mp4",
"audio/mpeg", "audio/wav"});
Json o = obj("input_file");
JsonX::set_if(o, "filename", filename);
o.set("file_data", Json(data_url(mime, bytes)));
return o;
}
Json ResponseInput::video_url(const std::string& url, double fps) {
Check::non_empty("video_url", url);
Json o = obj("input_video");
o.set("video_url", Json(url));
if (fps > 0) o.set("fps", Json(fps));
return o;
}
Json ResponseInput::video_file(const std::string& id, double fps) {
Check::segment("file_id", id);
Json o = obj("input_video");
o.set("file_id", Json(id));
if (fps > 0) o.set("fps", Json(fps));
return o;
}
Json ResponseInput::audio_bytes(const std::string& format, const std::string& bytes) {
Check::one_of("format", format, {"wav", "mp3"});
if (bytes.empty() || bytes.size() > MetaCaps::kMaxInlineBytes) Check::fail("audio size is out of range");
Json o = obj("input_audio");
Json a = Json::object();
a.set("data", Json(security::crypto::Base64::encode(bytes)));
a.set("format", Json(format));
o.set("input_audio", a);
return o;
}
Json ResponseInput::message(const std::string& role, const Json& content_parts,
const std::string& phase) {
Check::one_of("role", role, {"user", "assistant", "system", "developer"});
if (!phase.empty()) Check::one_of("phase", phase, {"commentary", "final_answer"});
Json o = obj("message");
o.set("role", Json(role));
o.set("content", content_parts);
JsonX::set_if(o, "phase", phase);
return o;
}
Json ResponseInput::user(const std::string& t) {
Json parts = Json::array();
parts.push(text(t));
return message("user", parts);
}
Json ResponseInput::developer(const std::string& t) {
Json parts = Json::array();
parts.push(text(t));
return message("developer", parts);
}
Json ResponseInput::assistant(const std::string& t, const std::string& phase) {
Json part = obj("output_text");
part.set("text", Json(t));
part.set("annotations", Json::array());
Json parts = Json::array();
parts.push(part);
return message("assistant", parts, phase);
}
Json ResponseInput::function_call(const std::string& call_id, const std::string& name,
const std::string& arguments_json) {
Check::call_id(call_id);
Check::function_name(name);
Json o = obj("function_call");
o.set("call_id", Json(call_id));
o.set("name", Json(name));
o.set("arguments", Json(arguments_json.empty() ? "{}" : arguments_json));
return o;
}
Json ResponseInput::function_call_output(const std::string& call_id, const std::string& output) {
Check::call_id(call_id);
Json o = obj("function_call_output");
o.set("call_id", Json(call_id));
o.set("output", Json(output));
return o;
}
Json ResponseInput::reasoning(const std::string& encrypted_content, const std::string& id,
const std::vector<std::string>& summary) {
Check::non_empty("encrypted_content", encrypted_content);
Json o = obj("reasoning");
if (!id.empty()) {
Check::segment("reasoning id", id);
o.set("id", Json(id));
}
o.set("encrypted_content", Json(encrypted_content));
Json s = Json::array();
for (const std::string& t : summary) {
Json part = obj("summary_text");
part.set("text", Json(t));
s.push(part);
}
o.set("summary", s); // required on replay, [] when there is none
return o;
}
Json ResponseInput::item_reference(const std::string& id) {
Check::segment("item id", id);
Json o = obj("item_reference");
o.set("id", Json(id));
return o;
}
std::size_t ResponseInput::count(const Json& input) {
if (input.is_string()) return 1;
if (input.is_array()) return input.as_array().size();
return 0;
}
Json ToolBuilder::function(const std::string& name, const std::string& description,
const Json& parameters, bool strict, bool defer_loading) {
Check::function_name(name);
Json o = obj("function");
o.set("name", Json(name));
JsonX::set_if(o, "description", description);
o.set("parameters", parameters.is_object() ? parameters : Json::object());
if (strict) o.set("strict", Json(true));
if (defer_loading) o.set("defer_loading", Json(true));
return o;
}
Json ToolBuilder::custom(const std::string& name, const std::string& description, bool strict,
bool defer_loading) {
Check::non_empty("name", name);
Json o = obj("custom");
o.set("name", Json(name));
JsonX::set_if(o, "description", description);
if (strict) o.set("strict", Json(true));
if (defer_loading) o.set("defer_loading", Json(true));
return o;
}
Json ToolBuilder::web_search(const std::string& size, const Json& location, const Json& filters) {
Json o = obj("web_search");
if (!size.empty()) {
Check::one_of("search_context_size", size, {"low", "medium", "high"});
o.set("search_context_size", Json(size));
}
if (location.is_object()) o.set("user_location", location);
if (filters.is_object()) o.set("filters", filters);
return o;
}
Json ToolBuilder::tool_search(const std::string& execution) {
Json o = obj("tool_search");
if (!execution.empty()) {
Check::one_of("execution", execution, {"server", "client"});
o.set("execution", Json(execution));
}
return o;
}
Json ToolBuilder::computer() { return obj("computer"); }
Json ToolBuilder::namespace_tool(const std::string& name, const std::string& description,
const std::vector<Json>& functions) {
Check::non_empty("name", name);
if (name.find('.') != std::string::npos) Check::fail("namespace name must contain no dots");
if (functions.empty() || functions.size() > MetaCaps::kMaxTools)
Check::fail("namespace needs 1.." + std::to_string(MetaCaps::kMaxTools) + " functions");
Json o = obj("namespace");
o.set("name", Json(name));
JsonX::set_if(o, "description", description);
Json tools = Json::array();
for (const Json& f : functions) tools.push(f);
o.set("tools", tools);
return o;
}
} // namespace molo::meta