#include "vendors/perplexity/sonar_params.h" #include "llm/chat_types.h" #include "vendors/perplexity/calls.h" #include "vendors/perplexity/caps.h" #include "vendors/perplexity/rules.h" namespace molo::perplexity { Json Messages::make(const std::string& role, const std::string& text) { Json m = Json::object(); m.set("role", Json(role)); m.set("content", Json(text)); return m; } Json Messages::system(const std::string& t) { return make("system", t); } Json Messages::user(const std::string& t) { return make("user", t); } Json Messages::assistant(const std::string& t) { return make("assistant", t); } Json Messages::tool(const std::string& t) { return make("tool", t); } Json Messages::with_parts(const std::string& role, const std::vector& parts) { 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("role", Json(role)); m.set("content", arr); return m; } Json Messages::text_part(const std::string& text) { Json p = Json::object(); p.set("type", Json("text")); p.set("text", Json(text)); return p; } Json Messages::image_part(const std::string& url) { bool data = url.rfind("data:image/", 0) == 0 && url.find(";base64,") != std::string::npos; Rules::require(data || Rules::is_http_url(url, true), "image url must be http(s) or a data:image/...;base64, URL"); Json u = Json::object(); u.set("url", Json(url)); Json p = Json::object(); p.set("type", Json("image_url")); p.set("image_url", u); return p; } Json Messages::file_part(const std::string& url, const std::string& file_name) { Rules::require(!url.empty(), "file url must not be empty"); Json u = Json::object(); u.set("url", Json(url)); Json p = Json::object(); p.set("type", Json("file_url")); p.set("file_url", u); if (!file_name.empty()) { Rules::filename(file_name); p.set("file_name", Json(file_name)); } return p; } Json Messages::pdf_part(const std::string& url) { Rules::require(!url.empty(), "pdf url must not be empty"); Json u = Json::object(); u.set("url", Json(url)); Json p = Json::object(); p.set("type", Json("pdf_url")); p.set("pdf_url", u); return p; } Json Messages::video_part(const std::string& url, int frame_interval) { Rules::require(Rules::is_http_url(url, true), "video url must be http(s)"); Rules::require(frame_interval >= 0, "frame_interval must not be negative"); Json u = Json::object(); u.set("url", Json(url)); if (frame_interval > 0) u.set("frame_interval", Json(frame_interval)); Json p = Json::object(); p.set("type", Json("video_url")); p.set("video_url", u); return p; } bool UserLocation::empty() const { return !latitude && !longitude && country.empty() && city.empty() && region.empty(); } void UserLocation::validate() const { if (latitude) Rules::range(*latitude, -90.0, 90.0, "latitude"); if (longitude) Rules::range(*longitude, -180.0, 180.0, "longitude"); Rules::require(latitude.has_value() == longitude.has_value(), "latitude and longitude go together"); if (!country.empty()) Rules::country(country); Rules::require(city.size() <= 256 && region.size() <= 256, "city and region must be at most 256 characters"); } Json UserLocation::to_json() const { Json o = Json::object(); if (latitude) o.set("latitude", Json(*latitude)); if (longitude) o.set("longitude", Json(*longitude)); if (!country.empty()) o.set("country", Json(country)); if (!city.empty()) o.set("city", Json(city)); if (!region.empty()) o.set("region", Json(region)); return o; } bool WebSearchOptions::empty() const { return search_context_size.empty() && search_type.empty() && user_location.empty() && !image_results_enhanced_relevance; } void WebSearchOptions::validate() const { if (!search_context_size.empty()) Rules::one_of(search_context_size, {"low", "medium", "high"}, "search_context_size"); if (!search_type.empty()) Rules::one_of(search_type, {"fast", "pro", "auto"}, "search_type"); user_location.validate(); } Json WebSearchOptions::to_json() const { Json o = Json::object(); if (!search_context_size.empty()) o.set("search_context_size", Json(search_context_size)); if (!search_type.empty()) o.set("search_type", Json(search_type)); if (!user_location.empty()) o.set("user_location", user_location.to_json()); if (image_results_enhanced_relevance) o.set("image_results_enhanced_relevance", Json(*image_results_enhanced_relevance)); return o; } Json StructuredOutput::text() { Json o = Json::object(); o.set("type", Json("text")); return o; } Json StructuredOutput::json_schema(const Json& schema, const std::string& name, bool strict, const std::string& description) { Rules::require(schema.is_object(), "json schema must be an object"); Json js = Json::object(); js.set("schema", schema); if (!name.empty()) { Rules::name_token(name, 64, "schema name"); js.set("name", Json(name)); } 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; } SonarParams SonarParams::prompt(const std::string& text, const std::string& model) { SonarParams p; p.model = model; p.messages.push_back(Messages::user(text)); return p; } SonarParams& SonarParams::add(const Json& message) { messages.push_back(message); return *this; } namespace { void check_message(const Json& m) { Rules::require(m.is_object(), "a message must be an object"); std::string role = J::str(m, "role"); Rules::one_of(role, {"system", "user", "assistant", "tool"}, "message role"); const Json* content = m.find("content"); Rules::require(content && (content->is_string() || content->is_array() || content->is_null()), "message content must be a string or an array of parts"); if (content->is_array()) { Rules::require(!content->as_array().empty(), "message content parts must not be empty"); for (const Json& p : content->as_array()) { Rules::require(p.is_object(), "a content part must be an object"); Rules::one_of(J::str(p, "type"), {"text", "image_url", "file_url", "pdf_url", "video_url"}, "content part type"); } } } } // namespace void SonarParams::validate() const { Rules::model(model); Rules::require(!messages.empty(), "messages must not be empty"); Rules::require(messages.size() <= PerplexityCaps::kMaxMessages, "too many messages"); for (const Json& m : messages) check_message(m); if (max_tokens) Rules::require(*max_tokens >= 1, "max_tokens must be at least 1"); if (temperature) Rules::temperature(*temperature); if (top_p) Rules::top_p(*top_p); Rules::require(stop.size() <= PerplexityCaps::kMaxStop, "at most 16 stop sequences"); for (const std::string& s : stop) Rules::require(!s.empty(), "stop sequences must not be empty"); if (!response_format.is_null()) { Rules::require(response_format.is_object(), "response_format must be an object"); std::string type = J::str(response_format, "type"); Rules::one_of(type, {"text", "json_schema"}, "response_format.type"); if (type == "json_schema") { const Json* js = J::obj(response_format, "json_schema"); Rules::require(js && J::obj(*js, "schema"), "response_format.json_schema.schema is required"); } } web_search_options.validate(); if (!search_mode.empty()) Rules::one_of(search_mode, {"web", "academic", "sec"}, "search_mode"); Rules::domains(search_domain_filter, PerplexityCaps::kMaxFilterEntries, "search_domain_filter"); Rules::require(search_language_filter.size() <= PerplexityCaps::kMaxFilterEntries, "too many language filters"); for (const std::string& l : search_language_filter) Rules::language(l, "search_language_filter entry"); if (!search_recency_filter.empty()) Rules::one_of(search_recency_filter, {"hour", "day", "week", "month", "year"}, "search_recency_filter"); auto date = [](const std::string& d, const char* n) { if (!d.empty()) Rules::date(d, n); }; date(search_after_date_filter, "search_after_date_filter"); date(search_before_date_filter, "search_before_date_filter"); date(last_updated_before_filter, "last_updated_before_filter"); date(last_updated_after_filter, "last_updated_after_filter"); Rules::require(image_format_filter.size() <= PerplexityCaps::kMaxFilterEntries, "too many image format filters"); for (const std::string& f : image_format_filter) Rules::name_token(f, 16, "image_format_filter entry"); Rules::domains(image_domain_filter, PerplexityCaps::kMaxFilterEntries, "image_domain_filter"); if (!stream_mode.empty()) Rules::one_of(stream_mode, {"full", "concise"}, "stream_mode"); if (!reasoning_effort.empty()) Rules::one_of(reasoning_effort, {"minimal", "low", "medium", "high"}, "reasoning_effort"); if (!language_preference.empty()) Rules::language(language_preference, "language_preference"); } namespace { Json string_array(const std::vector& v) { Json a = Json::array(); for (const std::string& s : v) a.push(Json(s)); return a; } } // namespace Json SonarParams::to_json(bool stream) const { validate(); Json b = Json::object(); b.set("model", Json(model)); Json msgs = Json::array(); for (const Json& m : messages) msgs.push(m); b.set("messages", msgs); if (stream) b.set("stream", Json(true)); if (max_tokens) b.set("max_tokens", Json(*max_tokens)); if (temperature) b.set("temperature", Json(*temperature)); if (top_p) b.set("top_p", Json(*top_p)); if (stop.size() == 1) b.set("stop", Json(stop[0])); else if (!stop.empty()) b.set("stop", string_array(stop)); if (!response_format.is_null()) b.set("response_format", response_format); if (!web_search_options.empty()) b.set("web_search_options", web_search_options.to_json()); if (!search_mode.empty()) b.set("search_mode", Json(search_mode)); if (return_images) b.set("return_images", Json(*return_images)); if (return_related_questions) b.set("return_related_questions", Json(*return_related_questions)); if (enable_search_classifier) b.set("enable_search_classifier", Json(*enable_search_classifier)); if (disable_search) b.set("disable_search", Json(*disable_search)); if (!search_domain_filter.empty()) b.set("search_domain_filter", string_array(search_domain_filter)); if (!search_language_filter.empty()) b.set("search_language_filter", string_array(search_language_filter)); if (!search_recency_filter.empty()) b.set("search_recency_filter", Json(search_recency_filter)); if (!search_after_date_filter.empty()) b.set("search_after_date_filter", Json(search_after_date_filter)); if (!search_before_date_filter.empty()) b.set("search_before_date_filter", Json(search_before_date_filter)); if (!last_updated_before_filter.empty()) b.set("last_updated_before_filter", Json(last_updated_before_filter)); if (!last_updated_after_filter.empty()) b.set("last_updated_after_filter", Json(last_updated_after_filter)); if (!image_format_filter.empty()) b.set("image_format_filter", string_array(image_format_filter)); if (!image_domain_filter.empty()) b.set("image_domain_filter", string_array(image_domain_filter)); if (!stream_mode.empty()) b.set("stream_mode", Json(stream_mode)); if (!reasoning_effort.empty()) b.set("reasoning_effort", Json(reasoning_effort)); if (!language_preference.empty()) b.set("language_preference", Json(language_preference)); if (extra.is_object()) { std::string known = std::string(",") + Calls::spec_of(Call::kSonar).body_fields + ","; for (const auto& [k, v] : extra.as_object()) { Rules::require(known.find("," + k + ",") != std::string::npos, "extra field '" + k + "' is not in the Sonar request schema"); if (!b.has(k)) b.set(k, v); } } return b; } namespace { constexpr SonarModel kModels[] = { {"sonar", "lightweight search-grounded model"}, {"sonar-pro", "advanced search model for complex queries"}, {"sonar-reasoning-pro", "chain-of-thought reasoning with search"}, {"sonar-deep-research", "exhaustive multi-step research; slow, usually used via async"}, }; } // namespace std::size_t SonarModels::count() { return sizeof(kModels) / sizeof(kModels[0]); } const SonarModel& SonarModels::at(std::size_t i) { if (i >= count()) throw llm::ApiError::invalid("perplexity", "model index out of range"); return kModels[i]; } bool SonarModels::known(const std::string& id) { for (const SonarModel& m : kModels) if (id == m.id) return true; return false; } } // namespace molo::perplexity