mirror of
https://github.com/lemonade-sdk/lemonade.git
synced 2026-11-23 08:49:28 -10:00
* main into zero shot, post refactoring * cleanup * cleanup * CR feedback: fixed the residency document clarification bug * cr feedback: fake tests with propoer assertions * stacks on the zero shot work in 3952 * pr feedback: better policy checks and logging for backend failures * pr feedback: adding on_truncation * pr feedback: adding the truncation support for classifier and fixing documentation issues * CI fix and doc enhancement * pr feedback: fixed edge cases * added check for 0.3.7 truncated verification * pr feedback: router validation and argument handling * Added the example lfm2 ONNX model * pr feedback: added max-length and 3 flag truncate flags * pin bump to 0.4.0 and more tests for zero-shot * CI fix * pr feedback: model level filtering * PR Feedback: CR feedback * restricting decision models based on model family. 8k context limit by default for encoder decision models * checks at router level to reject wrongly labeled systemone models with 400 * pr feedback: default set to 4k, limits passed down by llama-server, truncated is added to systemone * truncated documentation * pr feedback: checks the token count instead of the template
912 lines
44 KiB
C++
912 lines
44 KiB
C++
// Unit tests for the Lemonade Router classifier/leaf registry (#2379).
|
|
|
|
#include "fake_classifier_services.h"
|
|
#include "lemon/routing_policy.h"
|
|
|
|
#include <algorithm>
|
|
#include <cstdio>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using lemon::Condition;
|
|
using lemon::ConditionPtr;
|
|
using lemon::EvalContext;
|
|
using lemon::NamedLeafFactories;
|
|
using lemon::RouteContext;
|
|
using lemon::json;
|
|
|
|
static int g_failures = 0;
|
|
|
|
static void check(const char* name, bool ok) {
|
|
std::printf("[%s] %s\n", ok ? "PASS" : "FAIL", name);
|
|
if (!ok) ++g_failures;
|
|
}
|
|
|
|
namespace {
|
|
|
|
struct ConstCondition : Condition {
|
|
bool value;
|
|
int* calls;
|
|
std::string trace_name;
|
|
|
|
ConstCondition(bool v, int* c, std::string name)
|
|
: value(v), calls(c), trace_name(std::move(name)) {}
|
|
|
|
bool evaluate(EvalContext& ctx) const override {
|
|
++(*calls);
|
|
if (ctx.want_trace) ctx.trace.push_back(lemon::TraceEntry{trace_name, std::nullopt, value});
|
|
return value;
|
|
}
|
|
};
|
|
|
|
} // namespace
|
|
|
|
static RouteContext make_route_context() {
|
|
RouteContext route;
|
|
route.input = "my ssn is 123";
|
|
route.params.model = "user.Router";
|
|
route.params.chars = route.input.size();
|
|
return route;
|
|
}
|
|
|
|
static EvalContext make_eval_context(const RouteContext& route,
|
|
const lemon::ClassifierServices& services,
|
|
bool want_trace = false) {
|
|
EvalContext ctx{route, services};
|
|
ctx.want_trace = want_trace;
|
|
return ctx;
|
|
}
|
|
|
|
static bool throws_invalid_arg(const std::function<void()>& fn) {
|
|
try {
|
|
fn();
|
|
} catch (const std::invalid_argument&) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static void test_make_classifier() {
|
|
json cfg = {
|
|
{"id", "pii"},
|
|
{"type", "classifier"},
|
|
{"model", "pii-detector-small"},
|
|
{"labels", json::array({"PII", "NO_PII"})},
|
|
{"default_label", "PII"},
|
|
{"on_error", "match_true"},
|
|
};
|
|
auto classifier = lemon::make_classifier(cfg);
|
|
check("make_classifier builds generic classifier", classifier->id() == "pii" &&
|
|
classifier->type() == "classifier");
|
|
check("make_classifier preserves labels/default/on_error",
|
|
classifier->labels().size() == 2 &&
|
|
classifier->default_label().has_value() &&
|
|
*classifier->default_label() == "PII" &&
|
|
classifier->on_error() == lemon::OnError::MatchTrue);
|
|
|
|
lemon::testing::FakeClassifierServices fake;
|
|
fake.set_classifier_scores("pii-detector-small", {{"PII", 0.91}, {"NO_PII", 0.09}});
|
|
auto services = fake.make();
|
|
auto route = make_route_context();
|
|
EvalContext ctx = make_eval_context(route, services);
|
|
auto score = classifier->evaluate(lemon::ClassifierContext{ctx.request, ctx.services});
|
|
check("generic classifier calls run_classifier service", score.ok && score.score_of("PII") == 0.91);
|
|
}
|
|
|
|
static void test_make_zero_shot_classifier() {
|
|
json cfg = {
|
|
{"id", "route"},
|
|
{"type", "zero_shot"},
|
|
{"model", "lfm-prompt-router"},
|
|
{"labels", json::array({"coding", "cooking", "pii"})},
|
|
};
|
|
auto classifier = lemon::make_classifier(cfg);
|
|
check("make_classifier builds zero_shot classifier",
|
|
classifier->id() == "route" && classifier->type() == "zero_shot" &&
|
|
classifier->labels().size() == 3);
|
|
|
|
lemon::testing::FakeClassifierServices fake;
|
|
fake.set_zero_shot_scores("lfm-prompt-router",
|
|
{{"coding", 0.02}, {"cooking", 0.93}, {"pii", 0.05}});
|
|
// Configured on the other service too: if the classifier reached
|
|
// run_classifier instead, the scores below would be the wrong ones.
|
|
fake.set_classifier_scores("lfm-prompt-router", {{"coding", 1.0}});
|
|
auto services = fake.make();
|
|
auto route = make_route_context();
|
|
EvalContext ctx = make_eval_context(route, services);
|
|
auto score = classifier->evaluate(lemon::ClassifierContext{ctx.request, ctx.services});
|
|
check("zero_shot classifier calls run_zero_shot_classifier service",
|
|
score.ok && score.score_of("cooking") == 0.93);
|
|
check("zero_shot classifier sends its declared labels",
|
|
fake.last_zero_shot_labels() ==
|
|
std::vector<std::string>{"coding", "cooking", "pii"});
|
|
}
|
|
|
|
static void test_make_zero_shot_classifier_rejections() {
|
|
check("zero_shot rejects missing model",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "x"}, {"type", "zero_shot"},
|
|
{"labels", json::array({"a"})}});
|
|
}));
|
|
check("zero_shot rejects missing labels",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "x"}, {"type", "zero_shot"},
|
|
{"model", "m"}});
|
|
}));
|
|
check("zero_shot rejects empty labels",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "x"}, {"type", "zero_shot"},
|
|
{"model", "m"}, {"labels", json::array()}});
|
|
}));
|
|
check("zero_shot rejects prompt",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "x"}, {"type", "zero_shot"},
|
|
{"model", "m"},
|
|
{"labels", json::array({"a"})},
|
|
{"prompt", "pick one"}});
|
|
}));
|
|
check("zero_shot rejects reference_phrases",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "x"}, {"type", "zero_shot"},
|
|
{"model", "m"},
|
|
{"labels", json::array({"a"})},
|
|
{"reference_phrases", json{{"a", json::array({"p"})}}}});
|
|
}));
|
|
}
|
|
|
|
// An unconfigured run_zero_shot_classifier must surface as Score::ok=false so
|
|
// the owning condition applies on_error, not as a throw out of evaluate().
|
|
static void test_zero_shot_classifier_without_service_fails_soft() {
|
|
auto classifier = lemon::make_classifier(json{
|
|
{"id", "route"}, {"type", "zero_shot"}, {"model", "m"},
|
|
{"labels", json::array({"a", "b"})}});
|
|
lemon::ClassifierServices services; // every service left unset
|
|
auto route = make_route_context();
|
|
EvalContext ctx = make_eval_context(route, services);
|
|
auto score = classifier->evaluate(lemon::ClassifierContext{ctx.request, ctx.services});
|
|
check("zero_shot without service reports ok=false", !score.ok);
|
|
}
|
|
|
|
// Scores for any label set other than the declared one must fail the
|
|
// classifier, so a protective rule applies on_error instead of silently missing.
|
|
static void test_zero_shot_classifier_rejects_mismatched_label_set() {
|
|
auto classifier = lemon::make_classifier(json{
|
|
{"id", "route"}, {"type", "zero_shot"}, {"model", "m"},
|
|
{"labels", json::array({"pii", "chit-chat"})}});
|
|
auto route = make_route_context();
|
|
|
|
auto evaluate_with = [&](std::map<std::string, double> returned) {
|
|
lemon::testing::FakeClassifierServices fake;
|
|
fake.set_zero_shot_scores("m", std::move(returned));
|
|
auto services = fake.make();
|
|
EvalContext ctx = make_eval_context(route, services);
|
|
return classifier->evaluate(lemon::ClassifierContext{ctx.request, ctx.services});
|
|
};
|
|
|
|
check("zero_shot fails on the backend's own label set",
|
|
!evaluate_with({{"LABEL_0", 0.9}, {"LABEL_1", 0.1}}).ok);
|
|
check("zero_shot fails when a declared label is missing",
|
|
!evaluate_with({{"chit-chat", 1.0}}).ok);
|
|
check("zero_shot fails when an undeclared label is returned",
|
|
!evaluate_with({{"pii", 0.2}, {"chit-chat", 0.7}, {"O", 0.1}}).ok);
|
|
|
|
auto exact = evaluate_with({{"pii", 0.8}, {"chit-chat", 0.2}});
|
|
check("zero_shot accepts exactly the declared label set",
|
|
exact.ok && exact.score_of("pii") == 0.8);
|
|
}
|
|
|
|
static lemon::Score evaluate_systemone(const lemon::ClassifierPtr& classifier, json answer,
|
|
nlohmann::ordered_json* asked = nullptr) {
|
|
lemon::testing::FakeClassifierServices fake;
|
|
fake.set_systemone_answer("decider", std::move(answer));
|
|
auto services = fake.make();
|
|
auto route = make_route_context();
|
|
EvalContext ctx = make_eval_context(route, services);
|
|
lemon::Score score = classifier->evaluate(lemon::ClassifierContext{ctx.request, ctx.services});
|
|
if (asked) *asked = fake.last_systemone_question();
|
|
return score;
|
|
}
|
|
|
|
static void test_make_systemone_classifier() {
|
|
const json choice_question = {
|
|
{"type", "choice"},
|
|
{"instructions", "Which team should handle this?"},
|
|
{"criteria", json::array({"billing", json{{"shipping", "where an order is"}}})},
|
|
};
|
|
auto choice = lemon::make_classifier(json{
|
|
{"id", "team"}, {"type", "systemone"}, {"model", "decider"},
|
|
{"question", choice_question}});
|
|
check("make_classifier builds a systemone classifier",
|
|
choice->type() == "systemone" &&
|
|
choice->labels() == std::vector<std::string>{"billing", "shipping"});
|
|
|
|
nlohmann::ordered_json asked;
|
|
auto score = evaluate_systemone(
|
|
choice,
|
|
{{"type", "choice"}, {"choice", "billing"},
|
|
{"probabilities", {{"billing", 0.9}, {"shipping", 0.1}}}},
|
|
&asked);
|
|
json sent = choice_question;
|
|
sent["criteria"] = {{"billing", nullptr}, {"shipping", "where an order is"}};
|
|
check("systemone sends its declared question", json(asked) == sent);
|
|
check("systemone choice reports the option probabilities",
|
|
score.ok && score.score_of("billing") == 0.9 && score.score_of("shipping") == 0.1);
|
|
|
|
auto noul = lemon::make_classifier(json{
|
|
{"id", "pii"}, {"type", "systemone"}, {"model", "decider"},
|
|
{"question", {{"type", "noul"},
|
|
{"instructions", "Does the message contain personal data?"}}}});
|
|
score = evaluate_systemone(noul, {{"type", "noul"}, {"noul", 0.75}});
|
|
check("systemone noul declares true and false",
|
|
noul->labels() == std::vector<std::string>{"true", "false"});
|
|
check("systemone noul reports true and its complement",
|
|
score.ok && score.score_of("true") == 0.75 && score.score_of("false") == 0.25);
|
|
|
|
auto level = lemon::make_classifier(json{
|
|
{"id", "urgency"}, {"type", "systemone"}, {"model", "decider"},
|
|
{"question", {{"type", "score"},
|
|
{"instructions", "How urgent is this?"},
|
|
{"criteria", json::array({"can wait", "today", "right now"})}}}});
|
|
score = evaluate_systemone(
|
|
level, {{"type", "score"}, {"score", 1.2},
|
|
{"probabilities", {{"0", 0.1}, {"1", 0.6}, {"2", 0.3}}}});
|
|
check("systemone score keys each level probability by its description",
|
|
score.ok && score.score_of("can wait") == 0.1 && score.score_of("today") == 0.6 &&
|
|
score.score_of("right now") == 0.3);
|
|
}
|
|
|
|
// A stored policy is json, which sorts object keys; the array form is how a
|
|
// choice keeps the option order the model scores by.
|
|
static void test_systemone_choice_array_keeps_option_order() {
|
|
auto choice = lemon::make_classifier(json{
|
|
{"id", "team"}, {"type", "systemone"}, {"model", "decider"},
|
|
{"question", {{"type", "choice"}, {"instructions", "Which team?"},
|
|
{"criteria", json::array({"technical",
|
|
json{{"shipping", "where an order is"}},
|
|
"billing"})}}}});
|
|
check("a choice criteria array declares its options in array order",
|
|
choice->labels() ==
|
|
std::vector<std::string>{"technical", "shipping", "billing"});
|
|
|
|
nlohmann::ordered_json asked;
|
|
auto score = evaluate_systemone(
|
|
choice,
|
|
{{"probabilities", {{"technical", 0.1}, {"shipping", 0.2}, {"billing", 0.7}}}},
|
|
&asked);
|
|
check("a choice criteria array reaches the model as an object in array order",
|
|
asked["criteria"].dump() ==
|
|
R"({"technical":null,"shipping":"where an order is","billing":null})");
|
|
check("a choice criteria array is scored like an object",
|
|
score.ok && score.score_of("billing") == 0.7);
|
|
}
|
|
|
|
// An answer that does not cover the asked options must fail the
|
|
// classifier, so a protective rule applies on_error instead of silently missing.
|
|
static void test_systemone_classifier_rejects_mismatched_answer() {
|
|
auto choice = lemon::make_classifier(json{
|
|
{"id", "team"}, {"type", "systemone"}, {"model", "decider"},
|
|
{"question", {{"type", "choice"}, {"instructions", "Which team?"},
|
|
{"criteria", json::array({"billing", "shipping"})}}}});
|
|
check("systemone fails on another option set",
|
|
!evaluate_systemone(choice, {{"probabilities", {{"LABEL_0", 1.0}}}}).ok);
|
|
check("systemone fails when an option is missing",
|
|
!evaluate_systemone(choice, {{"probabilities", {{"billing", 1.0}}}}).ok);
|
|
check("systemone fails on an empty answer", !evaluate_systemone(choice, json::object()).ok);
|
|
check("systemone fails on a probability outside [0, 1]",
|
|
!evaluate_systemone(choice,
|
|
{{"probabilities", {{"billing", 1.5}, {"shipping", -0.5}}}}).ok);
|
|
|
|
auto level = lemon::make_classifier(json{
|
|
{"id", "urgency"}, {"type", "systemone"}, {"model", "decider"},
|
|
{"question", {{"type", "score"}, {"instructions", "How urgent?"},
|
|
{"criteria", json::array({"low", "high"})}}}});
|
|
check("systemone score fails on a level index out of range",
|
|
!evaluate_systemone(level, {{"probabilities", {{"0", 0.5}, {"5", 0.5}}}}).ok);
|
|
for (const char* key : {"0extra", " 0", "+0"}) {
|
|
const std::string name = std::string("systemone score fails on the level key '") + key + "'";
|
|
check(name.c_str(),
|
|
!evaluate_systemone(level, {{"probabilities", {{key, 0.5}, {"1", 0.5}}}}).ok);
|
|
}
|
|
|
|
lemon::ClassifierServices no_services;
|
|
auto route = make_route_context();
|
|
EvalContext ctx = make_eval_context(route, no_services);
|
|
check("systemone without service reports ok=false",
|
|
!choice->evaluate(lemon::ClassifierContext{ctx.request, ctx.services}).ok);
|
|
}
|
|
|
|
// lemond adds `truncated` to each /v1/systemone answer, so a systemone guard can
|
|
// fail closed on a cut input as a zero_shot one does.
|
|
static void test_systemone_on_truncation() {
|
|
auto evaluate_with = [](const char* on_truncation, std::optional<bool> truncated) {
|
|
json cfg{{"id", "pii"}, {"type", "systemone"}, {"model", "decider"},
|
|
{"question", {{"type", "noul"}, {"instructions", "Personal data?"}}}};
|
|
if (on_truncation != nullptr) cfg["on_truncation"] = on_truncation;
|
|
json answer = {{"type", "noul"}, {"noul", 0.2}};
|
|
if (truncated.has_value()) answer["truncated"] = *truncated;
|
|
return evaluate_systemone(lemon::make_classifier(cfg), answer);
|
|
};
|
|
|
|
const auto whole = evaluate_with("fail", false);
|
|
check("systemone carries truncated=false from the answer",
|
|
whole.ok && whole.truncated == false && whole.score_of("true") == 0.2);
|
|
const auto cut = evaluate_with("fail", true);
|
|
check("systemone on_truncation=fail turns a cut answer into a failure",
|
|
!cut.ok && cut.truncated == true);
|
|
check("systemone on_truncation=fail fails closed when the answer does not say",
|
|
!evaluate_with("fail", std::nullopt).ok);
|
|
const auto scored = evaluate_with(nullptr, true);
|
|
check("systemone on_truncation defaults to score",
|
|
scored.ok && scored.truncated == true && scored.score_of("true") == 0.2);
|
|
check("systemone without truncated reports nothing",
|
|
!evaluate_with(nullptr, std::nullopt).truncated.has_value());
|
|
}
|
|
|
|
static void test_make_systemone_classifier_rejections() {
|
|
auto make = [](json question, json extra = json::object()) {
|
|
json config = {{"id", "x"}, {"type", "systemone"}, {"model", "m"},
|
|
{"question", std::move(question)}};
|
|
config.update(extra);
|
|
return throws_invalid_arg([config] { lemon::make_classifier(config); });
|
|
};
|
|
const json ok_question = {{"type", "noul"}, {"instructions", "Is it?"}};
|
|
|
|
check("systemone rejects a missing question",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "x"}, {"type", "systemone"}, {"model", "m"}});
|
|
}));
|
|
check("systemone rejects a missing model",
|
|
throws_invalid_arg([&] {
|
|
lemon::make_classifier(json{{"id", "x"}, {"type", "systemone"},
|
|
{"question", ok_question}});
|
|
}));
|
|
check("systemone rejects labels", make(ok_question, {{"labels", json::array({"a"})}}));
|
|
check("systemone rejects prompt", make(ok_question, {{"prompt", "p"}}));
|
|
check("systemone rejects an unknown question type",
|
|
make({{"type", "rank"}, {"instructions", "?"}}));
|
|
check("systemone rejects a question without instructions", make({{"type", "noul"}}));
|
|
check("systemone rejects an unknown question key",
|
|
make({{"type", "noul"}, {"instructions", "?"}, {"options", json::array()}}));
|
|
check("systemone rejects an empty choice",
|
|
make({{"type", "choice"}, {"instructions", "?"}, {"criteria", json::array()}}));
|
|
check("systemone rejects an object-form choice, which would reach the model sorted", [] {
|
|
try {
|
|
lemon::make_classifier(json{
|
|
{"id", "x"}, {"type", "systemone"}, {"model", "m"},
|
|
{"question", {{"type", "choice"}, {"instructions", "?"},
|
|
{"criteria", {{"shipping", nullptr}, {"billing", nullptr}}}}}});
|
|
} catch (const std::invalid_argument& e) {
|
|
return std::string(e.what()).find("must be an array") != std::string::npos;
|
|
}
|
|
return false;
|
|
}());
|
|
check("systemone rejects a one-level score",
|
|
make({{"type", "score"}, {"instructions", "?"}, {"criteria", json::array({"a"})}}));
|
|
check("systemone rejects duplicate score levels",
|
|
make({{"type", "score"}, {"instructions", "?"},
|
|
{"criteria", json::array({"a", "a"})}}));
|
|
check("systemone rejects a non-string score level",
|
|
make({{"type", "score"}, {"instructions", "?"}, {"criteria", json::array({"a", 2})}}));
|
|
check("systemone rejects noul criteria keys other than true and false",
|
|
make({{"type", "noul"}, {"instructions", "?"}, {"criteria", {{"maybe", "?"}}}}));
|
|
check("systemone rejects an empty choice array",
|
|
make({{"type", "choice"}, {"instructions", "?"}, {"criteria", json::array()}}));
|
|
check("systemone rejects a choice array that lists an option twice",
|
|
make({{"type", "choice"}, {"instructions", "?"},
|
|
{"criteria", json::array({"a", json{{"a", "again"}}})}}));
|
|
check("systemone rejects a choice array entry with two options",
|
|
make({{"type", "choice"}, {"instructions", "?"},
|
|
{"criteria", json::array({json{{"a", nullptr}, {"b", nullptr}}})}}));
|
|
check("systemone rejects a choice array entry that is not an option",
|
|
make({{"type", "choice"}, {"instructions", "?"}, {"criteria", json::array({"a", 3})}}));
|
|
check("systemone rejects a choice option with a control character",
|
|
make({{"type", "choice"}, {"instructions", "?"},
|
|
{"criteria", json::array({"billing\nteam", "shipping"})}}));
|
|
check("systemone rejects a score level with a control character",
|
|
make({{"type", "score"}, {"instructions", "?"},
|
|
{"criteria", json::array({"low\tlevel", "high"})}}));
|
|
check("systemone rejects a one-option choice",
|
|
make({{"type", "choice"}, {"instructions", "?"}, {"criteria", json::array({"a"})}}));
|
|
for (const json& bad : {json(""), json(" "), json(42), json(true), json::object(),
|
|
json::array()}) {
|
|
const std::string name = "systemone rejects instructions " + bad.dump();
|
|
check(name.c_str(), make({{"type", "noul"}, {"instructions", bad}}));
|
|
}
|
|
check("systemone rejects a numeric choice description",
|
|
make({{"type", "choice"}, {"instructions", "?"},
|
|
{"criteria", json::array({json{{"a", 3}}, "b"})}}));
|
|
check("systemone rejects a boolean noul description",
|
|
make({{"type", "noul"}, {"instructions", "?"}, {"criteria", {{"true", false}}}}));
|
|
|
|
auto accepts = [](json question) {
|
|
return !throws_invalid_arg([question] {
|
|
lemon::make_classifier(json{{"id", "x"}, {"type", "systemone"}, {"model", "m"},
|
|
{"question", question}});
|
|
});
|
|
};
|
|
check("systemone accepts object instructions",
|
|
accepts({{"type", "noul"}, {"instructions", {{"task", "Is it urgent?"}}}}));
|
|
check("systemone accepts array instructions",
|
|
accepts({{"type", "noul"}, {"instructions", json::array({"Is it urgent?"})}}));
|
|
check("systemone accepts null and object choice descriptions",
|
|
accepts({{"type", "choice"}, {"instructions", "?"},
|
|
{"criteria", json::array({json{{"a", nullptr}},
|
|
json{{"b", {{"means", "b"}}}}})}}));
|
|
|
|
for (const char* type : {"classifier", "zero_shot", "llm"}) {
|
|
json config = {{"id", "x"}, {"type", type}, {"model", "m"},
|
|
{"labels", json::array({"a"})}, {"prompt", "p"}};
|
|
if (std::string(type) == "zero_shot") config.erase("prompt");
|
|
const bool accepted = !throws_invalid_arg([config] { lemon::make_classifier(config); });
|
|
config["question"] = ok_question;
|
|
const std::string name = std::string(type) + " rejects a question, which only systemone asks";
|
|
check(name.c_str(),
|
|
accepted && throws_invalid_arg([config] { lemon::make_classifier(config); }));
|
|
}
|
|
}
|
|
|
|
// The backend cuts the text, never the labels, so a truncated answer is still a
|
|
// complete label set and must stay usable — carrying the cut forward, not failing.
|
|
static void test_zero_shot_classifier_reports_truncation() {
|
|
auto classifier = lemon::make_classifier(json{
|
|
{"id", "route"}, {"type", "zero_shot"}, {"model", "m"},
|
|
{"labels", json::array({"pii", "chit-chat"})}});
|
|
auto route = make_route_context();
|
|
|
|
auto evaluate_with = [&](std::optional<bool> truncated) {
|
|
lemon::testing::FakeClassifierServices fake;
|
|
fake.set_zero_shot_scores("m", {{"pii", 0.8}, {"chit-chat", 0.2}}, truncated);
|
|
auto services = fake.make();
|
|
EvalContext ctx = make_eval_context(route, services);
|
|
return classifier->evaluate(lemon::ClassifierContext{ctx.request, ctx.services});
|
|
};
|
|
|
|
auto cut = evaluate_with(true);
|
|
check("zero_shot carries the backend's truncation flag",
|
|
cut.ok && cut.truncated == true && cut.score_of("pii") == 0.8);
|
|
check("zero_shot reports false when the backend scored the whole input",
|
|
evaluate_with(false).truncated == false);
|
|
check("zero_shot reports nothing when the backend does not say",
|
|
!evaluate_with(std::nullopt).truncated.has_value());
|
|
|
|
lemon::testing::FakeClassifierServices fake;
|
|
fake.set_zero_shot_scores("m", {{"LABEL_0", 0.9}}, true);
|
|
auto services = fake.make();
|
|
EvalContext ctx = make_eval_context(route, services);
|
|
const auto mismatch = classifier->evaluate(lemon::ClassifierContext{ctx.request, ctx.services});
|
|
check("zero_shot keeps the cut flag when the label set does not match",
|
|
!mismatch.ok && mismatch.truncated == true &&
|
|
mismatch.error.find("different label set") != std::string::npos);
|
|
}
|
|
|
|
// on_truncation=fail is the only way a rule reaches a cut input, because a score
|
|
// computed from cut text is a SUCCESS and on_error never sees it.
|
|
static void test_zero_shot_on_truncation() {
|
|
auto evaluate_with = [](const char* on_truncation, std::optional<bool> truncated) {
|
|
json cfg{{"id", "pii"}, {"type", "zero_shot"}, {"model", "m"},
|
|
{"labels", json::array({"private", "public"})}};
|
|
if (on_truncation != nullptr) cfg["on_truncation"] = on_truncation;
|
|
auto classifier = lemon::make_classifier(cfg);
|
|
auto route = make_route_context();
|
|
lemon::testing::FakeClassifierServices fake;
|
|
fake.set_zero_shot_scores("m", {{"private", 0.1}, {"public", 0.9}}, truncated);
|
|
auto services = fake.make();
|
|
EvalContext ctx = make_eval_context(route, services);
|
|
return classifier->evaluate(lemon::ClassifierContext{ctx.request, ctx.services});
|
|
};
|
|
|
|
const auto cut_fail = evaluate_with("fail", true);
|
|
check("on_truncation=fail turns a cut answer into a failure", !cut_fail.ok);
|
|
check("on_truncation=fail names the cut as the reason",
|
|
cut_fail.error.find("cut the input text") != std::string::npos);
|
|
// The trace must still show WHY it failed, and failed_score() starts clean.
|
|
check("on_truncation=fail keeps the truncation flag for the trace",
|
|
cut_fail.truncated == true);
|
|
check("on_truncation=fail drops the scores computed from cut text",
|
|
cut_fail.score_of("public") == 0.0);
|
|
|
|
check("on_truncation=fail is inert when nothing was cut", evaluate_with("fail", false).ok);
|
|
const auto unknown_fail = evaluate_with("fail", std::nullopt);
|
|
check("on_truncation=fail fails closed when the backend does not say",
|
|
!unknown_fail.ok && !unknown_fail.truncated.has_value());
|
|
check("on_truncation=score uses an answer that does not say",
|
|
evaluate_with("score", std::nullopt).ok);
|
|
|
|
const auto cut_score = evaluate_with("score", true);
|
|
check("on_truncation=score keeps using a cut answer",
|
|
cut_score.ok && cut_score.truncated == true && cut_score.score_of("public") == 0.9);
|
|
|
|
const auto cut_default = evaluate_with(nullptr, true);
|
|
check("on_truncation defaults to score",
|
|
cut_default.ok && cut_default.score_of("public") == 0.9);
|
|
}
|
|
|
|
static void test_on_truncation_rejections() {
|
|
check("on_truncation rejects an unknown value",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{
|
|
{"id", "x"}, {"type", "zero_shot"}, {"model", "m"},
|
|
{"labels", json::array({"a"})}, {"on_truncation", "match_true"}});
|
|
}));
|
|
check("on_truncation=fail is accepted on classifier", [] {
|
|
try {
|
|
lemon::make_classifier(json{{"id", "x"},
|
|
{"type", "classifier"},
|
|
{"model", "m"},
|
|
{"labels", json::array({"a"})},
|
|
{"on_truncation", "fail"}});
|
|
return true;
|
|
} catch (const std::exception&) {
|
|
return false;
|
|
}
|
|
}());
|
|
check("classifier still rejects an unknown on_truncation",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{
|
|
{"id", "x"}, {"type", "classifier"}, {"model", "m"},
|
|
{"labels", json::array({"a"})}, {"on_truncation", "match_true"}});
|
|
}));
|
|
|
|
check("on_truncation=fail is accepted on systemone", [] {
|
|
try {
|
|
lemon::make_classifier(json{
|
|
{"id", "x"}, {"type", "systemone"}, {"model", "m"}, {"on_truncation", "fail"},
|
|
{"question", {{"type", "noul"}, {"instructions", "Is it?"}}}});
|
|
return true;
|
|
} catch (const std::exception&) {
|
|
return false;
|
|
}
|
|
}());
|
|
|
|
for (const char* type : {"llm", "semantic_similarity"}) {
|
|
const std::string label = std::string("on_truncation is rejected on ") + type;
|
|
check(label.c_str(), throws_invalid_arg([type] {
|
|
json cfg{{"id", "x"}, {"type", type}, {"model", "m"},
|
|
{"on_truncation", "fail"}};
|
|
if (std::string(type) == "semantic_similarity") {
|
|
cfg["reference_phrases"] = json{{"a", json::array({"p"})}};
|
|
} else {
|
|
cfg["labels"] = json::array({"a"});
|
|
if (std::string(type) == "llm") cfg["prompt"] = "pick one";
|
|
}
|
|
lemon::make_classifier(cfg);
|
|
}));
|
|
}
|
|
}
|
|
|
|
static void test_label_list_rejections() {
|
|
auto classifier_with = [](const char* type, json labels) {
|
|
json cfg{{"id", "x"}, {"type", type}, {"model", "m"}, {"labels", std::move(labels)}};
|
|
if (std::string(type) == "llm") cfg["prompt"] = "pick one";
|
|
return [cfg] { lemon::make_classifier(cfg); };
|
|
};
|
|
|
|
for (const char* type : {"classifier", "zero_shot", "llm"}) {
|
|
const std::string prefix = std::string(type) + ": ";
|
|
const std::string dup = prefix + "rejects a duplicate label";
|
|
const std::string blank = prefix + "rejects a blank label";
|
|
const std::string tab = prefix + "rejects a tab-only label";
|
|
const std::string lf = prefix + "rejects a label holding a line feed";
|
|
const std::string crlf = prefix + "rejects a label holding a carriage return";
|
|
const std::string inner_tab = prefix + "rejects a label holding an inner tab";
|
|
const std::string del = prefix + "rejects a label holding DEL";
|
|
const std::string ok = prefix + "accepts distinct labels with inner spaces";
|
|
const std::string utf8 = prefix + "accepts a multi-byte UTF-8 label";
|
|
check(dup.c_str(),
|
|
throws_invalid_arg(classifier_with(type, json::array({"coding", "coding"}))));
|
|
check(blank.c_str(),
|
|
throws_invalid_arg(classifier_with(type, json::array({"coding", " "}))));
|
|
check(tab.c_str(),
|
|
throws_invalid_arg(classifier_with(type, json::array({"coding", "\t"}))));
|
|
check(lf.c_str(),
|
|
throws_invalid_arg(classifier_with(type, json::array({"coding\ncooking"}))));
|
|
check(crlf.c_str(),
|
|
throws_invalid_arg(classifier_with(type, json::array({"coding\r\ncooking"}))));
|
|
// A tab is neither blank nor a line break, so it only fails once the
|
|
// rule covers every control character, as ort-server's does.
|
|
check(inner_tab.c_str(),
|
|
throws_invalid_arg(classifier_with(type, json::array({"coding\tcooking"}))));
|
|
check(del.c_str(),
|
|
throws_invalid_arg(classifier_with(type, json::array({"coding\x7F"}))));
|
|
check(ok.c_str(),
|
|
!throws_invalid_arg(
|
|
classifier_with(type, json::array({"customer support", "billing"}))));
|
|
// The control-character scan walks bytes, so a continuation byte must
|
|
// not read as one.
|
|
check(utf8.c_str(),
|
|
!throws_invalid_arg(classifier_with(type, json::array({"café", "日本語"}))));
|
|
}
|
|
}
|
|
|
|
static void test_make_classifier_rejections() {
|
|
check("make_classifier rejects missing id",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"type", "classifier"}, {"model", "m"}});
|
|
}));
|
|
check("make_classifier rejects missing type",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "x"}, {"model", "m"}});
|
|
}));
|
|
check("make_classifier rejects missing model for classifier",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "x"}, {"type", "classifier"}});
|
|
}));
|
|
check("make_classifier rejects default_label without labels",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "x"}, {"type", "classifier"},
|
|
{"model", "m"}, {"default_label", "PII"}});
|
|
}));
|
|
check("make_classifier rejects default_label not in labels",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "x"}, {"type", "classifier"},
|
|
{"model", "m"},
|
|
{"labels", json::array({"NO_PII"})},
|
|
{"default_label", "PII"}});
|
|
}));
|
|
check("make_classifier rejects unknown type",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "x"}, {"type", "mystery"},
|
|
{"model", "m"}});
|
|
}));
|
|
check("make_classifier accepts semantic_similarity", [] {
|
|
try {
|
|
auto c = lemon::make_classifier(
|
|
json{{"id", "topic"}, {"type", "semantic_similarity"},
|
|
{"model", "m"},
|
|
{"reference_phrases", {{"coding", json::array({"write a function"})},
|
|
{"math", json::array({"integral"})}}}});
|
|
const auto& labels = c->labels();
|
|
return c && c->id() == "topic" && c->type() == "semantic_similarity" &&
|
|
labels.size() == 2 &&
|
|
std::find(labels.begin(), labels.end(), "coding") != labels.end() &&
|
|
std::find(labels.begin(), labels.end(), "math") != labels.end();
|
|
} catch (...) {
|
|
return false;
|
|
}
|
|
}());
|
|
check("make_classifier accepts semantic_similarity default_label", [] {
|
|
try {
|
|
auto c = lemon::make_classifier(
|
|
json{{"id", "topic"}, {"type", "semantic_similarity"}, {"model", "m"},
|
|
{"reference_phrases", {{"coding", json::array({"write a function"})}}},
|
|
{"default_label", "coding"}});
|
|
return c && c->default_label().has_value() && *c->default_label() == "coding";
|
|
} catch (...) {
|
|
return false;
|
|
}
|
|
}());
|
|
check("make_classifier rejects semantic_similarity without reference_phrases",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "topic"}, {"type", "semantic_similarity"},
|
|
{"model", "m"}});
|
|
}));
|
|
check("make_classifier rejects semantic_similarity with empty reference_phrases",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "topic"}, {"type", "semantic_similarity"},
|
|
{"model", "m"},
|
|
{"reference_phrases", json::object()}});
|
|
}));
|
|
check("make_classifier rejects semantic_similarity concept with empty phrase list",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "topic"}, {"type", "semantic_similarity"},
|
|
{"model", "m"},
|
|
{"reference_phrases", {{"coding", json::array()}}}});
|
|
}));
|
|
check("make_classifier rejects semantic_similarity concept with empty phrase string",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "topic"}, {"type", "semantic_similarity"},
|
|
{"model", "m"},
|
|
{"reference_phrases", {{"coding", json::array({""})}}}});
|
|
}));
|
|
check("make_classifier rejects semantic_similarity without model",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "topic"}, {"type", "semantic_similarity"},
|
|
{"reference_phrases", {{"coding", json::array({"x"})}}}});
|
|
}));
|
|
check("make_classifier rejects semantic_similarity with explicit labels",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "topic"}, {"type", "semantic_similarity"},
|
|
{"model", "m"},
|
|
{"reference_phrases", {{"coding", json::array({"x"})}}},
|
|
{"labels", json::array({"coding"})}});
|
|
}));
|
|
check("make_classifier rejects semantic_similarity default_label not a concept",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "topic"}, {"type", "semantic_similarity"},
|
|
{"model", "m"},
|
|
{"reference_phrases", {{"coding", json::array({"x"})}}},
|
|
{"default_label", "math"}});
|
|
}));
|
|
check("make_classifier clearly rejects llm for now",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "router"}, {"type", "llm"},
|
|
{"model", "m"}, {"prompt", "choose"}});
|
|
}));
|
|
check("make_classifier clearly rejects reserved presets for now",
|
|
throws_invalid_arg([] {
|
|
lemon::make_classifier(json{{"id", "pii"}, {"type", "pii_detection"},
|
|
{"model", "m"}});
|
|
}));
|
|
}
|
|
|
|
static void test_make_classifiers() {
|
|
json configs = json::array({
|
|
json{{"id", "pii"}, {"type", "classifier"}, {"model", "pii-detector-small"}},
|
|
json{{"id", "jailbreak"}, {"type", "classifier"}, {"model", "jailbreak-detector-small"}},
|
|
});
|
|
auto classifiers = lemon::make_classifiers(configs);
|
|
check("make_classifiers builds classifier map", classifiers.size() == 2 &&
|
|
classifiers.count("pii") == 1 && classifiers.count("jailbreak") == 1);
|
|
|
|
json dupes = json::array({
|
|
json{{"id", "pii"}, {"type", "classifier"}, {"model", "a"}},
|
|
json{{"id", "pii"}, {"type", "classifier"}, {"model", "b"}},
|
|
});
|
|
check("make_classifiers rejects duplicate ids",
|
|
throws_invalid_arg([&] { lemon::make_classifiers(dupes); }));
|
|
}
|
|
|
|
static void test_leaf_factory_classifier_refs() {
|
|
auto classifier = lemon::make_classifier(json{
|
|
{"id", "pii"},
|
|
{"type", "classifier"},
|
|
{"model", "pii-detector-small"},
|
|
{"labels", json::array({"PII", "NO_PII"})},
|
|
{"default_label", "PII"},
|
|
});
|
|
std::map<std::string, lemon::ClassifierPtr> classifiers = {{"pii", classifier}};
|
|
auto leaf_factory = lemon::make_leaf_factory(classifiers);
|
|
|
|
lemon::testing::FakeClassifierServices fake;
|
|
fake.set_classifier_scores("pii-detector-small", {{"PII", 0.88}, {"NO_PII", 0.12}});
|
|
auto services = fake.make();
|
|
auto route = make_route_context();
|
|
EvalContext ctx = make_eval_context(route, services, true);
|
|
auto condition = leaf_factory(json{{"classifier", "pii"}});
|
|
check("leaf factory uses classifier default_label", condition->evaluate(ctx) &&
|
|
ctx.trace.size() == 1 && ctx.trace[0].score.has_value() &&
|
|
*ctx.trace[0].score == 0.88);
|
|
|
|
check("leaf factory rejects dangling classifier ref",
|
|
throws_invalid_arg([&] { leaf_factory(json{{"classifier", "missing"}}); }));
|
|
check("leaf factory rejects dangling label ref",
|
|
throws_invalid_arg([&] {
|
|
leaf_factory(json{{"classifier", "pii"}, {"label", "SECRET"}});
|
|
}));
|
|
|
|
auto no_default = lemon::make_classifier(json{
|
|
{"id", "ambiguous"},
|
|
{"type", "classifier"},
|
|
{"model", "pii-detector-small"},
|
|
{"labels", json::array({"PII", "NO_PII"})},
|
|
});
|
|
check("leaf factory rejects omitted label without default_label",
|
|
throws_invalid_arg([&] {
|
|
auto lf = lemon::make_leaf_factory({{"ambiguous", no_default}});
|
|
lf(json{{"classifier", "ambiguous"}});
|
|
}));
|
|
}
|
|
|
|
static void test_leaf_factory_label_less_primary() {
|
|
// A `classifier` with no declared labels: a condition may omit `label`, and
|
|
// selected_score() falls back to Score::primary().
|
|
auto label_less = lemon::make_classifier(json{
|
|
{"id", "toxicity"},
|
|
{"type", "classifier"},
|
|
{"model", "toxicity-small"},
|
|
});
|
|
auto leaf_factory = lemon::make_leaf_factory({{"toxicity", label_less}});
|
|
auto route = make_route_context();
|
|
|
|
// Single-label score: primary() reads the lone entry, so the band applies.
|
|
{
|
|
lemon::testing::FakeClassifierServices fake;
|
|
fake.set_classifier_scores("toxicity-small", {{"toxic", 0.91}});
|
|
auto services = fake.make();
|
|
EvalContext ctx = make_eval_context(route, services);
|
|
auto condition = leaf_factory(json{{"classifier", "toxicity"}, {"min_score", 0.5}});
|
|
check("label-less classifier matches via primary() lone entry",
|
|
condition->evaluate(ctx));
|
|
}
|
|
|
|
// Multi-label score: primary() returns 0.0 rather than an arbitrary label,
|
|
// so an unlabeled condition does not silently match the first label.
|
|
{
|
|
lemon::testing::FakeClassifierServices fake;
|
|
fake.set_classifier_scores("toxicity-small", {{"toxic", 0.91}, {"clean", 0.09}});
|
|
auto services = fake.make();
|
|
EvalContext ctx = make_eval_context(route, services);
|
|
auto condition = leaf_factory(json{{"classifier", "toxicity"}, {"min_score", 0.5}});
|
|
check("label-less classifier does not match on a multi-label score",
|
|
!condition->evaluate(ctx));
|
|
}
|
|
}
|
|
|
|
static void test_leaf_factory_deterministic_dispatch_and_implicit_all() {
|
|
int keywords_calls = 0;
|
|
int chars_calls = 0;
|
|
NamedLeafFactories factories;
|
|
factories["keywords_any"] = [&](const json& leaf) -> ConditionPtr {
|
|
check("deterministic factory receives isolated keywords leaf",
|
|
leaf.size() == 1 && leaf.contains("keywords_any"));
|
|
return std::make_shared<ConstCondition>(true, &keywords_calls, "keywords_any");
|
|
};
|
|
factories["max_chars"] = [&](const json& leaf) -> ConditionPtr {
|
|
check("deterministic factory receives isolated max_chars leaf",
|
|
leaf.size() == 1 && leaf.contains("max_chars"));
|
|
return std::make_shared<ConstCondition>(true, &chars_calls, "max_chars");
|
|
};
|
|
|
|
auto leaf_factory = lemon::make_leaf_factory({}, factories);
|
|
auto condition = leaf_factory(json{{"keywords_any", json::array({"return"})}, {"max_chars", 1000}});
|
|
|
|
lemon::testing::FakeClassifierServices fake;
|
|
auto services = fake.make();
|
|
auto route = make_route_context();
|
|
EvalContext ctx = make_eval_context(route, services, true);
|
|
check("multi-key deterministic leaf composes as implicit all", condition->evaluate(ctx) &&
|
|
keywords_calls == 1 && chars_calls == 1 && ctx.trace.size() == 2);
|
|
|
|
check("leaf factory rejects unknown leaf",
|
|
throws_invalid_arg([&] { leaf_factory(json{{"unknown", true}}); }));
|
|
check("classifier-only fields require classifier",
|
|
throws_invalid_arg([&] { leaf_factory(json{{"min_score", 0.5}}); }));
|
|
}
|
|
|
|
static void test_leaf_factory_classifier_and_deterministic_implicit_all() {
|
|
auto classifier = lemon::make_classifier(json{
|
|
{"id", "pii"},
|
|
{"type", "classifier"},
|
|
{"model", "pii-detector-small"},
|
|
{"labels", json::array({"PII", "NO_PII"})},
|
|
{"default_label", "PII"},
|
|
});
|
|
|
|
int max_chars_calls = 0;
|
|
NamedLeafFactories factories;
|
|
factories["max_chars"] = [&](const json&) -> ConditionPtr {
|
|
return std::make_shared<ConstCondition>(true, &max_chars_calls, "max_chars");
|
|
};
|
|
|
|
auto leaf_factory = lemon::make_leaf_factory({{"pii", classifier}}, factories);
|
|
auto condition = leaf_factory(json{{"classifier", "pii"}, {"min_score", 0.5}, {"max_chars", 1000}});
|
|
|
|
lemon::testing::FakeClassifierServices fake;
|
|
fake.set_classifier_scores("pii-detector-small", {{"PII", 0.75}, {"NO_PII", 0.25}});
|
|
auto services = fake.make();
|
|
auto route = make_route_context();
|
|
EvalContext ctx = make_eval_context(route, services, true);
|
|
check("classifier plus deterministic leaf composes as implicit all",
|
|
condition->evaluate(ctx) && max_chars_calls == 1 && ctx.trace.size() == 2);
|
|
}
|
|
|
|
int main() {
|
|
test_make_classifier();
|
|
test_make_zero_shot_classifier();
|
|
test_make_zero_shot_classifier_rejections();
|
|
test_zero_shot_classifier_without_service_fails_soft();
|
|
test_zero_shot_classifier_rejects_mismatched_label_set();
|
|
test_make_systemone_classifier();
|
|
test_systemone_choice_array_keeps_option_order();
|
|
test_systemone_classifier_rejects_mismatched_answer();
|
|
test_make_systemone_classifier_rejections();
|
|
test_systemone_on_truncation();
|
|
test_zero_shot_classifier_reports_truncation();
|
|
test_zero_shot_on_truncation();
|
|
test_on_truncation_rejections();
|
|
test_label_list_rejections();
|
|
test_make_classifier_rejections();
|
|
test_make_classifiers();
|
|
test_leaf_factory_classifier_refs();
|
|
test_leaf_factory_label_less_primary();
|
|
test_leaf_factory_deterministic_dispatch_and_implicit_all();
|
|
test_leaf_factory_classifier_and_deterministic_implicit_all();
|
|
|
|
std::printf("\n%s\n", g_failures == 0 ? "ALL REGISTRY TESTS PASSED"
|
|
: "REGISTRY TESTS FAILED");
|
|
return g_failures == 0 ? 0 : 1;
|
|
}
|