| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | from vllm.config import VllmConfig |
| 5 | from vllm.entrypoints.chat_utils import ( |
| 6 | ChatCompletionMessageParam, |
| 7 | ConversationMessage, |
| 8 | parse_chat_messages, |
| 9 | parse_chat_messages_async, |
| 10 | ) |
| 11 | from vllm.tokenizers.deepseek_v32 import DeepseekV32Tokenizer |
| 12 | from vllm.utils.async_utils import make_async |
| 13 | |
| 14 | from .base import BaseRenderer |
| 15 | from .inputs import DictPrompt |
| 16 | from .inputs.preprocess import parse_dec_only_prompt |
| 17 | from .params import ChatParams |
| 18 | |
| 19 | |
| 20 | class DeepseekV32Renderer(BaseRenderer[DeepseekV32Tokenizer]): |
| 21 | def __init__( |
| 22 | self, |
| 23 | config: VllmConfig, |
| 24 | tokenizer: DeepseekV32Tokenizer | None, |
| 25 | ) -> None: |
| 26 | super().__init__(config, tokenizer) |
| 27 | |
| 28 | self._apply_chat_template_async = make_async( |
| 29 | self._apply_chat_template, executor=self._executor |
| 30 | ) |
| 31 | |
| 32 | def _apply_chat_template(self, *args, **kwargs): |
| 33 | return self.get_tokenizer().apply_chat_template(*args, **kwargs) |
| 34 | |
| 35 | def render_messages( |
| 36 | self, |
| 37 | messages: list[ChatCompletionMessageParam], |
| 38 | params: ChatParams, |
| 39 | ) -> tuple[list[ConversationMessage], DictPrompt]: |
| 40 | conversation, mm_data, mm_uuids = parse_chat_messages( |
| 41 | messages, |
| 42 | self.model_config, |
| 43 | content_format="string", |
| 44 | media_io_kwargs=params.media_io_kwargs, |
| 45 | mm_processor_kwargs=params.mm_processor_kwargs, |
| 46 | ) |
| 47 | |
| 48 | prompt_raw = self._apply_chat_template( |
| 49 | conversation=conversation, |
| 50 | messages=messages, |
| 51 | **params.get_apply_chat_template_kwargs(), |
| 52 | ) |
| 53 | |
| 54 | prompt = parse_dec_only_prompt(prompt_raw) |
| 55 | if mm_data is not None: |
| 56 | prompt["multi_modal_data"] = mm_data |
| 57 | if mm_uuids is not None: |
| 58 | prompt["multi_modal_uuids"] = mm_uuids |
| 59 | |
| 60 | return conversation, prompt |
| 61 | |
| 62 | async def render_messages_async( |
| 63 | self, |
| 64 | messages: list[ChatCompletionMessageParam], |
| 65 | params: ChatParams, |
| 66 | ) -> tuple[list[ConversationMessage], DictPrompt]: |
| 67 | conversation, mm_data, mm_uuids = await parse_chat_messages_async( |
| 68 | messages, |
| 69 | self.model_config, |
| 70 | content_format="string", |
| 71 | media_io_kwargs=params.media_io_kwargs, |
| 72 | mm_processor_kwargs=params.mm_processor_kwargs, |
| 73 | ) |
| 74 | |
| 75 | prompt_raw = await self._apply_chat_template_async( |
| 76 | conversation=conversation, |
| 77 | messages=messages, |
| 78 | **params.get_apply_chat_template_kwargs(), |
| 79 | ) |
| 80 | |
| 81 | prompt = parse_dec_only_prompt(prompt_raw) |
| 82 | if mm_data is not None: |
| 83 | prompt["multi_modal_data"] = mm_data |
| 84 | if mm_uuids is not None: |
| 85 | prompt["multi_modal_uuids"] = mm_uuids |
| 86 | |
| 87 | return conversation, prompt |