Skip to content

Commit 5ac3e94

Browse files
committed
Better Qwen2.5-VL chat template.
1 parent c37132b commit 5ac3e94

File tree

1 file changed

+47
-27
lines changed

1 file changed

+47
-27
lines changed

llama_cpp/llama_chat_format.py

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3465,33 +3465,53 @@ class Qwen25VLChatHandler(Llava15ChatHandler):
34653465
DEFAULT_SYSTEM_MESSAGE = "You are a helpful assistant."
34663466

34673467
CHAT_FORMAT = (
3468-
#"{% set image_count = namespace(value=0) %}"
3469-
#"{% set video_count = namespace(value=0) %}"
3470-
"{% for message in messages %}"
3471-
"{% if loop.first and message['role'] != 'system' %}"
3472-
"<|im_start|>system\n"
3473-
"{{ self.DEFAULT_SYSTEM_MESSAGE }}<|im_end|>\n"
3474-
"{% endif %}"
3475-
"<|im_start|>{{ message['role'] }}\n"
3476-
"{% if message['content'] is string %}"
3477-
"{{ message['content'] }}<|im_end|>\n"
3478-
"{% else %}"
3479-
"{% for content in message['content'] %}"
3480-
"{% if content['type'] == 'image_url' %}"
3481-
"{% if content.image_url is string %}"
3482-
"{{ content.image_url }}"
3483-
"{% else %}"
3484-
"{{ content.image_url.url }}"
3485-
"{% endif %}"
3486-
#"{% set image_count.value = image_count.value + 1 %}"
3487-
"{% elif content['type'] == 'text' %}"
3488-
"{{ content['text'] }}"
3489-
"{% endif %}"
3490-
"{% endfor %}"
3491-
"<|im_end|>\n"
3492-
"{% endif %}"
3493-
"{% endfor %}"
3494-
"<|im_start|>assistant\n"
3468+
"{%- set image_count = namespace(value=0) -%}"
3469+
"{%- for message in messages -%}"
3470+
"{%- if loop.first and message[\"role\"] != \"system\" -%}"
3471+
"{{- \"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n\" -}}"
3472+
"{%- endif -%}"
3473+
"{{- \"<|im_start|>\" -}}"
3474+
"{{- message[\"role\"] -}}"
3475+
"{{- \"\n\" -}}"
3476+
"{%- if message[\"content\"] is string -%}"
3477+
"{{- message[\"content\"] -}}"
3478+
"{{- \"<|im_end|>\n\" -}}"
3479+
"{%- else -%}"
3480+
"{%- for content in message[\"content\"] -%}"
3481+
"{%- if content[\"type\"] == \"image\" or \"image\" in content -%}"
3482+
"{%- set image_count.value = image_count.value + 1 -%}"
3483+
"{%- if add_vision_id -%}"
3484+
"{{- \"Picture \" -}}"
3485+
"{{- image_count.value -}}"
3486+
"{{- \": \" -}}"
3487+
"{%- endif -%}"
3488+
"{{- \"<|vision_start|>\" -}}"
3489+
"{{- content.image -}}"
3490+
"{{- \"<|vision_end|>\" -}}"
3491+
"{%- elif content[\"type\"] == \"image_url\" or \"image_url\" in content -%}"
3492+
"{%- set image_count.value = image_count.value + 1 -%}"
3493+
"{%- if add_vision_id -%}"
3494+
"{{- \"Picture \" -}}"
3495+
"{{- image_count.value -}}"
3496+
"{{- \": \" -}}"
3497+
"{%- endif -%}"
3498+
"{{- \"<|vision_start|>\" -}}"
3499+
"{%- if content.image_url is string -%}"
3500+
"{{- content.image_url -}}"
3501+
"{%- else -%}"
3502+
"{{- content.image_url.url -}}"
3503+
"{%- endif -%}"
3504+
"{{- \"<|vision_end|>\" -}}"
3505+
"{%- elif \"text\" in content -%}"
3506+
"{{- content[\"text\"] -}}"
3507+
"{%- endif -%}"
3508+
"{%- endfor -%}"
3509+
"{{- \"<|im_end|>\n\" -}}"
3510+
"{%- endif -%}"
3511+
"{%- endfor -%}"
3512+
"{%- if add_generation_prompt -%}"
3513+
"{{- \"<|im_start|>assistant\n\" -}}"
3514+
"{%- endif -%}"
34953515
)
34963516

34973517
def __call__(self, **kwargs):

0 commit comments

Comments
 (0)