Skip to content

Commit 5f696e8

Browse files
authored
sync : minja (inclusionAI/Ling) and update tests (#12699)
Signed-off-by: Xiaodong Ye <[email protected]>
1 parent 193c3e0 commit 5f696e8

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

common/minja/minja.hpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,14 +2606,18 @@ inline std::shared_ptr<Context> Context::builtins() {
26062606
auto & text = args.at("text");
26072607
return text.is_null() ? text : Value(strip(text.get<std::string>()));
26082608
}));
2609-
globals.set("lower", simple_function("lower", { "text" }, [](const std::shared_ptr<Context> &, Value & args) {
2610-
auto text = args.at("text");
2611-
if (text.is_null()) return text;
2612-
std::string res;
2613-
auto str = text.get<std::string>();
2614-
std::transform(str.begin(), str.end(), std::back_inserter(res), ::tolower);
2615-
return Value(res);
2616-
}));
2609+
auto char_transform_function = [](const std::string & name, const std::function<char(char)> & fn) {
2610+
return simple_function(name, { "text" }, [=](const std::shared_ptr<Context> &, Value & args) {
2611+
auto text = args.at("text");
2612+
if (text.is_null()) return text;
2613+
std::string res;
2614+
auto str = text.get<std::string>();
2615+
std::transform(str.begin(), str.end(), std::back_inserter(res), fn);
2616+
return Value(res);
2617+
});
2618+
};
2619+
globals.set("lower", char_transform_function("lower", ::tolower));
2620+
globals.set("upper", char_transform_function("upper", ::toupper));
26172621
globals.set("default", Value::callable([=](const std::shared_ptr<Context> &, ArgumentsValue & args) {
26182622
args.expectArgs("default", {2, 3}, {0, 1});
26192623
auto & value = args.args[0];

tests/test-chat-template.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,14 @@ int main(void) {
278278
/* .bos_token= */ "",
279279
/* .eos_token= */ "",
280280
},
281+
{
282+
/* .name= */ "inclusionAI/Ling-lite",
283+
/* .template_str */ "{% for message in messages %}{% set role = message['role'] | lower %}{% if role == 'user' %}{% set role = 'HUMAN' %}{% endif %}{% set role = role | upper %}{{ '<role>' + role + '</role>' + message['content'] }}{% endfor %}{% if add_generation_prompt %}{{ '<role>ASSISTANT</role>' }}{% endif %}",
284+
/* .expected_output= */ "<role>SYSTEM</role>You are a helpful assistant<role>HUMAN</role>Hello<role>ASSISTANT</role>Hi there<role>HUMAN</role>Who are you<role>ASSISTANT</role> I am an assistant <role>HUMAN</role>Another question<role>ASSISTANT</role>",
285+
/* .expected_output_jinja= */ "",
286+
/* .bos_token= */ "",
287+
/* .eos_token= */ "",
288+
},
281289
};
282290
std::vector<char> formatted_chat(1024);
283291
int32_t res;

0 commit comments

Comments
 (0)