Support tool calling in Tokenizers
Browse filesHi all! This PR modifies the chat template to add support for tool use with our [new API](https://huggingface.co/docs/transformers/main/en/chat_templating#a-complete-tool-use-example). In testing, results from applying the chat template seem to be identical to results from passing the same set of messages, tools, and tool calls / tool responses to the Mistral tokenizers (modulo a couple of spaces around special tokens that should get folded into the special token by our tokenizer).
- tokenizer_config.json +1 -1
tokenizer_config.json
CHANGED
|
@@ -6173,7 +6173,7 @@
|
|
| 6173 |
}
|
| 6174 |
},
|
| 6175 |
"bos_token": "<s>",
|
| 6176 |
-
"chat_template": "{{ bos_token }}{% for message in messages %}{
|
| 6177 |
"clean_up_tokenization_spaces": false,
|
| 6178 |
"eos_token": "</s>",
|
| 6179 |
"legacy": false,
|
|
|
|
| 6173 |
}
|
| 6174 |
},
|
| 6175 |
"bos_token": "<s>",
|
| 6176 |
+
"chat_template": "{{- bos_token }}\n{%- set user_messages = messages | selectattr('role', 'equalto', 'user') | list %}\n{%- for message in messages %}\n {%- if message['role'] == 'user' %}\n {%- if tools and (message == user_messages[-1]) %}\n {{- ' [AVAILABLE_TOOLS] [' }}\n {%- for tool in tools %}\n\t\t{%- set tool = tool.function %}\n\t\t{{- '{\"type\": \"function\", \"function\": {' }}\n\t\t{%- for key, val in tool.items() if key != \"return\" %}\n\t\t {%- if val is string %}\n\t\t\t{{- '\"' + key + '\": \"' + val + '\"' }}\n\t\t {%- else %}\n\t\t\t{{- '\"' + key + '\": ' + val|tojson }}\n\t\t {%- endif %}\n\t\t {%- if not loop.last %}\n\t\t\t{{- \", \" }}\n\t\t {%- endif %}\n\t\t{%- endfor %}\n\t\t{{- \"}}\" }}\n {%- if not loop.last %}\n {{- \", \" }}\n {%- else %}\n {{- \"]\" }}\n {%- endif %}\n {%- endfor %}\n {{- ' [/AVAILABLE_TOOLS]' }}\n {%- endif %}\n {{- ' [INST] ' + message['content'] + ' [/INST]' }}\n {%- elif message['role'] == 'assistant' %}\n {%- if message.tool_calls is defined and message.tool_calls|length > 0 %}\n {{- ' [TOOL_CALLS] [' }}\n {%- for tool_call in message.tool_calls %}\n {{- {\"name\": tool_call.function.name, \"arguments\": tool_call.function.arguments, \"id\": tool_call.id}|tojson }}\n {%- if not loop.last %}\n {{- \", \" }}\n {%- endif %}\n {%- endfor %}\n {{- '] ' }}\n {{- eos_token }}\n \t{%- elif message.content is defined %}\n\t {{- ' ' + message.content + ' ' + eos_token}}\n {%- endif %}\n {%- elif message['role'] == 'tool' %}\n {{- ' [TOOL_RESULTS] ' }}\n {{- '{\"call_id\": \"' + message.tool_call_id + '\", \"content\": ' + message.content|string + '}' }}\n {{- ' [/TOOL_RESULTS] ' }}\n {%- endif %}\n{%- endfor %}\n",
|
| 6177 |
"clean_up_tokenization_spaces": false,
|
| 6178 |
"eos_token": "</s>",
|
| 6179 |
"legacy": false,
|