{%- macro render_content(content) -%} {%- if content is none or content is undefined -%} {{- "" -}} {%- elif content is string -%} {{- content | trim -}} {%- elif content is iterable -%} {%- for item in content -%} {%- if item['type'] == 'image' -%} {{- '' -}} {%- elif item['type'] == 'text' -%} {{- item['text'] | trim -}} {%- endif -%} {%- endfor -%} {%- else -%} {{- raise_exception("Invalid content type") -}} {%- endif -%} {%- endmacro -%} {{ bos_token }} {%- if messages and messages[0]['role'] == 'system' -%} {%- set first_system = messages[0]['content'] -%} {%- set loop_messages = messages[1:] -%} {%- else -%} {%- set first_system = "" -%} {%- set loop_messages = messages -%} {%- endif -%} {%- set has_tools = (tools is defined and tools) -%} {%- if has_tools or first_system -%} {{- 'system\n' -}} {%- if has_tools -%} {{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n" -}} {{- "If no tool call is needed, respond normally without any tags.\n\n" -}} {{- "You are provided with function signatures within XML tags:\n" -}} {%- for tool in tools -%} {{- "\n" -}}{{- tool | tojson -}} {%- endfor -%} {{- "\n\n\n" -}} {{- "For each function call, return a JSON object with function name and arguments within XML tags:\n" -}} {{- "\n{\"name\": , \"arguments\": }\n\n" -}} {{- "You may emit multiple blocks in a single assistant turn.\n" -}} {%- if first_system -%} {{- "\n" -}} {%- endif -%} {%- endif -%} {%- if first_system -%} {{- render_content(first_system) -}} {%- endif -%} {{- '\n' -}} {%- endif -%} {%- for message in loop_messages -%} {%- if message['role'] == 'system' -%} {{- 'system\n' -}} {{- render_content(message['content']) -}} {{- '\n' -}} {%- elif message['role'] == 'tool' -%} {{- 'tool\n' -}} {{- '\n' -}} {%- if message['content'] is string -%} {{- message['content'] | trim -}} {%- else -%} {{- message['content'] | tojson -}} {%- endif -%} {{- '\n' -}} {{- '\n' -}} {%- else -%} {%- set role = message['role'] -%} {%- if message['role'] == 'assistant' -%} {%- set role = 'model' -%} {%- endif -%} {{- '' + role + '\n' -}} {{- render_content(message['content']) -}} {%- if message['role'] == 'assistant' and message.tool_calls -%} {%- set content_has_text = false -%} {%- if message['content'] is string -%} {%- set content_has_text = (message['content'] | trim) != '' -%} {%- elif message['content'] is iterable -%} {%- set content_has_text = (message['content'] | length) > 0 -%} {%- endif -%} {%- for tool_call in message.tool_calls -%} {%- if (loop.first and content_has_text) or (not loop.first) -%} {{- '\n' -}} {%- endif -%} {%- if tool_call.function -%} {%- set tool_call = tool_call.function -%} {%- endif -%} {{- '\n{\"name\": \"' -}} {{- tool_call.name -}} {{- '\", \"arguments\": ' -}} {%- if tool_call.arguments is string -%} {{- tool_call.arguments -}} {%- else -%} {{- tool_call.arguments | tojson -}} {%- endif -%} {{- '}\n' -}} {%- endfor -%} {%- endif -%} {{- '\n' -}} {%- endif -%} {%- endfor -%} {%- if add_generation_prompt -%} {{- 'model\n' -}} {%- endif -%}