juliendenize commited on
Commit
2680dd2
·
verified ·
1 Parent(s): 13ccaad

Upload chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +132 -0
chat_template.jinja ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {#- Default system message if no system prompt is passed. #}
2
+ {%- set default_system_message = '' %}
3
+
4
+ {#- Begin of sequence token. #}
5
+ {{- '<s>' }}
6
+
7
+ {#- Handle system prompt if it exists. #}
8
+ {#- System prompt supports text content or text chunks. #}
9
+ {%- if messages[0]['role'] == 'system' %}
10
+ {{- '[SYSTEM_PROMPT]' -}}
11
+ {%- if messages[0]['content'] is string %}
12
+ {{- messages[0]['content'] -}}
13
+ {%- else %}
14
+ {%- for block in messages[0]['content'] %}
15
+ {%- if block['type'] == 'text' %}
16
+ {{- block['text'] }}
17
+ {%- else %}
18
+ {{- raise_exception('Only text chunks are supported in system message contents.') }}
19
+ {%- endif %}
20
+ {%- endfor %}
21
+ {%- endif %}
22
+ {{- '[/SYSTEM_PROMPT]' -}}
23
+ {%- set loop_messages = messages[1:] %}
24
+ {%- else %}
25
+ {%- set loop_messages = messages %}
26
+ {%- if default_system_message != '' %}
27
+ {{- '[SYSTEM_PROMPT]' + default_system_message + '[/SYSTEM_PROMPT]' }}
28
+ {%- endif %}
29
+ {%- endif %}
30
+
31
+
32
+ {#- Tools definition #}
33
+ {%- set tools_definition = '' %}
34
+ {%- set has_tools = false %}
35
+ {%- if tools is defined and tools is not none and tools|length > 0 %}
36
+ {%- set has_tools = true %}
37
+ {%- set tools_definition = '[AVAILABLE_TOOLS]' + (tools| tojson) + '[/AVAILABLE_TOOLS]' %}
38
+ {{- tools_definition }}
39
+ {%- endif %}
40
+
41
+ {#- Model settings definition #}
42
+ {%- set reasoning_effort = reasoning_effort if reasoning_effort is defined and reasoning_effort is not none else 'none' %}
43
+ {%- if reasoning_effort not in ['none', 'high'] %}
44
+ {{- raise_exception('reasoning_effort must be either "none" or "high"') }}
45
+ {%- endif %}
46
+ {%- set model_settings = '[MODEL_SETTINGS]{"reasoning_effort": "' + reasoning_effort + '"}[/MODEL_SETTINGS]' %}
47
+ {{- model_settings }}
48
+
49
+ {#- Checks for alternating user/assistant messages. #}
50
+ {%- set ns = namespace(index=0) %}
51
+ {%- for message in loop_messages %}
52
+ {%- if message.role == 'user' or (message.role == 'assistant' and (message.tool_calls is not defined or message.tool_calls is none or message.tool_calls | length == 0)) %}
53
+ {%- if (message['role'] == 'user') != (ns.index % 2 == 0) %}
54
+ {{- raise_exception('After the optional system message, conversation roles must alternate user and assistant roles except for tool calls and results.') }}
55
+ {%- endif %}
56
+ {%- set ns.index = ns.index + 1 %}
57
+ {%- endif %}
58
+ {%- endfor %}
59
+
60
+ {#- Handle conversation messages. #}
61
+ {%- for message in loop_messages %}
62
+
63
+ {#- User messages supports text content or text and image chunks. #}
64
+ {%- if message['role'] == 'user' %}
65
+ {%- if message['content'] is string %}
66
+ {{- '[INST]' + message['content'] + '[/INST]' }}
67
+ {%- elif message['content'] | length > 0 %}
68
+ {{- '[INST]' }}
69
+ {%- if message['content'] | length == 2 %}
70
+ {%- set blocks = message['content'] | sort(attribute='type') %}
71
+ {%- else %}
72
+ {%- set blocks = message['content'] %}
73
+ {%- endif %}
74
+ {%- for block in blocks %}
75
+ {%- if block['type'] == 'text' %}
76
+ {{- block['text'] }}
77
+ {%- elif block['type'] in ['image', 'image_url'] %}
78
+ {{- '[IMG]' }}
79
+ {%- else %}
80
+ {{- raise_exception('Only text, image and image_url chunks are supported in user message content.') }}
81
+ {%- endif %}
82
+ {%- endfor %}
83
+ {{- '[/INST]' }}
84
+ {%- else %}
85
+ {{- raise_exception('User message must have a string or a list of chunks in content') }}
86
+ {%- endif %}
87
+
88
+ {#- Assistant messages supports text content or text, image and thinking chunks. #}
89
+ {%- elif message['role'] == 'assistant' %}
90
+ {%- if (message['content'] is none or message['content'] == '' or message['content']|length == 0) and (message['tool_calls'] is not defined or message['tool_calls'] is none or message['tool_calls']|length == 0) %}
91
+ {{- raise_exception('Assistant message must have a string or a list of chunks in content or a list of tool calls.') }}
92
+ {%- endif %}
93
+
94
+ {%- if message['content'] is string and message['content'] != '' %}
95
+ {{- message['content'] }}
96
+ {%- elif message['content'] | length > 0 %}
97
+ {%- for block in message['content'] %}
98
+ {%- if block['type'] == 'text' %}
99
+ {{- block['text'] }}
100
+ {%- elif block['type'] == 'thinking' %}
101
+ {{- '[THINK]' + block['thinking'] + '[/THINK]' }}
102
+ {%- else %}
103
+ {{- raise_exception('Only text and thinking chunks are supported in assistant message contents.') }}
104
+ {%- endif %}
105
+ {%- endfor %}
106
+ {%- endif %}
107
+
108
+ {%- if message['tool_calls'] is defined and message['tool_calls'] is not none and message['tool_calls']|length > 0 %}
109
+ {%- for tool in message['tool_calls'] %}
110
+ {{- '[TOOL_CALLS]' }}
111
+ {%- set name = tool['function']['name'] %}
112
+ {%- set arguments = tool['function']['arguments'] %}
113
+ {%- if arguments is not string %}
114
+ {%- set arguments = arguments|tojson|safe %}
115
+ {%- elif arguments == '' %}
116
+ {%- set arguments = '{}' %}
117
+ {%- endif %}
118
+ {{- name + '[ARGS]' + arguments }}
119
+ {%- endfor %}
120
+ {%- endif %}
121
+
122
+ {{- '</s>' }}
123
+
124
+ {#- Tool messages only supports text content. #}
125
+ {%- elif message['role'] == 'tool' %}
126
+ {{- '[TOOL_RESULTS]' + message['content']|string + '[/TOOL_RESULTS]' }}
127
+
128
+ {#- Raise exception for unsupported roles. #}
129
+ {%- else %}
130
+ {{- raise_exception('Only user, assistant and tool roles are supported, got ' + message['role'] + '.') }}
131
+ {%- endif %}
132
+ {%- endfor %}