"megatron/inference/text_generation/generation.py" did not exist on "5942af978a8a8ff706a302b1ba2d9ef3ce144444"
tool_chat_template_deepseekr1.jinja 4.58 KB
Newer Older
1
2
3
{% if not add_generation_prompt is defined %}
    {% set add_generation_prompt = false %}
{% endif %}
4
{% set ns = namespace(is_first=false, is_tool=false, is_output_first=true, system_prompt='', is_first_sp=true, is_last_user=false) %}
5
6
7
8
9
10
{%- for message in messages %}
    {%- if message['role'] == 'system' %}
        {%- if ns.is_first_sp %}
            {% set ns.system_prompt = ns.system_prompt + message['content'] %}
            {% set ns.is_first_sp = false %}
        {%- else %}
11
            {% set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %}
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
        {%- endif %}
    {%- endif %}
{%- endfor %}

{# --- Append tool descriptions if tools are defined --- #}
{% if tools is defined and tools is not none %}
    {% set tool_ns = namespace(text='You are a helpful assistant with tool calling capabilities. '
        'When a tool call is needed, you MUST use the following format to issue the call:\n'
        '<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>FUNCTION_NAME\n'
        '```json\n{"param1": "value1", "param2": "value2"}\n```<|tool▁call▁end|><|tool▁calls▁end|>\n\n'
        'Make sure the JSON is valid.'
        '## Tools\n\n### Function\n\nYou have the following functions available:\n\n') %}
    {% for tool in tools %}
        {% set tool_ns.text = tool_ns.text + '- `' + tool['name'] + '`:\n```json\n' + (tool | tojson) + '\n```\n' %}
    {% endfor %}
    {% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
{% endif %}

{{ bos_token }}
{{ ns.system_prompt }}
{%- for message in messages %}
33
    {% set content = message['content'] %}
34
35
    {%- if message['role'] == 'user' %}
        {%- set ns.is_tool = false -%}
36
37
38
        {%- set ns.is_first = false -%}
        {%- set ns.is_last_user = true -%}
        {{'<|User|>' + content + '<|Assistant|>'}}
39
    {%- endif %}
40
41
42
43
44
45
46
47
48
49
50
    {%- if message['role'] == 'assistant' %}
        {% if '</think>' in content %}
            {% set content = content.split('</think>')[-1] %}
        {% endif %}
    {% endif %}
    {%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
        {%- set ns.is_last_user = false -%}
        {%- if ns.is_tool %}
            {{'<|tool▁outputs▁end|>'}}
        {%- endif %}
        {%- set ns.is_first = false %}
51
        {%- set ns.is_tool = false -%}
52
        {%- set ns.is_output_first = true %}
53
54
        {%- for tool in message['tool_calls'] %}
            {%- if not ns.is_first %}
55
56
                {%- if content is none %}
                    {{'<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}
57
                {%- else %}
58
                    {{content + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}
59
60
61
                {%- endif %}
                {%- set ns.is_first = true -%}
            {%- else %}
62
                {{'\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}
63
64
65
66
            {%- endif %}
        {%- endfor %}
        {{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}
    {%- endif %}
67
68
    {%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none)%}
        {%- set ns.is_last_user = false -%}
69
        {%- if ns.is_tool %}
70
            {{'<|tool▁outputs▁end|>' + content + '<|end▁of▁sentence|>'}}
71
72
            {%- set ns.is_tool = false -%}
        {%- else %}
73
            {{content + '<|end▁of▁sentence|>'}}
74
75
76
        {%- endif %}
    {%- endif %}
    {%- if message['role'] == 'tool' %}
77
        {%- set ns.is_last_user = false -%}
78
79
        {%- set ns.is_tool = true -%}
        {%- if ns.is_output_first %}
80
            {{'<|tool▁outputs▁begin|><|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}
81
82
            {%- set ns.is_output_first = false %}
        {%- else %}
83
            {{'\n<|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}
84
85
86
87
88
89
        {%- endif %}
    {%- endif %}
{%- endfor -%}
{% if ns.is_tool %}
    {{'<|tool▁outputs▁end|>'}}
{% endif %}
90
{% if add_generation_prompt and not ns.is_last_user and not ns.is_tool %}
91
92
    {{'<|Assistant|>'}}
{% endif %}