llama-cpp-deepseek-r1.jinja 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. {%- if not add_generation_prompt is defined -%}
  2. {%- set add_generation_prompt = false -%}
  3. {%- endif -%}
  4. {%- set ns = namespace(is_first=false, is_tool_outputs=false, is_output_first=true, system_prompt='') -%}
  5. {%- for message in messages -%}
  6. {%- if message['role'] == 'system' -%}
  7. {%- set ns.system_prompt = message['content'] -%}
  8. {%- endif -%}
  9. {%- endfor -%}
  10. {{bos_token}}
  11. {%- if tools %}
  12. You can call any of the following function tools to satisfy the user's requests: {{tools | map(attribute='function') | tojson(indent=2)}}
  13. Example function tool call syntax:
  14. <|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>example_function_name
  15. ```json
  16. {
  17. "arg1": "some_value"
  18. ...
  19. }
  20. ```
  21. <|tool▁call▁end|><|tool▁calls▁end|>
  22. {% endif -%}
  23. {{ns.system_prompt}}
  24. {%- macro flush_tool_outputs() -%}
  25. {%- if ns.is_tool_outputs -%}
  26. {{- '<|tool▁outputs▁end|><|end▁of▁sentence|>' -}}
  27. {%- set ns.is_tool_outputs = false -%}
  28. {%- endif -%}
  29. {%- endmacro -%}
  30. {{- flush_tool_outputs() -}}
  31. {%- for message in messages -%}
  32. {%- if message['role'] != 'tool' -%}
  33. {{- flush_tool_outputs() -}}
  34. {%- endif -%}
  35. {%- if message['role'] == 'user' -%}
  36. {{- '<|User|>' + message['content'] + '<|end▁of▁sentence|>' -}}
  37. {%- endif -%}
  38. {%- if message['role'] == 'assistant' and message['content'] is none -%}
  39. {{- '<|Assistant|><|tool▁calls▁begin|>' -}}
  40. {%- set ns.is_first = true -%}
  41. {%- for tc in message['tool_calls'] -%}
  42. {%- if ns.is_first -%}
  43. {%- set ns.is_first = false -%}
  44. {%- else -%}
  45. {{- '\n' -}}
  46. {%- endif -%}
  47. {%- set tool_name = tc['function']['name'] -%}
  48. {%- set tool_args = tc['function']['arguments'] -%}
  49. {{- '<|tool▁call▁begin|>' + tc['type'] + '<|tool▁sep|>' + tool_name + '\n' + '```json' + '\n' + tool_args + '\n' + '```' + '<|tool▁call▁end|>' -}}
  50. {%- endfor -%}
  51. {{- '<|tool▁calls▁end|><|end▁of▁sentence|>' -}}
  52. {%- endif -%}
  53. {%- if message['role'] == 'assistant' and message['content'] is not none -%}
  54. {{- flush_tool_outputs() -}}
  55. {%- set content = message['content'] -%}
  56. {%- if '</think>' in content -%}
  57. {%- set content = content.split('</think>')[-1] -%}
  58. {%- endif -%}
  59. {{- '<|Assistant|>' + content + '<|end▁of▁sentence|>' -}}
  60. {%- endif -%}
  61. {%- if message['role'] == 'tool' -%}
  62. {%- set ns.is_tool_outputs = true -%}
  63. {%- if ns.is_output_first -%}
  64. {{- '<|tool▁outputs▁begin|>' -}}
  65. {%- set ns.is_output_first = false -%}
  66. {%- endif -%}
  67. {{- '\n<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>' -}}
  68. {%- endif -%}
  69. {%- endfor -%}
  70. {{- flush_tool_outputs() -}}
  71. {%- if add_generation_prompt and not ns.is_tool_outputs -%}
  72. {{- '<|Assistant|><think>\n' -}}
  73. {%- endif -%}