{{ bos_token }} {%- if enable_thinking is defined -%} {%- if enable_thinking is sameas true -%} {%- set _reasoning_mode = "/think" -%} {%- elif enable_thinking is sameas false -%} {%- set _reasoning_mode = "/nothink" -%} {%- else -%} {%- set _reasoning_mode = enable_thinking -%} {%- endif -%} {%- else -%} {%- set _reasoning_mode = "/think" -%} {%- endif -%} {%- set _custom_instructions = custom_instructions | default(None, true) -%} {%- set _xml_tools_list = xml_tools | default([], true) -%} {%- if tools is defined and tools -%} {%- set _xml_tools_list = tools -%} {%- endif -%} {%- set _python_tools = python_tools | default([], true) -%} {%- set _has_aux_header = (_reasoning_mode is not none) or _custom_instructions or (_xml_tools_list) or (_python_tools) -%} {%- if _has_aux_header -%} <|start_header_id|>system<|end_header_id|> {%- if _reasoning_mode is not none -%} Reasoning: {{ _reasoning_mode }} {%- endif %} {%- if _custom_instructions %} {{ _custom_instructions | trim }} {%- endif %} {% if _xml_tools_list or _python_tools %} {{ " ### Tools " }} You may call one or more functions to assist with the user query. {% if _xml_tools_list %} You are provided with function signatures within tags: {% for tool in _xml_tools_list %} {{ tool if tool is string else tool | tojson }}{{ " " }} {% endfor %} For each function call, pass a json object with function name and arguments within tags: {"name": , "arguments": } {% endif %} {% if _python_tools %} When you send a message containing Python code between <|python_tag|> and <|eom_id|> tags, it will be executed in a stateful Jupyter notebook environment, and you will then be given the output. You can use the following tools in your python code like regular functions: {% for tool in _python_tools %} {{ tool if tool is string else tool | tojson }}{{ " " }} {% endfor %} {% endif %} {% endif %} <|eot_id|> {%- endif -%} {%- macro text(content) -%} {%- if content is string -%} {{- content -}} {%- elif content is mapping -%} {{- content.get('text', '') -}} {%- elif content is iterable -%} {%- for chunk in content if chunk.get('type') == 'text' -%} {{- chunk.text -}} {%- endfor -%} {%- endif -%} {%- endmacro -%} {%- set tool_names = namespace(by_id={}) -%} {%- macro tool_calls(calls) -%} {%- for call in calls -%} {%- set function = call.function -%} {%- if call.get('id') -%} {%- set tool_names.by_id = dict(tool_names.by_id, **{call.id: function.name}) -%} {%- endif -%} {{- ' {"name": ' -}} {{- function.name | tojson -}} {{- ', "arguments": ' -}} {{- function.arguments if function.arguments is string else function.arguments | tojson -}} {{- '} ' -}} {%- endfor -%} {%- endmacro -%} {%- for message in messages -%} {%- set content = message.get('content') -%} {%- set text_content = content is string or (content is mapping and content.get('text') is string) or (content is sequence and content is not string and content is not mapping and content | selectattr('type', 'equalto', 'text') | list | length == content | length) -%} {{- '<|start_header_id|>' ~ message.role ~ '<|end_header_id|> ' -}} {%- if message.role == 'assistant' -%} {% generation %} {%- if message.get('reasoning_content') -%} {{- '<|start_think|>' ~ message.reasoning_content ~ '<|end_think|>' -}} {%- endif -%} {{- text(content) | trim -}} {{- tool_calls(message.get('tool_calls') or []) -}} {{- '<|eot_id|>' -}} {% endgeneration %} {%- elif message.role == 'tool' -%} {{- '' -}} {{- text(content) if text_content else content | tojson if content is not none else '' -}} {{- '<|eot_id|> ' -}} {%- elif message.role == 'ipython' -%} {{- {"output": text(content) if text_content else content} | tojson -}} {{- '<|eot_id|> ' -}} {%- else -%} {{- (text(content) | trim) ~ '<|eot_id|> ' -}} {%- endif -%} {%- endfor -%} {%- if add_generation_prompt -%} {{- '<|start_header_id|>assistant<|end_header_id|> ' -}} {%- endif -%}