k-l-lambda commited on
Commit
fea23a2
·
1 Parent(s): e44c3f8

enhanced chat_template for function-call.

Browse files
Files changed (2) hide show
  1. chat_template.jinja +96 -0
  2. tokenizer_config.json +1 -2
chat_template.jinja ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {% if not add_generation_prompt is defined %}
2
+ {% set add_generation_prompt = false %}
3
+ {% endif %}
4
+ {% if not thinking is defined %}
5
+ {% set thinking = false %}
6
+ {% endif %}
7
+ {% set ns = namespace(is_first=false, is_tool=false, system_prompt='', is_first_sp=true, is_last_user=false) %}
8
+ {%- for message in messages %}
9
+ {%- if message['role'] == 'system' %}
10
+ {%- if ns.is_first_sp %}
11
+ {% set ns.system_prompt = ns.system_prompt + message['content'] %}
12
+ {% set ns.is_first_sp = false %}
13
+ {%- else %}
14
+ {% set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %}
15
+ {%- endif %}
16
+ {%- endif %}
17
+ {%- endfor %}
18
+
19
+ {# --- Append tool descriptions if tools are defined --- #}
20
+ {% if tools is defined and tools is not none %}
21
+ {% set tool_ns = namespace(text='You are a helpful assistant with tool calling capabilities. '
22
+ 'When a tool call is needed, you MUST use the following format to issue the call:\n'
23
+ '<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>FUNCTION_NAME\n'
24
+ '```json\n{"param1": "value1", "param2": "value2"}\n```<|tool▁call▁end|><|tool▁calls▁end|>\n\n'
25
+ 'Make sure the JSON is valid.'
26
+ '## Tools\n\n### Function\n\nYou have the following functions available:\n\n') %}
27
+ {% for tool in tools %}
28
+ {% set tool_ns.text = tool_ns.text + '- `' + tool['name'] + '`:\n```json\n' + (tool | tojson) + '\n```\n' %}
29
+ {% endfor %}
30
+ {% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
31
+ {% endif %}
32
+
33
+ {{ bos_token }}{{ ns.system_prompt }}
34
+ {%- for message in messages %}
35
+ {%- if message['role'] == 'user' %}
36
+ {%- set ns.is_tool = false -%}
37
+ {%- set ns.is_first = false -%}
38
+ {%- set ns.is_last_user = true -%}
39
+ {{'<|User|>' + message['content']}}
40
+ {%- endif %}
41
+ {%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
42
+ {%- if ns.is_last_user %}
43
+ {{'<|Assistant|></think>'}}
44
+ {%- endif %}
45
+ {%- set ns.is_last_user = false -%}
46
+ {%- set ns.is_first = false %}
47
+ {%- set ns.is_tool = false -%}
48
+ {%- for tool in message['tool_calls'] %}
49
+ {%- if not ns.is_first %}
50
+ {%- if message['content'] is none %}
51
+ {{'<|tool▁calls▁begin|><|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments'] + '<|tool▁call▁end|>'}}
52
+ {%- else %}
53
+ {{message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments'] + '<|tool▁call▁end|>'}}
54
+ {%- endif %}
55
+ {%- set ns.is_first = true -%}
56
+ {%- else %}
57
+ {{'<|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments'] + '<|tool▁call▁end|>'}}
58
+ {%- endif %}
59
+ {%- endfor %}
60
+ {{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}
61
+ {%- endif %}
62
+ {%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none) %}
63
+ {%- if ns.is_last_user %}
64
+ {{'<|Assistant|>'}}
65
+ {%- if message['prefix'] is defined and message['prefix'] and thinking %}
66
+ {{'<think>'}}
67
+ {%- else %}
68
+ {{'</think>'}}
69
+ {%- endif %}
70
+ {%- endif %}
71
+ {%- set ns.is_last_user = false -%}
72
+ {%- if ns.is_tool %}
73
+ {{message['content'] + '<|end▁of▁sentence|>'}}
74
+ {%- set ns.is_tool = false -%}
75
+ {%- else %}
76
+ {%- set content = message['content'] -%}
77
+ {%- if '</think>' in content %}
78
+ {%- set content = content.split('</think>', 1)[1] -%}
79
+ {%- endif %}
80
+ {{content + '<|end▁of▁sentence|>'}}
81
+ {%- endif %}
82
+ {%- endif %}
83
+ {%- if message['role'] == 'tool' %}
84
+ {%- set ns.is_last_user = false -%}
85
+ {%- set ns.is_tool = true -%}
86
+ {{'<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
87
+ {%- endif %}
88
+ {%- endfor -%}
89
+ {%- if add_generation_prompt and ns.is_last_user and not ns.is_tool %}
90
+ {{'<|Assistant|>'}}
91
+ {%- if not thinking %}
92
+ {{'</think>'}}
93
+ {%- else %}
94
+ {{'<think>'}}
95
+ {%- endif %}
96
+ {% endif %}
tokenizer_config.json CHANGED
@@ -30,6 +30,5 @@
30
  },
31
  "sp_model_kwargs": {},
32
  "unk_token": null,
33
- "tokenizer_class": "LlamaTokenizerFast",
34
- "chat_template": "{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{% if not thinking is defined %}{% set thinking = false %}{% endif %}{% set ns = namespace(is_first=false, is_tool=false, system_prompt='', is_first_sp=true, is_last_user=false) %}{%- 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 %}{% set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %}{%- endif %}{%- endif %}{%- endfor %}{{ bos_token }}{{ ns.system_prompt }}{%- for message in messages %}{%- if message['role'] == 'user' %}{%- set ns.is_tool = false -%}{%- set ns.is_first = false -%}{%- set ns.is_last_user = true -%}{{'<|User|>' + message['content']}}{%- endif %}{%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}{%- if ns.is_last_user %}{{'<|Assistant|></think>'}}{%- endif %}{%- set ns.is_last_user = false -%}{%- set ns.is_first = false %}{%- set ns.is_tool = false -%}{%- for tool in message['tool_calls'] %}{%- if not ns.is_first %}{%- if message['content'] is none %}{{'<|tool▁calls▁begin|><|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments'] + '<|tool▁call▁end|>'}}{%- else %}{{message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments'] + '<|tool▁call▁end|>'}}{%- endif %}{%- set ns.is_first = true -%}{%- else %}{{'<|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments'] + '<|tool▁call▁end|>'}}{%- endif %}{%- endfor %}{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}{%- endif %}{%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none) %}{%- if ns.is_last_user %}{{'<|Assistant|>'}}{%- if message['prefix'] is defined and message['prefix'] and thinking %}{{'<think>'}} {%- else %}{{'</think>'}}{%- endif %}{%- endif %}{%- set ns.is_last_user = false -%}{%- if ns.is_tool %}{{message['content'] + '<|end▁of▁sentence|>'}}{%- set ns.is_tool = false -%}{%- else %}{%- set content = message['content'] -%}{%- if '</think>' in content %}{%- set content = content.split('</think>', 1)[1] -%}{%- endif %}{{content + '<|end▁of▁sentence|>'}}{%- endif %}{%- endif %}{%- if message['role'] == 'tool' %}{%- set ns.is_last_user = false -%}{%- set ns.is_tool = true -%}{{'<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}{%- endif %}{%- endfor -%}{%- if add_generation_prompt and ns.is_last_user and not ns.is_tool %}{{'<|Assistant|>'}}{%- if not thinking %}{{'</think>'}}{%- else %}{{'<think>'}}{%- endif %}{% endif %}"
35
  }
 
30
  },
31
  "sp_model_kwargs": {},
32
  "unk_token": null,
33
+ "tokenizer_class": "LlamaTokenizerFast"
 
34
  }