Spaces:
Running
Running
update
Browse files
doc/chat-template/DeepSeek-R1/chat_template.jinja
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{% set ns = namespace(is_first=false, is_tool=false, is_output_first=true, system_prompt='', is_first_sp=true) %}{%- 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 -%}{{'<|User|>' + message['content']}}{%- endif %}{%- if message['role'] == 'assistant' and 'tool_calls' in message %}{%- set ns.is_tool = false -%}{%- for tool in message['tool_calls'] %}{%- if not ns.is_first %}{%- if message['content'] is none %}{{'<|Assistant|><|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}{%- else %}{{'<|Assistant|>' + message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}{%- endif %}{%- set ns.is_first = true -%}{%- else %}{{'\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}{%- endif %}{%- endfor %}{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}{%- endif %}{%- if message['role'] == 'assistant' and 'tool_calls' not in message %}{%- if ns.is_tool %}{{'<|tool▁outputs▁end|>' + 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] %}{% endif %}{{'<|Assistant|>' + content + '<|end▁of▁sentence|>'}}{%- endif %}{%- endif %}{%- if message['role'] == 'tool' %}{%- set ns.is_tool = true -%}{%- if ns.is_output_first %}{{'<|tool▁outputs▁begin|><|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}{%- set ns.is_output_first = false %}{%- else %}{{'<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}{%- endif %}{%- endif %}{%- endfor -%}{% if ns.is_tool %}{{'<|tool▁outputs▁end|>'}}{% endif %}{% if add_generation_prompt and not ns.is_tool %}{{'<|Assistant|><think>\n'}}{% endif %}
|
doc/chat-template/export_chat_template.py
CHANGED
@@ -15,6 +15,8 @@ MODEL_PATH = "NousResearch/Hermes-3-Llama-3.1-405B" # messages里不支持tool_
|
|
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 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
19 |
chat_template = tokenizer.chat_template
|
20 |
|
|
|
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
|
22 |
|
doc/chat-template/gemma-3-27b-it/chat_template.jinja
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{{ bos_token }}
|
2 |
+
{%- if messages[0]['role'] == 'system' -%}
|
3 |
+
{%- if messages[0]['content'] is string -%}
|
4 |
+
{%- set first_user_prefix = messages[0]['content'] + '
|
5 |
+
|
6 |
+
' -%}
|
7 |
+
{%- else -%}
|
8 |
+
{%- set first_user_prefix = messages[0]['content'][0]['text'] + '
|
9 |
+
|
10 |
+
' -%}
|
11 |
+
{%- endif -%}
|
12 |
+
{%- set loop_messages = messages[1:] -%}
|
13 |
+
{%- else -%}
|
14 |
+
{%- set first_user_prefix = "" -%}
|
15 |
+
{%- set loop_messages = messages -%}
|
16 |
+
{%- endif -%}
|
17 |
+
{%- for message in loop_messages -%}
|
18 |
+
{%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%}
|
19 |
+
{{ raise_exception("Conversation roles must alternate user/assistant/user/assistant/...") }}
|
20 |
+
{%- endif -%}
|
21 |
+
{%- if (message['role'] == 'assistant') -%}
|
22 |
+
{%- set role = "model" -%}
|
23 |
+
{%- else -%}
|
24 |
+
{%- set role = message['role'] -%}
|
25 |
+
{%- endif -%}
|
26 |
+
{{ '<start_of_turn>' + role + '
|
27 |
+
' + (first_user_prefix if loop.first else "") }}
|
28 |
+
{%- if message['content'] is string -%}
|
29 |
+
{{ message['content'] | trim }}
|
30 |
+
{%- elif message['content'] is iterable -%}
|
31 |
+
{%- for item in message['content'] -%}
|
32 |
+
{%- if item['type'] == 'image' -%}
|
33 |
+
{{ '<start_of_image>' }}
|
34 |
+
{%- elif item['type'] == 'text' -%}
|
35 |
+
{{ item['text'] | trim }}
|
36 |
+
{%- endif -%}
|
37 |
+
{%- endfor -%}
|
38 |
+
{%- else -%}
|
39 |
+
{{ raise_exception("Invalid content type") }}
|
40 |
+
{%- endif -%}
|
41 |
+
{{ '<end_of_turn>
|
42 |
+
' }}
|
43 |
+
{%- endfor -%}
|
44 |
+
{%- if add_generation_prompt -%}
|
45 |
+
{{'<start_of_turn>model
|
46 |
+
'}}
|
47 |
+
{%- endif -%}
|
doc/chat-template/tool_demo.py
CHANGED
@@ -23,6 +23,9 @@ from transformers.utils import get_json_schema
|
|
23 |
MODEL_PATH = "Qwen/Qwen3-235B-A22B-Instruct-2507"
|
24 |
# MODEL_PATH = "mistralai/Mistral-7B-Instruct-v0.1" # messages里不支持tool_calls,不支持 role=tool,不支持 tools
|
25 |
# MODEL_PATH = "mistralai/Ministral-8B-Instruct-2410" # 支持 tools, 支持tool_calls(必须要有id), 格式非主流
|
|
|
|
|
|
|
26 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
27 |
|
28 |
# First, define a tool
|
@@ -41,8 +44,8 @@ def get_current_temperature(location: str) -> float:
|
|
41 |
messages = [
|
42 |
{"role": "system", "content": "You are a bot that responds to weather queries."},
|
43 |
{"role": "user", "content": "Hey, what's the temperature in Paris right now?"},
|
44 |
-
|
45 |
-
|
46 |
]
|
47 |
|
48 |
# step1:
|
@@ -59,6 +62,7 @@ print(json.dumps(json_schema, ensure_ascii=False, indent=2))
|
|
59 |
# step2: 调用 LLM,以下是LLM的返回
|
60 |
tool_call = {"name": "get_current_temperature", "arguments": {"location": "Paris, France"}}
|
61 |
messages.append({"role": "assistant", "tool_calls": [{"type": "function", "function": tool_call}]})
|
|
|
62 |
# messages.append({"role": "assistant", "tool_calls": [{"id": "123456789", "type": "function", "function": tool_call}]}) # Ministral-8B-Instruct-2410 仍然报错
|
63 |
|
64 |
# step3: 调用tool,以下是 tool 的返回
|
|
|
23 |
MODEL_PATH = "Qwen/Qwen3-235B-A22B-Instruct-2507"
|
24 |
# MODEL_PATH = "mistralai/Mistral-7B-Instruct-v0.1" # messages里不支持tool_calls,不支持 role=tool,不支持 tools
|
25 |
# MODEL_PATH = "mistralai/Ministral-8B-Instruct-2410" # 支持 tools, 支持tool_calls(必须要有id), 格式非主流
|
26 |
+
# MODEL_PATH = "deepseek-ai/DeepSeek-R1" # 不支持tools,tool_calls也有问题
|
27 |
+
# MODEL_PATH = "google/gemma-3-27b-it" # 不支持任何tool
|
28 |
+
MODEL_PATH = "moonshotai/Kimi-K2-Instruct"
|
29 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
30 |
|
31 |
# First, define a tool
|
|
|
44 |
messages = [
|
45 |
{"role": "system", "content": "You are a bot that responds to weather queries."},
|
46 |
{"role": "user", "content": "Hey, what's the temperature in Paris right now?"},
|
47 |
+
# {"role": "assitant", "content": "test1"},
|
48 |
+
# {"role": "user", "content": "test2"},
|
49 |
]
|
50 |
|
51 |
# step1:
|
|
|
62 |
# step2: 调用 LLM,以下是LLM的返回
|
63 |
tool_call = {"name": "get_current_temperature", "arguments": {"location": "Paris, France"}}
|
64 |
messages.append({"role": "assistant", "tool_calls": [{"type": "function", "function": tool_call}]})
|
65 |
+
# messages.append({"role": "assistant", "tool_calls": [{"type": "function", "function": tool_call}], "content": ""})
|
66 |
# messages.append({"role": "assistant", "tool_calls": [{"id": "123456789", "type": "function", "function": tool_call}]}) # Ministral-8B-Instruct-2410 仍然报错
|
67 |
|
68 |
# step3: 调用tool,以下是 tool 的返回
|
doc/chat-template/tools_and_llm_response.md
CHANGED
@@ -241,3 +241,6 @@ Hey, what's the temperature in Paris right now?<|im_end|>
|
|
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>`
|
|
|
|
|
|
|
|
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 |
+
|
245 |
+
|
246 |
+
##
|