get_chat_template method
Retrieve the chat template string used for tokenizing chat messages. This template is used
internally by the apply_chat_template method and can also be used externally to retrieve the model's chat
template for better generation tracking.
@param {Object} options An optional object containing the following properties:
@param {string} options.chat_template=null
A Jinja template or the name of a template to use for this conversion.
It is usually not necessary to pass anything to this argument,
as the model's template will be used by default.
@param {Object[]} options.tools=null
A list of tools (callable functions) that will be accessible to the model. If the template does not
support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema,
giving the name, description and argument types for the tool. See our
chat templating guide
for more information.
@returns {string} The chat template string.
Implementation
String get_chat_template({
String? chat_template,
List<Map<String, dynamic>>? tools,
}) {
// First, handle the cases when the model has a dict of multiple templates
if (this.chat_template != null && this.chat_template is Map) {
final Map<String, dynamic> templateMap = this.chat_template;
if (chat_template != null && templateMap.containsKey(chat_template)) {
// The user can pass the name of a template to the chat template argument instead of an entire template
chat_template = templateMap[chat_template];
} else if (chat_template == null) {
if (tools != null && templateMap.containsKey('tool_use')) {
chat_template = templateMap['tool_use'];
} else if (templateMap.containsKey('default')) {
chat_template = templateMap['default'];
} else {
throw ArgumentError(
"This model has multiple chat templates with no default specified! Please either pass a chat "
"template or the name of the template you wish to use to the 'chat_template' argument. Available "
"template names are ${templateMap.keys.toList(growable: false)..sort()}."
);
}
}
} else if (chat_template == null) {
// These are the cases when the model has a single template
// priority: `chat_template` argument > `tokenizer.chat_template`
if (this.chat_template != null) {
chat_template = this.chat_template;
} else {
throw ArgumentError(
"Cannot use apply_chat_template() because tokenizer.chat_template is not set and no template "
"argument was passed! For information about writing templates and setting the "
"tokenizer.chat_template attribute, please see the documentation at "
"https://huggingface.co/docs/transformers/main/en/chat_templating"
);
}
}
return chat_template!;
}