{%- if messages[0]['role'] == 'system' -%} {%- set system_message = messages[0]['content'] | trim -%} {%- set messages = messages[1:] -%} {%- else -%} {%- set system_message = '' -%} {%- endif -%} {%- if tools is not none -%} {{- '<|begin_of_text|><|start_header_id|>system<|end_header_id|>' + '\n\n' + system_message -}} {{- '\n\n' if system_message else '' -}} {{- '[' -}} {% for t in tools %} {{- (t.function if t.function is defined else t) | tojson() -}} {{- ', ' if not loop.last else '' -}} {%- endfor -%} {{- ']' -}} {{- '<|eot_id|>' -}} {%- else -%} {{- '<|begin_of_text|><|start_header_id|>system<|end_header_id|>' + '\n\n' + system_message + '<|eot_id|>' -}} {%- endif -%} {%- for message in messages -%} {%- if (message['role'] in ['user', 'tool']) != (loop.index0 % 2 == 0) -%} {{- raise_exception('Conversation roles must alternate between user/tool and assistant') -}} {%- elif message['role'] == 'user' -%} {{- '<|start_header_id|>user<|end_header_id|>' + '\n\n' + message['content'] | trim + '<|eot_id|>' -}} {%- elif message['role'] == 'tool' -%} {%- set tool_response = '[' + message['content'] | trim + ']' -%} {{- '<|start_header_id|>user<|end_header_id|>' + '\n\n' + tool_response + '<|eot_id|>' -}} {%- elif message['role'] == 'assistant' and message.get('tool_calls') is not none -%} {%- set tool_calls = message['tool_calls'] -%} {% generation %} {{- '<|start_header_id|>assistant<|end_header_id|>' + '\n\n' + '[' -}} {%- for tool_call in tool_calls -%} {{ '{' + '"name": "' + tool_call.function.name + '", "arguments": ' + tool_call.function.arguments | tojson + '}' }} {%- if not loop.last -%} {{ ', ' }} {%- else -%} {{ ']' + '<|eot_id|>' }} {%- endif -%} {%- endfor -%} {% endgeneration %} {%- elif message['role'] == 'assistant' -%} {% generation %} {{- '<|start_header_id|>assistant<|end_header_id|>' + '\n\n' + message['content'] | trim + '<|eot_id|>' -}} {% endgeneration %} {%- endif -%} {%- endfor -%} {%- if add_generation_prompt -%} {% generation %} {{ '<|start_header_id|>assistant<|end_header_id|>' + '\n\n' }} {% endgeneration %} {%- endif -%}