File size: 4,562 Bytes
2ec42d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{# ==================================================================== #}
{#  Deepseek v3 template with enable_thinking and tools support          #}
{# ==================================================================== #}
{%- if not enable_thinking is defined %}{% set enable_thinking = true %}{% endif -%}
{%- if not tools is defined %}{% set tools = none %}{% endif -%}
{%- if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif -%}

{# --------------------------- Collect system prompt -------------------- #}
{%- set ns = namespace(system_prompt='', is_last_user=false, outputs_open=false, first_output=true) -%}

{%- if messages and messages[0].role == 'system' -%}
  {%- set raw = messages[0].content -%}
  {%- set ns.system_prompt = raw if raw is string else raw[0].text -%}
  {%- set messages = messages[1:] -%}
{%- endif -%}

{# --------------------------- Inject deep thinking --------------------- #}
{%- if enable_thinking -%}
  {%- set ns.system_prompt = ns.system_prompt and 'Enable deep thinking subroutine.

' ~ ns.system_prompt or 'Enable deep thinking subroutine.' -%}
{%- endif -%}

{# --------------------------- Append tools block ----------------------- #}
{%- if tools is not none -%}
  {%- if ns.system_prompt -%}
    {%- set ns.system_prompt = ns.system_prompt ~ '

You have the following functions available:

' -%}
  {%- else -%}
    {%- set ns.system_prompt = 'You have the following functions available:

' -%}
  {%- endif -%}
  {%- for t in tools -%}
    {%- set ns.system_prompt = ns.system_prompt ~ "```json
" ~ (t | tojson(indent=4)) ~ "
```

" -%}
  {%- endfor -%}
{%- endif -%}

{{- bos_token -}}{{- ns.system_prompt -}}

{# --------------------------- Iterate conversation --------------------- #}
{%- for m in messages -%}
  {# --------------------------- USER ---------------------------------- #}
  {%- if m.role == 'user' -%}
    {%- set ns.is_last_user = true -%}
    {%- set txt = m.content if m.content is string else m.content | selectattr('type','equalto','text') | map(attribute='text') | join('') -%}
    {{- "<|User|>" -}}{{- txt -}}{{- "<|Assistant|>" -}}
  {%- endif -%}

  {# --------------------------- ASSISTANT with TOOL CALLS -------------- #}
  {%- if m.role == 'assistant' and m.tool_calls is defined and m.tool_calls -%}
    {%- set ns.is_last_user = false -%}
    {%- set lead = m.content is string and m.content|trim or (m.content and m.content | selectattr('type','equalto','text') | map(attribute='text') | join('')) or '' -%}
    {{- lead -}}{{- "<|tool▁calls▁begin|>" -}}
    {%- for call in m.tool_calls -%}
      {{- "<|tool▁call▁begin|>" -}}{{- call.type -}}{{- "<|tool▁sep|>" -}}{{- call.function.name -}}
      {{- "
```json
" -}}{{- call.function.arguments -}}{{- "
```" -}}{{- "<|tool▁call▁end|>" -}}
      {%- if not loop.last -%}{{- "
" -}}{%- endif -%}
    {%- endfor -%}
    {{- "<|tool▁calls▁end|>" -}}{{- "<|end▁of▁sentence|>" -}}
  {%- endif -%}

  {# --------------------------- ASSISTANT plain ------------------------ #}
  {%- if m.role == 'assistant' and (m.tool_calls is not defined or not m.tool_calls) -%}
    {%- set ns.is_last_user = false -%}
    {%- set txt = m.content if m.content is string else m.content | selectattr('type','equalto','text') | map(attribute='text') | join('') -%}
    {{- txt -}}{{- "<|end▁of▁sentence|>" -}}
  {%- endif -%}

  {# --------------------------- TOOL output ---------------------------- #}
  {%- if m.role == 'tool' -%}
    {%- set ns.is_last_user = false -%}
    {%- set out_txt = m.content if m.content is string else m.content | selectattr('type','equalto','text') | map(attribute='text') | join('') -%}
    {%- if not ns.outputs_open -%}
      {{- "<|tool▁outputs▁begin|>" -}}
      {%- set ns.outputs_open = true -%}
    {%- endif -%}
    {{- "<|tool▁output▁begin|>" -}}{{- out_txt -}}{{- "<|tool▁output▁end|>" -}}
    {%- if loop.nextitem is defined and loop.nextitem.role == 'tool' -%}
      {{- "
" -}}
    {%- endif -%}
    {%- if loop.nextitem is undefined or loop.nextitem.role != 'tool' -%}
      {{- "<|tool▁outputs▁end|>" -}}
      {%- set ns.outputs_open = false -%}
    {%- endif -%}
  {%- endif -%}
{%- endfor -%}

{%- if ns.outputs_open -%}
  {{- "<|tool▁outputs▁end|>" -}}
{%- endif -%}

{%- if add_generation_prompt and not ns.is_last_user -%}
  {{- "<|Assistant|>" -}}
{%- endif -%}

{%- if add_generation_prompt and enable_thinking -%}
  {{- '<think>\n' -}}
{%- endif -%}