Skip to content

Commit a1d18d5

Browse files
committed
feat(parser): support implicit string concatenation
1 parent 4dcded3 commit a1d18d5

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

jinja.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1873,7 +1873,12 @@ class Parser {
18731873
}
18741874

18751875
if (check(Token::String)) {
1876-
return make_unique<LiteralExpr>(advance().value);
1876+
// Support implicit string concatenation like Python: 'a' 'b' -> 'ab'
1877+
std::string result = advance().value;
1878+
while (check(Token::String)) {
1879+
result += advance().value;
1880+
}
1881+
return make_unique<LiteralExpr>(result);
18771882
}
18781883
if (check(Token::Identifier)) {
18791884
std::string name = advance().value;

tests/test_chat_template.json

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10707,5 +10707,125 @@
1070710707
"expected": "<|hy_begin▁of▁sentence|><|hy_User|><|hy_Assistant|><|hy_place▁holder▁no▁2|><|hy_place▁holder▁no▁8|>"
1070810708
}
1070910709
]
10710+
},
10711+
"google/translategemma-4b-it": {
10712+
"template": "{%- set languages = {\n \"aa\": \"Afar\",\n \"en\": \"English\",\n \"zh\": \"Chinese\",\n \"zh-Hans\": \"Chinese\",\n \"zh-Hant\": \"Chinese\",\n \"ja\": \"Japanese\",\n \"ko\": \"Korean\",\n \"fr\": \"French\",\n \"de\": \"German\",\n \"es\": \"Spanish\",\n \"ru\": \"Russian\",\n \"ar\": \"Arabic\",\n}\n-%}\n{{ bos_token }}\n{%- if (messages[0]['role'] \\!= 'user') -%}\n {{ raise_exception(\"Conversations must start with a user prompt.\") }}\n{%- endif -%}\n{%- for message in messages -%}\n {%- if (message['role'] == 'user') \\!= (loop.index0 % 2 == 0) -%}\n {{ raise_exception(\"Conversation roles must alternate user/assistant/user/assistant/...\") }}\n {%- endif -%}\n {%- if (message['role'] == 'assistant') -%}\n {%- if message['content'] is none or message['content'] is not string -%}\n {{ raise_exception(\"Assistant role must provide content as a string\") }}\n {%- endif -%}\n {{ '<start_of_turn>model\n'}}\n {{ message[\"content\"] | trim }}\n {%- elif (message['role'] == 'user') -%}\n {%- if message['content'] is none or message['content'] is not iterable or message['content'] | length \\!= 1 -%}\n {{ raise_exception(\n \"User role must provide content as an iterable with exactly one item.\"\n ) }}\n {%- endif -%}\n {%- set content = message[\"content\"][0] -%}\n {%- set source_lang_code = content[\"source_lang_code\"] | replace(\"_\", \"-\") -%}\n {%- set source_lang = languages[source_lang_code] -%}\n {%- set target_lang_code = content[\"target_lang_code\"] | replace(\"_\", \"-\") -%}\n {%- set target_lang = languages[target_lang_code] -%}\n {{ '<start_of_turn>user\nYou are a professional ' + source_lang + ' (' + source_lang_code + ') to ' +\n target_lang + ' (' + target_lang_code + ') translator. Your goal is to accurately convey the meaning and '\n 'nuances of the original ' + source_lang + ' text while adhering to ' + target_lang + ' grammar, '\n 'vocabulary, and cultural sensitivities.\n'\n }}\n {%- if content[\"type\"] == 'text' -%}\n {{\n 'Produce only the ' + target_lang + ' translation, without any additional explanations or ' +\n 'commentary. Please translate the following ' + source_lang + ' text into ' + target_lang + ':\n\n\n' +\n content[\"text\"] | trim\n }}\n {%- elif content[\"type\"] == 'image' -%}\n {{\n 'Please translate the ' + source_lang + ' text in the provided image into ' + target_lang + '. ' +\n 'Produce only the ' + target_lang + ' translation, without any additional explanations, ' +\n 'alternatives or commentary. Focus only on the text, do not output where the text is located, ' +\n 'surrounding objects or any other explanation about the picture. Ignore symbols, pictogram, and ' +\n 'arrows\\!\n\n\n<start_of_image>'\n }}\n {%- else -%}\n {{ raise_exception(\"User role content can contain either text or image data.\") }}\n {%- endif -%}\n {%- else -%}\n {{ raise_exception(\"Conversations must only contain user or assistant roles.\") }}\n {%- endif -%}\n {{ '<end_of_turn>\n' }}\n{%- endfor -%}\n{%- if add_generation_prompt -%}\n {{'<start_of_turn>model\n'}}\n{%- endif -%}\n",
10713+
"special_tokens": {
10714+
"bos_token": "<bos>",
10715+
"eos_token": "<eos>",
10716+
"unk_token": "<unk>",
10717+
"pad_token": "<pad>"
10718+
},
10719+
"cases": [
10720+
{
10721+
"description": "basic_en_to_zh_translation",
10722+
"messages": [
10723+
{
10724+
"role": "user",
10725+
"content": [
10726+
{
10727+
"type": "text",
10728+
"source_lang_code": "en",
10729+
"target_lang_code": "zh",
10730+
"text": "Hello, how are you?"
10731+
}
10732+
]
10733+
}
10734+
],
10735+
"add_generation_prompt": true,
10736+
"expected": "<bos><start_of_turn>user\nYou are a professional English (en) to Chinese (zh) translator. Your goal is to accurately convey the meaning and nuances of the original English text while adhering to Chinese grammar, vocabulary, and cultural sensitivities.\nProduce only the Chinese translation, without any additional explanations or commentary. Please translate the following English text into Chinese:\n\n\nHello, how are you?<end_of_turn>\n<start_of_turn>model\n"
10737+
},
10738+
{
10739+
"description": "en_to_ja_translation",
10740+
"messages": [
10741+
{
10742+
"role": "user",
10743+
"content": [
10744+
{
10745+
"type": "text",
10746+
"source_lang_code": "en",
10747+
"target_lang_code": "ja",
10748+
"text": "Good morning\\!"
10749+
}
10750+
]
10751+
}
10752+
],
10753+
"add_generation_prompt": true,
10754+
"expected": "<bos><start_of_turn>user\nYou are a professional English (en) to Japanese (ja) translator. Your goal is to accurately convey the meaning and nuances of the original English text while adhering to Japanese grammar, vocabulary, and cultural sensitivities.\nProduce only the Japanese translation, without any additional explanations or commentary. Please translate the following English text into Japanese:\n\n\nGood morning\\!<end_of_turn>\n<start_of_turn>model\n"
10755+
},
10756+
{
10757+
"description": "zh_to_en_translation",
10758+
"messages": [
10759+
{
10760+
"role": "user",
10761+
"content": [
10762+
{
10763+
"type": "text",
10764+
"source_lang_code": "zh-Hans",
10765+
"target_lang_code": "en",
10766+
"text": "你好,世界!"
10767+
}
10768+
]
10769+
}
10770+
],
10771+
"add_generation_prompt": true,
10772+
"expected": "<bos><start_of_turn>user\nYou are a professional Chinese (zh-Hans) to English (en) translator. Your goal is to accurately convey the meaning and nuances of the original Chinese text while adhering to English grammar, vocabulary, and cultural sensitivities.\nProduce only the English translation, without any additional explanations or commentary. Please translate the following Chinese text into English:\n\n\n你好,世界!<end_of_turn>\n<start_of_turn>model\n"
10773+
},
10774+
{
10775+
"description": "translation_with_assistant_response",
10776+
"messages": [
10777+
{
10778+
"role": "user",
10779+
"content": [
10780+
{
10781+
"type": "text",
10782+
"source_lang_code": "en",
10783+
"target_lang_code": "zh",
10784+
"text": "Thank you"
10785+
}
10786+
]
10787+
},
10788+
{
10789+
"role": "assistant",
10790+
"content": "谢谢"
10791+
}
10792+
],
10793+
"add_generation_prompt": false,
10794+
"expected": "<bos><start_of_turn>user\nYou are a professional English (en) to Chinese (zh) translator. Your goal is to accurately convey the meaning and nuances of the original English text while adhering to Chinese grammar, vocabulary, and cultural sensitivities.\nProduce only the Chinese translation, without any additional explanations or commentary. Please translate the following English text into Chinese:\n\n\nThank you<end_of_turn>\n<start_of_turn>model\n\n 谢谢<end_of_turn>\n"
10795+
},
10796+
{
10797+
"description": "multi_turn_translation",
10798+
"messages": [
10799+
{
10800+
"role": "user",
10801+
"content": [
10802+
{
10803+
"type": "text",
10804+
"source_lang_code": "en",
10805+
"target_lang_code": "zh",
10806+
"text": "Hello"
10807+
}
10808+
]
10809+
},
10810+
{
10811+
"role": "assistant",
10812+
"content": "你好"
10813+
},
10814+
{
10815+
"role": "user",
10816+
"content": [
10817+
{
10818+
"type": "text",
10819+
"source_lang_code": "en",
10820+
"target_lang_code": "zh",
10821+
"text": "Goodbye"
10822+
}
10823+
]
10824+
}
10825+
],
10826+
"add_generation_prompt": true,
10827+
"expected": "<bos><start_of_turn>user\nYou are a professional English (en) to Chinese (zh) translator. Your goal is to accurately convey the meaning and nuances of the original English text while adhering to Chinese grammar, vocabulary, and cultural sensitivities.\nProduce only the Chinese translation, without any additional explanations or commentary. Please translate the following English text into Chinese:\n\n\nHello<end_of_turn>\n<start_of_turn>model\n\n 你好<end_of_turn>\n<start_of_turn>user\nYou are a professional English (en) to Chinese (zh) translator. Your goal is to accurately convey the meaning and nuances of the original English text while adhering to Chinese grammar, vocabulary, and cultural sensitivities.\nProduce only the Chinese translation, without any additional explanations or commentary. Please translate the following English text into Chinese:\n\n\nGoodbye<end_of_turn>\n<start_of_turn>model\n"
10828+
}
10829+
]
1071010830
}
1071110831
}

0 commit comments

Comments
 (0)