33 lines
1.3 KiB
C++
33 lines
1.3 KiB
C++
#include "google/optimize/simulated_user.h"
|
|||
|
|
|
||
|
|
#include "google/optimize/caps.h"
|
||
|
|
|
||
|
|
namespace molo::google::optimize {
|
||
|
|
|
||
|
|
using clients::body::Json;
|
||
|
|
|
||
|
|
SimulatedUser::SimulatedUser(std::shared_ptr<LanguageModel> model, std::string model_name)
|
||
|
|
: model_(std::move(model), std::move(model_name)) {}
|
||
|
|
|
||
|
|
std::optional<std::string> SimulatedUser::next(const Scenario& scenario,
|
||
|
|
const std::vector<Turn>& history) const {
|
||
|
|
Conversation c;
|
||
|
|
c.turns = history;
|
||
|
|
std::string system =
|
||
|
|
"You play a human user talking to an AI agent. Follow your hidden plan, "
|
||
|
|
"react naturally to what the agent said, and keep each message short. "
|
||
|
|
"When the plan is complete or cannot progress, finish. Reply with one "
|
||
|
|
"JSON object: {\"done\":true|false,\"message\":\"<next user message>\"}.\n"
|
||
|
|
"Hidden plan: " + scenario.plan;
|
||
|
|
Json reply = model_.ask(system, "Conversation so far:\n" + c.transcript());
|
||
|
|
const Json* done = reply.find("done");
|
||
|
|
if (done && done->is_bool() && done->as_bool()) return std::nullopt;
|
||
|
|
const Json* msg = reply.find("message");
|
||
|
|
if (!msg || !msg->is_string() || msg->as_string().empty()) return std::nullopt;
|
||
|
|
std::string text = msg->as_string();
|
||
|
|
if (text.size() > OptimizeCaps::kMaxTextChars) text.resize(OptimizeCaps::kMaxTextChars);
|
||
|
|
return text;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::google::optimize
|