Skip to content

Commit c08862c

Browse files
CatherineSuetarinkk
authored andcommitted
feat: Refactor DeepSeekV3 function call (sgl-project#5908)
1 parent 1394d41 commit c08862c

File tree

3 files changed

+95
-29
lines changed

3 files changed

+95
-29
lines changed

docs/references/deepseek.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ See [Separate Reasoning](https://docs.sglang.ai/backend/separate_reasoning.html)
171171

172172
### Function calling for DeepSeek Models
173173

174-
Add arguments `--tool-call-parser deepseekv3` to enable this feature. For example (running on 1 * H20 node):
174+
Add arguments `--tool-call-parser deepseekv3` and `--chat-template ./examples/chat_template/tool_chat_template_deepseekv3.jinja`(recommended) to enable this feature. For example (running on 1 * H20 node):
175175

176176
```
177-
python3 -m sglang.launch_server --model deepseek-ai/DeepSeek-V3-0324 --tp 8 --port 30000 --host 0.0.0.0 --mem-fraction-static 0.9 --disable-cuda-graph --tool-call-parser deepseekv3
177+
python3 -m sglang.launch_server --model deepseek-ai/DeepSeek-V3-0324 --tp 8 --port 30000 --host 0.0.0.0 --mem-fraction-static 0.9 --disable-cuda-graph --tool-call-parser deepseekv3 --chat-template ./examples/chat_template/tool_chat_template_deepseekv3.jinja
178178
```
179179

180180
Sample Request:
@@ -188,7 +188,7 @@ curl "http://127.0.0.1:30000/v1/chat/completions" \
188188
Expected Response
189189

190190
```
191-
{"id": "62af80528930423a82c806651ec66e7c", "object": "chat.completion", "created": 1744431333, "model": "deepseek-ai/DeepSeek-V3-0324", "choices": [{"index": 0, "message": {"role": "assistant", "content": null, "reasoning_content": null, "tool_calls": [{"id": "0", "type": "function", "function": {"name": "query_weather", "arguments": "{\\"city\\": \\"Guangzhou\\"}"}}]}, "logprobs": null, "finish_reason": "tool_calls", "matched_stop": null}], "usage": {"prompt_tokens": 118, "total_tokens": 140, "completion_tokens": 22, "prompt_tokens_details": null}}
191+
{"id":"6501ef8e2d874006bf555bc80cddc7c5","object":"chat.completion","created":1745993638,"model":"deepseek-ai/DeepSeek-V3-0324","choices":[{"index":0,"message":{"role":"assistant","content":null,"reasoning_content":null,"tool_calls":[{"id":"0","index":null,"type":"function","function":{"name":"query_weather","arguments":"{\"city\": \"Qingdao\"}"}}]},"logprobs":null,"finish_reason":"tool_calls","matched_stop":null}],"usage":{"prompt_tokens":116,"total_tokens":138,"completion_tokens":22,"prompt_tokens_details":null}}
192192
193193
```
194194
Sample Streaming Request:
@@ -215,6 +215,7 @@ The client needs to concatenate all arguments fragments to reconstruct the compl
215215
```
216216
Important Notes:
217217
1. Use a lower `"temperature"` value for better results.
218+
2. To receive more consistent tool call results, it is recommended to use `--chat-template examples/chat_template/tool_chat_template_deepseekv3.jinja`. It provides an improved unified prompt.
218219

219220

220221

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{% if not add_generation_prompt is defined %}
2+
{% set add_generation_prompt = false %}
3+
{% endif %}
4+
5+
{% set ns = namespace(is_first=false, is_tool=false, is_output_first=true, system_prompt='', is_first_sp=true, is_last_user=false) %}
6+
{%- for message in messages %}
7+
{%- if message['role'] == 'system' %}
8+
{%- if ns.is_first_sp %}
9+
{% set ns.system_prompt = ns.system_prompt + message['content'] %}
10+
{% set ns.is_first_sp = false %}
11+
{%- else %}
12+
{% set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %}
13+
{%- endif %}
14+
{%- endif %}
15+
{%- endfor %}
16+
17+
{# --- Append tool descriptions if tools are defined --- #}
18+
{% if tools is defined and tools is not none %}
19+
{% set tool_ns = namespace(text='You are a helpful assistant with tool calling capabilities. '
20+
'When a tool call is needed, you MUST use the following format to issue the call:\n'
21+
'<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>FUNCTION_NAME\n'
22+
'```json\n{"param1": "value1", "param2": "value2"}\n```<|tool▁call▁end|><|tool▁calls▁end|>\n\n'
23+
'Make sure the JSON is valid.'
24+
'## Tools\n\n### Function\n\nYou have the following functions available:\n\n') %}
25+
{% for tool in tools %}
26+
{% set tool_ns.text = tool_ns.text + '- `' + tool['name'] + '`:\n```json\n' + (tool | tojson) + '\n```\n' %}
27+
{% endfor %}
28+
{% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
29+
{% endif %}
30+
31+
{{ bos_token }}
32+
{{ ns.system_prompt }}
33+
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'] + '<|Assistant|>'}}
40+
{%- endif %}
41+
{%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
42+
{%- set ns.is_last_user = false -%}
43+
{%- if ns.is_tool %}
44+
{{'<|tool▁outputs▁end|>'}}
45+
{%- endif %}
46+
{%- set ns.is_first = false %}
47+
{%- set ns.is_tool = false -%}
48+
{%- set ns.is_output_first = true %}
49+
{%- for tool in message['tool_calls'] %}
50+
{%- if not ns.is_first %}
51+
{%- if message['content'] is none %}
52+
{{'<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}
53+
{%- else %}
54+
{{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|>'}}
55+
{%- endif %}
56+
{%- set ns.is_first = true -%}
57+
{%- else %}
58+
{{'\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}
59+
{%- endif %}
60+
{%- endfor %}
61+
{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}
62+
{%- endif %}
63+
{%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none)%}
64+
{%- set ns.is_last_user = false -%}
65+
{%- if ns.is_tool %}
66+
{{'<|tool▁outputs▁end|>' + message['content'] + '<|end▁of▁sentence|>'}}
67+
{%- set ns.is_tool = false -%}
68+
{%- else %}
69+
{% set content = message['content'] %}
70+
{{content + '<|end▁of▁sentence|>'}}
71+
{%- endif %}
72+
{%- endif %}
73+
{%- if message['role'] == 'tool' %}
74+
{%- set ns.is_last_user = false -%}
75+
{%- set ns.is_tool = true -%}
76+
{%- if ns.is_output_first %}
77+
{{ 'Use the results below to formulate an answer to the user question unless additional information is needed.' }}
78+
{{'<|tool▁outputs▁begin|><|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
79+
{%- set ns.is_output_first = false %}
80+
{%- else %}
81+
{{'\n<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
82+
{%- endif %}
83+
{%- endif %}
84+
{%- endfor -%}
85+
86+
{% if ns.is_tool %}
87+
{{"<|tool▁outputs▁end|>"}}
88+
{% endif %}
89+
{% if add_generation_prompt and not ns.is_last_user and not ns.is_tool %}
90+
{{'<|Assistant|>'}}
91+
{% endif %}

python/sglang/srt/openai_api/adapter.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -966,32 +966,6 @@ def v1_chat_generate_request(
966966

967967
if chat_template_name is None:
968968
openai_compatible_messages = []
969-
if (
970-
tools
971-
and tokenizer_manager.server_args.tool_call_parser == "deepseekv3"
972-
):
973-
# add function call prompt to deepseekv3
974-
openai_compatible_messages.append(
975-
{
976-
"role": "system",
977-
"content": """You are a helpful Assistant.
978-
## Tools
979-
### Function
980-
You have the following functions available:
981-
"""
982-
+ "".join(
983-
[
984-
f"""
985-
- `{tool['name']}`:
986-
```json
987-
{json.dumps(tool)}
988-
```
989-
"""
990-
for tool in tools
991-
]
992-
),
993-
}
994-
)
995969

996970
for message in request.messages:
997971
if message.content is None:

0 commit comments

Comments
 (0)