diff --git a/python/sglang/srt/entrypoints/openai/encoding_dsv4.py b/python/sglang/srt/entrypoints/openai/encoding_dsv4.py new file mode 100644 index 0000000000..de9bfe9e8a --- /dev/null +++ b/python/sglang/srt/entrypoints/openai/encoding_dsv4.py @@ -0,0 +1,105 @@ +# SPDX-License-Identifier: Apache-2.0 +"""Compatibility shim for Dynamo's SGLang chat processor. + +Upstream SGLang builds expose ``sglang.srt.entrypoints.openai.encoding_dsv4`` +with ``encode_messages(messages, *, thinking_mode, reasoning_effort) -> str``. +Dynamo's frontend (``dynamo.frontend.sglang_prepost``) imports that module to +render DeepSeek-V4/V4.1 prompts when the model ships no HF chat template. + +Our DeepSeek-V4 fork replaced the encoder with the ``deepseek-recipe`` +renderer (``sglang.srt.parser.deepseek_recipe_renderer``). This module exposes +the upstream API on top of the recipe, so ``--dyn-chat-processor sglang`` +works without installing a second SGLang build. + +The returned string is tokenized by the caller. For the V4.1 checkpoint the +resulting ids match ``DeepSeekRecipe.encode()`` (the native serving path) +byte-for-byte; the renderer is the same code path, only the tokenizer call +moves to the caller. +""" + +from __future__ import annotations + +import copy +import json +from functools import lru_cache +from typing import Any, Optional + +from sglang.srt.parser.deepseek_recipe_renderer import ( + IMAGE_TOKEN, + MODEL_NAMES, + load_deepseek_recipe, + pre_normalize, +) + +# The Dynamo frontend uses this encoder for the V4 family. Our deployment is +# DeepSeek-V4.1-Flash; V4-Pro checkpoints need the ``dsv4`` spec instead. +_CHAT_ENCODING_SPEC = "dsv41" + + +@lru_cache(maxsize=1) +def _renderer(): + recipe = load_deepseek_recipe() + special_tokens = recipe.SpecialTokens(image=IMAGE_TOKEN) + encoding = recipe.DeepseekV41Encoding().with_special_tokens(special_tokens) + options = recipe.ConversionOptions().with_special_tokens(special_tokens) + return recipe, special_tokens, encoding, options + + +def encode_messages( + messages: list[dict[str, Any]], + *, + thinking_mode: str = "chat", + reasoning_effort: Optional[str] = None, +) -> str: + """Render OpenAI-style ``messages`` to a DeepSeek-V4.1 prompt string. + + Mirrors upstream ``encoding_dsv4.encode_messages``: + + * ``thinking_mode`` is ``"thinking"`` or ``"chat"``; + * ``reasoning_effort`` is a named tier (``"high"`` / ``"max"``) or None; + * tool schemas are attached to the first system message by the caller + (Dynamo's convention), not passed as a separate argument. + + Raises ``ValueError`` for prompt shapes the recipe rejects. + """ + recipe, _special_tokens, encoding, options = _renderer() + + encoding_messages = copy.deepcopy(messages) + tools = None + if encoding_messages: + tools = encoding_messages[0].pop("tools", None) + if not tools: + tools = None + + # Dynamo already flattens message content and re-serializes tool-call + # arguments; keep this defensive normalization for direct callers. + for message in encoding_messages: + if message.get("content") is None: + message["content"] = "" + for call in message.get("tool_calls") or []: + function = call.get("function") if isinstance(call, dict) else None + if isinstance(function, dict) and isinstance( + function.get("arguments"), (dict, list) + ): + function["arguments"] = json.dumps( + function["arguments"], ensure_ascii=False + ) + + body: dict[str, Any] = { + "messages": encoding_messages, + "thinking": thinking_mode == "thinking", + } + if tools: + body["tools"] = tools + if reasoning_effort is not None: + body["reasoning_effort"] = reasoning_effort + + normalized = pre_normalize(body) + normalized["model"] = MODEL_NAMES[_CHAT_ENCODING_SPEC] + try: + converted = recipe.ChatCompletionRequest(normalized).convert(options) + except recipe.ConversionError as error: # type: ignore[attr-defined] + detail = getattr(error, "body", None) or str(error) + raise ValueError(f"DeepSeek-V4.1 prompt conversion failed: {detail}") from error + + return encoding.render_conversation(converted.conversation).prompt