File size: 3,592 Bytes
3479e2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{%- if messages[0]['role'] == 'system' %}
    {{- '<|im_start|>system\n' }}
    {%- if messages[0]['content'] is string %}
        {{- messages[0]['content'] }}
    {%- else %}
        {%- for item in messages[0]['content'] %}
            {%- if item['type'] == 'text' %}
                {{- item['text'] }}
            {%- endif %}
        {%- endfor %}
    {%- endif %}
    {{- '<|im_end|>\n' }}
{%- else %}
    {{- '<|im_start|>system\n你是Qianfan-VL,由百度智能云研发的多模态大语言模型。<|im_end|>\n' }}
{%- endif %}
{%- set ns = namespace(found_last_user=false, last_query_index=messages|length - 1) %}
{%- for message in messages[::-1] %}
    {%- set index = (messages|length - 1) - loop.index0 %}
    {%- if not ns.found_last_user and message['role'] == 'user' %}
        {%- set ns.found_last_user = true %}
        {%- set ns.last_query_index = index %}
    {%- endif %}
{%- endfor %}
{%- for message in messages %}
    {%- if messages[0]['role'] != 'system' or not loop.first %}
        {%- if message['role'] == 'user' or (message['role'] == 'system' and not loop.first) %}
            {%- set append_think = (enable_thinking is defined and enable_thinking and message['role'] == 'user' and loop.index0 == ns.last_query_index) %}
                {{- '<|im_start|>' + message['role'] + '\n' }}
            {%- if message['content'] is string %}
                {{- message['content'] }}
            {%- else %}
            {%- for item in message['content'] %}
                {%- if item['type'] == 'image' %}
                    {{- '<image>\n' }}
                {%- elif item['type'] == 'video' %}
                    {{- '<video>\n' }}
                {%- elif item['type'] == 'text' %}
                    {{- item['text'] }}
                {%- endif %}
            {%- endfor %}
        {%- endif %}
        {%- if append_think %}
            {{- '<think>' }}
        {%- endif %}
        {{- '<|im_end|>\n' }}
        {%- elif message['role'] == 'assistant' %}
            {%- if message['content'] is string %}
                {%- set raw_content = message['content'] %}
            {%- else %}
                {%- set content_ns = namespace(raw='') %}
                {%- for item in message['content'] %}
                    {%- if item['type'] == 'text' %}
                        {%- set content_ns.raw = content_ns.raw + item['text'] %}
                    {%- endif %}
                {%- endfor %}
                {%- set raw_content = content_ns.raw %}
            {%- endif %}
            {%- set content = raw_content %}
            {%- set reasoning_content = '' %}
            {%- if 'reasoning_content' in message and message['reasoning_content'] is not none %}
                {%- set reasoning_content = message['reasoning_content'] %}
            {%- elif '</think>' in raw_content %}
                {%- set content = raw_content.split('</think>')[-1].lstrip('\n') %}
                {%- set reasoning_content = raw_content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
            {%- endif %}
            {%- if loop.index0 > ns.last_query_index and reasoning_content %}
                {{- '<|im_start|>' + message['role'] + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
            {%- else %}
                {{- '<|im_start|>' + message['role'] + '\n' + content }}
            {%- endif %}
            {{- '<|im_end|>\n' }}
        {%- endif %}
    {%- endif %}
{%- endfor %}
{%- if add_generation_prompt %}
    {{- '<|im_start|>assistant\n' }}
{%- endif %}