Skip to content

LLM inference in C/C++

C++ ggml

jinja : add count/d/e filter aliases (#24606)

Sigbjørn Skjæretacd79d603cb2
diff --git a/common/jinja/runtime.cpp b/common/jinja/runtime.cpp
index 8d8d80b22c..1fae7884e1 100644
--- a/common/jinja/runtime.cpp
+++ b/common/jinja/runtime.cpp
@@ -316,12 +316,22 @@ value filter_expression::execute_impl(context & ctx) {
JJ_DEBUG("Applying filter to %s", input->type().c_str());
+ auto set_filter_alias = [](auto & filter_id) {
+ if (filter_id == "count") {
+ filter_id = "length";
+ } else if (filter_id == "d") {
+ filter_id = "default";
+ } else if (filter_id == "e") {
+ filter_id = "escape";
+ } else if (filter_id == "trim") {
+ filter_id = "strip";
+ }
+ };
+
if (is_stmt<identifier>(filter)) {
auto filter_id = cast_stmt<identifier>(filter)->val;
- if (filter_id == "trim") {
- filter_id = "strip"; // alias
- }
+ set_filter_alias(filter_id);
JJ_DEBUG("Applying filter '%s' to %s", filter_id.c_str(), input->type().c_str());
// TODO: Refactor filters so this coercion can be done automatically
if (!input->is_undefined() && !is_val<value_string>(input) && (
@@ -345,9 +355,7 @@ value filter_expression::execute_impl(context & ctx) {
}
auto filter_id = cast_stmt<identifier>(call->callee)->val;
- if (filter_id == "trim") {
- filter_id = "strip"; // alias
- }
+ set_filter_alias(filter_id);
JJ_DEBUG("Applying filter '%s' with arguments to %s", filter_id.c_str(), input->type().c_str());
func_args args(ctx);
for (const auto & arg_expr : call->args) {
diff --git a/tests/test-jinja.cpp b/tests/test-jinja.cpp
index 7bf6791b0a..8039956246 100644
--- a/tests/test-jinja.cpp
+++ b/tests/test-jinja.cpp
@@ -601,8 +601,8 @@ static void test_filters(testing & t) {
"hello jinja"
);
- test_template(t, "length list",
- "{{ items|length }}",
+ test_template(t, "length (count alias) list",
+ "{{ items|count }}",
{{"items", json::array({1, 2, 3})}},
"3"
);
@@ -711,8 +711,8 @@ static void test_filters(testing & t) {
"fallback"
);
- test_template(t, "default with falsy value",
- "{{ ''|default('fallback', true) }}",
+ test_template(t, "default (d alias) with falsy value",
+ "{{ ''|d('fallback', true) }}",
json::object(),
"fallback"
);