jackcloudman commited on
Commit
5c308fc
·
verified ·
1 Parent(s): 14c9baa

Create chat_template.jinja

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