Skip to content

Commit 15e0373

Browse files
committed
[CS-1549] Eanble function call DeepSeek-V3.1
1 parent 97d553d commit 15e0373

File tree

4 files changed

+470
-1
lines changed

4 files changed

+470
-1
lines changed
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+
{% 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+
{% if tools is defined and tools is not none %}
20+
{% set tool_ns = namespace(text='## Tools\nYou have access to the following tools:\n') %}
21+
{% for tool in tools %}
22+
{% set tool_ns.text = tool_ns.text + '\n### ' + tool.function.name + '\nDescription: ' + tool.function.description + '\n\nParameters: ' + (tool.function.parameters | tojson) + '\n' %}
23+
{% endfor %}
24+
{% 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" %}
25+
{% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
26+
{% endif %}
27+
28+
{{ bos_token }}{{ ns.system_prompt }}
29+
{%- for message in messages %}
30+
{%- if message['role'] == 'user' %}
31+
{%- set ns.is_tool = false -%}
32+
{%- set ns.is_first = false -%}
33+
{%- set ns.is_last_user = true -%}
34+
{{'<|User|>' + message['content']}}
35+
{%- endif %}
36+
{%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
37+
{%- if ns.is_last_user %}
38+
{{'<|Assistant|></think>'}}
39+
{%- endif %}
40+
{%- set ns.is_last_user = false -%}
41+
{%- set ns.is_first = false %}
42+
{%- set ns.is_tool = false -%}
43+
{%- for tool in message['tool_calls'] %}
44+
{%- if not ns.is_first %}
45+
{%- if message['content'] is none %}
46+
{{'<|tool▁calls▁begin|><|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}}
47+
{%- else %}
48+
{{message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}}
49+
{%- endif %}
50+
{%- set ns.is_first = true -%}
51+
{%- else %}
52+
{{'<|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}}
53+
{%- endif %}
54+
{%- endfor %}
55+
{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}
56+
{%- endif %}
57+
{%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none) %}
58+
{%- if ns.is_last_user %}
59+
{{'<|Assistant|>'}}
60+
{%- if message['prefix'] is defined and message['prefix'] and thinking %}
61+
{{'<think>'}}
62+
{%- else %}
63+
{{'</think>'}}
64+
{%- endif %}
65+
{%- endif %}
66+
{%- set ns.is_last_user = false -%}
67+
{%- if ns.is_tool %}
68+
{{message['content'] + '<|end▁of▁sentence|>'}}
69+
{%- set ns.is_tool = false -%}
70+
{%- else %}
71+
{%- set content = message['content'] -%}
72+
{%- if '</think>' in content %}
73+
{%- set content = content.split('</think>', 1)[1] -%}
74+
{%- endif %}
75+
{{content + '<|end▁of▁sentence|>'}}
76+
{%- endif %}
77+
{%- endif %}
78+
{%- if message['role'] == 'tool' %}
79+
{%- set ns.is_last_user = false -%}
80+
{%- set ns.is_tool = true -%}
81+
{{'<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
82+
{%- endif %}
83+
{%- endfor -%}
84+
{%- if add_generation_prompt and ns.is_last_user and not ns.is_tool %}
85+
{{'<|Assistant|>'}}
86+
{%- if not thinking %}
87+
{{'</think>'}}
88+
{%- else %}
89+
{{'<think>'}}
90+
{%- endif %}
91+
{% endif %}

vllm/entrypoints/chat_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
from vllm.multimodal.utils import MediaConnector
3939
from vllm.transformers_utils.tokenizer import AnyTokenizer, MistralTokenizer
4040

41+
from vllm.utils import random_uuid
42+
4143
logger = init_logger(__name__)
4244

4345

@@ -1005,3 +1007,11 @@ def apply_mistral_chat_template(
10051007
messages=messages,
10061008
**kwargs,
10071009
)
1010+
1011+
1012+
def make_tool_call_id(id_type: str = "random", func_name=None, idx=None):
1013+
if id_type == "kimi_k2":
1014+
return f"functions.{func_name}:{idx}"
1015+
else:
1016+
# by default return random
1017+
return f"chatcmpl-tool-{random_uuid()}"

vllm/entrypoints/openai/tool_parsers/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: Apache-2.0
22

33
from .abstract_tool_parser import ToolParser, ToolParserManager
4+
from .deepseekv31_tool_parser import DeepSeekV31ToolParser
45
from .granite_20b_fc_tool_parser import Granite20bFCToolParser
56
from .granite_tool_parser import GraniteToolParser
67
from .hermes_tool_parser import Hermes2ProToolParser
@@ -14,5 +15,5 @@
1415
"ToolParser", "ToolParserManager", "Granite20bFCToolParser",
1516
"GraniteToolParser", "Hermes2ProToolParser", "MistralToolParser",
1617
"Internlm2ToolParser", "Llama3JsonToolParser", "JambaToolParser",
17-
"PythonicToolParser"
18+
"PythonicToolParser","DeepSeekV31ToolParser",
1819
]

0 commit comments

Comments
 (0)