Skip to content

LLM inference in C/C++

C++ ggml

jinja : support noncall test statements with arg (#29443)

Sigbjørn Skjæreta1de614ba37d
diff --git a/common/jinja/parser.cpp b/common/jinja/parser.cpp
index 1ddceede91..4ad5bb9702 100644
--- a/common/jinja/parser.cpp
+++ b/common/jinja/parser.cpp
@@ -429,8 +429,15 @@ private:
bool negate = false;
if (is_identifier("not")) { ++current; negate = true; }
auto test_id = parse_primary_expression();
- // FIXME: tests can also be expressed like this: if x is eq 3
- if (is(token::open_paren)) test_id = parse_call_expression(std::move(test_id));
+ if (is(token::open_paren)) {
+ test_id = parse_call_expression(std::move(test_id));
+ } else if (is(token::numeric_literal) || is(token::string_literal) || is(token::open_curly_bracket) || is(token::open_square_bracket) ||
+ (is(token::identifier) && !is_identifier("and") && !is_identifier("or") && !is_identifier("else"))) {
+ size_t call_pos = current;
+ statements args;
+ args.push_back(std::move(parse_unary_expression()));
+ test_id = mk_stmt<call_expression>(call_pos, std::move(test_id), std::move(args));
+ }
operand = mk_stmt<test_expression>(start_pos, std::move(operand), negate, std::move(test_id));
}
return operand;
diff --git a/tests/test-jinja.cpp b/tests/test-jinja.cpp
index cd8fa723ca..064f5927ba 100644
--- a/tests/test-jinja.cpp
+++ b/tests/test-jinja.cpp
@@ -1153,7 +1153,7 @@ static void test_tests(testing & t) {
);
test_template(t, "is not equalto",
- "{{ 'yes' if 3 is not equalto(4) }}",
+ "{{ 'yes' if 3 is not equalto 4 }}",
json::object(),
"yes"
);
@@ -1165,7 +1165,7 @@ static void test_tests(testing & t) {
);
test_template(t, "is gt",
- "{{ 'yes' if 3 is gt(2) }}",
+ "{{ 'yes' if 3 is gt 2 }}",
json::object(),
"yes"
);
@@ -1177,7 +1177,7 @@ static void test_tests(testing & t) {
);
test_template(t, "is lt",
- "{{ 'yes' if 2 is lt(3) }}",
+ "{{ 'yes' if 2 is lt 3 }}",
json::object(),
"yes"
);
@@ -1194,6 +1194,12 @@ static void test_tests(testing & t) {
"yes"
);
+ test_template(t, "is lt and gt",
+ "{{ 'yes' if x is lt 3 and x is gt 1 }}",
+ {{"x", 2}},
+ "yes"
+ );
+
test_template(t, "is lower",
"{{ 'yes' if 'lowercase' is lower }}",
json::object(),
@@ -1248,6 +1254,12 @@ static void test_tests(testing & t) {
"yes"
);
+ test_template(t, "is integer or float",
+ "{{ 'yes' if x.y is integer or x.y is float else 'no' }}",
+ {{"x", {{"y", 1.1}}}},
+ "yes"
+ );
+
test_template(t, "is sequence",
"{{ 'yes' if x is sequence }}",
{{"x", json::array({1, 2, 3})}},