File size: 7,654 Bytes
975dd62 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | {%- set ns_img = namespace(count=0) %}
{%- set ns_vid = namespace(count=0) %}
{%- if tools %}
{{- '<|im_start|>system\n' }}
{%- if messages[0].role == 'system' %}
{%- if messages[0].content is string %}
{%- if messages[0].content %}
{{- messages[0].content + '\n\n' }}
{%- endif %}
{%- elif messages[0].content is sequence %}
{%- for content_part in messages[0].content %}
{%- if content_part.type == 'text' %}
{{- content_part.text + '\n\n' }}
{%- endif %}
{%- endfor %}
{%- endif %}
{%- endif %}
{{- '# Tools\n\n' }}
{{- 'You may call one or more functions to assist with the user query.\n\n' }}
{{- 'You are provided with function signatures within <tools></tools> XML tags:\n' }}
{{- '<tools>\n' }}
{%- for tool in tools %}
{{- tool | tojson(ensure_ascii=False) }}
{%- endfor %}
{{- '\n</tools>\n\n' }}
{{- 'For each function call, output the function name and arguments within the following XML format:\n' }}
{{- '<tool_call>{function-name}\n' }}
{{- '<arg_key>{arg-key-1}</arg_key>\n' }}
{{- '<arg_value>{arg-value-1}</arg_value>\n' }}
{{- '<arg_key>{arg-key-2}</arg_key>\n' }}
{{- '<arg_value>{arg-value-2}</arg_value>\n' }}
{{- '...\n' }}
{{- '</tool_call><|im_end|>\n' }}
{%- else %}
{%- if messages[0].role == 'system' %}
{{- '<|im_start|>system\n' }}
{%- if messages[0].content is string %}
{{- messages[0].content }}
{%- elif messages[0].content is sequence %}
{%- for content_part in messages[0].content %}
{%- if content_part.type == 'text' %}
{{- content_part.text }}
{%- endif %}
{%- endfor %}
{%- endif %}
{%- if messages[0].content %}
{{- '\n' }}
{%- endif %}
{{- '<|im_end|>\n' }}
{%- endif %}
{%- endif %}
{%- set ns = namespace(last_user_index=-1) %}
{%- for m in messages %}
{%- if m.role == 'user' %}
{%- set ns.last_user_index = loop.index0 %}
{%- endif %}
{%- endfor %}
{%- for message in messages %}
{%- set content = message.content %}
{%- if (message.role == 'system' and not loop.first) %}
{{- '<|im_start|>' + message.role + '\n' }}
{%- if content is string %}
{{- content }}
{%- elif content is sequence %}
{%- for content_part in content %}
{%- if content_part.type == 'text' %}
{{- content_part.text }}
{%- endif %}
{%- endfor %}
{%- endif %}
{{- '<|im_end|>' + '\n' }}
{%- elif message.role == 'user' %}
{{- '<|im_start|>user\n' }}
{%- if message['content'] is string %}
{{- message['content'] + '<|im_end|>\n' }}
{%- elif message['content'] is sequence %}
{%- for content in message['content'] %}
{%- if not loop.first %}
{{- '\n' }}
{%- endif %}
{%- if content['type'] == 'image' or 'image' in content or 'image_url' in content %}
{%- set image_id = 'image_%02d' % ns_img.count %}
{%- set ns_img.count = ns_img.count + 1 %}
{%- set mime_type = content.get('mime_type', 'image/jpeg') %}
{{- '<|mime_start|>{"id": "' + image_id + '", "type": "' + mime_type + '", "filename": "' + content.get('filename', "a.jpg") + '"}<|mime_end|>\n' }}
{{- '<|image_start|><|IMAGE_PAD|><|image_end|>' }}
{%- elif content['type'] == 'video' or 'video' in content or 'video_url' in content %}
{%- set video_id = 'video_%02d' % ns_vid.count %}
{%- set ns_vid.count = ns_vid.count + 1 %}
{%- set mime_type = content.get('mime_type', 'video/mp4') %}
{{- '<|mime_start|>{"id": "' + video_id + '", "type": "' + mime_type + '", "filename": "' + content.get('filename', "a.mp4") + '"}<|mime_end|>\n' }}
{{- '<|video_aux_start|>다음 중 video_duration은 비디오 길이 정보입니다. 참고하여 답변하세요. {"video_duration": ' + (content.get('video_duration') | tojson if content.get('video_duration') else '<|video_duration|>') + '}<|video_aux_end|>\n'}}
{{- '<|video_start|><|VIDEO_PAD|><|video_end|>'}}
{%- elif content['type'] == 'text' %}
{{- content['text'] }}
{%- endif %}
{%- endfor %}
{{- '<|im_end|>\n'}}
{%- endif %}
{%- elif message.role == 'assistant' %}
{{- '<|im_start|>assistant\n' }}
{%- if message.content is string %}
{%- set reasoning_content = '' %}
{%- if message.reasoning_content is string %}
{%- set reasoning_content = message.reasoning_content %}
{%- else %}
{%- if '</think>' in content %}
{%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
{%- set content = content.split('</think>')[-1].lstrip('\n') %}
{%- endif %}
{%- endif %}
{%- if loop.index0 > ns.last_user_index %}
{%- if (loop.index0 > 0 and messages[loop.index0 - 1].role == 'user') or reasoning_content %}
{{- '<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
{%- else %}
{{- content }}
{%- endif %}
{%- else %}
{{- content }}
{%- endif %}
{%- elif message.content is sequence %}
{%- for content_part in message.content %}
{%- if content_part.type == 'text' %}
{{- content_part.text }}
{{- '\n' if not loop.last else '' -}}
{%- endif %}
{%- endfor %}
{%- endif %}
{%- if message.tool_calls %}
{%- for tool_call in message.tool_calls %}
{%- if not loop.first or content %}
{{- '\n' }}
{%- endif %}
{%- if tool_call.function %}
{%- set tool_call = tool_call.function %}
{%- endif %}
{{- '<tool_call>' + tool_call.name + '\n' }}
{%- set _args = tool_call.arguments %}
{%- for k, v in _args.items() %}
{{- '<arg_key>' + k + '</arg_key>\n' }}
{{- '<arg_value>' + (v | tojson(ensure_ascii=False) if v is not string else v) + '</arg_value>\n' }}
{%- endfor %}
{{- '</tool_call>' }}
{%- endfor %}
{%- endif %}
{{- '<|im_end|>\n' }}
{%- elif message.role == 'tool' %}
{%- if loop.first or (messages[loop.index0 - 1].role != 'tool') %}
{{- '<|im_start|>tool' }}
{%- endif %}
{{- '\n<tool_response>' + message.get('name', '') + '\n' }}
{{- content }}
{{- '\n</tool_response>' }}
{%- if loop.last or (messages[loop.index0 + 1].role != 'tool') %}
{{- '<|im_end|>\n' }}
{%- endif %}
{%- endif %}
{%- endfor %}
{%- if add_generation_prompt %}
{{- '<|im_start|>assistant\n<think>\n' }}
{%- if skip_reasoning is defined and skip_reasoning is true %}
{{- '\n</think>\n\n' }}
{%- endif %}
{%- endif %} |