Jeff Moe commited on
Commit
a2a2916
·
1 Parent(s): de65573

Chat template

Browse files
Files changed (1) hide show
  1. chat_template.jinja +122 -0
chat_template.jinja ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- if tools %}
2
+ {{- '<|im_start|>system\n' }}
3
+ {%- if messages[0].role == 'system' %}
4
+ {%- if messages[0].content is string %}
5
+ {{- messages[0].content }}
6
+ {%- else %}
7
+ {%- for content in messages[0].content %}
8
+ {%- if content.type == 'image' or 'image' in content or 'image_url' in content %}
9
+ {{- "<|vision_start|><|image_pad|><|vision_end|>" }}
10
+ {%- elif content.type == 'audio' or 'audio' in content or 'audio_url' in content %}
11
+ {{- "<|audio_start|><|audio_pad|><|audio_end|>" }}
12
+ {%- elif content.type == 'video' or 'video' in content %}
13
+ {{- "<|vision_start|><|video_pad|><|vision_end|>" }}
14
+ {%- elif content.type == 'text' %}
15
+ {{- content.text }}
16
+ {%- endif %}
17
+ {%- endfor %}
18
+ {%- endif %}
19
+ {%- endif %}
20
+ {{- '\n\n' }}
21
+ {{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
22
+ {%- for tool in tools %}
23
+ {{- "\n" }}
24
+ {{- tool | tojson }}
25
+ {%- endfor %}
26
+ {{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
27
+ {%- else %}
28
+ {%- if messages[0].role == 'system' %}
29
+ {%- if messages[0].content is string %}
30
+ {{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }}
31
+ {%- else %}
32
+ {%- for content in messages[0].content %}
33
+ {%- if content.type == 'image' or 'image' in content or 'image_url' in content %}
34
+ {{- '<|im_start|>system\n' +"<|vision_start|><|image_pad|><|vision_end|>"+ '<|im_end|>\n' }}
35
+ {%- elif content.type == 'audio' or 'audio' in content or 'audio_url' in content %}
36
+ {{- '<|im_start|>system\n' +"<|audio_start|><|audio_pad|><|audio_end|>"+ '<|im_end|>\n' }}
37
+ {%- elif content.type == 'video' or 'video' in content %}
38
+ {{- '<|im_start|>system\n' +"<|vision_start|><|video_pad|><|vision_end|>"+ '<|im_end|>\n' }}
39
+ {%- elif content.type == 'text' %}
40
+ {{- '<|im_start|>system\n' +content.text+ '<|im_end|>\n' }}
41
+ {%- endif %}
42
+ {%- endfor %}
43
+ {%- endif %}
44
+ {%- endif %}
45
+ {%- endif %}
46
+ {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
47
+ {%- for message in messages[::-1] %}
48
+ {%- set index = (messages|length - 1) - loop.index0 %}
49
+ {%- if ns.multi_step_tool and message.role == "user" and message.content is string and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}
50
+ {%- set ns.multi_step_tool = false %}
51
+ {%- set ns.last_query_index = index %}
52
+ {%- endif %}
53
+ {%- endfor %}
54
+ {%- for message in messages %}
55
+ {%- if message.content is string %}
56
+ {%- set content = message.content %}
57
+ {%- else %}
58
+ {%- set content = namespace(text="") %}
59
+ {%- for mcontent in message.content %}
60
+ {%- if mcontent.type == 'image' or 'image' in mcontent or 'image_url' in mcontent %}
61
+ {%- set content.text = content.text~"<|vision_start|><|image_pad|><|vision_end|>" %}
62
+ {%- elif mcontent.type == 'audio' or 'audio' in mcontent or 'audio_url' in mcontent %}
63
+ {%- set content.text = content.text~"<|audio_start|><|audio_pad|><|audio_end|>" %}
64
+ {%- elif mcontent.type == 'video' or 'video' in mcontent %}
65
+ {%- set content.text = content.text~"<|vision_start|><|video_pad|><|vision_end|>" %}
66
+ {%- elif mcontent.type == 'text' %}
67
+ {%- set content.text = content.text~mcontent.text %}
68
+ {%- endif %}
69
+ {%- endfor %}
70
+ {%- set content = content.text %}
71
+ {%- endif %}
72
+ {%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
73
+ {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
74
+ {%- elif message.role == "assistant" %}
75
+ {%- set reasoning_content = "" %}
76
+ {%- if message.reasoning_content is string %}
77
+ {%- set reasoning_content = message.reasoning_content %}
78
+ {%- else %}
79
+ {%- if '</think>' in content %}
80
+ {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
81
+ {%- set content = content.split('</think>')[-1].lstrip('\n') %}
82
+ {%- endif %}
83
+ {%- endif %}
84
+ {%- if loop.index0 > ns.last_query_index %}
85
+ {%- if loop.last or (not loop.last and reasoning_content) %}
86
+ {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip("\n") + '\n</think>\n\n' + content.lstrip('\n') }}
87
+ {%- else %}
88
+ {{- '<|im_start|>' + message.role + '\n' + content }}
89
+ {%- endif %}
90
+ {%- else %}
91
+ {{- '<|im_start|>' + message.role + '\n' + content }}
92
+ {%- endif %}
93
+ {%- if message.tool_calls %}
94
+ {%- for tool_call in message.tool_calls %}
95
+ {%- if (loop.first and content) or (not loop.first) %}{{- '\n' }}{%- endif %}
96
+ {%- if tool_call.function %}
97
+ {%- set tool_call = tool_call.function %}
98
+ {%- endif %}
99
+ {{- '<tool_call>\n{"name": "' }}
100
+ {{- tool_call.name }}
101
+ {{- '", "arguments": ' }}
102
+ {%- if tool_call.arguments is string %}
103
+ {{- tool_call.arguments }}
104
+ {%- else %}
105
+ {{- tool_call.arguments | tojson }}
106
+ {%- endif %}
107
+ {{- '}\n</tool_call>' }}
108
+ {%- endfor %}
109
+ {%- endif %}
110
+ {{- '<|im_end|>\n' }}
111
+ {%- elif message.role == "tool" %}
112
+ {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}{{- '<|im_start|>user' }}{%- endif %}
113
+ {{- '\n<tool_response>\n' }}
114
+ {{- content }}
115
+ {{- '\n</tool_response>' }}
116
+ {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}{{- '<|im_end|>\n' }}{%- endif %}
117
+ {%- endif %}
118
+ {%- endfor %}
119
+ {%- if add_generation_prompt %}
120
+ {{- '<|im_start|>assistant\n' }}
121
+ {%- if enable_thinking is defined and enable_thinking is false %}{{- '<think>\n\n</think>\n\n' }}{%- endif %}
122
+ {%- endif %}