Fix chat template: render assistant tool_calls history

#19
Files changed (1) hide show
  1. chat_template.jinja +31 -3
chat_template.jinja CHANGED
@@ -1,5 +1,27 @@
1
  {{- bos_token -}}
2
  {%- set keep_past_thinking = keep_past_thinking | default(false) -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  {%- set ns = namespace(system_prompt="") -%}
4
  {%- if messages[0]["role"] == "system" -%}
5
  {%- set ns.system_prompt = messages[0]["content"] -%}
@@ -29,8 +51,10 @@
29
  {%- endfor -%}
30
  {%- for message in messages -%}
31
  {{- "<|im_start|>" + message["role"] + "\n" -}}
32
- {%- set content = message["content"] -%}
33
- {%- if content is not string -%}
 
 
34
  {%- set content = content | tojson -%}
35
  {%- endif -%}
36
  {%- if message["role"] == "assistant" and not keep_past_thinking and loop.index0 != ns.last_assistant_index -%}
@@ -38,7 +62,11 @@
38
  {%- set content = content.split("</think>")[-1] | trim -%}
39
  {%- endif -%}
40
  {%- endif -%}
41
- {{- content + "<|im_end|>\n" -}}
 
 
 
 
42
  {%- endfor -%}
43
  {%- if add_generation_prompt -%}
44
  {{- "<|im_start|>assistant\n" -}}
 
1
  {{- bos_token -}}
2
  {%- set keep_past_thinking = keep_past_thinking | default(false) -%}
3
+ {%- macro format_arg_value(arg_value) -%}
4
+ {%- if arg_value is string -%}
5
+ {{- "'" + arg_value + "'" -}}
6
+ {%- elif arg_value is mapping -%}
7
+ {{- arg_value | tojson -}}
8
+ {%- else -%}
9
+ {{- arg_value | string -}}
10
+ {%- endif -%}
11
+ {%- endmacro -%}
12
+ {%- macro render_tool_calls(tool_calls) -%}
13
+ {%- set tool_calls_ns = namespace(tool_calls=[]) -%}
14
+ {%- for tool_call in tool_calls -%}
15
+ {%- set func_name = tool_call["function"]["name"] -%}
16
+ {%- set func_args = tool_call["function"]["arguments"] -%}
17
+ {%- set args_ns = namespace(arg_strings=[]) -%}
18
+ {%- for arg_name, arg_value in func_args.items() -%}
19
+ {%- set args_ns.arg_strings = args_ns.arg_strings + [arg_name + "=" + format_arg_value(arg_value)] -%}
20
+ {%- endfor -%}
21
+ {%- set tool_calls_ns.tool_calls = tool_calls_ns.tool_calls + [func_name + "(" + (args_ns.arg_strings | join(", ")) + ")"] -%}
22
+ {%- endfor -%}
23
+ {{- "<|tool_call_start|>[" + (tool_calls_ns.tool_calls | join(", ")) + "]<|tool_call_end|>" -}}
24
+ {%- endmacro -%}
25
  {%- set ns = namespace(system_prompt="") -%}
26
  {%- if messages[0]["role"] == "system" -%}
27
  {%- set ns.system_prompt = messages[0]["content"] -%}
 
51
  {%- endfor -%}
52
  {%- for message in messages -%}
53
  {{- "<|im_start|>" + message["role"] + "\n" -}}
54
+ {%- set content = message.get("content") -%}
55
+ {%- if not content -%}
56
+ {%- set content = "" -%}
57
+ {%- elif content is not string -%}
58
  {%- set content = content | tojson -%}
59
  {%- endif -%}
60
  {%- if message["role"] == "assistant" and not keep_past_thinking and loop.index0 != ns.last_assistant_index -%}
 
62
  {%- set content = content.split("</think>")[-1] | trim -%}
63
  {%- endif -%}
64
  {%- endif -%}
65
+ {{- content -}}
66
+ {%- if message["role"] == "assistant" and message.get("tool_calls") -%}
67
+ {{- render_tool_calls(message["tool_calls"]) -}}
68
+ {%- endif -%}
69
+ {{- "<|im_end|>\n" -}}
70
  {%- endfor -%}
71
  {%- if add_generation_prompt -%}
72
  {{- "<|im_start|>assistant\n" -}}