48 lines
2.0 KiB
C++
48 lines
2.0 KiB
C++
#include "google/optimize/direct_model.h"
|
|||
|
|
|
||
|
|
#include "google/credentials.h"
|
||
|
|
#include "google/endpoints.h"
|
||
|
|
#include "google/errors.h"
|
||
|
|
#include "google/generative.h"
|
||
|
|
#include "google/resource.h"
|
||
|
|
#include "google/voice_backend.h"
|
||
|
|
|
||
|
|
namespace molo::google::optimize {
|
||
|
|
|
||
|
|
DirectModel::DirectModel(std::shared_ptr<GoogleClient> client, std::string model,
|
||
|
|
ServiceEndpoint endpoint)
|
||
|
|
: Service(std::move(client), std::move(endpoint)),
|
||
|
|
model_(std::move(model)) {}
|
||
|
|
|
||
|
|
std::shared_ptr<DirectModel> DirectModel::from_env(std::shared_ptr<GoogleClient> client,
|
||
|
|
std::string model) {
|
||
|
|
// OAuth tokens need the project-scoped Vertex path (express paths refuse them).
|
||
|
|
CloudContext ctx = CloudContext::from_env();
|
||
|
|
if (!Credentials::env("GOOGLE_ACCESS_TOKEN").empty() && ctx.valid()) {
|
||
|
|
auto m = std::make_shared<DirectModel>(std::move(client), std::move(model),
|
||
|
|
endpoints::aiplatform("global"));
|
||
|
|
m->path_prefix_ = "/projects/" + ctx.project + "/locations/global/publishers/google/models/";
|
||
|
|
return m;
|
||
|
|
}
|
||
|
|
ServiceEndpoint ep = voice_backend_from_env() == VoiceBackend::kVertex
|
||
|
|
? endpoints::vertex_generative()
|
||
|
|
: endpoints::generative();
|
||
|
|
return std::make_shared<DirectModel>(std::move(client), std::move(model), std::move(ep));
|
||
|
|
}
|
||
|
|
|
||
|
|
GenerateResponse DirectModel::generate(const GenerateRequest& request) {
|
||
|
|
std::string model = !request.model.empty() ? request.model
|
||
|
|
: !model_.empty() ? model_
|
||
|
|
: Generative::default_model();
|
||
|
|
if (!valid_segment(model)) throw GoogleError::invalid("bad model name");
|
||
|
|
return GenerateResponse::from_json(
|
||
|
|
call(path_prefix_ + model + ":generateContent", request.to_json()));
|
||
|
|
}
|
||
|
|
|
||
|
|
void DirectModel::stream(const GenerateRequest& request,
|
||
|
|
const std::function<bool(const GenerateResponse&)>& on_chunk) {
|
||
|
|
on_chunk(generate(request));
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::google::optimize
|