#include "google/optimize/model_json.h" #include "google/errors.h" #include "google/optimize/caps.h" namespace molo::google::optimize { using clients::body::Json; JsonModelCall::JsonModelCall(std::shared_ptr model, std::string model_name) : model_(std::move(model)), model_name_(std::move(model_name)), calls_(std::make_shared(0)) { if (!model_) throw GoogleError::invalid("a language model is required"); } GenerateRequest JsonModelCall::build(const std::string& system, const std::string& user, bool json) const { if (user.size() > OptimizeCaps::kMaxTranscriptChars + OptimizeCaps::kMaxTextChars) throw GoogleError::invalid("model input over the character cap"); GenerateRequest req; req.model = model_name_; if (!system.empty()) { Content sys; sys.parts.push_back(Part::of_text(system)); req.system = sys; } req.contents.push_back(Content::user(user)); req.config.temperature = 0.0; req.config.max_output_tokens = 1024; req.config.thinking_budget = 0; if (json) req.config.response_mime_type = "application/json"; return req; } Json JsonModelCall::extract(const std::string& text) { std::size_t start = text.find('{'); while (start != std::string::npos) { int depth = 0; bool in_str = false; bool esc = false; for (std::size_t i = start; i < text.size(); ++i) { char c = text[i]; if (in_str) { if (esc) esc = false; else if (c == '\\') esc = true; else if (c == '"') in_str = false; continue; } if (c == '"') in_str = true; else if (c == '{') ++depth; else if (c == '}' && --depth == 0) { try { Json doc = Json::parse(text.substr(start, i - start + 1)); if (doc.is_object()) return doc; } catch (const clients::body::JsonError&) { } break; } } start = text.find('{', start + 1); } return Json(); } Json JsonModelCall::ask(const std::string& system, const std::string& user) const { GenerateRequest req = build(system, user, true); for (int attempt = 0; attempt <= OptimizeCaps::kMaxModelRetries; ++attempt) { ++*calls_; Json doc = extract(model_->generate(req).text()); if (doc.is_object()) return doc; } throw GoogleError(502, "BAD_RESPONSE", "model reply held no JSON object", "optimize"); } std::string JsonModelCall::ask_text(const std::string& system, const std::string& user) const { ++*calls_; return model_->generate(build(system, user, false)).text(); } } // namespace molo::google::optimize