{{ bos_token }} {%- set ns = namespace(prev_message_type=None) -%} {#- Tool Declarations -#} {%- set loop_messages = messages -%} {%- if tools or messages[0]['role'] == 'system' -%} {{- 'developer\n' -}} {%- if messages[0]['role'] == 'system' -%} {%- if messages[0]['content'] is string -%} {{- messages[0]['content'] | trim -}} {%- else -%} {%- for item in messages[0]['content'] -%} {%- if item['type'] == 'text' -%} {{- item['text'] | trim -}} {%- endif -%} {%- endfor -%} {%- endif -%} {%- set loop_messages = messages[1:] -%} {%- if tools -%} {{- '\n\n' -}} {%- endif -%} {%- endif -%} {%- for tool in tools %} {{- '' -}} {{- tool | trim }} {{- '' -}} {%- endfor %} {{- '\n'}} {%- endif %} {#- Loop through messages. -#} {%- for message in loop_messages -%} {%- if (message['role'] == 'assistant') -%} {#- Rename "assistant" to "model". -#} {%- set role = "model" -%} {%- else -%} {%- set role = message['role'] -%} {%- endif -%} {%- if role != 'tool' -%} {%- if ns.prev_message_type == 'tool_call' -%} {{ raise_exception("Tool call must be followed by a tool response.") }} {%- endif -%} {%- if ns.prev_message_type != 'tool_response' -%} {{- '' + role + '\n'}} {%- endif -%} {%- set ns.prev_message_type = None -%} {%- if 'content' in message -%} {%- if message['content'] is string -%} {{ message['content'] | trim }} {%- elif message['content'] is iterable -%} {%- for item in message['content'] -%} {%- if item['type'] == 'text' -%} {{ item['text'] | trim }} {%- endif -%} {%- endfor -%} {%- else -%} {{ raise_exception("Invalid content type") }} {%- endif -%} {%- set ns.prev_message_type = 'content' -%} {%- endif -%} {%- if 'tool_calls' in message and message['tool_calls'] and message['tool_calls'] is iterable -%} {#- Tool Calls -#} {%- for tool_call in message['tool_calls'] -%} {%- if 'function' in tool_call -%} {%- set tool_call = tool_call['function'] -%} {%- endif -%} {{- 'call:' + tool_call['name'] + '{' -}} {%- if 'arguments' in tool_call -%} {%- for key in tool_call['arguments'] -%} {{- key + ':' + tool_call['arguments'][key] -}} {% if not loop.last %} {{- ',' -}} {% endif %} {%- endfor %} {%- endif -%} {{- '}' + '' -}} {%- endfor -%} {%- if loop.last -%} {{ '' }} {%- endif -%} {%- set ns.prev_message_type = 'tool_call' -%} {%- endif -%} {%- else -%} {#- Tool Responses -#} {%- if 'content' in message -%} {%- if message['content'] is string -%} {{- 'response:' -}} {{ message['content'] | trim }} {{- '' -}} {%- elif message['content'] is iterable -%} {%- for item in message['content'] -%} {%- if item['type'] == 'text' -%} {{ 'response:' + item['text'] + '' }} {%- else -%} {{ raise_exception("Invalid content type for tool response.") }} {%- endif -%} {%- endfor -%} {%- else -%} {{ raise_exception("Invalid content type") }} {%- endif -%} {%- endif -%} {%- set ns.prev_message_type = 'tool_response' -%} {%- endif -%} {%- if ns.prev_message_type not in ['tool_call', 'tool_response'] -%} {{ '\n' }} {%- endif -%} {%- endfor -%} {%- if add_generation_prompt -%} {%- if ns.prev_message_type == 'tool_call' -%} {{- '' -}} {%- elif ns.prev_message_type != 'tool_response' -%} {{- 'model\n' -}} {%- endif -%} {%- endif -%}