98 lines
3.6 KiB
C++
98 lines
3.6 KiB
C++
#include "google/adk/direct_model.h"
|
|||
|
|
|
||
|
|
#include "google/adk/caps.h"
|
||
|
|
#include "google/resource.h"
|
||
|
|
|
||
|
|
namespace molo::google::adk {
|
||
|
|
|
||
|
|
using clients::body::Json;
|
||
|
|
|
||
|
|
DirectModel::DirectModel(std::shared_ptr<GoogleClient> client,
|
||
|
|
std::string default_model, ServiceEndpoint endpoint)
|
||
|
|
: client_(std::move(client)),
|
||
|
|
default_model_(std::move(default_model)),
|
||
|
|
endpoint_(std::move(endpoint)) {
|
||
|
|
if (!client_) throw GoogleError::invalid("DirectModel needs a client");
|
||
|
|
if (!valid_segment(default_model_))
|
||
|
|
throw GoogleError::invalid("invalid default model id");
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string DirectModel::model_path(const GenerateRequest& request) const {
|
||
|
|
const std::string& m = request.model.empty() ? default_model_ : request.model;
|
||
|
|
if (!valid_segment(m)) throw GoogleError::invalid("invalid model id");
|
||
|
|
return "/models/" + m;
|
||
|
|
}
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
std::string call_key(const Json& call) {
|
||
|
|
const Json* n = call.find("name");
|
||
|
|
const Json* a = call.find("args");
|
||
|
|
return (n && n->is_string() ? n->as_string() : "") + "\n" +
|
||
|
|
(a ? a->dump() : "{}");
|
||
|
|
}
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
void DirectModel::remember_signatures(const Json& response) {
|
||
|
|
const Json* cands = response.find("candidates");
|
||
|
|
if (!cands || !cands->is_array()) return;
|
||
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
||
|
|
for (const Json& c : cands->as_array()) {
|
||
|
|
const Json* content = c.find("content");
|
||
|
|
const Json* parts = content ? content->find("parts") : nullptr;
|
||
|
|
if (!parts || !parts->is_array()) continue;
|
||
|
|
for (const Json& p : parts->as_array()) {
|
||
|
|
const Json* call = p.find("functionCall");
|
||
|
|
const Json* sig = p.find("thoughtSignature");
|
||
|
|
if (!call || !sig || !sig->is_string()) continue;
|
||
|
|
std::string key = call_key(*call);
|
||
|
|
if (!signatures_.count(key)) signature_order_.push_back(key);
|
||
|
|
signatures_[key] = sig->as_string();
|
||
|
|
while (signature_order_.size() > AdkCaps::kMaxThoughtSignatures) {
|
||
|
|
signatures_.erase(signature_order_.front());
|
||
|
|
signature_order_.pop_front();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Json DirectModel::restore_signatures(Json body) {
|
||
|
|
Json* contents = body.is_object() && body.has("contents")
|
||
|
|
? &body.as_object().find("contents")->second
|
||
|
|
: nullptr;
|
||
|
|
if (!contents || !contents->is_array()) return body;
|
||
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
||
|
|
for (Json& content : contents->as_array()) {
|
||
|
|
if (!content.is_object() || !content.as_object().count("parts")) continue;
|
||
|
|
Json& parts = content.as_object().find("parts")->second;
|
||
|
|
if (!parts.is_array()) continue;
|
||
|
|
for (Json& p : parts.as_array()) {
|
||
|
|
if (!p.is_object() || p.has("thoughtSignature")) continue;
|
||
|
|
auto call = p.as_object().find("functionCall");
|
||
|
|
if (call == p.as_object().end()) continue;
|
||
|
|
auto sig = signatures_.find(call_key(call->second));
|
||
|
|
if (sig != signatures_.end()) p.set("thoughtSignature", Json(sig->second));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return body;
|
||
|
|
}
|
||
|
|
|
||
|
|
GenerateResponse DirectModel::generate(const GenerateRequest& request) {
|
||
|
|
std::string path = model_path(request) + ":generateContent";
|
||
|
|
Json raw = client_->post_json(endpoint_, path, restore_signatures(request.to_json()));
|
||
|
|
remember_signatures(raw);
|
||
|
|
return GenerateResponse::from_json(raw);
|
||
|
|
}
|
||
|
|
|
||
|
|
void DirectModel::stream(
|
||
|
|
const GenerateRequest& request,
|
||
|
|
const std::function<bool(const GenerateResponse&)>& on_chunk) {
|
||
|
|
client_->stream_json(
|
||
|
|
endpoint_, model_path(request) + ":streamGenerateContent?alt=sse",
|
||
|
|
restore_signatures(request.to_json()), [&](const Json& event) {
|
||
|
|
remember_signatures(event);
|
||
|
|
return on_chunk(GenerateResponse::from_json(event));
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::google::adk
|