Skip to content

Commit 3489ef0

Browse files
committed
fix: Ensure image renders before text in chat formats regardless of message content order.
1 parent d03f15b commit 3489ef0

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

llama_cpp/llama_chat_format.py

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,19 +2175,25 @@ class Llava15ChatHandler:
21752175
"{% if message.role == 'user' %}"
21762176
"{% if message.content is string %}"
21772177
"\nUSER: {{ message.content }}"
2178-
"{% elif message.content is iterable %}"
2178+
"{% endif %}"
2179+
"{% if message.content is iterable %}"
21792180
"\nUSER: "
2181+
21802182
"{% for content in message.content %}"
2181-
"{% if content.type == 'text' %}"
2182-
"{{ content.text }}"
2183-
"{% endif %}"
21842183
"{% if content.type == 'image_url' and content.image_url is string %}"
21852184
"{{ content.image_url }}"
21862185
"{% endif %}"
21872186
"{% if content.type == 'image_url' and content.image_url is mapping %}"
21882187
"{{ content.image_url.url }}"
21892188
"{% endif %}"
21902189
"{% endfor %}"
2190+
2191+
"{% for content in message.content %}"
2192+
"{% if content.type == 'text' %}"
2193+
"{{ content.text }}"
2194+
"{% endif %}"
2195+
"{% endfor %}"
2196+
21912197
"{% endif %}"
21922198
"{% endif %}"
21932199
"{% if message.role == 'assistant' and message.content is not none %}"
@@ -2575,14 +2581,22 @@ class ObsidianChatHandler(Llava15ChatHandler):
25752581
"{{ message.content }}"
25762582
"{% endif %}"
25772583
"{% if message.content is iterable %}"
2584+
2585+
"{% for content in message.content %}"
2586+
"{% if content.type == 'image_url' and content.image_url is string %}"
2587+
"{{ content.image_url }}"
2588+
"{% endif %}"
2589+
"{% if content.type == 'image_url' and content.image_url is mapping %}"
2590+
"{{ content.image_url.url }}"
2591+
"{% endif %}"
2592+
"{% endfor %}"
2593+
25782594
"{% for content in message.content %}"
25792595
"{% if content.type == 'text' %}"
25802596
"{{ content.text }}"
25812597
"{% endif %}"
2582-
"{% if content.type == 'image_url' %}"
2583-
"{{ content.image_url }}"
2584-
"{% endif %}"
25852598
"{% endfor %}"
2599+
25862600
"{% endif %}"
25872601
"###\n"
25882602
"{% endif %}"
@@ -2606,9 +2620,9 @@ class MoondreamChatHandler(Llava15ChatHandler):
26062620
"{% for message in messages %}"
26072621
"{% if message.role == 'user' %}"
26082622
"{% if message.content is iterable %}"
2609-
"{% for content in message.content %}"
26102623

26112624
# <image>
2625+
"{% for content in message.content %}"
26122626
"{% if content.type == 'image_url' %}"
26132627
"{% if content.image_url is string %}"
26142628
"{{ content.image_url }}\n\n"
@@ -2617,12 +2631,15 @@ class MoondreamChatHandler(Llava15ChatHandler):
26172631
"{{ content.image_url.url }}\n\n"
26182632
"{% endif %}"
26192633
"{% endif %}"
2634+
"{% endfor %}"
26202635

26212636
# Question:
2637+
"{% for content in message.content %}"
26222638
"{% if content.type == 'text' %}"
26232639
"Question: {{ content.text }}\n\n"
26242640
"{% endif %}"
26252641
"{% endfor %}"
2642+
26262643
"{% endif %}"
26272644

26282645
# Question:
@@ -2657,9 +2674,9 @@ class Llava16ChatHandler(Llava15ChatHandler):
26572674
"{% endif %}"
26582675
"{% if message.role == 'user' %}"
26592676
"{% if message.content is iterable %}"
2660-
"{% for content in message.content %}"
26612677

26622678
# <image>
2679+
"{% for content in message.content %}"
26632680
"{% if content.type == 'image_url' %}"
26642681
"{% if content.image_url is string %}"
26652682
"{{ content.image_url }}\n"
@@ -2668,12 +2685,15 @@ class Llava16ChatHandler(Llava15ChatHandler):
26682685
"{{ content.image_url.url }}\n"
26692686
"{% endif %}"
26702687
"{% endif %}"
2688+
"{% endfor %}"
26712689

26722690
# Question:
2691+
"{% for content in message.content %}"
26732692
"{% if content.type == 'text' %}"
26742693
"{{ content.text }}"
26752694
"{% endif %}"
26762695
"{% endfor %}"
2696+
26772697
"{% endif %}"
26782698

26792699
# Question:
@@ -2719,14 +2739,22 @@ class NanoLlavaChatHandler(Llava15ChatHandler):
27192739
"{{ message.content }}"
27202740
"{% endif %}"
27212741
"{% if message.content is iterable %}"
2742+
2743+
"{% for content in message.content %}"
2744+
"{% if content.type == 'image_url' and content.image_url is string %}"
2745+
"{{ content.image_url }}"
2746+
"{% endif %}"
2747+
"{% if content.type == 'image_url' and content.image_url is mapping %}"
2748+
"{{ content.image_url.url }}"
2749+
"{% endif %}"
2750+
"{% endfor %}"
2751+
27222752
"{% for content in message.content %}"
27232753
"{% if content.type == 'text' %}"
27242754
"{{ content.text }}"
27252755
"{% endif %}"
2726-
"{% if content.type == 'image_url' %}"
2727-
"{{ content.image_url }}"
2728-
"{% endif %}"
27292756
"{% endfor %}"
2757+
27302758
"{% endif %}"
27312759
"<|im_end|>"
27322760
"{% endif %}"

0 commit comments

Comments
 (0)