Skip to content

Commit ca14b13

Browse files
committed
feat: support ds v3.2 encoding and remove redundant char in ds v3.1 jinja template
1 parent e5a336b commit ca14b13

File tree

6 files changed

+716
-32
lines changed

6 files changed

+716
-32
lines changed

rtp_llm/models/deepseek_v2.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,3 +714,4 @@ def get_weight_cls():
714714
register_model("deepseek-v3-mtp", DeepSeekV3Mtp, ["DeepseekV3ForCausalLMNextN"])
715715
register_model("kimi_k2", DeepSeekV2, [])
716716
register_model("deepseek_v31", DeepSeekV2, [])
717+
register_model("deepseek_v32", DeepSeekV2, []) # DeepSeek-V3.2 uses same architecture as V3.1, with encoding script update

rtp_llm/openai/renderers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .chatglm4_renderer import ChatGlm4Renderer
44
from .chatglm45_renderer import ChatGlm45Renderer
55
from .deepseekv31_renderer import DeepseekV31Renderer
6+
from .deepseekv32_renderer import DeepseekV32Renderer
67
from .internvl_renderer import InternVLRenderer
78
from .kimik2_renderer import KimiK2Renderer
89
from .llava_renderer import LlavaRenderer

rtp_llm/openai/renderers/deepseekv31_renderer.py

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,80 +36,78 @@
3636
{%- endif %}
3737
{%- endif %}
3838
{%- endfor %}
39-
4039
{% if tools is defined and tools is not none %}
4140
{% set tool_ns = namespace(text='## Tools\nYou have access to the following tools:\n') %}
4241
{% for tool in tools %}
4342
{% set tool_ns.text = tool_ns.text + '\n### ' + tool.function.name + '\nDescription: ' + tool.function.description + '\n\nParameters: ' + (tool.function.parameters | tojson) + '\n' %}
4443
{% endfor %}
4544
{% set tool_ns.text = tool_ns.text + "\nIMPORTANT: ALWAYS adhere to this exact format for tool use:\n<|tool▁calls▁begin|><|tool▁call▁begin|>tool_call_name<|tool▁sep|>tool_call_arguments<|tool▁call▁end|>{{additional_tool_calls}}<|tool▁calls▁end|>\n\nWhere:\n\n- `tool_call_name` must be an exact match to one of the available tools\n- `tool_call_arguments` must be valid JSON that strictly follows the tool's Parameters Schema\n- For multiple tool calls, chain them directly without separators or spaces\n" %}
46-
{% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
45+
{% set ns.system_prompt = (ns.system_prompt + '\n\n' if ns.system_prompt else '') + tool_ns.text %}
4746
{% endif %}
48-
49-
{{ bos_token }}{{ ns.system_prompt }}
47+
{{- bos_token }}{{ ns.system_prompt }}
5048
{%- for message in messages %}
5149
{%- if message['role'] == 'user' %}
5250
{%- set ns.is_tool = false -%}
5351
{%- set ns.is_first = false -%}
5452
{%- set ns.is_last_user = true -%}
55-
{{'<|User|>' + message['content']}}
53+
{{- '<|User|>' + message['content'] -}}
5654
{%- endif %}
5755
{%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
58-
{%- if ns.is_last_user %}
59-
{{'<|Assistant|></think>'}}
56+
{%- if ns.is_last_user -%}
57+
{{- '<|Assistant|></think>' -}}
6058
{%- endif %}
6159
{%- set ns.is_last_user = false -%}
6260
{%- set ns.is_first = false %}
6361
{%- set ns.is_tool = false -%}
6462
{%- for tool in message['tool_calls'] %}
6563
{%- if not ns.is_first %}
6664
{%- if message['content'] is none %}
67-
{{'<|tool▁calls▁begin|><|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}}
65+
{{- '<|tool▁calls▁begin|><|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>' -}}
6866
{%- else %}
69-
{{message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}}
67+
{{- message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>' -}}
7068
{%- endif %}
7169
{%- set ns.is_first = true -%}
7270
{%- else %}
73-
{{'<|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}}
71+
{{- '<|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>' -}}
7472
{%- endif %}
7573
{%- endfor %}
76-
{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}
74+
{{- '<|tool▁calls▁end|><|end▁of▁sentence|>' -}}
7775
{%- endif %}
7876
{%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none) %}
79-
{%- if ns.is_last_user %}
80-
{{'<|Assistant|>'}}
81-
{%- if message['prefix'] is defined and message['prefix'] and thinking %}
82-
{{'<think>'}}
83-
{%- else %}
84-
{{'</think>'}}
77+
{%- if ns.is_last_user -%}
78+
{{- '<|Assistant|>' -}}
79+
{%- if message['prefix'] is defined and message['prefix'] and thinking -%}
80+
{{- '<think>' -}}
81+
{%- else -%}
82+
{{- '</think>' -}}
8583
{%- endif %}
8684
{%- endif %}
8785
{%- set ns.is_last_user = false -%}
8886
{%- if ns.is_tool %}
89-
{{message['content'] + '<|end▁of▁sentence|>'}}
87+
{{- message['content'] + '<|end▁of▁sentence|>' -}}
9088
{%- set ns.is_tool = false -%}
9189
{%- else %}
9290
{%- set content = message['content'] -%}
9391
{%- if '</think>' in content %}
9492
{%- set content = content.split('</think>', 1)[1] -%}
9593
{%- endif %}
96-
{{content + '<|end▁of▁sentence|>'}}
94+
{{- content + '<|end▁of▁sentence|>' -}}
9795
{%- endif %}
9896
{%- endif %}
9997
{%- if message['role'] == 'tool' %}
10098
{%- set ns.is_last_user = false -%}
10199
{%- set ns.is_tool = true -%}
102-
{{'<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
100+
{{- '<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>' -}}
103101
{%- endif %}
104102
{%- endfor -%}
105-
{%- if add_generation_prompt and ns.is_last_user and not ns.is_tool %}
106-
{{'<|Assistant|>'}}
107-
{%- if not thinking %}
108-
{{'</think>'}}
109-
{%- else %}
110-
{{'<think>'}}
103+
{%- if add_generation_prompt and ns.is_last_user and not ns.is_tool -%}
104+
{{- '<|Assistant|>' -}}
105+
{%- if not thinking -%}
106+
{{- '</think>' -}}
107+
{%- else -%}
108+
{{- '<think>' -}}
111109
{%- endif %}
112-
{% endif %}"""
110+
{%- endif %}"""
113111

114112

115113
class DeepseekV31Renderer(ReasoningToolBaseRenderer):

0 commit comments

Comments
 (0)