{%- if tools %} {{- '<|im_start|>system\n' }} {%- if messages[0]['role'] == 'system' %} {{- messages[0]['content'] + '\n\n' }} {%- endif %} {{- '# Tools\n' }} {%- if tokenizer and tokenizer.chat_template and tokenizer.chat_template.find('"tools"') != -1 %} {{- tokenizer.chat_template.format(tools=tools, messages=messages, add_generation_prompt=add_generation_prompt) }} {%- else %} {{- 'You may use the following tools if necessary:\n' }} {%- for tool in tools %} {{- '- ' + tool['function']['name'] + ': ' + tool['function']['description'] + '\n' }} {%- endfor %} {%- endif %} {{- '<|im_end|>\n' }} {%- endif %} {%- if not tools %} {%- for message in messages %} {%- if message['role'] == 'system' %} {{- '<|im_start|>system\n' + message['content'] + '<|im_end|>\n' }} {%- elif message['role'] == 'user' %} {{- '<|im_start|>user\n' + message['content'] + '<|im_end|>\n' }} {%- elif message['role'] == 'assistant' %} {{- '<|im_start|>assistant\n' + message['content'] + '<|im_end|>\n' }} {%- endif %} {%- endfor %} {%- endif %} {%- if add_generation_prompt %} {{- '<|im_start|>assistant\n' }} {%- endif %}