|
34 | 34 | logger = logging.getLogger(__name__) |
35 | 35 |
|
36 | 36 |
|
| 37 | +def _build_reasoning_parser( |
| 38 | + name: str, chat_template_kwargs: dict[str, Any] |
| 39 | +) -> Any: |
| 40 | + from tensorrt_llm.llmapi.reasoning_parser import ReasoningParserFactory |
| 41 | + |
| 42 | + if name == "deepseek-r1" and "enable_thinking" in chat_template_kwargs: |
| 43 | + from tensorrt_llm.llmapi.reasoning_parser import DeepSeekR1Parser |
| 44 | + |
| 45 | + return DeepSeekR1Parser( |
| 46 | + reasoning_at_start=bool(chat_template_kwargs["enable_thinking"]), |
| 47 | + chat_template_kwargs=chat_template_kwargs, |
| 48 | + ) |
| 49 | + |
| 50 | + return ReasoningParserFactory.create_reasoning_parser( |
| 51 | + name, chat_template_kwargs |
| 52 | + ) |
| 53 | + |
| 54 | + |
37 | 55 | def create_app( |
38 | 56 | llm: Any, |
39 | 57 | tokenizer: Any, |
@@ -69,13 +87,7 @@ def create_app( |
69 | 87 | ) |
70 | 88 |
|
71 | 89 | if reasoning_parser is not None: |
72 | | - from tensorrt_llm.llmapi.reasoning_parser import ReasoningParserFactory |
73 | | - |
74 | | - # Validate the configured parser when the server starts. A fresh parser is |
75 | | - # created per request below with that request's chat-template arguments. |
76 | | - ReasoningParserFactory.create_reasoning_parser( |
77 | | - reasoning_parser, _server_template_kwargs |
78 | | - ) |
| 90 | + _build_reasoning_parser(reasoning_parser, _server_template_kwargs) |
79 | 91 |
|
80 | 92 | # Match TRT-LLM's effective EOS set so this adapter can trim every returned |
81 | 93 | # stop token and its logprob together, preserving multi-turn continuity. |
@@ -125,9 +137,7 @@ async def chat_completions(request: Request): |
125 | 137 | effective_template_kwargs = {**_server_template_kwargs, **per_request_kwargs} |
126 | 138 |
|
127 | 139 | _active_reasoning_parser = ( |
128 | | - ReasoningParserFactory.create_reasoning_parser( |
129 | | - reasoning_parser, effective_template_kwargs |
130 | | - ) |
| 140 | + _build_reasoning_parser(reasoning_parser, effective_template_kwargs) |
131 | 141 | if reasoning_parser is not None |
132 | 142 | else None |
133 | 143 | ) |
|
0 commit comments