|
17 | 17 | {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %} |
18 | 18 | {%- for message in messages[::-1] %} |
19 | 19 | {%- set index = (messages|length - 1) - loop.index0 %} |
20 | | - {%- if ns.multi_step_tool and message.role == "user" and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %} |
| 20 | + {%- if ns.multi_step_tool and message.role == "user" and message.content is string and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %} |
21 | 21 | {%- set ns.multi_step_tool = false %} |
22 | 22 | {%- set ns.last_query_index = index %} |
23 | 23 | {%- endif %} |
24 | 24 | {%- endfor %} |
25 | 25 | {%- for message in messages %} |
| 26 | + {%- if message.content is string %} |
| 27 | + {%- set content = message.content %} |
| 28 | + {%- else %} |
| 29 | + {%- set content = '' %} |
| 30 | + {%- endif %} |
26 | 31 | {%- if (message.role == "user") or (message.role == "system" and not loop.first) %} |
27 | | - {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }} |
| 32 | + {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }} |
28 | 33 | {%- elif message.role == "assistant" %} |
29 | | - {%- set content = message.content %} |
30 | 34 | {%- set reasoning_content = '' %} |
31 | | - {%- if message.reasoning_content is defined and message.reasoning_content is not none %} |
| 35 | + {%- if message.reasoning_content is string %} |
32 | 36 | {%- set reasoning_content = message.reasoning_content %} |
33 | 37 | {%- else %} |
34 | | - {%- if '</think>' in message.content %} |
35 | | - {%- set content = message.content.split('</think>')[-1].lstrip('\n') %} |
36 | | - {%- set reasoning_content = message.content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %} |
| 38 | + {%- if '</think>' in content %} |
| 39 | + {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %} |
| 40 | + {%- set content = content.split('</think>')[-1].lstrip('\n') %} |
37 | 41 | {%- endif %} |
38 | 42 | {%- endif %} |
39 | 43 | {%- if loop.index0 > ns.last_query_index %} |
|
70 | 74 | {{- '<|im_start|>user' }} |
71 | 75 | {%- endif %} |
72 | 76 | {{- '\n<tool_response>\n' }} |
73 | | - {{- message.content }} |
| 77 | + {{- content }} |
74 | 78 | {{- '\n</tool_response>' }} |
75 | 79 | {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %} |
76 | 80 | {{- '<|im_end|>\n' }} |
|
0 commit comments