[Refactor] Remove deepseek dead code (#48780)
Signed-off-by: yewentao256 <zhyanwentao@126.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent
c4dd6d78fd
commit
877dae9c68
@@ -12,7 +12,6 @@ from transformers import DeepseekV2Config, DeepseekV3Config
|
|||||||
|
|
||||||
from vllm.compilation.decorators import support_torch_compile
|
from vllm.compilation.decorators import support_torch_compile
|
||||||
from vllm.config import VllmConfig, get_current_vllm_config
|
from vllm.config import VllmConfig, get_current_vllm_config
|
||||||
from vllm.logger import init_logger
|
|
||||||
from vllm.model_executor.layers.layernorm import RMSNorm
|
from vllm.model_executor.layers.layernorm import RMSNorm
|
||||||
from vllm.model_executor.layers.linear import ReplicatedLinear
|
from vllm.model_executor.layers.linear import ReplicatedLinear
|
||||||
from vllm.model_executor.layers.logits_processor import LogitsProcessor
|
from vllm.model_executor.layers.logits_processor import LogitsProcessor
|
||||||
@@ -36,8 +35,6 @@ from .utils import (
|
|||||||
process_eagle_weight,
|
process_eagle_weight,
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = init_logger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class DeepseekV2Eagle3DecoderLayer(nn.Module):
|
class DeepseekV2Eagle3DecoderLayer(nn.Module):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ from vllm._aiter_ops import rocm_aiter_ops
|
|||||||
from vllm.compilation.decorators import support_torch_compile
|
from vllm.compilation.decorators import support_torch_compile
|
||||||
from vllm.config import VllmConfig
|
from vllm.config import VllmConfig
|
||||||
from vllm.distributed import tensor_model_parallel_all_gather
|
from vllm.distributed import tensor_model_parallel_all_gather
|
||||||
from vllm.logger import init_logger
|
|
||||||
from vllm.model_executor.layers.fused_moe import (
|
from vllm.model_executor.layers.fused_moe import (
|
||||||
fused_moe_make_expert_params_mapping,
|
fused_moe_make_expert_params_mapping,
|
||||||
)
|
)
|
||||||
@@ -38,8 +37,6 @@ from .deepseek_v2 import (
|
|||||||
)
|
)
|
||||||
from .utils import get_pp_missing_layer_names, maybe_prefix
|
from .utils import get_pp_missing_layer_names, maybe_prefix
|
||||||
|
|
||||||
logger = init_logger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
def _restore_full_token_layout_if_needed(
|
def _restore_full_token_layout_if_needed(
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ import torch
|
|||||||
|
|
||||||
from vllm.logger import init_logger
|
from vllm.logger import init_logger
|
||||||
from vllm.tracing import instrument
|
from vllm.tracing import instrument
|
||||||
from vllm.utils.math_utils import cdiv
|
|
||||||
|
|
||||||
logger = init_logger(__name__)
|
logger = init_logger(__name__)
|
||||||
|
|
||||||
@@ -39,23 +38,6 @@ _DEFAULT_TOKEN_SIZE_CANDIDATES = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _compute_mhc_pre_num_split(
|
|
||||||
*,
|
|
||||||
num_tokens: int,
|
|
||||||
hidden_size: int,
|
|
||||||
hc_mult: int,
|
|
||||||
num_sms: int,
|
|
||||||
) -> int:
|
|
||||||
block_k = 64
|
|
||||||
block_m = 64
|
|
||||||
k = hc_mult * hidden_size
|
|
||||||
grid_size = cdiv(num_tokens, block_m)
|
|
||||||
split_k = num_sms // grid_size
|
|
||||||
num_block_k = cdiv(k, block_k)
|
|
||||||
split_k = min(split_k, num_block_k // 4)
|
|
||||||
return max(split_k, 1)
|
|
||||||
|
|
||||||
|
|
||||||
def _normalize_token_sizes(
|
def _normalize_token_sizes(
|
||||||
token_sizes: Iterable[int],
|
token_sizes: Iterable[int],
|
||||||
*,
|
*,
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ from vllm.entrypoints.chat_utils import (
|
|||||||
parse_chat_messages,
|
parse_chat_messages,
|
||||||
parse_chat_messages_async,
|
parse_chat_messages_async,
|
||||||
)
|
)
|
||||||
from vllm.logger import init_logger
|
|
||||||
from vllm.tokenizers.deepseek_v32 import DeepseekV32Tokenizer
|
from vllm.tokenizers.deepseek_v32 import DeepseekV32Tokenizer
|
||||||
from vllm.utils.async_utils import make_async
|
from vllm.utils.async_utils import make_async
|
||||||
|
|
||||||
@@ -17,8 +16,6 @@ from .inputs import DictPrompt
|
|||||||
from .inputs.preprocess import parse_dec_only_prompt
|
from .inputs.preprocess import parse_dec_only_prompt
|
||||||
from .params import ChatParams
|
from .params import ChatParams
|
||||||
|
|
||||||
logger = init_logger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class DeepseekV32Renderer(BaseRenderer[DeepseekV32Tokenizer]):
|
class DeepseekV32Renderer(BaseRenderer[DeepseekV32Tokenizer]):
|
||||||
def __init__(
|
def __init__(
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ from vllm.entrypoints.chat_utils import (
|
|||||||
parse_chat_messages,
|
parse_chat_messages,
|
||||||
parse_chat_messages_async,
|
parse_chat_messages_async,
|
||||||
)
|
)
|
||||||
from vllm.logger import init_logger
|
|
||||||
from vllm.tokenizers.deepseek_v4 import DeepseekV4Tokenizer
|
from vllm.tokenizers.deepseek_v4 import DeepseekV4Tokenizer
|
||||||
from vllm.utils.async_utils import make_async
|
from vllm.utils.async_utils import make_async
|
||||||
|
|
||||||
@@ -17,8 +16,6 @@ from .inputs import DictPrompt
|
|||||||
from .inputs.preprocess import parse_dec_only_prompt
|
from .inputs.preprocess import parse_dec_only_prompt
|
||||||
from .params import ChatParams
|
from .params import ChatParams
|
||||||
|
|
||||||
logger = init_logger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class DeepseekV4Renderer(BaseRenderer[DeepseekV4Tokenizer]):
|
class DeepseekV4Renderer(BaseRenderer[DeepseekV4Tokenizer]):
|
||||||
def __init__(
|
def __init__(
|
||||||
|
|||||||
@@ -7,8 +7,6 @@ import copy
|
|||||||
import json
|
import json
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import regex as re
|
|
||||||
|
|
||||||
# flake8: noqa: E501
|
# flake8: noqa: E501
|
||||||
TOOLS_SYSTEM_TEMPLATE = """## Tools
|
TOOLS_SYSTEM_TEMPLATE = """## Tools
|
||||||
You have access to a set of tools you can use to answer the user's question.
|
You have access to a set of tools you can use to answer the user's question.
|
||||||
@@ -79,19 +77,6 @@ def tool_calls_from_openai_format(tool_calls):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def tool_calls_to_openai_format(tool_calls):
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
"type": "function",
|
|
||||||
"function": {
|
|
||||||
"name": tool_call["name"],
|
|
||||||
"arguments": tool_call["arguments"],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for tool_call in tool_calls
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def encode_arguments_to_dsml(tool_call: dict[str, str]) -> str:
|
def encode_arguments_to_dsml(tool_call: dict[str, str]) -> str:
|
||||||
p_dsml_template = """<{dsml_token}parameter name="{key}" string="{is_str}">{value}</{dsml_token}parameter>"""
|
p_dsml_template = """<{dsml_token}parameter name="{key}" string="{is_str}">{value}</{dsml_token}parameter>"""
|
||||||
P_dsml_strs = []
|
P_dsml_strs = []
|
||||||
@@ -113,24 +98,6 @@ def encode_arguments_to_dsml(tool_call: dict[str, str]) -> str:
|
|||||||
return "\n".join(P_dsml_strs)
|
return "\n".join(P_dsml_strs)
|
||||||
|
|
||||||
|
|
||||||
def decode_dsml_to_arguments(
|
|
||||||
tool_name: str, tool_args: dict[str, tuple[str, str]]
|
|
||||||
) -> dict[str, str]:
|
|
||||||
def _decode_value(key: str, value: str, string: str):
|
|
||||||
if string == "true":
|
|
||||||
value = to_json(value)
|
|
||||||
return f"{to_json(key)}: {value}"
|
|
||||||
|
|
||||||
tool_args_json = (
|
|
||||||
"{"
|
|
||||||
+ ", ".join(
|
|
||||||
[_decode_value(k, v, string=is_str) for k, (v, is_str) in tool_args.items()]
|
|
||||||
)
|
|
||||||
+ "}"
|
|
||||||
)
|
|
||||||
return dict(name=tool_name, arguments=tool_args_json)
|
|
||||||
|
|
||||||
|
|
||||||
def render_tools(tools: list[dict[str, str | dict[str, Any]]]) -> str:
|
def render_tools(tools: list[dict[str, str | dict[str, Any]]]) -> str:
|
||||||
tools_json = [to_json(t) for t in tools]
|
tools_json = [to_json(t) for t in tools]
|
||||||
|
|
||||||
@@ -333,139 +300,3 @@ def encode_messages(
|
|||||||
)
|
)
|
||||||
|
|
||||||
return prompt
|
return prompt
|
||||||
|
|
||||||
|
|
||||||
def _read_until_stop(
|
|
||||||
index: int, text: str, stop: list[str]
|
|
||||||
) -> tuple[int, str, None | str]:
|
|
||||||
min_pos = len(text)
|
|
||||||
matched_stop = None
|
|
||||||
|
|
||||||
for s in stop:
|
|
||||||
pos = text.find(s, index)
|
|
||||||
if pos != -1 and pos < min_pos:
|
|
||||||
min_pos = pos
|
|
||||||
matched_stop = s
|
|
||||||
|
|
||||||
if matched_stop:
|
|
||||||
content = text[index:min_pos]
|
|
||||||
return min_pos + len(matched_stop), content, matched_stop
|
|
||||||
else:
|
|
||||||
content = text[index:]
|
|
||||||
return len(text), content, None
|
|
||||||
|
|
||||||
|
|
||||||
def parse_tool_calls(index: int, text: str):
|
|
||||||
tool_calls: list[dict[str, Any]] = []
|
|
||||||
stop_token = None
|
|
||||||
tool_calls_end_token = f"</{dsml_token}function_calls>"
|
|
||||||
|
|
||||||
while index < len(text):
|
|
||||||
index, _, stop_token = _read_until_stop(
|
|
||||||
index, text, [f"<{dsml_token}invoke", tool_calls_end_token]
|
|
||||||
)
|
|
||||||
if _ != ">\n":
|
|
||||||
raise RuntimeError("Tool call format error")
|
|
||||||
|
|
||||||
if stop_token == tool_calls_end_token:
|
|
||||||
break
|
|
||||||
|
|
||||||
if stop_token is None:
|
|
||||||
raise RuntimeError("Missing special token")
|
|
||||||
|
|
||||||
index, tool_name_content, stop_token = _read_until_stop(
|
|
||||||
index, text, [f"<{dsml_token}parameter", f"</{dsml_token}invoke"]
|
|
||||||
)
|
|
||||||
|
|
||||||
p_tool_name = re.findall(
|
|
||||||
r'^\s*name="(.*?)">\n$', tool_name_content, flags=re.DOTALL
|
|
||||||
)
|
|
||||||
if len(p_tool_name) != 1:
|
|
||||||
raise RuntimeError("Tool name format error")
|
|
||||||
tool_name = p_tool_name[0]
|
|
||||||
|
|
||||||
tool_args: dict[str, tuple[str, str]] = {}
|
|
||||||
while stop_token == f"<{dsml_token}parameter":
|
|
||||||
index, param_content, stop_token = _read_until_stop(
|
|
||||||
index, text, [f"/{dsml_token}parameter"]
|
|
||||||
)
|
|
||||||
|
|
||||||
param_kv = re.findall(
|
|
||||||
r'^ name="(.*?)" string="(true|false)">(.*?)<$',
|
|
||||||
param_content,
|
|
||||||
flags=re.DOTALL,
|
|
||||||
)
|
|
||||||
if len(param_kv) != 1:
|
|
||||||
raise RuntimeError("Parameter format error")
|
|
||||||
param_name, string, param_value = param_kv[0]
|
|
||||||
|
|
||||||
if param_name in tool_args:
|
|
||||||
raise RuntimeError("Duplicate parameter name")
|
|
||||||
tool_args[param_name] = (param_value, string)
|
|
||||||
|
|
||||||
index, content, stop_token = _read_until_stop(
|
|
||||||
index, text, [f"<{dsml_token}parameter", f"</{dsml_token}invoke"]
|
|
||||||
)
|
|
||||||
if content != ">\n":
|
|
||||||
raise RuntimeError("Parameter format error")
|
|
||||||
|
|
||||||
tool_call = decode_dsml_to_arguments(tool_name=tool_name, tool_args=tool_args)
|
|
||||||
tool_calls.append(tool_call)
|
|
||||||
|
|
||||||
return index, stop_token, tool_calls
|
|
||||||
|
|
||||||
|
|
||||||
# NOTE: This function is designed to parse only correctly
|
|
||||||
# formatted string and will not attempt to correct malformed output
|
|
||||||
# that may be generated by the model.
|
|
||||||
def parse_message_from_completion_text(text: str, thinking_mode: str):
|
|
||||||
summary_content, reasoning, tool_calls = "", "", []
|
|
||||||
index, stop_token = 0, None
|
|
||||||
tool_calls_start_token = f"\n\n<{dsml_token}function_calls"
|
|
||||||
|
|
||||||
is_thinking, is_tool_calling = thinking_mode == "thinking", False
|
|
||||||
|
|
||||||
if is_thinking:
|
|
||||||
index, content_delta, stop_token = _read_until_stop(
|
|
||||||
index, text, [thinking_end_token, tool_calls_start_token]
|
|
||||||
)
|
|
||||||
reasoning = content_delta
|
|
||||||
if stop_token != thinking_end_token:
|
|
||||||
raise RuntimeError("Invalid thinking format")
|
|
||||||
|
|
||||||
index, content_delta, stop_token = _read_until_stop(
|
|
||||||
index, text, [eos_token, tool_calls_start_token]
|
|
||||||
)
|
|
||||||
summary_content = content_delta
|
|
||||||
if stop_token == tool_calls_start_token:
|
|
||||||
is_tool_calling = True
|
|
||||||
else:
|
|
||||||
if stop_token != eos_token:
|
|
||||||
raise RuntimeError("Invalid summary format")
|
|
||||||
|
|
||||||
if is_tool_calling:
|
|
||||||
index, stop_token, tool_calls = parse_tool_calls(index, text)
|
|
||||||
|
|
||||||
index, tool_ends_text, stop_token = _read_until_stop(index, text, [eos_token])
|
|
||||||
if tool_ends_text:
|
|
||||||
raise RuntimeError("Unexpected content after tool calls")
|
|
||||||
|
|
||||||
if not (len(text) == index and stop_token in [eos_token, None]):
|
|
||||||
raise RuntimeError("Unexpected content at end")
|
|
||||||
|
|
||||||
for sp_token in [
|
|
||||||
bos_token,
|
|
||||||
eos_token,
|
|
||||||
thinking_start_token,
|
|
||||||
thinking_end_token,
|
|
||||||
dsml_token,
|
|
||||||
]:
|
|
||||||
if sp_token in summary_content or sp_token in reasoning:
|
|
||||||
raise RuntimeError("Unexpected special token in content")
|
|
||||||
|
|
||||||
return {
|
|
||||||
"role": "assistant",
|
|
||||||
"content": summary_content,
|
|
||||||
"reasoning": reasoning,
|
|
||||||
"tool_calls": tool_calls_to_openai_format(tool_calls),
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -6,16 +6,14 @@
|
|||||||
"""
|
"""
|
||||||
DeepSeek-V4 Encoding
|
DeepSeek-V4 Encoding
|
||||||
|
|
||||||
A self-contained implementation for encoding/decoding DeepSeek-V4 chat messages
|
A self-contained implementation for encoding DeepSeek-V4 chat messages with tool
|
||||||
with tool calling, thinking mode, and quick instruction task support.
|
calling, thinking mode, and quick instruction task support.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Any, Dict, List, Union, Optional, Tuple
|
from typing import Any, Dict, List, Union, Optional
|
||||||
import copy
|
import copy
|
||||||
import json
|
import json
|
||||||
|
|
||||||
import regex as re
|
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
# Special Tokens
|
# Special Tokens
|
||||||
# ============================================================
|
# ============================================================
|
||||||
@@ -128,20 +126,6 @@ def tool_calls_from_openai_format(tool_calls):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def tool_calls_to_openai_format(tool_calls):
|
|
||||||
"""Convert internal tool calls to OpenAI format."""
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
"type": "function",
|
|
||||||
"function": {
|
|
||||||
"name": tool_call["name"],
|
|
||||||
"arguments": tool_call["arguments"],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for tool_call in tool_calls
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def encode_arguments_to_dsml(tool_call: Dict[str, Any]) -> str:
|
def encode_arguments_to_dsml(tool_call: Dict[str, Any]) -> str:
|
||||||
"""
|
"""
|
||||||
Encode tool call arguments into DSML parameter format.
|
Encode tool call arguments into DSML parameter format.
|
||||||
@@ -172,26 +156,6 @@ def encode_arguments_to_dsml(tool_call: Dict[str, Any]) -> str:
|
|||||||
return "\n".join(P_dsml_strs)
|
return "\n".join(P_dsml_strs)
|
||||||
|
|
||||||
|
|
||||||
def decode_dsml_to_arguments(tool_name: str, tool_args: Dict[str, Tuple[str, str]]) -> Dict[str, str]:
|
|
||||||
"""
|
|
||||||
Decode DSML parameters back to a tool call dict.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
tool_name: Name of the tool.
|
|
||||||
tool_args: Dict mapping param_name -> (value, is_string_flag).
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Dict with "name" and "arguments" (JSON string) keys.
|
|
||||||
"""
|
|
||||||
def _decode_value(key: str, value: str, string: str):
|
|
||||||
if string == "true":
|
|
||||||
value = to_json(value)
|
|
||||||
return f"{to_json(key)}: {value}"
|
|
||||||
|
|
||||||
tool_args_json = "{" + ", ".join([_decode_value(k, v, string=is_str) for k, (v, is_str) in tool_args.items()]) + "}"
|
|
||||||
return dict(name=tool_name, arguments=tool_args_json)
|
|
||||||
|
|
||||||
|
|
||||||
def render_tools(tools: List[Dict[str, Union[str, Dict[str, Any]]]]) -> str:
|
def render_tools(tools: List[Dict[str, Union[str, Dict[str, Any]]]]) -> str:
|
||||||
"""
|
"""
|
||||||
Render tool schemas into the system prompt format.
|
Render tool schemas into the system prompt format.
|
||||||
@@ -605,153 +569,4 @@ def _drop_thinking_messages(messages: List[Dict[str, Any]]) -> List[Dict[str, An
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
# ============================================================
|
|
||||||
# Parsing (Decoding model output)
|
|
||||||
# ============================================================
|
|
||||||
|
|
||||||
def _read_until_stop(index: int, text: str, stop: List[str]) -> Tuple[int, str, Optional[str]]:
|
|
||||||
"""
|
|
||||||
Read text from index until one of the stop strings is found.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Tuple of (new_index, content_before_stop, matched_stop_string_or_None).
|
|
||||||
"""
|
|
||||||
min_pos = len(text)
|
|
||||||
matched_stop = None
|
|
||||||
|
|
||||||
for s in stop:
|
|
||||||
pos = text.find(s, index)
|
|
||||||
if pos != -1 and pos < min_pos:
|
|
||||||
min_pos = pos
|
|
||||||
matched_stop = s
|
|
||||||
|
|
||||||
if matched_stop:
|
|
||||||
content = text[index:min_pos]
|
|
||||||
return min_pos + len(matched_stop), content, matched_stop
|
|
||||||
else:
|
|
||||||
content = text[index:]
|
|
||||||
return len(text), content, None
|
|
||||||
|
|
||||||
|
|
||||||
def parse_tool_calls(index: int, text: str) -> Tuple[int, Optional[str], List[Dict[str, str]]]:
|
|
||||||
"""
|
|
||||||
Parse DSML tool calls from text starting at the given index.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
index: Starting position in text.
|
|
||||||
text: The full text to parse.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Tuple of (new_index, last_stop_token, list_of_tool_call_dicts).
|
|
||||||
Each tool call dict has "name" and "arguments" keys.
|
|
||||||
"""
|
|
||||||
tool_calls: List[Dict[str, Any]] = []
|
|
||||||
stop_token = None
|
|
||||||
tool_calls_end_token = f"</{dsml_token}{tool_calls_block_name}>"
|
|
||||||
|
|
||||||
while index < len(text):
|
|
||||||
index, content_before, stop_token = _read_until_stop(index, text, [f"<{dsml_token}invoke", tool_calls_end_token])
|
|
||||||
if content_before != ">\n":
|
|
||||||
raise ValueError(f"Tool call format error: expected '>\\n' but got '{content_before}'")
|
|
||||||
|
|
||||||
if stop_token == tool_calls_end_token:
|
|
||||||
break
|
|
||||||
|
|
||||||
if stop_token is None:
|
|
||||||
raise ValueError("Missing special token in tool calls")
|
|
||||||
|
|
||||||
index, tool_name_content, stop_token = _read_until_stop(index, text, [f"<{dsml_token}parameter", f"</{dsml_token}invoke"])
|
|
||||||
|
|
||||||
p_tool_name = re.findall(r'^\s*name="(.*?)">\n$', tool_name_content, flags=re.DOTALL)
|
|
||||||
if len(p_tool_name) != 1:
|
|
||||||
raise ValueError(f"Tool name format error: '{tool_name_content}'")
|
|
||||||
tool_name = p_tool_name[0]
|
|
||||||
|
|
||||||
tool_args: Dict[str, Tuple[str, str]] = {}
|
|
||||||
while stop_token == f"<{dsml_token}parameter":
|
|
||||||
index, param_content, stop_token = _read_until_stop(index, text, [f"/{dsml_token}parameter"])
|
|
||||||
|
|
||||||
param_kv = re.findall(r'^ name="(.*?)" string="(true|false)">(.*?)<$', param_content, flags=re.DOTALL)
|
|
||||||
if len(param_kv) != 1:
|
|
||||||
raise ValueError(f"Parameter format error: '{param_content}'")
|
|
||||||
param_name, string, param_value = param_kv[0]
|
|
||||||
|
|
||||||
if param_name in tool_args:
|
|
||||||
raise ValueError(f"Duplicate parameter name: '{param_name}'")
|
|
||||||
tool_args[param_name] = (param_value, string)
|
|
||||||
|
|
||||||
index, content, stop_token = _read_until_stop(index, text, [f"<{dsml_token}parameter", f"</{dsml_token}invoke"])
|
|
||||||
if content != ">\n":
|
|
||||||
raise ValueError(f"Parameter format error: expected '>\\n' but got '{content}'")
|
|
||||||
|
|
||||||
tool_call = decode_dsml_to_arguments(tool_name=tool_name, tool_args=tool_args)
|
|
||||||
tool_calls.append(tool_call)
|
|
||||||
|
|
||||||
return index, stop_token, tool_calls
|
|
||||||
|
|
||||||
|
|
||||||
def parse_message_from_completion_text(text: str, thinking_mode: str) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Parse a model completion text into a structured assistant message.
|
|
||||||
|
|
||||||
This function takes the raw text output from the model (a single assistant turn)
|
|
||||||
and extracts:
|
|
||||||
- reasoning (thinking block)
|
|
||||||
- content (summary/response)
|
|
||||||
- tool_calls (if any)
|
|
||||||
|
|
||||||
NOTE: This function is designed to parse only correctly formatted strings and
|
|
||||||
will raise ValueError for malformed output.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
text: The raw completion text (including EOS token).
|
|
||||||
thinking_mode: Either "chat" or "thinking".
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Dict with keys: "role", "content", "reasoning", "tool_calls".
|
|
||||||
tool_calls are in OpenAI format.
|
|
||||||
"""
|
|
||||||
summary_content, reasoning = "", ""
|
|
||||||
tool_calls: List[Dict[str, str]] = []
|
|
||||||
index, stop_token = 0, None
|
|
||||||
tool_calls_start_token = f"\n\n<{dsml_token}{tool_calls_block_name}"
|
|
||||||
|
|
||||||
is_thinking = thinking_mode == "thinking"
|
|
||||||
is_tool_calling = False
|
|
||||||
|
|
||||||
if is_thinking:
|
|
||||||
index, content_delta, stop_token = _read_until_stop(index, text, [thinking_end_token, tool_calls_start_token])
|
|
||||||
reasoning = content_delta
|
|
||||||
if stop_token != thinking_end_token:
|
|
||||||
raise ValueError("Invalid thinking format: missing </think>")
|
|
||||||
|
|
||||||
index, content_delta, stop_token = _read_until_stop(index, text, [eos_token, tool_calls_start_token])
|
|
||||||
summary_content = content_delta
|
|
||||||
if stop_token == tool_calls_start_token:
|
|
||||||
is_tool_calling = True
|
|
||||||
else:
|
|
||||||
if stop_token != eos_token:
|
|
||||||
raise ValueError("Invalid format: missing EOS token")
|
|
||||||
|
|
||||||
if is_tool_calling:
|
|
||||||
index, stop_token, tool_calls = parse_tool_calls(index, text)
|
|
||||||
|
|
||||||
index, tool_ends_text, stop_token = _read_until_stop(index, text, [eos_token])
|
|
||||||
if tool_ends_text:
|
|
||||||
raise ValueError("Unexpected content after tool calls")
|
|
||||||
|
|
||||||
if len(text) != index or stop_token not in [eos_token, None]:
|
|
||||||
raise ValueError("Unexpected content at end")
|
|
||||||
|
|
||||||
for sp_token in [bos_token, eos_token, thinking_start_token, thinking_end_token, dsml_token]:
|
|
||||||
if sp_token in summary_content or sp_token in reasoning:
|
|
||||||
raise ValueError(f"Unexpected special token '{sp_token}' in content")
|
|
||||||
|
|
||||||
return {
|
|
||||||
"role": "assistant",
|
|
||||||
"content": summary_content,
|
|
||||||
"reasoning": reasoning,
|
|
||||||
"tool_calls": tool_calls_to_openai_format(tool_calls)
|
|
||||||
}
|
|
||||||
|
|
||||||
# fmt: on
|
# fmt: on
|
||||||
|
|||||||
Reference in New Issue
Block a user