feat(clients): models: Gemini on Vertex, Model Garden, Claude, endpoint models, media, VertexModel
One call table verified against Discovery (29 calls), live-verified read-only with an express key and a service account token, /agent/models example routes. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,252 @@
|
||||
#include "google/models/gemini.h"
|
||||
|
||||
#include "google/errors.h"
|
||||
#include "google/models/caps.h"
|
||||
#include "security/crypto/Base64.h"
|
||||
|
||||
namespace molo::google::models {
|
||||
|
||||
using clients::body::Json;
|
||||
|
||||
namespace {
|
||||
|
||||
bool token_chars(const std::string& s) {
|
||||
if (s.size() > 64) return false;
|
||||
for (char c : s)
|
||||
if (!((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
Embedding parse_vector(const Json& holder, const Json* stats) {
|
||||
Embedding e;
|
||||
if (const Json* v = holder.find("values"); v && v->is_array())
|
||||
for (const Json& x : v->as_array())
|
||||
if (x.is_number()) e.values.push_back(x.as_number());
|
||||
if (stats && stats->is_object()) {
|
||||
e.token_count = static_cast<int>(json_num(*stats, "token_count"));
|
||||
if (const Json* t = stats->find("truncated"); t && t->is_bool())
|
||||
e.truncated = t->as_bool();
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
const char* kPublisher = "google";
|
||||
|
||||
} // namespace
|
||||
|
||||
Gemini::Gemini(std::shared_ptr<GoogleClient> client, VertexTarget target)
|
||||
: VertexService(std::move(client), std::move(target), "aiplatform") {}
|
||||
|
||||
void Gemini::validate(const GenerateRequest& request) {
|
||||
if (request.contents.empty()) throw GoogleError::invalid("no contents");
|
||||
if (request.contents.size() > ModelsCaps::kMaxContents)
|
||||
throw GoogleError::invalid("more than " +
|
||||
std::to_string(ModelsCaps::kMaxContents) +
|
||||
" contents");
|
||||
std::size_t chars = 0;
|
||||
for (const Content& c : request.contents) {
|
||||
if (c.parts.empty()) throw GoogleError::invalid("a content has no parts");
|
||||
for (const Part& p : c.parts) {
|
||||
chars += p.text.size();
|
||||
if (p.text.empty() && !p.inline_data && !p.file_data && !p.function_call &&
|
||||
!p.function_response)
|
||||
throw GoogleError::invalid("a part is empty");
|
||||
}
|
||||
}
|
||||
if (request.system)
|
||||
for (const Part& p : request.system->parts) chars += p.text.size();
|
||||
if (chars > ModelsCaps::kMaxTextChars)
|
||||
throw GoogleError::invalid("request text over " +
|
||||
std::to_string(ModelsCaps::kMaxTextChars) +
|
||||
" characters");
|
||||
const auto& cfg = request.config;
|
||||
if (cfg.max_output_tokens &&
|
||||
(*cfg.max_output_tokens < 1 || *cfg.max_output_tokens > ModelsCaps::kMaxOutputTokens))
|
||||
throw GoogleError::invalid("max_output_tokens outside 1.." +
|
||||
std::to_string(ModelsCaps::kMaxOutputTokens));
|
||||
if (cfg.candidate_count &&
|
||||
(*cfg.candidate_count < 1 || *cfg.candidate_count > ModelsCaps::kMaxCandidates))
|
||||
throw GoogleError::invalid("candidate_count outside 1.." +
|
||||
std::to_string(ModelsCaps::kMaxCandidates));
|
||||
}
|
||||
|
||||
PlannedCall Gemini::plan_generate(const std::string& model,
|
||||
const GenerateRequest& r) const {
|
||||
validate(r);
|
||||
Json body = r.to_json();
|
||||
return planner_.plan("gemini.generate", model_args(kPublisher, model), &body);
|
||||
}
|
||||
|
||||
PlannedCall Gemini::plan_stream(const std::string& model,
|
||||
const GenerateRequest& r) const {
|
||||
validate(r);
|
||||
Json body = r.to_json();
|
||||
return planner_.plan("gemini.stream", model_args(kPublisher, model), &body,
|
||||
"alt=sse");
|
||||
}
|
||||
|
||||
PlannedCall Gemini::plan_count_tokens(const std::string& model,
|
||||
const GenerateRequest& r) const {
|
||||
validate(r);
|
||||
Json full = r.to_json();
|
||||
Json body = Json::object();
|
||||
for (const char* key : {"contents", "systemInstruction", "tools", "generationConfig"})
|
||||
if (const Json* v = full.find(key)) body.set(key, *v);
|
||||
return planner_.plan("gemini.count_tokens", model_args(kPublisher, model), &body);
|
||||
}
|
||||
|
||||
PlannedCall Gemini::plan_compute_tokens(const std::string& model,
|
||||
const std::vector<Content>& contents) const {
|
||||
if (contents.empty()) throw GoogleError::invalid("no contents");
|
||||
if (contents.size() > ModelsCaps::kMaxContents)
|
||||
throw GoogleError::invalid("too many contents");
|
||||
Json arr = Json::array();
|
||||
for (const Content& c : contents) arr.push(c.to_json());
|
||||
Json body = Json::object();
|
||||
body.set("contents", arr);
|
||||
return planner_.plan("gemini.compute_tokens", model_args(kPublisher, model), &body);
|
||||
}
|
||||
|
||||
GenerateResponse Gemini::generate(const std::string& model,
|
||||
const GenerateRequest& request) const {
|
||||
return GenerateResponse::from_json(run(plan_generate(model, request)));
|
||||
}
|
||||
|
||||
void Gemini::stream(
|
||||
const std::string& model, const GenerateRequest& request,
|
||||
const std::function<bool(const GenerateResponse&)>& on_chunk) const {
|
||||
run_stream(plan_stream(model, request), [&](const Json& event) {
|
||||
return on_chunk(GenerateResponse::from_json(event));
|
||||
});
|
||||
}
|
||||
|
||||
CountTokensResult Gemini::count_tokens(const std::string& model,
|
||||
const GenerateRequest& request) const {
|
||||
Json reply = run(plan_count_tokens(model, request));
|
||||
CountTokensResult out;
|
||||
out.total_tokens = static_cast<int>(json_num(reply, "totalTokens"));
|
||||
out.billable_characters = static_cast<int>(json_num(reply, "totalBillableCharacters"));
|
||||
out.raw = reply;
|
||||
return out;
|
||||
}
|
||||
|
||||
std::vector<TokenInfo> Gemini::compute_tokens(
|
||||
const std::string& model, const std::vector<Content>& contents) const {
|
||||
Json reply = run(plan_compute_tokens(model, contents));
|
||||
std::vector<TokenInfo> out;
|
||||
const Json* infos = reply.find("tokensInfo");
|
||||
if (!infos || !infos->is_array()) return out;
|
||||
for (const Json& i : infos->as_array()) {
|
||||
if (out.size() >= ModelsCaps::kMaxContents) break;
|
||||
TokenInfo t;
|
||||
t.role = json_str(i, "role");
|
||||
if (const Json* toks = i.find("tokens"); toks && toks->is_array())
|
||||
for (const Json& x : toks->as_array()) {
|
||||
std::string raw;
|
||||
if (x.is_string() && security::crypto::Base64::decode(x.as_string(), raw))
|
||||
t.tokens.push_back(raw);
|
||||
}
|
||||
if (const Json* ids = i.find("tokenIds"); ids && ids->is_array())
|
||||
for (const Json& x : ids->as_array())
|
||||
if (x.is_string()) t.token_ids.push_back(x.as_string());
|
||||
out.push_back(std::move(t));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void Gemini::validate_embed(const EmbedOptions& options) {
|
||||
if (!token_chars(options.task_type))
|
||||
throw GoogleError::invalid("task_type must be UPPER_SNAKE_CASE");
|
||||
if (options.output_dimensionality < 0 ||
|
||||
options.output_dimensionality > ModelsCaps::kMaxEmbedDimensions)
|
||||
throw GoogleError::invalid("output_dimensionality outside 0.." +
|
||||
std::to_string(ModelsCaps::kMaxEmbedDimensions));
|
||||
if (options.title.size() > 1000) throw GoogleError::invalid("title over 1000 characters");
|
||||
}
|
||||
|
||||
PlannedCall Gemini::plan_embed_content(const std::string& model,
|
||||
const std::string& text,
|
||||
const EmbedOptions& options) const {
|
||||
validate_embed(options);
|
||||
if (text.empty()) throw GoogleError::invalid("text is empty");
|
||||
if (text.size() > ModelsCaps::kMaxEmbedChars)
|
||||
throw GoogleError::invalid("text over " +
|
||||
std::to_string(ModelsCaps::kMaxEmbedChars) +
|
||||
" characters");
|
||||
Content content;
|
||||
content.parts.push_back(Part::of_text(text));
|
||||
Json body = Json::object();
|
||||
body.set("content", content.to_json());
|
||||
Json config = Json::object();
|
||||
if (!options.task_type.empty()) config.set("taskType", Json(options.task_type));
|
||||
if (!options.title.empty()) config.set("title", Json(options.title));
|
||||
if (options.output_dimensionality > 0)
|
||||
config.set("outputDimensionality", Json(options.output_dimensionality));
|
||||
if (options.auto_truncate) config.set("autoTruncate", Json(*options.auto_truncate));
|
||||
if (!config.as_object().empty()) body.set("embedContentConfig", config);
|
||||
return planner_.plan("gemini.embed_content", model_args(kPublisher, model), &body);
|
||||
}
|
||||
|
||||
Embedding Gemini::embed_content(const std::string& model, const std::string& text,
|
||||
const EmbedOptions& options) const {
|
||||
Json reply = run(plan_embed_content(model, text, options));
|
||||
Embedding e;
|
||||
if (const Json* emb = reply.find("embedding"); emb && emb->is_object())
|
||||
e = parse_vector(*emb, nullptr);
|
||||
if (const Json* u = reply.find("usageMetadata"); u && u->is_object())
|
||||
e.token_count = static_cast<int>(json_num(*u, "promptTokenCount"));
|
||||
if (const Json* t = reply.find("truncated"); t && t->is_bool()) e.truncated = t->as_bool();
|
||||
if (e.values.empty())
|
||||
throw GoogleError(502, "EMPTY_RESPONSE", "model returned no embedding",
|
||||
endpoint_.name);
|
||||
return e;
|
||||
}
|
||||
|
||||
PlannedCall Gemini::plan_embed(const std::string& model,
|
||||
const std::vector<std::string>& texts,
|
||||
const EmbedOptions& options) const {
|
||||
validate_embed(options);
|
||||
if (texts.empty()) throw GoogleError::invalid("no texts");
|
||||
if (texts.size() > ModelsCaps::kMaxEmbedBatch)
|
||||
throw GoogleError::invalid("more than " +
|
||||
std::to_string(ModelsCaps::kMaxEmbedBatch) + " texts");
|
||||
Json instances = Json::array();
|
||||
for (const std::string& t : texts) {
|
||||
if (t.empty()) throw GoogleError::invalid("text is empty");
|
||||
if (t.size() > ModelsCaps::kMaxEmbedChars)
|
||||
throw GoogleError::invalid("text over " +
|
||||
std::to_string(ModelsCaps::kMaxEmbedChars) +
|
||||
" characters");
|
||||
Json one = Json::object();
|
||||
one.set("content", Json(t));
|
||||
if (!options.task_type.empty()) one.set("task_type", Json(options.task_type));
|
||||
if (!options.title.empty()) one.set("title", Json(options.title));
|
||||
instances.push(one);
|
||||
}
|
||||
Json params = Json::object();
|
||||
if (options.output_dimensionality > 0)
|
||||
params.set("outputDimensionality", Json(options.output_dimensionality));
|
||||
if (options.auto_truncate) params.set("autoTruncate", Json(*options.auto_truncate));
|
||||
Json body = Json::object();
|
||||
body.set("instances", instances);
|
||||
if (!params.as_object().empty()) body.set("parameters", params);
|
||||
return planner_.plan("gemini.embed_predict", model_args(kPublisher, model), &body);
|
||||
}
|
||||
|
||||
std::vector<Embedding> Gemini::embed(const std::string& model,
|
||||
const std::vector<std::string>& texts,
|
||||
const EmbedOptions& options) const {
|
||||
Json reply = run(plan_embed(model, texts, options));
|
||||
std::vector<Embedding> out;
|
||||
if (const Json* preds = reply.find("predictions"); preds && preds->is_array())
|
||||
for (const Json& p : preds->as_array())
|
||||
if (const Json* emb = p.find("embeddings"); emb && emb->is_object())
|
||||
out.push_back(parse_vector(*emb, emb->find("statistics")));
|
||||
if (out.size() != texts.size())
|
||||
throw GoogleError(502, "BAD_RESPONSE",
|
||||
"embedding count does not match the request", endpoint_.name);
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace molo::google::models
|
||||
Reference in New Issue
Block a user