mirror of
https://github.com/OpenBMB/ChatDev.git
synced 2026-07-31 10:26:07 +00:00
Merge pull request #617 from benjamin7007/fix/strip-thinking-tokens
fix: strip <think> tokens from reasoning model output
This commit is contained in:
commit
3615218e31
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import base64
|
import base64
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import re
|
||||||
|
|
||||||
import binascii
|
import binascii
|
||||||
import os
|
import os
|
||||||
@ -383,18 +384,31 @@ class OpenAIProvider(ModelProvider):
|
|||||||
type="function"
|
type="function"
|
||||||
))
|
))
|
||||||
|
|
||||||
|
content = self._get_attr(msg, "content") or ""
|
||||||
|
content = self._strip_thinking_tokens(content)
|
||||||
|
|
||||||
return Message(
|
return Message(
|
||||||
role=MessageRole.ASSISTANT,
|
role=MessageRole.ASSISTANT,
|
||||||
content=self._get_attr(msg, "content") or "",
|
content=content,
|
||||||
tool_calls=tool_calls
|
tool_calls=tool_calls
|
||||||
)
|
)
|
||||||
|
|
||||||
|
_THINK_PATTERN = re.compile(r"<think>.*?</think>\s*", re.DOTALL)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _strip_thinking_tokens(cls, text: str) -> str:
|
||||||
|
"""Strip <think>...</think> blocks from model output (e.g. DeepSeek-R1, MiniMax-M2.7)."""
|
||||||
|
if "<think>" not in text:
|
||||||
|
return text
|
||||||
|
return cls._THINK_PATTERN.sub("", text).strip()
|
||||||
|
|
||||||
def _append_chat_response_output(self, timeline: List[Any], response: Any) -> None:
|
def _append_chat_response_output(self, timeline: List[Any], response: Any) -> None:
|
||||||
"""Add chat response to timeline, preserving tool_calls (Chat API compatible)."""
|
"""Add chat response to timeline, preserving tool_calls (Chat API compatible)."""
|
||||||
msg = response.choices[0].message
|
msg = response.choices[0].message
|
||||||
|
content = self._strip_thinking_tokens(msg.content or "")
|
||||||
assistant_msg = {
|
assistant_msg = {
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
"content": msg.content or ""
|
"content": content
|
||||||
}
|
}
|
||||||
|
|
||||||
if getattr(msg, "tool_calls", None):
|
if getattr(msg, "tool_calls", None):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user