{%- set ns = namespace() %} {%- if messages[0]['role'] != 'system' -%} {%- set ns.non_tool_system_content = '' -%} {{- 'System\n' -}} {%- else -%} {%- set ns.non_tool_system_content = messages[0]['content'].strip() -%} {{- 'System\n' + ns.non_tool_system_content }} {%- endif -%} {%- if tools -%} {%- if ns.non_tool_system_content is defined and ns.non_tool_system_content != '' -%} {{- '\n\n' -}} {%- endif -%} {{- 'You can use the following tools to assist the user if required:' -}} {{- '\n[' -}} {%- for tool in tools -%} {{- (tool.function if tool.function is defined else tool) | tojson -}} {{- ', ' if not loop.last else '' -}} {%- endfor -%} {{- ']\n\n' -}} {{- 'If you decide to call any tool(s), use the following format:\n' -}} {{- '[{{"name": "tool_name1", "arguments": "tool_args1"}}, ' -}} {{- '{{"name": "tool_name2", "arguments": "tool_args2"}}]\n\n' -}} {{- 'The user will execute tool-calls and return responses from tool(s) in this format:\n' -}} {{- '[{{"tool_response1"}}, {{"tool_response2"}}]\n\n' -}} {{- 'Based on the tool responses, you can call additional tools if needed, correct tool calls if any errors are found, or just respond to the user.' -}} {%- endif -%} {{- '\n' -}} {%- set messages = messages[1:] if messages[0]['role'] == 'system' else messages -%} {%- if messages[-1]['role'] == 'assistant' -%} {%- set ns.last_turn_assistant_content = messages[-1]['content'].strip() -%} {%- set messages = messages[:-1] -%} {%- endif -%} {%- for message in messages %} {%- set content = message['content'] if 'content' in message else '' %} {%- if message['role'] == 'user' -%} {{- 'User\n' + content.strip() + '\n' }} {%- elif message['role'] == 'tool' -%} {%- if loop.first or (messages[loop.index0 - 1].role != 'tool') -%} {{- 'User\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') -%} {{- ']\n' -}} {%- endif -%} {%- elif message['role'] == 'assistant' -%} {%- if '' in content -%} {%- set content = content.split('')[1].strip() %} {%- endif -%} {{- 'Assistant\n' + content.strip() }} {%- if message.tool_calls -%} {%- if content.strip() != '' -%} {{- '\n\n' -}} {%- endif -%} {{- '[' -}} {%- for call in message.tool_calls -%} {%- set fn = call.function if call.function is defined else call -%} {{- '{"name": "' + fn.name + '", "arguments": ' -}} {%- if fn.arguments is string -%} {{- fn.arguments -}} {%- else -%} {{- fn.arguments | tojson -}} {%- endif -%} {{- '}' + (', ' if not loop.last else '') -}} {%- endfor -%}{{- ']' -}} {%- endif -%} {{- '\n\n' -}} {%- endif -%} {%- endfor -%} {%- if add_generation_prompt -%} {{- 'Assistant\n' -}} {%- if enable_thinking is defined and not enable_thinking -%} {{- '' -}} {%- else -%} {{- '\n' -}} {%- endif -%} {%- if ns.last_turn_assistant_content is defined and ns.last_turn_assistant_content != '' -%} {{- ns.last_turn_assistant_content -}} {%- endif -%} {%- else -%} {%- if ns.last_turn_assistant_content is defined and ns.last_turn_assistant_content != '' -%} {{- 'Assistant\n' -}} {%- if enable_thinking is defined and not enable_thinking -%} {{- '' -}} {%- else -%} {{- '\n' -}} {%- endif -%} {{- ns.last_turn_assistant_content -}} {%- if continue_final_message is defined -%} {%- if continue_final_message is false -%} {{- '\n\n' -}} {%- endif -%} {%- else -%} {{- '\n\n' -}} {%- endif -%} {%- endif -%} {%- endif -%}