Gryphe commited on
Commit
30ca42b
·
verified ·
1 Parent(s): 95f5b83

Upload magistral_chat_template.jinja

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