| {%- if api %} |
| {{- '<think>system\n' }} |
| {%- if messages[0]['role'] == 'system' %} |
| {{- messages[0]['content'] }} |
| {%- else %} |
| {{- 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.' }} |
| {%- endif %} |
| {{- "\n\n# api\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <api></api> XML tags:\n<api>" }} |
| {%- for tool in api %} |
| {{- "\n" }} |
| {{- tool | tojson }} |
| {%- endfor %} |
| {{- "\n</api>\n\nFor each function call, return a json object with function name and arguments within <api></api> XML tags:\n<api>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</api></think>\n" }} |
| {%- else %} |
| {%- if messages[0]['role'] == 'system' %} |
| {{- '<think>system\n' + messages[0]['content'] + '</think>\n' }} |
| {%- else %} |
| {{- '<think>system\n{system_message}</think>\n' }} |
| {%- endif %} |
| {%- endif %} |
| {%- for message in messages %} |
| {%- if (message.role == "user") or (message.role == "system" and not loop.first) or (message.role == "assistant" and not message.apis) %} |
| {{- '<think>' + message.role + '\n' + message.content + '</think>' + '\n' }} |
| {%- elif message.role == "assistant" %} |
| {{- '<think>' + message.role }} |
| {%- if message.content %} |
| {{- '\n' + message.content }} |
| {%- endif %} |
| {%- for api in message.apis %} |
| {%- if api.function is defined %} |
| {%- set api = api.function %} |
| {%- endif %} |
| {{- '\n<api>\n{"name": "' }} |
| {{- api.name }} |
| {{- '", "arguments": ' }} |
| {{- api.arguments | tojson }} |
| {{- '}\n</api>' }} |
| {%- endfor %} |
| {{- '</think>\n' }} |
| {%- elif message.role == "tool" %} |
| {%- if (loop.index0 == 0) or (messages[loop.index0 - 1].role != "tool") %} {{- '<think>user' }} |
| {%- endif %} |
| {{- '\n<tool_response>\n' }} |
| {{- message.content }} |
| {{- '\n</tool_response>' }} |
| {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %} |
| {{- '</think>\n' }} |
| {%- endif %} |
| {%- endif %} |
| {%- endfor %} |
| {%- if add_generation_prompt %} |
| {{- '<think>assistant\n' }} |
| {%- endif %} |