{%- for message in messages -%} {%- if loop.first -%}{{- bos_token -}}{%- endif -%} {{- '<|im_start|>' + message['role'] + '\n' -}} {%- if message['role'] == 'tool' -%}{{- '<|tool_response|>\n' -}}{%- endif -%} {%- if message['content'] is string -%} {{- message['content'] -}} {%- elif message['content'] is iterable -%} {%- for item in message['content'] -%} {%- if item['type'] == 'text' -%}{{- item['text'] -}}{%- endif -%} {%- endfor -%} {%- endif -%} {%- if message['tool_calls'] is defined and message['tool_calls'] -%} {{- '\n<|tool_call|>\n' + (message['tool_calls'] | tojson) -}} {%- endif -%} {{- '<|im_end|>\n' -}} {%- endfor -%} {%- if add_generation_prompt -%}{{- '<|im_start|>assistant\n' -}}{%- endif -%}