algorithms-learning/correctional-GPTaM
092
1{{- bos_token -}}2{%- set preserve_thinking = preserve_thinking | default(false) -%}3 4{%- macro format_arg_value(arg_value) -%}5 {%- if arg_value is string -%}6 {{- "'" + arg_value + "'" -}}7 {%- elif arg_value is mapping -%}8 {{- arg_value | tojson -}}9 {%- else -%}10 {{- arg_value | string -}}11 {%- endif -%}12{%- endmacro -%}13 14{%- macro parse_content(content) -%}15 {%- if content is string -%}16 {{- content -}}17 {%- else -%}18 {%- set _ns = namespace(result="") -%}19 {%- for item in content -%}20 {%- if item["type"] == "image" -%}21 {%- set _ns.result = _ns.result + "<image>" -%}22 {%- elif item["type"] == "text" -%}23 {%- set _ns.result = _ns.result + item["text"] -%}24 {%- else -%}25 {%- set _ns.result = _ns.result + item | tojson -%}26 {%- endif -%}27 {%- endfor -%}28 {{- _ns.result -}}29 {%- endif -%}30{%- endmacro -%}31 32{%- macro render_tool_calls(tool_calls) -%}33 {%- set tool_calls_ns = namespace(tool_calls=[]) -%}34 {%- for tool_call in tool_calls -%}35 {%- set func_name = tool_call["function"]["name"] -%}36 {%- set func_args = tool_call["function"]["arguments"] -%}37 {%- set args_ns = namespace(arg_strings=[]) -%}38 {%- for arg_name, arg_value in func_args.items() -%}39 {%- set args_ns.arg_strings = args_ns.arg_strings + [arg_name + "=" + format_arg_value(arg_value)] -%}40 {%- endfor -%}41 {%- set tool_calls_ns.tool_calls = tool_calls_ns.tool_calls + [func_name + "(" + (args_ns.arg_strings | join(", ")) + ")"] -%}42 {%- endfor -%}43 {{- "<|tool_call_start|>[" + (tool_calls_ns.tool_calls | join(", ")) + "]<|tool_call_end|>" -}}44{%- endmacro -%}45 46{%- set ns = namespace(system_prompt="", last_user_index=-1) -%}47{%- if messages[0]["role"] == "system" -%}48 {%- if messages[0].get("content") -%}49 {%- set ns.system_prompt = parse_content(messages[0]["content"]) -%}50 {%- endif -%}51 {%- set messages = messages[1:] -%}52{%- endif -%}53{%- if tools -%}54 {%- set ns.system_prompt = ns.system_prompt + ("\n\n" if ns.system_prompt else "") + "Today's date: " + strftime_now("%Y-%m-%d") + "\n\nList of tools: " + (tools | tojson) -%}55{%- endif -%}56{%- if ns.system_prompt -%}57 {{- "<|im_start|>system\n" + ns.system_prompt + "<|im_end|>\n" -}}58{%- endif -%}59{%- for message in messages -%}60 {%- if message["role"] == "user" -%}61 {%- set ns.last_user_index = loop.index0 -%}62 {%- endif -%}63{%- endfor -%}64{%- for message in messages -%}65 {{- "<|im_start|>" + message.role + "\n" -}}66 {%- if message.role == "assistant" -%}67 {%- generation -%}68 {%- if message.thinking is defined and (preserve_thinking or loop.index0 > ns.last_user_index) -%}69 {{- "<think>" + message.thinking + "</think>" -}}70 {%- endif -%}71 {%- set _cfm_tag = "CONTINUE_FINAL_MESSAGE_TAG " -%}72 {%- set _has_cfm = false -%}73 {%- if message.content is defined -%}74 {%- set content = parse_content(message.content) -%}75 {%- if not (preserve_thinking or loop.index0 > ns.last_user_index) -%}76 {%- if "</think>" in content -%}77 {%- set content = content.split("</think>")[-1] | trim -%}78 {%- endif -%}79 {%- endif -%}80 {%- if message.tool_calls is defined and content.endswith(_cfm_tag) -%}81 {%- set _has_cfm = true -%}82 {%- set _trunc_len = (content | length) - (_cfm_tag | length) -%}83 {{- content[:_trunc_len] -}}84 {%- else -%}85 {{- content -}}86 {%- endif -%}87 {%- endif -%}88 {%- if message.tool_calls is defined -%}89 {{- render_tool_calls(message.tool_calls) -}}90 {%- endif -%}91 {%- if _has_cfm -%}92 {{- _cfm_tag -}}93 {%- endif -%}94 {{- "<|im_end|>\n" -}}95 {%- endgeneration -%}96 {%- else %}97 {%- if message.get("content") -%}98 {{- parse_content(message["content"]) -}}99 {%- endif -%}100 {{- "<|im_end|>\n" -}}101 {%- endif %}102{%- endfor -%}103{%- if add_generation_prompt -%}104 {{- "<|im_start|>assistant\n" -}}105{%- endif -%}106 