Files
moloapp/tests/src/vendors/perplexity/test_model_tables.cpp
T

195 lines
8.8 KiB
C++
Raw Normal View History

// PerplexityChatModel (neutral seam), the call table, error table and rules.
#include <set>
#include "support.h"
#include "vendors/perplexity/calls.h"
#include "vendors/perplexity/chat_model.h"
#include "vendors/perplexity/errors.h"
#include "vendors/perplexity/rules.h"
using namespace molo::perplexity;
using namespace perplexity_test;
using molo::llm::ApiError;
namespace {
const char* kReply =
"{\"id\":\"c1\",\"model\":\"sonar\",\"created\":1,\"choices\":[{\"index\":0,\"finish_reason\":\"length\",\"message\":{\"role\":\"assistant\",\"content\":\"4\"},\"delta\":{\"role\":\"assistant\",\"content\":\"\"}}],"
"\"citations\":[\"https://example.com/a\"],\"usage\":{\"prompt_tokens\":3,\"completion_tokens\":1,\"total_tokens\":4,\"reasoning_tokens\":1,\"cost\":{\"input_tokens_cost\":0,\"output_tokens_cost\":0,\"total_cost\":0}}}";
const char* kStream =
"data: {\"id\":\"s\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"4\"},\"finish_reason\":null}]}\n\n"
"data: {\"id\":\"s\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\"},\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":3,\"completion_tokens\":1,\"total_tokens\":4,\"cost\":{\"input_tokens_cost\":0,\"output_tokens_cost\":0,\"total_cost\":0}}}\n\n"
"data: [DONE]\n\n";
} // namespace
TEST(perplexity_chat_model, generate_maps_request_and_response) {
Rig rig;
rig.transport->expect_response(Method::kPost, "/v1/sonar", reply(kReply));
PerplexityChatModel model(std::make_shared<Sonar>(rig.client()));
molo::llm::ChatRequest req;
req.system = "Be terse.";
req.messages.push_back(molo::llm::Message::user("2+2?"));
req.max_tokens = 5;
req.temperature = 0.2;
req.reasoning_effort = "low";
req.extra = Json::parse("{\"search_mode\":\"academic\",\"disable_search\":true}");
molo::llm::ChatResponse res = model.generate(req);
ASSERT_EQ(model.vendor(), "perplexity");
ASSERT_EQ(res.text(), "4");
ASSERT_EQ(res.finish_reason, "length");
ASSERT_EQ(res.usage.input_tokens, 3);
ASSERT_EQ(res.usage.reasoning_tokens, 1);
ASSERT_EQ(res.raw.find("citations")->as_array().size(), 1u);
auto s = rig.last();
ASSERT_TRUE(has(s.body, "\"model\":\"sonar\""));
ASSERT_TRUE(has(s.body, "{\"content\":\"Be terse.\",\"role\":\"system\"}"));
ASSERT_TRUE(has(s.body, "\"max_tokens\":5"));
ASSERT_TRUE(has(s.body, "\"search_mode\":\"academic\""));
ASSERT_TRUE(has(s.body, "\"disable_search\":true"));
}
TEST(perplexity_chat_model, response_schema_and_images) {
Rig rig;
rig.transport->expect_response(Method::kPost, "/v1/sonar", reply(kReply));
PerplexityChatModel model(std::make_shared<Sonar>(rig.client()), "sonar-pro");
molo::llm::ChatRequest req;
molo::llm::Message m;
m.role = molo::llm::Role::kUser;
m.parts.push_back(molo::llm::ContentPart::of_text("what is this"));
molo::llm::ImageData img;
img.mime = "image/png";
img.bytes = "abc";
m.parts.push_back(molo::llm::ContentPart::of_image(img));
req.messages.push_back(m);
req.response_schema = Json::parse("{\"type\":\"object\"}");
model.generate(req);
auto s = rig.last();
ASSERT_TRUE(has(s.body, "\"model\":\"sonar-pro\""));
ASSERT_TRUE(has(s.body, "\"image_url\":{\"url\":\"data:image/png;base64,YWJj\"}"));
ASSERT_TRUE(has(s.body, "\"response_format\":{\"json_schema\":"));
}
TEST(perplexity_chat_model, unsupported_features_are_rejected_loudly) {
Rig rig;
PerplexityChatModel model(std::make_shared<Sonar>(rig.client()));
molo::llm::ChatRequest req;
req.messages.push_back(molo::llm::Message::user("hi"));
molo::llm::ToolSpec t;
t.name = "f";
req.tools.push_back(t);
ASSERT_THROW(model.generate(req), ApiError);
req.tools.clear();
req.json_mode = true;
ASSERT_THROW(model.generate(req), ApiError);
req.json_mode = false;
req.extra = Json::parse("{\"temperature_typo\":1}");
ASSERT_THROW(model.generate(req), ApiError);
req.extra = Json();
molo::llm::Message tool;
tool.role = molo::llm::Role::kTool;
tool.parts.push_back(molo::llm::ContentPart::of_result({"c", "r", false}));
req.messages.push_back(tool);
ASSERT_THROW(model.generate(req), ApiError);
ASSERT_EQ(rig.transport->send_count(), 0u);
}
TEST(perplexity_chat_model, stream_emits_text_finish_and_usage) {
Rig rig;
rig.transport->expect_response(Method::kPost, "/v1/sonar", sse(kStream));
PerplexityChatModel model(std::make_shared<Sonar>(rig.client()));
molo::llm::ChatRequest req;
req.messages.push_back(molo::llm::Message::user("2+2?"));
std::string text, finish;
int total = 0;
model.stream(req, [&](const molo::llm::ChatChunk& c) {
text += c.text_delta;
if (!c.finish_reason.empty()) finish = c.finish_reason;
if (c.usage) total = c.usage->total_tokens;
return true;
});
ASSERT_EQ(text, "4");
ASSERT_EQ(finish, "stop");
ASSERT_EQ(total, 4);
}
TEST(perplexity_calls, table_is_consistent) {
std::set<std::string> seen;
for (std::size_t i = 0; i < Calls::count(); ++i) {
const CallSpec& s = Calls::at(i);
ASSERT_TRUE(std::string(s.path).front() == '/');
ASSERT_TRUE(seen.insert(std::string(s.http_method) + " " + s.path).second); // no duplicate rows
ASSERT_EQ(&Calls::spec_of(s.call) == &s, true);
}
ASSERT_EQ(Calls::count(), 39u);
ASSERT_TRUE(Calls::schema_count() > 40);
}
TEST(perplexity_calls, render_validates_every_id) {
ASSERT_EQ(Calls::render(Call::kSonar), "/v1/sonar");
ASSERT_EQ(Calls::render(Call::kAgentGet, "resp_1"), "/v1/agent/resp_1");
ASSERT_EQ(Calls::render(Call::kAgentFileContent, std::vector<std::string>{"r", "f"}), "/v1/agent/r/files/f/content");
ASSERT_EQ(Calls::render(Call::kOrchModel, "perplexity/kimi-k3"), "/orchestrator/v1/models/perplexity/kimi-k3");
ASSERT_THROW(Calls::render(Call::kAgentGet, "../x"), ApiError);
ASSERT_THROW(Calls::render(Call::kAgentGet, ""), ApiError);
ASSERT_THROW(Calls::render(Call::kAgentGet), ApiError); // missing id
ASSERT_THROW(Calls::render(Call::kSonar, "extra"), ApiError); // unexpected id
ASSERT_THROW(Calls::render(Call::kBatchGet, "a/b"), ApiError); // only models may carry a slash
ASSERT_EQ(Calls::method_of(Call::kSkillUpdate) == molo::clients::Method::kPut, true);
ASSERT_EQ(Calls::method_of(Call::kFileDelete) == molo::clients::Method::kDelete, true);
}
TEST(perplexity_errors, table_and_advice) {
ASSERT_TRUE(Errors::kind_of(401) == ErrorKind::kAuth);
ASSERT_TRUE(Errors::kind_of(402) == ErrorKind::kPayment);
ASSERT_TRUE(Errors::kind_of(429) == ErrorKind::kRateLimit);
ASSERT_TRUE(Errors::kind_of(204) == ErrorKind::kNone);
ASSERT_TRUE(Errors::kind_of(418) == ErrorKind::kOther);
ASSERT_TRUE(Errors::retryable(429));
ASSERT_TRUE(Errors::retryable(503));
ASSERT_FALSE(Errors::retryable(400));
ASSERT_FALSE(Errors::retryable(401));
ASSERT_EQ(Errors::wait_seconds(400), 0);
ASSERT_TRUE(Errors::wait_seconds(429) >= 1 && Errors::wait_seconds(429) <= Errors::kMaxWaitSeconds);
ASSERT_TRUE(Errors::is_rate_limit_type("token_rate_limit_exceeded"));
ASSERT_FALSE(Errors::is_rate_limit_type("invalid_api_key"));
ApiError e("perplexity", 402, "x", "no credits");
ASSERT_TRUE(has(Errors::explain(e), "Payment Required"));
ASSERT_TRUE(has(Errors::explain(ApiError("perplexity", 599, "weird", "m")), "weird"));
}
TEST(perplexity_rules, formats) {
Rules::date("01/31/2025", "d");
Rules::date("1/2/2025", "d");
ASSERT_THROW(Rules::date("2025-01-31", "d"), ApiError);
ASSERT_THROW(Rules::date("13/01/2025", "d"), ApiError);
ASSERT_THROW(Rules::date("01/32/2025", "d"), ApiError);
ASSERT_THROW(Rules::date("01/01/25", "d"), ApiError);
Rules::language("en", "l");
ASSERT_THROW(Rules::language("english", "l"), ApiError);
ASSERT_THROW(Rules::language("e1", "l"), ApiError);
Rules::model("perplexity/kimi-k3");
ASSERT_THROW(Rules::model(""), ApiError);
ASSERT_THROW(Rules::model("a b"), ApiError);
ASSERT_THROW(Rules::model(std::string(129, 'a')), ApiError);
ASSERT_THROW(Rules::https_url("http://example.com", "u"), ApiError);
ASSERT_THROW(Rules::https_url("https://user@example.com", "u"), ApiError);
Rules::https_url("https://example.com/mcp", "u");
ASSERT_EQ(Rules::url_encode("a b&c"), "a%20b%26c");
ASSERT_THROW(Rules::filename("a/b"), ApiError);
ASSERT_THROW(Rules::top_p(1.5), ApiError);
Rules::top_p(0.0);
Rules::temperature(2.0);
ASSERT_THROW(Rules::temperature(-0.1), ApiError);
}
TEST(perplexity_platform, key_travels_in_the_header_only) {
Rig rig;
rig.transport->expect_response(Method::kPost, "/v1/sonar", reply(kReply));
Sonar(rig.client()).create(SonarParams::prompt("hi"));
auto s = rig.last();
ASSERT_EQ(s.headers.get_or("Authorization", ""), std::string("Bearer ") + kKey);
ASSERT_EQ(s.target.find(kKey), std::string::npos);
ASSERT_EQ(s.body.find(kKey), std::string::npos);
ASSERT_EQ(std::string(Platform::kKeyEnv), "PERPLEXITY_API_KEY");
ASSERT_EQ(std::string(Platform::config().base_url), "https://api.perplexity.ai");
}