{%- set system_start = '<|im_start|>system\n' -%} {%- set user_start = '<|im_start|>user\n' -%} {%- set assistant_start = '<|im_start|>assistant\n' -%} {%- set system_end = '<|im_end|>\n' -%} {%- set user_end = '<|im_end|>\n' -%} {%- set assistant_end = '<|im_end|>\n' -%} {%- set tool_defs_start = '<|tools_start|>\n' -%} {%- set tool_defs_end = '\n<|tools_end|>' -%} {%- set tool_call_start = '\n<|tool_start|>\n' -%} {%- set tool_call_end = '\n<|tool_end|>' -%} {%- set tool_result_start = '<|im_start|>tool\n' -%} {%- set tool_result_sep = '\n' -%} {%- set tool_result_end = '\n<|im_end|>\n' -%} {%- set reasoning_start = '<|thinking|>\n' -%} {%- set reasoning_end = '\n<|thinking_end|>\n' -%} {%- set default_system_prompt = "" -%} {%- set tool_defs_preamble = "You are provided with function signatures within " + tool_defs_start.strip() + tool_defs_end.strip() + "\n" -%} {%- set tool_defs_postamble = "\nFor each function call, call them within " + tool_call_start.strip() + tool_call_end.strip() + "." -%} {%- set tool_defs_style = "python" -%} {%- set tool_call_style = "python" -%} {%- set content_ns = namespace(content='') %} {%- if messages[0].role == 'system' %} {%- if messages[0].content is string %} {%- set content_ns.content = messages[0].content %} {%- elif messages[0].content is iterable %} {%- set content_ns.content = '' %} {%- for part in messages[0].content %} {%- set content_ns.content = content_ns.content + part.text %} {%- endfor %} {%- else %} {%- set content_ns.content = '' %} {%- endif %} {%- set messages = messages[1:] %} {%- else %} {%- set content_ns.content = default_system_prompt %} {%- endif %} {%- macro json_to_python_type(json_spec) %} {%- set basic_type_map = { "string": "str", "number": "float", "integer": "int", "boolean": "bool" } %} {%- if basic_type_map[json_spec.type] is defined %} {{- basic_type_map[json_spec.type] }} {%- elif json_spec.type == "array" %} {{- "List[" + json_to_python_type(json_spec.items) + "]"}} {%- elif json_spec.type == "object" %} {{- "Dict[str, " + json_to_python_type(json_spec.additionalProperties) + ']'}} {%- elif json_spec.type is iterable %} {{- "Union[" }} {%- for t in json_spec.type %} {{- json_to_python_type({"type": t}) }} {%- if not loop.last %} {{- "," }} {%- endif %} {%- endfor %} {{- "]" }} {%- else %} {{- "Any" }} {%- endif %} {%- endmacro %} {%- macro json_to_python_value(json_value) %} {%- if json_value is string %} {{- json_value|tojson }} {%- elif json_value is integer or json_value is float %} {{- json_value|tojson }} {%- elif json_value is boolean %} {%- if json_value %} {{- "True" }} {%- else %} {{- "False" }} {%- endif %} {%- elif json_value is iterable %} {{- "[" }} {%- for item in json_value %} {{- json_to_python_value(item) }} {%- if not loop.last %} {{- ',' }} {%- endif %} {%- endfor %} {{- "]" }} {%- else %} {{- "None" }} {%- endif %} {%- endmacro%} {%- macro python_tools_parser(tools) %} {%- for tool in tools %} {%- if loop.index0 != 0 %} {{- '\n\n'}} {%- endif %} {%- if tool.function is defined %} {%- set tool = tool.function %} {%- endif %} {{-'def ' + tool.name + '('}} {%- for param_name, param_fields in tool.parameters.properties|items %} {%- if loop.index0 != 0 %} {{- ', '}} {%- endif %} {{-param_name + ": "}} {%- if not param_name in tool.parameters.required %} {{-'Optional[' + json_to_python_type(param_fields) + '] = None'}} {%- else %} {{- json_to_python_type(param_fields) }} {%- endif %} {%- endfor %} {{- ') -> str:\n """'}} {{- tool.description }} {%- if tool.parameters.properties|length != 0 %} {{- '\n\n Args:\n '}} {%- for param_name, param_fields in tool.parameters.properties|items %} {%- if loop.index0 != 0 %} {{- '\n ' }} {%- endif %} {{- param_name + ' ('}} {%- if not param_name in tool.parameters.required %} {{-'Optional[' + json_to_python_type(param_fields) + ']'}} {%- else %} {{- json_to_python_type(param_fields) }} {%- endif %} {{- ')' }} {%- if param_fields.description %} {{- ': ' + param_fields.description }} {%- endif%} {%- endfor %} {%- endif %} {{- '\n """\n pass' }} {%- endfor %} {%- endmacro %} {%- macro python_tool_call_parser(tool_call) %} {{- tool_call.name + '(' }} {%- if tool_call.arguments is string %} {{- raise_exception("Python tool call parser doesn't support arguments as a json string") }} {%- endif %} {%- for param_name, param_field in tool_call.arguments|items %} {{- param_name + '= '}} {{- json_to_python_value(param_field) }} {%- if not loop.last %} {{- ', '}} {%- endif %} {%- endfor %} {{- ')' }} {%- endmacro %} {%- macro json_tools_parser(tools) %} {%- for tool in tools %} {%- if not loop.first %} {{- "\n" }} {%- endif %} {{- tool | tojson }} {%- endfor %} {%- endmacro %} {%- macro json_tool_call_parser(tool_call) %} {{- '{"name": "' }} {{- tool_call.name }} {{- '", "arguments": ' }} {%- if tool_call.arguments is string %} {{- tool_call.arguments }} {%- else %} {{- tool_call.arguments | tojson }} {%- endif %} {{- '}' }} {%- endmacro %} {%- if tools %} {{- system_start }} {%- if content_ns.content | length > 0 %} {{- content_ns.content + '\n\n' }} {%- endif %} {{- tool_defs_preamble + tool_defs_start }} {%- if tool_defs_style == "json" %} {{- json_tools_parser(tools) }} {%- elif tool_defs_style == "python" %} {{- python_tools_parser(tools) }} {%- else %} {{- raise_exception("Invalid tool definition style (" + tool_defs_style + "), must be 'json' or 'python'") }} {%- endif %} {{- tool_defs_end + tool_defs_postamble }} {%- if (messages|length == 0) and not add_generation_prompt %} {{- system_end.rstrip('\n') }} {%- else %} {{- system_end }} {%- endif %} {%- else %} {%- if content_ns.content | length > 0 %} {{- system_start + content_ns.content }} {%- if (messages|length == 0) and not add_generation_prompt %} {{- system_end.rstrip('\n') }} {%- else %} {{- system_end }} {%- endif %} {%- endif%} {%- endif %} {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %} {%- for message in messages[::-1] %} {%- set index = (messages|length - 1) - loop.index0 %} {%- if ns.multi_step_tool and message.role == "user" and message.content is string and not(message.content.startswith(tool_result_start) and message.content.endswith(tool_result_end)) %} {%- set ns.multi_step_tool = false %} {%- set ns.last_query_index = index %} {%- endif %} {%- endfor %} {%- for message in messages %} {%- if message.content is string %} {%- set content_ns.content = message.content %} {%- elif message.content is iterable %} {%- set content_ns.content = '' %} {%- for part in message.content %} {%- set content_ns.content = content_ns.content + part.text %} {%- endfor %} {%- else %} {%- set content_ns.content = '' %} {%- endif %} {%- if message.role == "system" %} {{- system_start }} {%- if loop.last and not add_generation_prompt %} {%- set end_marker = system_end.rstrip('\n') %} {%- else %} {%- set end_marker = system_end %} {%- endif %} {%- if message.content is string %} {{- content_ns.content + end_marker }} {%- else %} {%- for item in message.content %} {%- if item.mask is defined and not item.mask %} {%- generation %} {{- item.text }} {%- if loop.last %} {{- end_marker }} {%- endif %} {%- endgeneration %} {%- else %} {{- item.text }} {%- if loop.last %} {{- end_marker }} {%- endif %} {%- endif %} {%- endfor %} {%- endif %} {%- elif message.role == "user" %} {{- user_start }} {%- if loop.last and not add_generation_prompt %} {%- set end_marker = user_end.rstrip('\n') %} {%- else %} {%- set end_marker = user_end %} {%- endif %} {%- if message.content is string %} {{- content_ns.content + end_marker }} {%- else %} {%- for item in message.content %} {%- if item.mask is defined and not item.mask %} {%- generation %} {{- item.text }} {%- if loop.last %} {{- end_marker }} {%- endif %} {%- endgeneration %} {%- else %} {{- item.text }} {%- if loop.last %} {{- end_marker }} {%- endif %} {%- endif %} {%- endfor %} {%- endif %} {%- elif message.role == "assistant" %} {%- set reasoning_content = none %} {%- if message.reasoning_content is string %} {%- set reasoning_content = message.reasoning_content %} {%- else %} {%- if reasoning_start in content_ns.content %} {%- set reasoning_content = content_ns.content.split(reasoning_end)[0].rstrip('\n').split(reasoning_start)[-1].lstrip('\n') %} {%- set content = content_ns.content.split(reasoning_end)[-1].lstrip('\n') %} {%- endif %} {%- endif %} {{- assistant_start }} {%- if loop.index0 > ns.last_query_index %} {%- if loop.last and reasoning_content %} {%- set reasoning_end_marker = reasoning_end %} {%- if content_ns.content is string %} {%- set content_ns.content = content_ns.content.lstrip('\n') %} {%- if content_ns.content == '' %} {%- set reasoning_end_marker = reasoning_end.rstrip('\n') %} {%- endif %} {%- endif %} {%- generation %} {{- reasoning_start + reasoning_content.strip('\n') + reasoning_end_marker }} {%- endgeneration %} {%- endif %} {%- endif %} {%- if message.content is string %} {%- generation %} {{- content_ns.content }} {%- endgeneration %} {%- else %} {%- for item in message.content %} {%- if not item.mask is defined or not item.mask %} {%- generation %} {{- item.text }} {%- endgeneration %} {%- else %} {{- item.text }} {%- endif %} {%- endfor %} {%- endif %} {%- generation %} {%- if message.tool_calls %} {%- for tool_call in message.tool_calls %} {%- if tool_call.function %} {%- set tool_call = tool_call.function %} {%- endif %} {%- if (loop.first and content_ns.content) or (not loop.first) %} {{- tool_call_start }} {%- else %} {{- tool_call_start.lstrip('\n') }} {%- endif %} {%- if tool_call_style == 'json' %} {{- json_tool_call_parser(tool_call) }} {%- elif tool_call_style == 'python' %} {{- python_tool_call_parser(tool_call) }} {%- endif %} {{- tool_call_end }} {%- endfor %} {%- endif %} {%- endgeneration %} {%- if not continue_final_message %} {%- if loop.last %} {%- set end_marker = assistant_end.rstrip('\n') %} {%- else %} {%- set end_marker = assistant_end %} {%- endif %} {%- endif %} {%- if message.content is string or (message.content is defined and (not message.content[message.content|length - 1].mask is defined or not message.content[message.content|length - 1].mask)) or (message.tool_calls) %} {%- generation%} {{- end_marker }} {%- endgeneration %} {%- else %} {{- end_marker }} {%- endif %} {%- elif message.role == "tool" %} {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %} {{- tool_result_start }} {%- else %} {{- tool_result_sep }} {%- endif %} {{- content_ns.content }} {%- if loop.last %} {{- tool_result_end.rstrip('\n') }} {%- elif (messages[loop.index0 + 1].role != "tool") %} {{- tool_result_end.rstrip('\n') }} {%- endif %} {%- endif %} {%- endfor %} {%- if add_generation_prompt %} {{- assistant_start }} {%- if enable_thinking is defined and enable_thinking is true %} {%- generation %} {{- reasoning_start }} {%- endgeneration %} {%- endif %} {%- endif -%}