Modify chat_template.jinja
Browse files- chat_template.jinja +50 -60
chat_template.jinja
CHANGED
|
@@ -2,16 +2,8 @@
|
|
| 2 |
{%- set ns_vid = namespace(count=0) %}
|
| 3 |
{%- if tools %}
|
| 4 |
{{- '<|im_start|>system\n' }}
|
| 5 |
-
{%- if messages[0].role == 'system' %}
|
| 6 |
-
{
|
| 7 |
-
{{- messages[0].content + '\n\n' }}
|
| 8 |
-
{%- elif messages[0].content is sequence %}
|
| 9 |
-
{%- for content_part in messages[0].content %}
|
| 10 |
-
{%- if content_part.type == 'text' %}
|
| 11 |
-
{{- content_part.text + '\n\n' }}
|
| 12 |
-
{%- endif %}
|
| 13 |
-
{%- endfor %}
|
| 14 |
-
{%- endif %}
|
| 15 |
{%- endif %}
|
| 16 |
{{- '# Tools\n\n' }}
|
| 17 |
{{- 'You may call one or more functions to assist with the user query.\n\n' }}
|
|
@@ -31,17 +23,7 @@
|
|
| 31 |
{{- '</tool_call><|im_end|>\n' }}
|
| 32 |
{%- else %}
|
| 33 |
{%- if messages[0].role == 'system' %}
|
| 34 |
-
{{- '<|im_start|>system\n' }}
|
| 35 |
-
{%- if messages[0].content is string %}
|
| 36 |
-
{{- messages[0].content }}
|
| 37 |
-
{%- elif messages[0].content is sequence %}
|
| 38 |
-
{%- for content_part in messages[0].content %}
|
| 39 |
-
{%- if content_part.type == 'text' %}
|
| 40 |
-
{{- content_part.text }}
|
| 41 |
-
{%- endif %}
|
| 42 |
-
{%- endfor %}
|
| 43 |
-
{%- endif %}
|
| 44 |
-
{{- '<|im_end|>\n' }}
|
| 45 |
{%- endif %}
|
| 46 |
{%- endif %}
|
| 47 |
{%- set ns = namespace(last_user_index=-1) %}
|
|
@@ -51,19 +33,9 @@
|
|
| 51 |
{%- endif %}
|
| 52 |
{%- endfor %}
|
| 53 |
{%- for message in messages %}
|
| 54 |
-
{%- set content = message.content %}
|
| 55 |
{%- if (message.role == 'system' and not loop.first) %}
|
| 56 |
-
{{- '<|im_start|>' + message.role + '\n' }}
|
| 57 |
-
{%- if content is string %}
|
| 58 |
-
{{- content }}
|
| 59 |
-
{%- elif content is sequence %}
|
| 60 |
-
{%- for content_part in content %}
|
| 61 |
-
{%- if content_part.type == 'text' %}
|
| 62 |
-
{{- content_part.text }}
|
| 63 |
-
{%- endif %}
|
| 64 |
-
{%- endfor %}
|
| 65 |
-
{%- endif %}
|
| 66 |
-
{{- '<|im_end|>' + '\n' }}
|
| 67 |
{%- elif message.role == 'user' %}
|
| 68 |
{{- '<|im_start|>user\n' }}
|
| 69 |
{%- if message['content'] is string %}
|
|
@@ -73,17 +45,30 @@
|
|
| 73 |
{%- if not loop.first %}
|
| 74 |
{{- '\n' }}
|
| 75 |
{%- endif %}
|
| 76 |
-
{%- if content['type'] == '
|
| 77 |
-
{%- set
|
| 78 |
-
{%- set
|
| 79 |
-
{
|
| 80 |
-
{
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
{
|
| 86 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
{%- elif content['type'] == 'text' %}
|
| 88 |
{{- content['text'] }}
|
| 89 |
{%- endif %}
|
|
@@ -92,8 +77,8 @@
|
|
| 92 |
{%- endif %}
|
| 93 |
{%- elif message.role == 'assistant' %}
|
| 94 |
{%- set reasoning_content = '' %}
|
| 95 |
-
{%- if message.reasoning_content is string %}
|
| 96 |
-
{%- set reasoning_content = message.reasoning_content %}
|
| 97 |
{%- else %}
|
| 98 |
{%- if '</think>' in content %}
|
| 99 |
{%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
|
|
@@ -109,20 +94,24 @@
|
|
| 109 |
{%- else %}
|
| 110 |
{{- '<|im_start|>' + message.role + '\n' + content }}
|
| 111 |
{%- endif %}
|
| 112 |
-
{%- if message.tool_calls %}
|
| 113 |
-
{%- for tool_call in message.tool_calls %}
|
| 114 |
{%- if not loop.first or content %}
|
| 115 |
{{- '\n' }}
|
| 116 |
{%- endif %}
|
| 117 |
-
{%- if tool_call.function %}
|
| 118 |
-
{%- set tool_call = tool_call.function %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
{%- endif %}
|
| 120 |
-
{{- '<tool_call>' + tool_call.name + '\n' }}
|
| 121 |
-
{%- set _args = tool_call.arguments %}
|
| 122 |
-
{%- for k, v in _args.items() %}
|
| 123 |
-
{{- '<arg_key>' + k + '</arg_key>\n' }}
|
| 124 |
-
{{- '<arg_value>' + (v | tojson(ensure_ascii=False) if v is not string else v) + '</arg_value>\n' }}
|
| 125 |
-
{%- endfor %}
|
| 126 |
{{- '</tool_call>' }}
|
| 127 |
{%- endfor %}
|
| 128 |
{%- endif %}
|
|
@@ -140,8 +129,9 @@
|
|
| 140 |
{%- endif %}
|
| 141 |
{%- endfor %}
|
| 142 |
{%- if add_generation_prompt %}
|
| 143 |
-
{
|
| 144 |
-
|
| 145 |
-
|
|
|
|
| 146 |
{%- endif %}
|
| 147 |
-
{%- endif %}
|
|
|
|
| 2 |
{%- set ns_vid = namespace(count=0) %}
|
| 3 |
{%- if tools %}
|
| 4 |
{{- '<|im_start|>system\n' }}
|
| 5 |
+
{%- if messages[0].role == 'system' and messages[0].content is string %}
|
| 6 |
+
{{- messages[0].content + '\n\n' }}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
{%- endif %}
|
| 8 |
{{- '# Tools\n\n' }}
|
| 9 |
{{- 'You may call one or more functions to assist with the user query.\n\n' }}
|
|
|
|
| 23 |
{{- '</tool_call><|im_end|>\n' }}
|
| 24 |
{%- else %}
|
| 25 |
{%- if messages[0].role == 'system' %}
|
| 26 |
+
{{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
{%- endif %}
|
| 28 |
{%- endif %}
|
| 29 |
{%- set ns = namespace(last_user_index=-1) %}
|
|
|
|
| 33 |
{%- endif %}
|
| 34 |
{%- endfor %}
|
| 35 |
{%- for message in messages %}
|
| 36 |
+
{%- set content = message.get('content', '') or '' %}
|
| 37 |
{%- if (message.role == 'system' and not loop.first) %}
|
| 38 |
+
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>\n' }}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
{%- elif message.role == 'user' %}
|
| 40 |
{{- '<|im_start|>user\n' }}
|
| 41 |
{%- if message['content'] is string %}
|
|
|
|
| 45 |
{%- if not loop.first %}
|
| 46 |
{{- '\n' }}
|
| 47 |
{%- endif %}
|
| 48 |
+
{%- if content['type'] == 'image_url' %}
|
| 49 |
+
{%- set media_url = content.get('image_url', {}).get('url', '') %}
|
| 50 |
+
{%- set url_lower = media_url.lower() %}
|
| 51 |
+
{%- set video_extensions = ['.mp4', '.avi', '.mov', '.mkv', '.webm', '.flv', '.wmv', '.m4v'] %}
|
| 52 |
+
{%- set ns_check = namespace(is_video=False) %}
|
| 53 |
+
{%- for ext in video_extensions %}
|
| 54 |
+
{%- if url_lower.endswith(ext) %}
|
| 55 |
+
{%- set ns_check.is_video = True %}
|
| 56 |
+
{%- endif %}
|
| 57 |
+
{%- endfor %}
|
| 58 |
+
{%- if ns_check.is_video %}
|
| 59 |
+
{%- set video_id = 'video_%02d' % ns_vid.count %}
|
| 60 |
+
{%- set ns_vid.count = ns_vid.count + 1 %}
|
| 61 |
+
{%- set filename = media_url.split('/')[-1] %}
|
| 62 |
+
{{- '<|mime_start|>{"id": "' + video_id + '", "type": "video/mp4", "filename": "' + filename + '"}<|mime_end|>\n' }}
|
| 63 |
+
{{- '<|video_aux_start|>다음 중 video_duration은 비디오 길이 정보입니다. 참고하여 답변하세요. {"video_duration": "<|video_meta_duration|>"}<|video_aux_end|>\n'}}
|
| 64 |
+
{{- '<|video_start|><|VIDEO_PAD|><|video_end|>\n'}}
|
| 65 |
+
{%- else %}
|
| 66 |
+
{%- set image_id = 'image_%02d' % ns_img.count %}
|
| 67 |
+
{%- set ns_img.count = ns_img.count + 1 %}
|
| 68 |
+
{%- set filename = media_url.split('/')[-1] %}
|
| 69 |
+
{{- '<|mime_start|>{"id": "' + image_id + '", "type": "image/jpeg", "filename": "' + filename + '"}<|mime_end|>\n' }}
|
| 70 |
+
{{- '<|image_start|><|IMAGE_PAD|><|image_end|>' }}
|
| 71 |
+
{%- endif %}
|
| 72 |
{%- elif content['type'] == 'text' %}
|
| 73 |
{{- content['text'] }}
|
| 74 |
{%- endif %}
|
|
|
|
| 77 |
{%- endif %}
|
| 78 |
{%- elif message.role == 'assistant' %}
|
| 79 |
{%- set reasoning_content = '' %}
|
| 80 |
+
{%- if message.get('reasoning_content') is string %}
|
| 81 |
+
{%- set reasoning_content = message.get('reasoning_content') %}
|
| 82 |
{%- else %}
|
| 83 |
{%- if '</think>' in content %}
|
| 84 |
{%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
|
|
|
|
| 94 |
{%- else %}
|
| 95 |
{{- '<|im_start|>' + message.role + '\n' + content }}
|
| 96 |
{%- endif %}
|
| 97 |
+
{%- if message.get('tool_calls') %}
|
| 98 |
+
{%- for tool_call in message.get('tool_calls', []) %}
|
| 99 |
{%- if not loop.first or content %}
|
| 100 |
{{- '\n' }}
|
| 101 |
{%- endif %}
|
| 102 |
+
{%- if tool_call.get('function') %}
|
| 103 |
+
{%- set tool_call = tool_call.get('function') %}
|
| 104 |
+
{%- endif %}
|
| 105 |
+
{{- '<tool_call>' + tool_call.get('name', '') + '\n' }}
|
| 106 |
+
{%- set _args = tool_call.get('arguments', {}) %}
|
| 107 |
+
{%- if _args is string %}
|
| 108 |
+
{{- '<arguments>' + _args + '</arguments>\n' }}
|
| 109 |
+
{%- elif _args is mapping %}
|
| 110 |
+
{%- for k, v in _args.items() %}
|
| 111 |
+
{{- '<arg_key>' + k + '</arg_key>\n' }}
|
| 112 |
+
{{- '<arg_value>' + (v | tojson(ensure_ascii=False) if v is not string else v) + '</arg_value>\n' }}
|
| 113 |
+
{%- endfor %}
|
| 114 |
{%- endif %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
{{- '</tool_call>' }}
|
| 116 |
{%- endfor %}
|
| 117 |
{%- endif %}
|
|
|
|
| 129 |
{%- endif %}
|
| 130 |
{%- endfor %}
|
| 131 |
{%- if add_generation_prompt %}
|
| 132 |
+
{%- if thinking is defined and thinking is true %}
|
| 133 |
+
{{- '<|im_start|>assistant\n<think>\n' }}
|
| 134 |
+
{%- else %}
|
| 135 |
+
{{- '<|im_start|>assistant\n<think>\n\n</think>\n\n' }}
|
| 136 |
{%- endif %}
|
| 137 |
+
{%- endif %}
|