{%- if tools %} {{- '<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n' -}} {%- if messages[0].role == 'system' -%} {{- messages[0].content + '\n\n' -}} {%- endif -%} {{- '[' -}} {%- for tool in tools -%} {{- (tool.function if tool.function is defined else tool) | tojson -}}{{- ', ' if not loop.last else '' -}} {%- endfor -%} {{- ']' -}}{{- '<|eot_id|>' -}} {%- else %} {%- if messages[0].role == 'system' %} {{- '<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n' + messages[0].content + '<|eot_id|>' -}} {%- endif %} {%- endif %} {%- for message in messages -%} {%- if (message.role == 'user') -%} {{- '<|start_header_id|>user<|end_header_id|>\n\n' + message.content + '<|eot_id|>' -}} {%- elif message.role == 'assistant' -%} {%- set content = message.content -%} {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' + content -}} {%- if message.tool_calls -%} {{- '[' -}} {%- for tool_call in message.tool_calls -%} {%- if tool_call.function -%} {%- set tool_call = tool_call.function -%} {%- endif -%} {{- '{"name": "' }}{{- tool_call.name }}{{- '", "arguments": ' -}} {%- if tool_call.arguments is string -%} {{- tool_call.arguments -}} {%- else -%} {{- tool_call.arguments | tojson -}} {%- endif -%} {{- ', ' if not loop.last else '' -}} {%- endfor -%} {{- ']' -}} {%- endif %} {{- '<|eot_id|>' -}} {%- elif message.role == 'tool' -%} {%- if loop.first or (messages[loop.index0 - 1].role != 'tool') -%} {{- '<|start_header_id|>user<|end_header_id|>\n\n' }}{{- '[' -}} {%- endif -%} {{- message.content -}}{{- ', ' if not loop.last and (messages[loop.index0 + 1].role == 'tool') else '' -}} {%- if loop.last or (messages[loop.index0 + 1].role != 'tool') -%} {{- ']' -}}{{- '<|eot_id|>' -}} {%- endif %} {%- endif %} {%- endfor %} {%- if add_generation_prompt %} {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' -}} {%- endif %}