|
| 1 | +{% if not add_generation_prompt is defined %} |
| 2 | + {% set add_generation_prompt = false %} |
| 3 | +{% endif %} |
| 4 | +{% if not thinking is defined %} |
| 5 | + {% set thinking = false %} |
| 6 | +{% endif %} |
| 7 | +{% set ns = namespace(is_first=false, is_tool=false, system_prompt='', is_first_sp=true, is_last_user=false, is_only_sys=false, is_prefix=false) %} |
| 8 | +{%- for message in messages %} |
| 9 | + {%- if message['role'] == 'system' %} |
| 10 | + {%- if ns.is_first_sp %} |
| 11 | + {% set ns.system_prompt = ns.system_prompt + message['content'] %} |
| 12 | + {% set ns.is_first_sp = false %} |
| 13 | + {%- else %} |
| 14 | + {% set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %} |
| 15 | + {%- endif %} |
| 16 | + {% set ns.is_only_sys = true %} |
| 17 | + {%- endif %} |
| 18 | +{%- endfor %} |
| 19 | + |
| 20 | +{% if tools is defined and tools is not none %} |
| 21 | + {% set tool_ns = namespace(text='## Tools\nYou have access to the following tools:\n') %} |
| 22 | + {% for tool in tools %} |
| 23 | + {% set tool_ns.text = tool_ns.text + '\n### ' + tool.function.name + '\nDescription: ' + tool.function.description + '\n\nParameters: ' + (tool.function.parameters | tojson) + '\n' %} |
| 24 | + {% endfor %} |
| 25 | + {% set tool_ns.text = tool_ns.text + "\nIMPORTANT: ALWAYS adhere to this exact format for tool use:\n<|tool▁calls▁begin|><|tool▁call▁begin|>tool_call_name<|tool▁sep|>tool_call_arguments<|tool▁call▁end|>{{additional_tool_calls}}<|tool▁calls▁end|>\n\nWhere:\n\n- `tool_call_name` must be an exact match to one of the available tools\n- `tool_call_arguments` must be valid JSON that strictly follows the tool's Parameters Schema\n- For multiple tool calls, chain them directly without separators or spaces\n" %} |
| 26 | + {% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %} |
| 27 | +{% endif %} |
| 28 | + |
| 29 | +{{ bos_token }}{{ ns.system_prompt }} |
| 30 | +{%- for message in messages %} |
| 31 | + {%- if message['role'] == 'user' %} |
| 32 | + {%- set ns.is_tool = false -%} |
| 33 | + {%- set ns.is_first = false -%} |
| 34 | + {%- set ns.is_last_user = true -%} |
| 35 | + {{'<|User|>' + message['content']}} |
| 36 | + {%- endif %} |
| 37 | + {%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %} |
| 38 | + {%- if ns.is_last_user or ns.is_only_sys %} |
| 39 | + {{'<|Assistant|></think>'}} |
| 40 | + {%- endif %} |
| 41 | + {%- set ns.is_last_user = false -%} |
| 42 | + {%- set ns.is_first = false %} |
| 43 | + {%- set ns.is_tool = false -%} |
| 44 | + {%- for tool in message['tool_calls'] %} |
| 45 | + {%- if not ns.is_first %} |
| 46 | + {%- if message['content'] is none %} |
| 47 | + {{'<|tool▁calls▁begin|><|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}} |
| 48 | + {%- else %} |
| 49 | + {{message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}} |
| 50 | + {%- endif %} |
| 51 | + {%- set ns.is_first = true -%} |
| 52 | + {%- else %} |
| 53 | + {{'<|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}} |
| 54 | + {%- endif %} |
| 55 | + {%- endfor %} |
| 56 | + {{'<|tool▁calls▁end|><|end▁of▁sentence|>'}} |
| 57 | + {%- endif %} |
| 58 | + {%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none) %} |
| 59 | + {%- if ns.is_last_user %} |
| 60 | + {{'<|Assistant|>'}} |
| 61 | + {%- if message['prefix'] is defined and message['prefix'] and thinking %} |
| 62 | + {{'<think>'}} |
| 63 | + {%- else %} |
| 64 | + {{'</think>'}} |
| 65 | + {%- endif %} |
| 66 | + {%- endif %} |
| 67 | + {%- if message['prefix'] is defined and message['prefix'] %} |
| 68 | + {%- set ns.is_prefix = true -%} |
| 69 | + {%- endif %} |
| 70 | + {%- set ns.is_last_user = false -%} |
| 71 | + {%- if ns.is_tool %} |
| 72 | + {{message['content'] + '<|end▁of▁sentence|>'}} |
| 73 | + {%- set ns.is_tool = false -%} |
| 74 | + {%- else %} |
| 75 | + {%- set content = message['content'] -%} |
| 76 | + {%- if '</think>' in content %} |
| 77 | + {%- set content = content.split('</think>', 1)[1] -%} |
| 78 | + {%- endif %} |
| 79 | + {{content + '<|end▁of▁sentence|>'}} |
| 80 | + {%- endif %} |
| 81 | + {%- endif %} |
| 82 | + {%- if message['role'] == 'tool' %} |
| 83 | + {%- set ns.is_last_user = false -%} |
| 84 | + {%- set ns.is_tool = true -%} |
| 85 | + {{'<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}} |
| 86 | + {%- endif %} |
| 87 | + {%- if message['role'] != 'system' %} |
| 88 | + {% set ns.is_only_sys = false %} |
| 89 | + {%- endif %} |
| 90 | +{%- endfor -%} |
| 91 | +{% if add_generation_prompt and not ns.is_tool%} |
| 92 | + {% if ns.is_last_user or ns.is_only_sys or not ns.is_prefix %} |
| 93 | + {{'<|Assistant|>'}} |
| 94 | + {%- if not thinking %} |
| 95 | + {{'</think>'}} |
| 96 | + {%- else %} |
| 97 | + {{'<think>'}} |
| 98 | + {%- endif %} |
| 99 | + {% endif %} |
| 100 | +{% endif %} |
0 commit comments