| {%- if messages[0]['role'] == 'system' %} |
| {{- '<|im_start|>system\n' }} |
| {%- if messages[0]['content'] is string %} |
| {{- messages[0]['content'] }} |
| {%- else %} |
| {%- for item in messages[0]['content'] %} |
| {%- if item['type'] == 'text' %} |
| {{- item['text'] }} |
| {%- endif %} |
| {%- endfor %} |
| {%- endif %} |
| {{- '<|im_end|>\n' }} |
| {%- else %} |
| {{- '<|im_start|>system\n你是Qianfan-VL,由百度智能云研发的多模态大语言模型。<|im_end|>\n' }} |
| {%- endif %} |
| {%- set ns = namespace(found_last_user=false, last_query_index=messages|length - 1) %} |
| {%- for message in messages[::-1] %} |
| {%- set index = (messages|length - 1) - loop.index0 %} |
| {%- if not ns.found_last_user and message['role'] == 'user' %} |
| {%- set ns.found_last_user = true %} |
| {%- set ns.last_query_index = index %} |
| {%- endif %} |
| {%- endfor %} |
| {%- for message in messages %} |
| {%- if messages[0]['role'] != 'system' or not loop.first %} |
| {%- if message['role'] == 'user' or (message['role'] == 'system' and not loop.first) %} |
| {%- set append_think = (enable_thinking is defined and enable_thinking and message['role'] == 'user' and loop.index0 == ns.last_query_index) %} |
| {{- '<|im_start|>' + message['role'] + '\n' }} |
| {%- if message['content'] is string %} |
| {{- message['content'] }} |
| {%- else %} |
| {%- for item in message['content'] %} |
| {%- if item['type'] == 'image' %} |
| {{- '<image>\n' }} |
| {%- elif item['type'] == 'video' %} |
| {{- '<video>\n' }} |
| {%- elif item['type'] == 'text' %} |
| {{- item['text'] }} |
| {%- endif %} |
| {%- endfor %} |
| {%- endif %} |
| {%- if append_think %} |
| {{- '<think>' }} |
| {%- endif %} |
| {{- '<|im_end|>\n' }} |
| {%- elif message['role'] == 'assistant' %} |
| {%- if message['content'] is string %} |
| {%- set raw_content = message['content'] %} |
| {%- else %} |
| {%- set content_ns = namespace(raw='') %} |
| {%- for item in message['content'] %} |
| {%- if item['type'] == 'text' %} |
| {%- set content_ns.raw = content_ns.raw + item['text'] %} |
| {%- endif %} |
| {%- endfor %} |
| {%- set raw_content = content_ns.raw %} |
| {%- endif %} |
| {%- set content = raw_content %} |
| {%- set reasoning_content = '' %} |
| {%- if 'reasoning_content' in message and message['reasoning_content'] is not none %} |
| {%- set reasoning_content = message['reasoning_content'] %} |
| {%- elif '</think>' in raw_content %} |
| {%- set content = raw_content.split('</think>')[-1].lstrip('\n') %} |
| {%- set reasoning_content = raw_content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %} |
| {%- endif %} |
| {%- if loop.index0 > ns.last_query_index and reasoning_content %} |
| {{- '<|im_start|>' + message['role'] + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }} |
| {%- else %} |
| {{- '<|im_start|>' + message['role'] + '\n' + content }} |
| {%- endif %} |
| {{- '<|im_end|>\n' }} |
| {%- endif %} |
| {%- endif %} |
| {%- endfor %} |
| {%- if add_generation_prompt %} |
| {{- '<|im_start|>assistant\n' }} |
| {%- endif %} |
| |