tool_chat_template_deepseekv32.jinja 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. {% if tools is defined and tools is not none %}
  20. {% set tool_ns = namespace(text='## Tools\nYou have access to the following tools:\n') %}
  21. {% for tool in tools %}
  22. {% set tool_ns.text = tool_ns.text + '\n### ' + tool.function.name + '\nDescription: ' + tool.function.description + '\n\nParameters: ' + (tool.function.parameters | tojson) + '\n' %}
  23. {% endfor %}
  24. {% 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" %}
  25. {% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
  26. {% endif %}
  27. {{ bos_token }}{{ ns.system_prompt }}
  28. {%- for message in messages %}
  29. {%- if message['role'] == 'user' %}
  30. {%- set ns.is_tool = false -%}
  31. {%- set ns.is_first = false -%}
  32. {%- set ns.is_last_user = true -%}
  33. {{'<|User|>' + message['content']}}
  34. {%- endif %}
  35. {%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
  36. {%- if ns.is_last_user or ns.is_only_sys %}
  37. {{'<|Assistant|></think>'}}
  38. {%- endif %}
  39. {%- set ns.is_last_user = false -%}
  40. {%- set ns.is_first = false %}
  41. {%- set ns.is_tool = false -%}
  42. {%- for tool in message['tool_calls'] %}
  43. {%- set formatted_args = tool['function']['arguments'] if tool['function']['arguments'] is string else tool['function']['arguments']|tojson %}
  44. {%- if not ns.is_first %}
  45. {%- if message['content'] is none %}
  46. {{'<|tool▁calls▁begin|><|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + formatted_args + '<|tool▁call▁end|>'}}
  47. {%- else %}
  48. {{message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['function']['name'] + '<|tool▁sep|>' + formatted_args + '<|tool▁call▁end|>'}}
  49. {%- endif %}
  50. {%- set ns.is_first = true -%}
  51. {%- else %}
  52. {{'<|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + formatted_args + '<|tool▁call▁end|>'}}
  53. {%- endif %}
  54. {%- endfor %}
  55. {{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}
  56. {%- endif %}
  57. {%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none) %}
  58. {%- if ns.is_last_user %}
  59. {{'<|Assistant|>'}}
  60. {%- if message['prefix'] is defined and message['prefix'] and thinking %}
  61. {{'<think>'}}
  62. {%- else %}
  63. {{'</think>'}}
  64. {%- endif %}
  65. {%- endif %}
  66. {%- if message['prefix'] is defined and message['prefix'] %}
  67. {%- set ns.is_prefix = true -%}
  68. {%- endif %}
  69. {%- set ns.is_last_user = false -%}
  70. {%- if ns.is_tool %}
  71. {{message['content'] + '<|end▁of▁sentence|>'}}
  72. {%- set ns.is_tool = false -%}
  73. {%- else %}
  74. {%- set content = message['content'] -%}
  75. {%- if '</think>' in content %}
  76. {%- set content = content.split('</think>', 1)[1] -%}
  77. {%- endif %}
  78. {{content + '<|end▁of▁sentence|>'}}
  79. {%- endif %}
  80. {%- endif %}
  81. {%- if message['role'] == 'tool' %}
  82. {%- set ns.is_last_user = false -%}
  83. {%- set ns.is_tool = true -%}
  84. {{'<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
  85. {%- endif %}
  86. {%- if message['role'] != 'system' %}
  87. {% set ns.is_only_sys = false %}
  88. {%- endif %}
  89. {%- endfor -%}
  90. {% if add_generation_prompt and not ns.is_tool%}
  91. {% if ns.is_last_user or ns.is_only_sys or not ns.is_prefix %}
  92. {{'<|Assistant|>'}}
  93. {%- if not thinking %}
  94. {{'</think>'}}
  95. {%- else %}
  96. {{'<think>'}}
  97. {%- endif %}
  98. {% endif %}
  99. {% endif %}