Transformers
dlwh commited on
Commit
7f1a125
·
verified ·
1 Parent(s): 37a7a2a

Upload tokenizer

Browse files
Files changed (1) hide show
  1. chat_template.jinja +79 -5
chat_template.jinja CHANGED
@@ -61,14 +61,88 @@ You can use the following tools in your python code like regular functions:
61
  <|eot_id|>
62
  {%- endif -%}
63
  {%- for message in messages -%}
64
- {%- if message['role'] == 'assistant' -%}
65
- <|start_header_id|>{{ message['role'] }}<|end_header_id|>
66
- {% generation %}{{- message['content'] | trim }}<|eot_id|>{% endgeneration %}
67
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  {% else %}
 
 
 
69
  <|start_header_id|>{{ message['role'] }}<|end_header_id|>
70
- {{ message['content'] | trim }}<|eot_id|>
 
 
 
 
 
 
 
 
 
 
 
 
71
  {% endif %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  {%- endfor -%}
73
  {%- if add_generation_prompt -%}
74
  <|start_header_id|>assistant<|end_header_id|>
 
61
  <|eot_id|>
62
  {%- endif -%}
63
  {%- for message in messages -%}
64
+ {%- set has_tool_calls = message.get('tool_calls') is not none and message.get('tool_calls') -%}
65
+ {%- if not (message.get('role') in ['tool', 'ipython'] or has_tool_calls) -%}
66
+ {%- if message.get('role') == 'assistant' -%}
67
+ <|start_header_id|>assistant<|end_header_id|>
68
+ {% set content = message.get('content') %}
69
+ {% if content is string %}
70
+ {% generation %}{{- content | trim }}<|eot_id|>{% endgeneration %}
71
+ {% elif content is mapping %}
72
+ {% generation %}{{- content.get('text', '') | trim }}<|eot_id|>{% endgeneration %}
73
+ {% elif content is iterable %}
74
+ {% generation %}
75
+ {%- for chunk in content -%}
76
+ {%- if chunk.get('type') == 'text' -%}
77
+ {{ chunk.get('text', '') | trim }}
78
+ {%- endif -%}
79
+ {%- endfor -%}
80
+ <|eot_id|>
81
+ {% endgeneration %}
82
  {% else %}
83
+ {% generation %}{% endgeneration %}<|eot_id|>
84
+ {% endif %}
85
+ {%- else -%}
86
  <|start_header_id|>{{ message['role'] }}<|end_header_id|>
87
+ {% set content = message.get('content') %}
88
+ {% if content is string %}
89
+ {{ content | trim }}<|eot_id|>
90
+ {% elif content is mapping %}
91
+ {{ content.get('text', '') | trim }}<|eot_id|>
92
+ {% elif content is iterable %}
93
+ {%- for chunk in content -%}
94
+ {%- if chunk.get('type') == 'text' -%}
95
+ {{ chunk.get('text', '') | trim }}
96
+ {%- endif -%}
97
+ {%- endfor -%}<|eot_id|>
98
+ {% else %}
99
+ <|eot_id|>
100
  {% endif %}
101
+ {%- endif -%}
102
+
103
+ {%- elif message.get('role') == 'tool' -%}
104
+ {%- set _tool_name = message.get('name') -%}
105
+ {%- set _tool_id = message.get('tool_call_id') -%}
106
+ {%- set _attr_name = ' name="' ~ _tool_name ~ '"' if _tool_name else '' -%}
107
+ {%- set _attr_id = ' id="' ~ _tool_id ~ '"' if _tool_id else '' -%}
108
+ <|start_header_id|>tool<|end_header_id|>
109
+ <tool_response{{ _attr_name }}{{ _attr_id }}>
110
+ {%- set tool_content = message.get('content') -%}
111
+ {%- if tool_content is mapping or (tool_content is iterable and tool_content is not string) -%}
112
+ {{- tool_content | tojson }}
113
+ {%- else -%}
114
+ {{- tool_content if tool_content is not none else '' }}
115
+ {%- endif -%}
116
+ </tool_response><|eot_id|>
117
+ {{- "
118
+ " -}}
119
+ {%- elif message.get('role') == 'ipython' -%}
120
+ <|start_header_id|>ipython<|end_header_id|>
121
+ {% set ipy_content = message.get('content') %}
122
+ {% if ipy_content is string %}
123
+ {{- { "output": ipy_content } | tojson -}}
124
+ {% elif ipy_content is iterable %}
125
+ {%- for chunk in ipy_content -%}
126
+ {%- if chunk.get('type') == 'text' -%}
127
+ {{- { "output": chunk.get('text', '') } | tojson -}}
128
+ {%- endif -%}
129
+ {%- endfor -%}
130
+ {% else %}
131
+ {{- { "output": ipy_content } | tojson -}}
132
+ {% endif %}
133
+ <|eot_id|>
134
+ {% elif has_tool_calls -%}
135
+ {%- if message.tool_calls|length != 1 -%}
136
+ {{- raise_exception("This template expects exactly one tool call per assistant turn.") -}}
137
+ {%- endif -%}
138
+ {%- set tool_call = message.tool_calls[0].function -%}
139
+ <|start_header_id|>assistant<|end_header_id|>
140
+ {% generation %}
141
+ {{- '{"name": "' + tool_call.name + '", "arguments": ' -}}
142
+ {{- tool_call.arguments | tojson -}}
143
+ {{- "}" -}}<|eot_id|>
144
+ {% endgeneration %}
145
+ {%- endif -%}
146
  {%- endfor -%}
147
  {%- if add_generation_prompt -%}
148
  <|start_header_id|>assistant<|end_header_id|>