#include "vendors/perplexity/decisions.h" #include "vendors/perplexity/calls.h" #include "vendors/perplexity/caps.h" #include "vendors/perplexity/rules.h" namespace molo::perplexity { Json DecisionQuestions::noul(const std::string& instructions, const std::string& yes, const std::string& no) { Rules::require(!instructions.empty() || !yes.empty() || !no.empty(), "a noul question needs instructions or criteria"); Json q = Json::object(); q.set("type", Json("noul")); if (!instructions.empty()) q.set("instructions", Json(instructions)); if (!yes.empty() || !no.empty()) { Json c = Json::object(); if (!yes.empty()) c.set("true", Json(yes)); if (!no.empty()) c.set("false", Json(no)); q.set("criteria", c); } return q; } Json DecisionQuestions::choice(const std::map& options, const std::string& instructions) { Rules::require(!options.empty() && options.size() <= PerplexityCaps::kChoiceOptions, "a choice question needs 1..255 options"); Json c = Json::object(); for (const auto& [name, desc] : options) { Rules::require(!name.empty(), "option names must not be empty"); c.set(name, desc.empty() ? Json(nullptr) : Json(desc)); } Json q = Json::object(); q.set("type", Json("choice")); q.set("criteria", c); if (!instructions.empty()) q.set("instructions", Json(instructions)); return q; } Json DecisionQuestions::score(const std::vector& levels, const std::string& instructions) { Rules::require(!levels.empty() && levels.size() <= PerplexityCaps::kScoreLevels, "a score question needs 1..10 levels"); Json a = Json::array(); for (const std::string& l : levels) a.push(Json(l)); Json q = Json::object(); q.set("type", Json("score")); q.set("criteria", a); if (!instructions.empty()) q.set("instructions", Json(instructions)); return q; } void DecisionsParams::validate() const { Rules::model(model); Rules::require(state.is_string() || state.is_object() || state.is_array(), "state must be a string, object or array"); if (state.is_string()) Rules::require(!state.as_string().empty(), "state must not be empty"); Rules::require(!questions.empty() && questions.size() <= PerplexityCaps::kDecisionQuestions, "1..128 questions are required"); for (const auto& [name, q] : questions) { Rules::require(!name.empty() && name.size() <= 256, "question names must be 1..256 characters"); Rules::require(q.is_object(), "a question must be an object"); Rules::one_of(J::str(q, "type"), {"noul", "choice", "score"}, "question type"); } } Json DecisionsParams::to_json() const { validate(); Json b = Json::object(); b.set("model", Json(model)); b.set("state", state); Json qs = Json::object(); for (const auto& [name, q] : questions) qs.set(name, q); b.set("questions", qs); return b; } DecisionsResult DecisionsResult::parse(const Json& doc) { DecisionsResult r; r.raw = doc; r.model = J::str(doc, "model"); if (const Json* a = J::obj(doc, "answers")) for (const auto& [name, ans] : a->as_object()) { DecisionAnswer d; d.type = J::str(ans, "type"); d.noul = J::dbl(ans, "noul"); d.choice = J::str(ans, "choice"); d.score = J::dbl(ans, "score"); d.confidence = J::dbl(ans, "confidence"); if (const Json* p = J::obj(ans, "probabilities")) for (const auto& [k, v] : p->as_object()) if (v.is_number()) d.probabilities[k] = v.as_number(); if (const Json* l = J::obj(ans, "legend")) for (const auto& [k, v] : l->as_object()) if (v.is_string()) d.legend[k] = v.as_string(); r.answers[name] = std::move(d); } if (const Json* u = J::obj(doc, "usage")) { r.input_tokens = J::num(*u, "input_tokens"); r.output_tokens = J::num(*u, "output_tokens"); } return r; } DecisionsResult Decisions::answer(const DecisionsParams& params) { return DecisionsResult::parse(client_->post(Calls::render(Call::kDecisions), params.to_json())); } } // namespace molo::perplexity