xu-song commited on
Commit
a21f3cd
·
1 Parent(s): 398e54a
docs/chat-template/DeepSeek-V3.1/chat_template.jinja ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {% 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 + '
2
+
3
+ ' + 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 %}
docs/chat-template/Llama-4-Maverick-17B-128E-Instruct/chat_template.jinja ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {{- bos_token }}
2
+ {%- if custom_tools is defined %}
3
+ {%- set tools = custom_tools %}
4
+ {%- endif %}
5
+ {%- if not tools_in_user_message is defined %}
6
+ {%- set tools_in_user_message = true %}
7
+ {%- endif %}
8
+ {%- if not date_string is defined %}
9
+ {%- if strftime_now is defined %}
10
+ {%- set date_string = strftime_now("%d %b %Y") %}
11
+ {%- else %}
12
+ {%- set date_string = "26 Jul 2024" %}
13
+ {%- endif %}
14
+ {%- endif %}
15
+ {%- if not tools is defined %}
16
+ {%- set tools = none %}
17
+ {%- endif %}
18
+
19
+ {#- This block extracts the system message, so we can slot it into the right place. #}
20
+ {%- if messages[0]['role'] == 'system' %}
21
+ {%- if messages[0]['content'] is string %}
22
+ {%- set system_message = messages[0]['content']|trim %}
23
+ {%- else %}
24
+ {#- FIXME: The processor requires an array, always. #}
25
+ {%- set system_message = messages[0]['content'][0]['text']|trim %}
26
+ {%- endif %}
27
+ {%- set messages = messages[1:] %}
28
+ {%- set user_supplied_system_message = true %}
29
+ {%- else %}
30
+ {%- set system_message = "" %}
31
+ {%- set user_supplied_system_message = false %}
32
+ {%- endif %}
33
+
34
+ {#- System message if the user supplied one #}
35
+ {%- if user_supplied_system_message %}
36
+ {{- "<|header_start|>system<|header_end|>\n\n" }}
37
+ {%- if tools is not none %}
38
+ {{- "Environment: ipython\n" }}
39
+ {%- endif %}
40
+ {%- if tools is not none and not tools_in_user_message %}
41
+ {{- "You have access to the following functions. To call a function, please respond with JSON for a function call." }}
42
+ {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
43
+ {{- "Do not use variables.\n\n" }}
44
+ {%- for t in tools %}
45
+ {{- t | tojson(indent=4) }}
46
+ {{- "\n\n" }}
47
+ {%- endfor %}
48
+ {%- endif %}
49
+ {{- system_message }}
50
+ {{- "<|eot|>" }}
51
+ {%- endif %}
52
+
53
+ {#- Custom tools are passed in a user message with some extra guidance #}
54
+ {%- if tools_in_user_message and not tools is none %}
55
+ {#- Extract the first user message so we can plug it in here #}
56
+ {%- if messages | length != 0 %}
57
+ {%- set first_user_message = messages[0]['content']|trim %}
58
+ {%- set messages = messages[1:] %}
59
+ {%- else %}
60
+ {{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }}
61
+ {%- endif %}
62
+ {{- '<|header_start|>user<|header_end|>\n\n' -}}
63
+ {{- "Given the following functions, please respond with a JSON for a function call " }}
64
+ {{- "with its proper arguments that best answers the given prompt.\n\n" }}
65
+ {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
66
+ {{- "Do not use variables.\n\n" }}
67
+ {%- for t in tools %}
68
+ {{- t | tojson(indent=4) }}
69
+ {{- "\n\n" }}
70
+ {%- endfor %}
71
+ {{- first_user_message + "<|eot|>"}}
72
+ {%- endif %}
73
+
74
+ {%- for message in messages %}
75
+ {%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %}
76
+ {{- '<|header_start|>' + message['role'] + '<|header_end|>\n\n' }}
77
+ {%- if message['content'] is string %}
78
+ {{- message['content'] }}
79
+ {%- else %}
80
+ {%- for content in message['content'] %}
81
+ {%- if content['type'] == 'image' %}
82
+ {{- '<|image|>' }}
83
+ {%- elif content['type'] == 'text' %}
84
+ {{- content['text'] }}
85
+ {%- endif %}
86
+ {%- endfor %}
87
+ {%- endif %}
88
+ {{- "<|eot|>" }}
89
+ {%- elif 'tool_calls' in message and message.tool_calls|length > 0 %}
90
+ {{- '<|header_start|>assistant<|header_end|>\n\n' -}}
91
+ {{- '<|python_start|>' }}
92
+ {%- if message['content'] is string %}
93
+ {{- message['content'] }}
94
+ {%- else %}
95
+ {%- for content in message['content'] %}
96
+ {%- if content['type'] == 'image' %}
97
+ {{- '<|image|>' }}
98
+ {%- elif content['type'] == 'text' %}
99
+ {{- content['text'] }}
100
+ {%- endif %}
101
+ {%- endfor %}
102
+ {%- endif %}
103
+ {{- '<|python_end|>' }}
104
+ {%- for tool_call in message.tool_calls %}
105
+ {{- '{"name": "' + tool_call.function.name + '", ' }}
106
+ {{- '"parameters": ' }}
107
+ {{- tool_call.function.arguments | tojson }}
108
+ {{- "}" }}
109
+ {%- endfor %}
110
+ {{- "<|eot|>" }}
111
+ {%- elif message.role == "tool" or message.role == "ipython" %}
112
+ {{- "<|header_start|>ipython<|header_end|>\n\n" }}
113
+ {%- if message.content is mapping or message.content is iterable %}
114
+ {{- message.content | tojson }}
115
+ {%- else %}
116
+ {{- message.content }}
117
+ {%- endif %}
118
+ {{- "<|eot|>" }}
119
+ {%- endif %}
120
+ {%- endfor %}
121
+ {%- if add_generation_prompt %}
122
+ {{- '<|header_start|>assistant<|header_end|>\n\n' }}
123
+ {%- endif %}
docs/chat-template/export_chat_template.py CHANGED
@@ -1,21 +1,21 @@
1
 
2
 
3
  import os
4
- import json
5
  from transformers import AutoTokenizer
6
- from transformers.utils import get_json_schema
7
 
8
 
9
  # MODEL_PATH = "meta-llama/Llama-3.1-405B-Instruct"
10
- MODEL_PATH = "NousResearch/Hermes-3-Llama-3.1-405B" # messages里不支持tool_calls
11
- # MODEL_PATH = "../../test/Llama-4-Maverick-17B-128E-Instruct/"
12
  # MODEL_PATH = "meta-llama/Llama-4-Maverick-17B-128E-Instruct"
13
  # MODEL_PATH = "Qwen/Qwen3-235B-A22B-Instruct-2507"
14
  # MODEL_PATH = "mistralai/Mistral-7B-Instruct-v0.1" # messages里不支持tool_calls,不支持 role=tool,不支持 tools
15
  # MODEL_PATH = "mistralai/Ministral-8B-Instruct-2410" # 支持 tools, 支持tool_calls(必须要有id), 格式非主流
16
- MODEL_PATH = "deepseek-ai/DeepSeek-R1"
17
  # MODEL_PATH = "deepseek-ai/DeepSeek-R1-0528"
18
- MODEL_PATH = "google/gemma-3-27b-it"
 
 
19
 
20
  tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
21
  chat_template = tokenizer.chat_template
 
1
 
2
 
3
  import os
 
4
  from transformers import AutoTokenizer
 
5
 
6
 
7
  # MODEL_PATH = "meta-llama/Llama-3.1-405B-Instruct"
8
+ # MODEL_PATH = "NousResearch/Hermes-3-Llama-3.1-405B" # messages里不支持tool_calls
9
+ # MODEL_PATH = "../../test/Llama-4-Maverick-17B-128E-Instruct"
10
  # MODEL_PATH = "meta-llama/Llama-4-Maverick-17B-128E-Instruct"
11
  # MODEL_PATH = "Qwen/Qwen3-235B-A22B-Instruct-2507"
12
  # MODEL_PATH = "mistralai/Mistral-7B-Instruct-v0.1" # messages里不支持tool_calls,不支持 role=tool,不支持 tools
13
  # MODEL_PATH = "mistralai/Ministral-8B-Instruct-2410" # 支持 tools, 支持tool_calls(必须要有id), 格式非主流
14
+ # MODEL_PATH = "deepseek-ai/DeepSeek-R1"
15
  # MODEL_PATH = "deepseek-ai/DeepSeek-R1-0528"
16
+ MODEL_PATH = 'deepseek-ai/DeepSeek-V3.1'
17
+ # MODEL_PATH = "google/gemma-3-27b-it"
18
+
19
 
20
  tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
21
  chat_template = tokenizer.chat_template
docs/chat-template/tools_and_llm_response.md CHANGED
@@ -147,7 +147,7 @@ Hey, what's the temperature in Paris right now?<|im_end|>
147
 
148
  - **入参**:
149
  - **tools格式**: 支持的工具列表(`tools`) 是基于自定义格式的,详见 [chat_template](https://github.com/vllm-project/vllm/blob/v0.10.1/examples/tool_chat_template_hermes.jinja#L41)
150
- - **tools在prompt中的位置**: 额外增加了一个`system`轮,放在最前面。(用户设置的`system`保持不变)
151
  - **出参**: 返回的`respone` 要求是 `<tool_call>` 包裹的json
152
  `return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows: <tool_call>{"name": <function-name>, "arguments": <args-dict>}</tool_call>`
153
 
@@ -238,7 +238,7 @@ Hey, what's the temperature in Paris right now?<|im_end|>
238
 
239
  - **入参**:
240
  - **tools格式**: 支持的工具列表(`tools`) 是基于 json-schema 的.
241
- - **tools在prompt中的位置**: 拼接到原始`system`的结尾。
242
  - **出参**: 返回的`respone` 要求是 `<tool_call>` 包裹的json
243
  `return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{"name": <function-name>, "arguments": <args-json-object>}\n</tool_call>`
244
 
 
147
 
148
  - **入参**:
149
  - **tools格式**: 支持的工具列表(`tools`) 是基于自定义格式的,详见 [chat_template](https://github.com/vllm-project/vllm/blob/v0.10.1/examples/tool_chat_template_hermes.jinja#L41)
150
+ - **tools在prompt中的位置**: 额外增加了一个`system`轮,放在最前面。(用户设置的`system`保持不变),这个额外的system轮告诉大模型可以使用tools中的工具,并且指定返回格式
151
  - **出参**: 返回的`respone` 要求是 `<tool_call>` 包裹的json
152
  `return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows: <tool_call>{"name": <function-name>, "arguments": <args-dict>}</tool_call>`
153
 
 
238
 
239
  - **入参**:
240
  - **tools格式**: 支持的工具列表(`tools`) 是基于 json-schema 的.
241
+ - **tools在prompt中的位置**: 拼接到原始`system`的结尾 + 额外的prompt (告诉大模型可以使用tools中的工具,并且指定返回格式)
242
  - **出参**: 返回的`respone` 要求是 `<tool_call>` 包裹的json
243
  `return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{"name": <function-name>, "arguments": <args-json-object>}\n</tool_call>`
244