Package molo::perplexity covering all 39 operations of the five published OpenAPI documents, PerplexityChatModel, demo routes under /ai/perplexity, check_spec.py, SPEC.md, 46 unit cases and 111 route checks. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
402 lines
16 KiB
C++
402 lines
16 KiB
C++
#include "vendors/perplexity/agent_params.h"
|
|
|
|
#include <cctype>
|
|
|
|
#include "vendors/perplexity/caps.h"
|
|
#include "vendors/perplexity/rules.h"
|
|
|
|
namespace molo::perplexity {
|
|
|
|
namespace {
|
|
Json str_array(const std::vector<std::string>& v) {
|
|
Json a = Json::array();
|
|
for (const std::string& s : v) a.push(Json(s));
|
|
return a;
|
|
}
|
|
} // namespace
|
|
|
|
Json AgentInput::message(const std::string& role, const std::string& text) {
|
|
Rules::one_of(role, {"user", "assistant", "system", "developer"}, "role");
|
|
Json m = Json::object();
|
|
m.set("type", Json("message"));
|
|
m.set("role", Json(role));
|
|
m.set("content", Json(text));
|
|
return m;
|
|
}
|
|
|
|
Json AgentInput::message_parts(const std::string& role, const std::vector<Json>& parts) {
|
|
Rules::one_of(role, {"user", "assistant", "system", "developer"}, "role");
|
|
Rules::require(!parts.empty(), "a message needs at least one content part");
|
|
Json arr = Json::array();
|
|
for (const Json& p : parts) arr.push(p);
|
|
Json m = Json::object();
|
|
m.set("type", Json("message"));
|
|
m.set("role", Json(role));
|
|
m.set("content", arr);
|
|
return m;
|
|
}
|
|
|
|
Json AgentInput::text_part(const std::string& text) {
|
|
Json p = Json::object();
|
|
p.set("type", Json("input_text"));
|
|
p.set("text", Json(text));
|
|
return p;
|
|
}
|
|
|
|
Json AgentInput::image_part(const std::string& url) {
|
|
bool data = url.rfind("data:image/", 0) == 0 && url.find(";base64,") != std::string::npos;
|
|
Rules::require(data || (url.rfind("https://", 0) == 0 && Rules::is_http_url(url)), "image url must be https or a data:image/...;base64, URL");
|
|
Json p = Json::object();
|
|
p.set("type", Json("input_image"));
|
|
p.set("image_url", Json(url));
|
|
return p;
|
|
}
|
|
|
|
Json AgentInput::file_part(const std::string& file_data, const std::string& file_url, const std::string& filename) {
|
|
Rules::require(file_data.empty() != file_url.empty(), "exactly one of file_data and file_url is required");
|
|
if (!file_url.empty()) Rules::https_url(file_url, "file_url");
|
|
Json p = Json::object();
|
|
p.set("type", Json("input_file"));
|
|
if (!file_data.empty()) p.set("file_data", Json(file_data));
|
|
if (!file_url.empty()) p.set("file_url", Json(file_url));
|
|
if (!filename.empty()) {
|
|
Rules::require(filename.size() <= PerplexityCaps::kMaxFilenameBytes, "filename over 255 bytes");
|
|
p.set("filename", Json(filename));
|
|
}
|
|
return p;
|
|
}
|
|
|
|
Json AgentInput::function_call_output(const std::string& call_id, const std::string& output) {
|
|
Rules::require(!call_id.empty(), "call_id is required");
|
|
Json o = Json::object();
|
|
o.set("type", Json("function_call_output"));
|
|
o.set("call_id", Json(call_id));
|
|
o.set("output", Json(output));
|
|
return o;
|
|
}
|
|
|
|
Json AgentInput::function_call(const std::string& call_id, const std::string& name, const std::string& arguments) {
|
|
Rules::require(!call_id.empty() && !name.empty(), "call_id and name are required");
|
|
Json o = Json::object();
|
|
o.set("type", Json("function_call"));
|
|
o.set("call_id", Json(call_id));
|
|
o.set("name", Json(name));
|
|
o.set("arguments", Json(arguments));
|
|
return o;
|
|
}
|
|
|
|
Json AgentInput::mcp_approval_response(const std::string& approval_request_id, bool approve,
|
|
const std::string& reason) {
|
|
Rules::require(!approval_request_id.empty(), "approval_request_id is required");
|
|
Rules::require(!(approve && !reason.empty()), "a reason is rejected when approve is true");
|
|
Json o = Json::object();
|
|
o.set("type", Json("mcp_approval_response"));
|
|
o.set("approval_request_id", Json(approval_request_id));
|
|
o.set("approve", Json(approve));
|
|
if (!reason.empty()) o.set("reason", Json(reason));
|
|
return o;
|
|
}
|
|
|
|
Json AgentTools::web_search(const WebSearchToolOptions& o) {
|
|
Json t = Json::object();
|
|
t.set("type", Json("web_search"));
|
|
if (!o.search_type.empty()) {
|
|
Rules::one_of(o.search_type, {"web", "fast"}, "search_type");
|
|
t.set("search_type", Json(o.search_type));
|
|
}
|
|
if (o.max_results) {
|
|
Rules::require(*o.max_results >= 1 && *o.max_results <= PerplexityCaps::kWebToolMaxResults, "max_results must be 1..50");
|
|
t.set("max_results", Json(*o.max_results));
|
|
}
|
|
if (o.max_tokens) {
|
|
Rules::require(*o.max_tokens >= 1, "max_tokens must be at least 1");
|
|
t.set("max_tokens", Json(*o.max_tokens));
|
|
}
|
|
if (o.max_tokens_per_page) {
|
|
Rules::require(*o.max_tokens_per_page >= 1, "max_tokens_per_page must be at least 1");
|
|
t.set("max_tokens_per_page", Json(*o.max_tokens_per_page));
|
|
}
|
|
Json filters = Json::object();
|
|
if (!o.search_domain_filter.empty()) {
|
|
Rules::domains(o.search_domain_filter, PerplexityCaps::kSearchDomainFilter, "search_domain_filter");
|
|
filters.set("search_domain_filter", str_array(o.search_domain_filter));
|
|
}
|
|
if (!o.search_recency_filter.empty()) {
|
|
Rules::one_of(o.search_recency_filter, {"hour", "day", "week", "month", "year"}, "search_recency_filter");
|
|
filters.set("search_recency_filter", Json(o.search_recency_filter));
|
|
}
|
|
if (filters.as_object().size()) t.set("filters", filters);
|
|
if (!o.country.empty()) {
|
|
Rules::country(o.country);
|
|
Json loc = Json::object();
|
|
loc.set("country", Json(o.country));
|
|
t.set("user_location", loc);
|
|
}
|
|
return t;
|
|
}
|
|
|
|
Json AgentTools::image_search(int max_results, const std::vector<std::string>& domains,
|
|
const std::vector<std::string>& formats, std::optional<bool> safe_search) {
|
|
Json t = Json::object();
|
|
t.set("type", Json("image_search"));
|
|
if (max_results) {
|
|
Rules::require(max_results >= 1 && max_results <= PerplexityCaps::kImageToolMaxResults, "max_results must be 1..30");
|
|
t.set("max_results", Json(max_results));
|
|
}
|
|
Json f = Json::object();
|
|
if (!domains.empty()) {
|
|
Rules::require(domains.size() <= PerplexityCaps::kImageFilterEntries, "at most 10 domain filters");
|
|
Rules::domains(domains, PerplexityCaps::kImageFilterEntries, "domain_filter");
|
|
f.set("domain_filter", str_array(domains));
|
|
}
|
|
if (!formats.empty()) {
|
|
Rules::require(formats.size() <= PerplexityCaps::kImageFilterEntries, "at most 10 format filters");
|
|
for (const std::string& x : formats) Rules::one_of(x, {"bmp", "gif", "jpeg", "png", "webp", "svg"}, "format_filter entry");
|
|
f.set("format_filter", str_array(formats));
|
|
}
|
|
if (safe_search) f.set("safe_search", Json(*safe_search));
|
|
if (f.as_object().size()) t.set("filters", f);
|
|
return t;
|
|
}
|
|
|
|
Json AgentTools::finance_search() {
|
|
Json t = Json::object();
|
|
t.set("type", Json("finance_search"));
|
|
return t;
|
|
}
|
|
Json AgentTools::people_search() {
|
|
Json t = Json::object();
|
|
t.set("type", Json("people_search"));
|
|
return t;
|
|
}
|
|
Json AgentTools::sandbox() {
|
|
Json t = Json::object();
|
|
t.set("type", Json("sandbox"));
|
|
return t;
|
|
}
|
|
Json AgentTools::fetch_url(int max_urls) {
|
|
Json t = Json::object();
|
|
t.set("type", Json("fetch_url"));
|
|
if (max_urls) {
|
|
Rules::require(max_urls >= 1 && max_urls <= PerplexityCaps::kFetchToolMaxUrls, "max_urls must be 1..10");
|
|
t.set("max_urls", Json(max_urls));
|
|
}
|
|
return t;
|
|
}
|
|
|
|
Json AgentTools::function(const std::string& name, const std::string& description, const Json& parameters,
|
|
std::optional<bool> strict) {
|
|
Rules::name_token(name, 64, "function name");
|
|
Rules::require(parameters.is_object(), "function parameters must be a JSON Schema object");
|
|
Json t = Json::object();
|
|
t.set("type", Json("function"));
|
|
t.set("name", Json(name));
|
|
if (!description.empty()) t.set("description", Json(description));
|
|
t.set("parameters", parameters);
|
|
if (strict) t.set("strict", Json(*strict));
|
|
return t;
|
|
}
|
|
|
|
namespace {
|
|
void approval(Json& t, const std::string& require_approval) {
|
|
if (require_approval.empty()) return;
|
|
Rules::one_of(require_approval, {"always", "never"}, "require_approval");
|
|
t.set("require_approval", Json(require_approval));
|
|
}
|
|
} // namespace
|
|
|
|
Json AgentTools::mcp(const std::string& server_label, const std::string& server_url, const std::string& authorization,
|
|
const std::vector<std::string>& allowed_tools, const std::string& require_approval) {
|
|
Rules::name_token(server_label, 64, "server_label");
|
|
Rules::https_url(server_url, "server_url");
|
|
Json t = Json::object();
|
|
t.set("type", Json("mcp"));
|
|
t.set("server_label", Json(server_label));
|
|
t.set("server_url", Json(server_url));
|
|
if (!authorization.empty()) t.set("authorization", Json(authorization));
|
|
if (!allowed_tools.empty()) t.set("allowed_tools", str_array(allowed_tools));
|
|
approval(t, require_approval);
|
|
return t;
|
|
}
|
|
|
|
Json AgentTools::connector(const std::string& id, const std::string& server_label,
|
|
const std::vector<std::string>& allowed_tools, const std::string& require_approval) {
|
|
Rules::require(!id.empty(), "connector id is required");
|
|
Rules::name_token(server_label, 64, "server_label");
|
|
Json t = Json::object();
|
|
t.set("type", Json("connector"));
|
|
t.set("id", Json(id));
|
|
t.set("server_label", Json(server_label));
|
|
if (!allowed_tools.empty()) t.set("allowed_tools", str_array(allowed_tools));
|
|
approval(t, require_approval);
|
|
return t;
|
|
}
|
|
|
|
Json AgentTools::choice(const std::string& mode) {
|
|
Rules::one_of(mode, {"none", "auto", "required"}, "tool_choice");
|
|
return Json(mode);
|
|
}
|
|
|
|
Json AgentTools::choice_tool(const std::string& type) {
|
|
Rules::require(!type.empty(), "tool type is required");
|
|
Json o = Json::object();
|
|
o.set("type", Json(type));
|
|
return o;
|
|
}
|
|
|
|
Json AgentSkills::builtin(const std::string& name) {
|
|
Rules::one_of(name, {"office", "office/docx", "office/pdf", "office/pptx", "office/xlsx"}, "builtin skill");
|
|
Json s = Json::object();
|
|
s.set("type", Json("builtin"));
|
|
s.set("name", Json(name));
|
|
return s;
|
|
}
|
|
|
|
Json AgentSkills::inline_skill(const std::string& name, const std::string& description,
|
|
const std::string& instructions) {
|
|
Rules::require(!name.empty() && name.size() <= 128, "inline skill name must be 1..128 characters");
|
|
for (char c : name)
|
|
Rules::require((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-', "inline skill name is lowercase ASCII separated by hyphens");
|
|
Rules::require(name.front() != '-' && name.back() != '-' && name.find("--") == std::string::npos,
|
|
"inline skill name uses single hyphens between words");
|
|
Rules::require(!description.empty() && description.size() <= PerplexityCaps::kInlineSkillDescription,
|
|
"description must be 1..1024 bytes");
|
|
Rules::require(!instructions.empty() && instructions.size() <= PerplexityCaps::kInlineSkillInstructions,
|
|
"instructions must be 1..65536 bytes");
|
|
Json s = Json::object();
|
|
s.set("type", Json("inline"));
|
|
s.set("name", Json(name));
|
|
s.set("description", Json(description));
|
|
s.set("instructions", Json(instructions));
|
|
return s;
|
|
}
|
|
|
|
Json AgentSkills::custom(const std::string& id, const std::string& version) {
|
|
Rules::require(id.rfind("skill_", 0) == 0 && id.size() > 6, "custom skill id has the form skill_<id>");
|
|
Json s = Json::object();
|
|
s.set("type", Json("custom"));
|
|
s.set("id", Json(id));
|
|
if (!version.empty()) s.set("version", Json(version));
|
|
return s;
|
|
}
|
|
|
|
AgentParams AgentParams::prompt(const std::string& text, const std::string& model) {
|
|
AgentParams p;
|
|
p.input = Json(text);
|
|
p.model = model;
|
|
return p;
|
|
}
|
|
|
|
AgentParams AgentParams::with_effort(const std::string& text, const std::string& effort) {
|
|
AgentParams p;
|
|
p.input = Json(text);
|
|
p.effort = effort;
|
|
return p;
|
|
}
|
|
|
|
Json AgentParams::json_schema(const std::string& name, const Json& schema, bool strict, const std::string& description) {
|
|
Rules::name_token(name, 64, "schema name");
|
|
Rules::require(schema.is_object(), "json schema must be an object");
|
|
Json js = Json::object();
|
|
js.set("name", Json(name));
|
|
js.set("schema", schema);
|
|
js.set("strict", Json(strict));
|
|
if (!description.empty()) js.set("description", Json(description));
|
|
Json o = Json::object();
|
|
o.set("type", Json("json_schema"));
|
|
o.set("json_schema", js);
|
|
return o;
|
|
}
|
|
|
|
void AgentParams::validate() const {
|
|
Rules::require(input.is_string() || input.is_array(), "input must be a string or an array of input items");
|
|
if (input.is_string()) Rules::require(!input.as_string().empty(), "input must not be empty");
|
|
else {
|
|
Rules::require(!input.as_array().empty(), "input items must not be empty");
|
|
Rules::require(input.as_array().size() <= PerplexityCaps::kMaxInputItems, "too many input items");
|
|
for (const Json& i : input.as_array()) Rules::require(i.is_object() && i.has("type"), "every input item needs a type");
|
|
}
|
|
if (!model.empty()) Rules::model(model);
|
|
Rules::require(models.size() <= PerplexityCaps::kAgentModels, "at most 5 fallback models");
|
|
for (const std::string& m : models) Rules::model(m);
|
|
if (!effort.empty()) {
|
|
Rules::require(effort.size() <= 128, "effort over 128 characters");
|
|
for (char c : effort)
|
|
Rules::require(std::isalnum(static_cast<unsigned char>(c)) || c == '-' || c == '_' || c == '.' || c == '@',
|
|
"effort has invalid characters");
|
|
}
|
|
Rules::require(profile_id.empty() || effort.empty(), "profile cannot be combined with effort");
|
|
Rules::require(profile_version.empty() || !profile_id.empty(), "profile_version needs profile_id");
|
|
if (max_output_tokens) Rules::require(*max_output_tokens >= 1, "max_output_tokens must be at least 1");
|
|
bool anthropic = model.rfind("anthropic/", 0) == 0;
|
|
for (const std::string& m : models) anthropic = anthropic || m.rfind("anthropic/", 0) == 0;
|
|
Rules::require(!anthropic || max_output_tokens.has_value(), "max_output_tokens is required for anthropic/* models");
|
|
if (max_steps) Rules::require(*max_steps >= 1 && *max_steps <= PerplexityCaps::kAgentMaxSteps, "max_steps must be 1..100");
|
|
if (!reasoning_effort.empty())
|
|
Rules::one_of(reasoning_effort, {"minimal", "low", "medium", "high", "xhigh", "max"}, "reasoning effort");
|
|
if (!language_preference.empty()) Rules::language(language_preference, "language_preference");
|
|
if (!response_format.is_null()) {
|
|
Rules::require(J::str(response_format, "type") == "json_schema" && J::obj(response_format, "json_schema"),
|
|
"response_format must be {type:json_schema, json_schema:{name, schema}}");
|
|
const Json* js = J::obj(response_format, "json_schema");
|
|
Rules::require(!J::str(*js, "name").empty() && J::obj(*js, "schema"), "json_schema needs name and schema");
|
|
}
|
|
Rules::require(tools.size() <= PerplexityCaps::kMaxTools, "too many tools");
|
|
for (const Json& t : tools) Rules::require(t.is_object() && t.has("type"), "every tool needs a type");
|
|
Rules::require(skills.size() <= PerplexityCaps::kAgentSkills, "at most 16 skills");
|
|
std::size_t inline_bytes = 0;
|
|
for (const Json& s : skills) {
|
|
Rules::require(s.is_object() && s.has("type"), "every skill needs a type");
|
|
if (J::str(s, "type") == "inline") inline_bytes += J::str(s, "instructions").size();
|
|
}
|
|
Rules::require(inline_bytes <= PerplexityCaps::kInlineSkillsTotal, "inline skill instructions over 262144 bytes in total");
|
|
if (temperature) Rules::temperature(*temperature);
|
|
if (top_p) Rules::top_p(*top_p);
|
|
}
|
|
|
|
Json AgentParams::to_json(bool stream) const {
|
|
validate();
|
|
Json b = Json::object();
|
|
b.set("input", input);
|
|
if (!model.empty()) b.set("model", Json(model));
|
|
if (!models.empty()) b.set("models", str_array(models));
|
|
if (!effort.empty()) b.set("effort", Json(effort));
|
|
if (!profile_id.empty()) {
|
|
Json p = Json::object();
|
|
p.set("type", Json("custom"));
|
|
p.set("id", Json(profile_id));
|
|
if (!profile_version.empty()) p.set("version", Json(profile_version));
|
|
b.set("profile", p);
|
|
}
|
|
if (!instructions.empty()) b.set("instructions", Json(instructions));
|
|
if (!language_preference.empty()) b.set("language_preference", Json(language_preference));
|
|
if (max_output_tokens) b.set("max_output_tokens", Json(*max_output_tokens));
|
|
if (max_steps) b.set("max_steps", Json(*max_steps));
|
|
if (!previous_response_id.empty()) b.set("previous_response_id", Json(previous_response_id));
|
|
if (!reasoning_effort.empty()) {
|
|
Json r = Json::object();
|
|
r.set("effort", Json(reasoning_effort));
|
|
b.set("reasoning", r);
|
|
}
|
|
if (!response_format.is_null()) b.set("response_format", response_format);
|
|
if (store) b.set("store", Json(*store));
|
|
if (background) b.set("background", Json(*background));
|
|
if (stream) b.set("stream", Json(true));
|
|
if (!tools.empty()) {
|
|
Json a = Json::array();
|
|
for (const Json& t : tools) a.push(t);
|
|
b.set("tools", a);
|
|
}
|
|
if (!tool_choice.is_null()) b.set("tool_choice", tool_choice);
|
|
if (!skills.empty()) {
|
|
Json a = Json::array();
|
|
for (const Json& s : skills) a.push(s);
|
|
b.set("skills", a);
|
|
}
|
|
if (temperature) b.set("temperature", Json(*temperature));
|
|
if (top_p) b.set("top_p", Json(*top_p));
|
|
return b;
|
|
}
|
|
|
|
} // namespace molo::perplexity
|