-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathreasoning_parser.py
More file actions
236 lines (194 loc) · 8.9 KB
/
reasoning_parser.py
File metadata and controls
236 lines (194 loc) · 8.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import json
from abc import ABC, abstractmethod
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Optional, Type
@dataclass
class ReasoningParserResult:
content: str = ""
reasoning_content: str = ""
def register_reasoning_parser(*keys: str, **default_kwargs):
"""Decorator that registers a BaseReasoningParser under one or more keys.
Any extra keyword arguments are stored as defaults and forwarded to
the parser constructor at creation time.
Usage::
@register_reasoning_parser("my-model", reasoning_at_start=True)
class MyParser(BaseReasoningParser):
...
"""
def decorator(parser_cls: Type["BaseReasoningParser"]):
for key in keys:
ReasoningParserFactory._parsers[key] = (parser_cls, default_kwargs)
return parser_cls
return decorator
class ReasoningParserFactory:
_parsers: dict[str, tuple[Type["BaseReasoningParser"], dict[str, Any]]] = {}
@classmethod
def create_reasoning_parser(
cls,
reasoning_parser: str,
chat_template_kwargs: Optional[dict[str, Any]] = None,
) -> "BaseReasoningParser":
key = reasoning_parser.lower()
try:
parser_cls, default_kwargs = cls._parsers[key]
except KeyError as e:
raise ValueError(
f"Invalid reasoning parser: {reasoning_parser}\n"
f"Supported parsers: {list(cls._parsers.keys())}") from e
return parser_cls(chat_template_kwargs=chat_template_kwargs,
**default_kwargs)
@classmethod
def keys(cls):
return cls._parsers.keys()
class BaseReasoningParser(ABC):
def __init__(self,
*,
chat_template_kwargs: Optional[dict[str, Any]] = None) -> None:
pass
@abstractmethod
def parse(self, text: str) -> ReasoningParserResult:
raise NotImplementedError
@abstractmethod
def parse_delta(self, delta_text: str) -> ReasoningParserResult:
raise NotImplementedError
@register_reasoning_parser("deepseek-r1", reasoning_at_start=True)
@register_reasoning_parser("qwen3")
class DeepSeekR1Parser(BaseReasoningParser):
"""
Reasoning parser for DeepSeek-R1. Reasoning format: <think>(.*)</think>.
Since the latest official tokenizer_config.json initially adds "<think>\\n" at the end of the prompt
(https://huggingface.co/deepseek-ai/DeepSeek-R1/blob/main/tokenizer_config.json),
treat all the text before the </think> tag as `reasoning_content` and the text after as `content`.
"""
def __init__(self,
*,
reasoning_at_start: bool = False,
chat_template_kwargs: Optional[dict[str, Any]] = None) -> None:
super().__init__(chat_template_kwargs=chat_template_kwargs)
self.reasoning_start = "<think>"
self.reasoning_end = "</think>"
self.reasoning_at_start = reasoning_at_start
self.in_reasoning = self.reasoning_at_start
self._buffer = ""
def _create_reasoning_end_result(self, content: str,
reasoning_content: str):
if len(content) == 0:
reasoning_parser_result = ReasoningParserResult(
reasoning_content=reasoning_content)
elif len(reasoning_content) == 0:
reasoning_parser_result = ReasoningParserResult(content=content)
else:
reasoning_parser_result = ReasoningParserResult(
content=content, reasoning_content=reasoning_content)
return reasoning_parser_result
def parse(self, text: str) -> ReasoningParserResult:
if not self.reasoning_at_start:
splits = text.partition(self.reasoning_start)
if splits[1] == "":
# no reasoning start tag found
return ReasoningParserResult(content=text)
# reasoning start tag found
# text before reasoning start tag is dropped
text = splits[2]
splits = text.partition(self.reasoning_end)
reasoning_content, content = splits[0], splits[2]
return ReasoningParserResult(content=content,
reasoning_content=reasoning_content)
def parse_delta(self, delta_text: str) -> ReasoningParserResult:
self._buffer += delta_text
delta_text = self._buffer
reasoning_content = None
content = None
if (self.reasoning_start.startswith(delta_text)
or self.reasoning_end.startswith(delta_text)):
# waiting for more text to determine if it's a reasoning start or end tag
return ReasoningParserResult()
if not self.in_reasoning:
begin_idx = delta_text.find(self.reasoning_start)
if begin_idx == -1:
self._buffer = ""
return ReasoningParserResult(content=delta_text)
self.in_reasoning = True
# set reasoning_content, will be processed by the next block
reasoning_content = delta_text[begin_idx +
len(self.reasoning_start):]
if self.in_reasoning:
delta_text = reasoning_content if reasoning_content is not None else delta_text
end_idx = delta_text.find(self.reasoning_end)
if end_idx == -1:
last_idx = delta_text.rfind(self.reasoning_end[0])
if last_idx != -1 and self.reasoning_end.startswith(
delta_text[last_idx:]):
self._buffer = delta_text[last_idx:]
reasoning_content = delta_text[:last_idx]
else:
self._buffer = ""
reasoning_content = delta_text
return ReasoningParserResult(
reasoning_content=reasoning_content)
reasoning_content = delta_text[:end_idx]
content = delta_text[end_idx + len(self.reasoning_end):]
self.in_reasoning = False
self._buffer = ""
return ReasoningParserResult(content=content,
reasoning_content=reasoning_content)
raise RuntimeError(
"Unreachable code reached in `DeepSeekR1Parser.parse_delta`")
MODEL_TYPE_TO_REASONING_PARSER: dict[str, str] = {
"qwen3": "qwen3",
"qwen3_moe": "qwen3",
"qwen3_5": "qwen3",
"qwen3_5_moe": "qwen3",
"qwen3_next": "qwen3",
"deepseek_v3": "deepseek-r1",
"deepseek_v32": "deepseek-r1",
"nemotron_h": "nano-v3",
}
def resolve_auto_reasoning_parser(model: str) -> Optional[str]:
"""Resolve 'auto' reasoning parser by reading the model's HF config.
For DeepSeek models, only maps to deepseek-r1 if the model path
suggests it is a reasoning model (contains 'R1' in the name).
"""
config_path = Path(model) / "config.json"
if not config_path.exists():
return None
with open(config_path) as f:
config = json.load(f)
model_type = config.get("model_type", "")
if model_type in ("deepseek_v3", "deepseek_v32"):
model_name = Path(model).name.lower()
if "r1" not in model_name:
return None
return MODEL_TYPE_TO_REASONING_PARSER.get(model_type)
@register_reasoning_parser("nano-v3")
class NemotronV3ReasoningParser(DeepSeekR1Parser):
"""Reasoning parser for Nemotron Nano v3.
If the model is with reasoning (default behavior), `reasoning_at_start` is `True` and the
starting response is parsed into `reasoning_content`.
When the model is without reasoning, `reasoning_at_start` is `False` so the response is parsed
into `content` fields.
The `enable_thinking` flag is read from `chat_template_kwargs`.
"""
def __init__(self,
*,
reasoning_at_start: bool = True,
chat_template_kwargs: Optional[dict[str, Any]] = None) -> None:
self._force_nonempty_content = False
if isinstance(chat_template_kwargs, dict):
reasoning_at_start = chat_template_kwargs.get(
"enable_thinking", reasoning_at_start)
self._force_nonempty_content = chat_template_kwargs.get(
"force_nonempty_content", False) is True
super().__init__(reasoning_at_start=reasoning_at_start,
chat_template_kwargs=chat_template_kwargs)
def _maybe_swap_content(
self, result: ReasoningParserResult) -> ReasoningParserResult:
"""When force_nonempty_content is set and content is empty, move
reasoning_content into content so the response always has content."""
if self._force_nonempty_content and not result.content and result.reasoning_content:
return ReasoningParserResult(content=result.reasoning_content,
reasoning_content="")
return result
def parse(self, text: str) -> ReasoningParserResult:
return self._maybe_swap_content(super().parse(text))