From 5f696e88e0eb490c8028421d3c5419df9dcf5385 Mon Sep 17 00:00:00 2001 From: R0CKSTAR Date: Thu, 3 Apr 2025 19:51:35 +0800 Subject: [PATCH] sync : minja (inclusionAI/Ling) and update tests (#12699) Signed-off-by: Xiaodong Ye --- common/minja/minja.hpp | 20 ++++++++++++-------- tests/test-chat-template.cpp | 8 ++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/common/minja/minja.hpp b/common/minja/minja.hpp index 1a30499e7..c4f40b17e 100644 --- a/common/minja/minja.hpp +++ b/common/minja/minja.hpp @@ -2606,14 +2606,18 @@ inline std::shared_ptr Context::builtins() { auto & text = args.at("text"); return text.is_null() ? text : Value(strip(text.get())); })); - globals.set("lower", simple_function("lower", { "text" }, [](const std::shared_ptr &, Value & args) { - auto text = args.at("text"); - if (text.is_null()) return text; - std::string res; - auto str = text.get(); - std::transform(str.begin(), str.end(), std::back_inserter(res), ::tolower); - return Value(res); - })); + auto char_transform_function = [](const std::string & name, const std::function & fn) { + return simple_function(name, { "text" }, [=](const std::shared_ptr &, Value & args) { + auto text = args.at("text"); + if (text.is_null()) return text; + std::string res; + auto str = text.get(); + std::transform(str.begin(), str.end(), std::back_inserter(res), fn); + return Value(res); + }); + }; + globals.set("lower", char_transform_function("lower", ::tolower)); + globals.set("upper", char_transform_function("upper", ::toupper)); globals.set("default", Value::callable([=](const std::shared_ptr &, ArgumentsValue & args) { args.expectArgs("default", {2, 3}, {0, 1}); auto & value = args.args[0]; diff --git a/tests/test-chat-template.cpp b/tests/test-chat-template.cpp index 894d83714..a9627df68 100644 --- a/tests/test-chat-template.cpp +++ b/tests/test-chat-template.cpp @@ -278,6 +278,14 @@ int main(void) { /* .bos_token= */ "", /* .eos_token= */ "", }, + { + /* .name= */ "inclusionAI/Ling-lite", + /* .template_str */ "{% for message in messages %}{% set role = message['role'] | lower %}{% if role == 'user' %}{% set role = 'HUMAN' %}{% endif %}{% set role = role | upper %}{{ '' + role + '' + message['content'] }}{% endfor %}{% if add_generation_prompt %}{{ 'ASSISTANT' }}{% endif %}", + /* .expected_output= */ "SYSTEMYou are a helpful assistantHUMANHelloASSISTANTHi thereHUMANWho are youASSISTANT I am an assistant HUMANAnother questionASSISTANT", + /* .expected_output_jinja= */ "", + /* .bos_token= */ "", + /* .eos_token= */ "", + }, }; std::vector formatted_chat(1024); int32_t res;