#include "google/armor/model_armor.h" #include "google/armor/armor_util.h" #include "google/armor/caps.h" #include "google/errors.h" #include "google/operation.h" #include "security/crypto/Base64.h" namespace molo::google::armor { using clients::body::Json; namespace { bool valid_byte_type(const std::string& t) { return t == "PLAINTEXT_UTF8" || t == "PDF" || t == "WORD_DOCUMENT" || t == "EXCEL_DOCUMENT" || t == "POWERPOINT_DOCUMENT" || t == "TXT" || t == "CSV" || t == "IMAGE"; } bool valid_filter_text(const std::string& v) { if (v.size() > ArmorCaps::kMaxFilterValueChars) return false; for (unsigned char c : v) if (c < 0x20 || c == 0x7f) return false; return true; } void validate_hint(const LanguageHint& hint) { if (hint.source_language.size() > 35) throw GoogleError::invalid("source language too long"); for (char c : hint.source_language) if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '-')) throw GoogleError::invalid("bad source language"); } } // namespace DataItem DataItem::of_text(std::string text) { DataItem d; d.text = std::move(text); return d; } DataItem DataItem::of_bytes(std::string byte_type, std::string bytes, std::string file_label) { DataItem d; d.byte_type = std::move(byte_type); d.bytes = std::move(bytes); d.file_label = std::move(file_label); return d; } void DataItem::validate(const char* what) const { bool is_bytes = !byte_type.empty() || !bytes.empty(); if (is_bytes && !text.empty()) throw GoogleError::invalid(std::string(what) + " is text or bytes, not both"); if (!is_bytes) { if (text.empty()) throw GoogleError::invalid(std::string(what) + " is empty"); if (text.size() > ArmorCaps::kMaxTextChars) throw GoogleError::invalid(std::string(what) + " exceeds 100000 characters"); return; } if (!valid_byte_type(byte_type)) throw GoogleError::invalid("byte type is not a documented ByteItemType"); if (bytes.empty()) throw GoogleError::invalid(std::string(what) + " bytes are empty"); if (bytes.size() > ArmorCaps::kMaxByteItemBytes) throw GoogleError::invalid(std::string(what) + " bytes exceed 5 MiB"); if (file_label.size() > 128) throw GoogleError::invalid("file label too long"); } Json DataItem::to_json() const { Json o = Json::object(); if (byte_type.empty() && bytes.empty()) { o.set("text", text); return o; } Json b = Json::object(); b.set("byteDataType", byte_type); b.set("byteData", security::crypto::Base64::encode(bytes)); if (!file_label.empty()) b.set("fileLabel", file_label); o.set("byteItem", b); return o; } ServiceEndpoint ModelArmor::endpoint_for(const std::string& location) { if (location == "global") return endpoints::custom("modelarmor", "https://modelarmor.googleapis.com/v1"); return endpoints::custom( "modelarmor", "https://modelarmor." + location + ".rep.googleapis.com/v1"); } ModelArmor::ModelArmor(std::shared_ptr client, CloudContext context) : Service(std::move(client), endpoint_for(context.valid() ? context.location : "us-central1")), context_(std::move(context)) {} void ModelArmor::require_regional() const { if (context_.location == "global") throw GoogleError::invalid("templates need a regional location, not global"); } std::string ModelArmor::template_path(const std::string& id) const { require_regional(); return "/" + context_.child("templates", id); } ServiceEndpoint ModelArmor::floor_endpoint() const { return endpoint_for(context_.location); } std::string ModelArmor::floor_path(const std::string& scope) const { std::string s = scope; context_.parent(); // Validates project and location. if (s.empty()) s = "projects/" + context_.project; if (!valid_scope(s)) throw GoogleError::invalid("bad floor setting scope"); return "/" + s + "/locations/" + context_.location + "/floorSetting"; } std::string ModelArmor::request_id_query(const std::string& request_id, char sep) { if (request_id.empty()) return ""; if (!valid_request_id(request_id)) throw GoogleError::invalid("request id must be a non-zero UUID"); return std::string(1, sep) + "requestId=" + request_id; } Template ModelArmor::create_template(const std::string& id, const Template& tmpl, const std::string& request_id) { template_path(id); // Validates the id and the location. std::string rid = request_id_query(request_id, '&'); tmpl.validate(); return Template::from_json(call( "/" + context_.parent() + "/templates?templateId=" + query_escape(id) + rid, tmpl.to_json())); } Template ModelArmor::get_template(const std::string& id) { return Template::from_json(fetch(template_path(id))); } TemplatePage ModelArmor::list_templates(int page_size, const std::string& filter, const std::string& order_by) { require_regional(); if (page_size < 0) throw GoogleError::invalid("page_size must not be negative"); if (page_size > ArmorCaps::kMaxTemplatePageSize) page_size = ArmorCaps::kMaxTemplatePageSize; if (!valid_filter_text(filter) || !valid_filter_text(order_by)) throw GoogleError::invalid("bad filter or order_by"); std::string base = "/" + context_.parent() + "/templates"; std::string query; auto add = [&](const std::string& k, const std::string& v) { query += (query.empty() ? "?" : "&") + k + "=" + query_escape(v); }; if (page_size > 0) add("pageSize", std::to_string(page_size)); if (!filter.empty()) add("filter", filter); if (!order_by.empty()) add("orderBy", order_by); TemplatePage page; std::string token; for (std::size_t pages = 0; pages < ArmorCaps::kMaxTemplatePages; ++pages) { std::string path = base + query; if (!token.empty()) path += (query.empty() ? "?" : "&") + std::string("pageToken=") + query_escape(token); Json doc = fetch(path); if (const Json* t = doc.find("templates"); t && t->is_array()) for (const Json& item : t->as_array()) page.templates.push_back(Template::from_json(item)); if (const Json* u = doc.find("unreachable"); u && u->is_array()) for (const Json& x : u->as_array()) if (x.is_string() && page.unreachable.size() < ArmorCaps::kMaxLocations) page.unreachable.push_back(x.as_string()); const Json* next = doc.find("nextPageToken"); if (!next || !next->is_string() || next->as_string().empty()) break; token = next->as_string(); } return page; } Template ModelArmor::patch_template(const std::string& id, const Template& tmpl, const std::vector& update_mask, const std::string& request_id) { std::string path = template_path(id); std::string mask = join_update_mask(update_mask); std::string rid = request_id_query(request_id, '&'); tmpl.validate(); return Template::from_json( patch(path + "?updateMask=" + query_escape(mask) + rid, tmpl.to_json())); } void ModelArmor::delete_template(const std::string& id, const std::string& request_id) { std::string path = template_path(id); remove(path + request_id_query(request_id, '?')); } FloorSetting ModelArmor::get_floor_setting(const std::string& scope) { return FloorSetting::from_json(client_->get_json(floor_endpoint(), floor_path(scope))); } FloorSetting ModelArmor::update_floor_setting( const FloorSetting& setting, const std::vector& update_mask, const std::string& scope) { std::string path = floor_path(scope); std::string mask = join_update_mask(update_mask); setting.validate(); return FloorSetting::from_json(client_->patch_json( floor_endpoint(), path + "?updateMask=" + query_escape(mask), setting.to_json())); } std::vector ModelArmor::list_locations() { context_.parent(); Json items = list_all(*client_, floor_endpoint(), "/projects/" + context_.project + "/locations", "locations", 2); std::vector out; for (const Json& l : items.as_array()) { if (out.size() >= ArmorCaps::kMaxLocations) break; out.push_back({string_at(l, "name"), string_at(l, "locationId"), string_at(l, "displayName")}); } return out; } ArmorLocation ModelArmor::get_location() { Json l = client_->get_json(floor_endpoint(), "/" + context_.parent()); return {string_at(l, "name"), string_at(l, "locationId"), string_at(l, "displayName")}; } Json ModelArmor::user_prompt_body(const DataItem& item, const LanguageHint& hint) { Json body = Json::object(); body.set("userPromptData", item.to_json()); if (hint.present()) { Json m = Json::object(); m.set("enableMultiLanguageDetection", hint.enable_detection); if (!hint.source_language.empty()) m.set("sourceLanguage", hint.source_language); body.set("multiLanguageDetectionMetadata", m); } return body; } Json ModelArmor::model_response_body(const DataItem& item, const std::string& user_prompt, const LanguageHint& hint) { Json body = Json::object(); body.set("modelResponseData", item.to_json()); if (!user_prompt.empty()) body.set("userPrompt", user_prompt); if (hint.present()) { Json m = Json::object(); m.set("enableMultiLanguageDetection", hint.enable_detection); if (!hint.source_language.empty()) m.set("sourceLanguage", hint.source_language); body.set("multiLanguageDetectionMetadata", m); } return body; } SanitizationResult ModelArmor::sanitize_user_prompt(const std::string& template_id, const std::string& text) { return sanitize_user_prompt(template_id, DataItem::of_text(text)); } SanitizationResult ModelArmor::sanitize_user_prompt(const std::string& template_id, const DataItem& item, const LanguageHint& hint) { std::string path = template_path(template_id); item.validate("prompt"); validate_hint(hint); return SanitizationResult::from_json( call(path + ":sanitizeUserPrompt", user_prompt_body(item, hint))); } SanitizationResult ModelArmor::sanitize_model_response(const std::string& template_id, const std::string& text, const std::string& user_prompt) { return sanitize_model_response(template_id, DataItem::of_text(text), user_prompt); } SanitizationResult ModelArmor::sanitize_model_response(const std::string& template_id, const DataItem& item, const std::string& user_prompt, const LanguageHint& hint) { std::string path = template_path(template_id); item.validate("response"); validate_hint(hint); if (user_prompt.size() > ArmorCaps::kMaxTextChars) throw GoogleError::invalid("user prompt exceeds 100000 characters"); return SanitizationResult::from_json( call(path + ":sanitizeModelResponse", model_response_body(item, user_prompt, hint))); } } // namespace molo::google::armor