{%- if tools %} {{- '<|im_start|>system\n' }} {%- if messages[0].role == 'system' %} {{- messages[0].content + '\n\n' }} {%- endif %} {{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within XML tags:\n" }} {%- for tool in tools %} {{- "\n" }} {{- tool | tojson }} {%- endfor %} {{- "\n\n\nFor each function call, return a json object with function name and arguments within XML tags:\n\n{\"name\": , \"arguments\": }\n<|im_end|>\n" }} {%- else %} {%- if messages[0].role == 'system' %} {{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }} {%- endif %} {%- endif %} {%- for message in messages %} {%- if message.role == 'user' or (message.role == 'assistant' and message.tool_calls is not defined) or (message.role == 'tool' and loop.index > 1) %} {%- if message.role == 'user' %} {%- if tools and not loop.first %} {{- '<|im_start|>user\n' + message.content + '<|im_end|>\n' }} {%- elif not tools or (tools and loop.first) %} {{- '<|im_start|>user\n' + message.content + '<|im_end|>\n' }} {%- endif %} {%- elif message.role == 'assistant' %} {{- '<|im_start|>assistant\n' + message.content + '<|im_end|>\n' }} {%- elif message.role == 'tool' %} {{- '<|im_start|>user\n\n' + message.content + '\n<|im_end|>\n' }} {%- endif %} {%- elif message.role == 'assistant' and message.tool_calls is defined %} {{- '<|im_start|>assistant\n' }} {%- if message.content %} {{- message.content + '\n\n' }} {%- endif %} {%- for tool_call in message.tool_calls %} {%- if tool_call.function is defined %} {{- '\n{\"name\": \"' + tool_call.function.name + '\", \"arguments\": ' + tool_call.function.arguments + '}\n' }} {%- endif %} {%- endfor %} {{- '<|im_end|>\n' }} {%- endif %} {%- endfor %} {%- if add_generation_prompt %} {{- '<|im_start|>assistant\n' }} {%- endif %}