Skip to content

Commit 78a8c84

Browse files
deepseek-assistantPlanteAmigor
authored andcommitted
fix: --reasoning off 过滤思考前缀(Here/Alright 等)
- _make_streamer 初始 in_think=True,丢弃 </think> 前的思考内容 - 正确处理 <think></think> 完整块和孤立的 </think>
1 parent 6f0ecec commit 78a8c84

1 file changed

Lines changed: 44 additions & 7 deletions

File tree

ov_cli/chat.py

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,16 @@
5555
}
5656

5757

58-
def _make_streamer(reply_parts, stop_flag, on_first_token=None):
58+
def _make_streamer(reply_parts, stop_flag, on_first_token=None, thinking_filter=False):
5959
"""创建 streamer callback。
6060
6161
on_first_token: 首个 token 到达时的回调(用于停止进度指示器)。
62+
thinking_filter: 是否过滤 <think> 标签及思考内容。
6263
"""
6364
import select as _sel
6465
_first = [True]
66+
# 启用 filter 时,初始假设在 think 块内(模型可能先输出思考才输出 </think>)
67+
in_think = [thinking_filter]
6568

6669
def cb(t):
6770
if stop_flag[0]:
@@ -75,9 +78,40 @@ def cb(t):
7578
if c == '\x03':
7679
stop_flag[0] = True
7780
return True
78-
reply_parts.append(t)
79-
sys.stdout.write(t)
80-
sys.stdout.flush()
81+
82+
if thinking_filter:
83+
if in_think[0]:
84+
# 在 think 块内:只找 </think>
85+
if '</think>' in t:
86+
idx = t.index('</think>')
87+
after = t[idx + 8:]
88+
if after:
89+
reply_parts.append(after)
90+
sys.stdout.write(after)
91+
in_think[0] = False
92+
# 否则丢弃(思考内容)
93+
else:
94+
if '<think>' in t:
95+
# 进入 think 块,丢弃前面的内容
96+
idx = t.index('<think>')
97+
after = t[idx + 7:]
98+
if '</think>' in after:
99+
idx2 = after.index('</think>')
100+
rest = after[idx2 + 8:]
101+
if rest:
102+
reply_parts.append(rest)
103+
sys.stdout.write(rest)
104+
# 同一块内打开了又关闭,不改变状态
105+
else:
106+
in_think[0] = True
107+
else:
108+
reply_parts.append(t)
109+
sys.stdout.write(t)
110+
sys.stdout.flush()
111+
else:
112+
reply_parts.append(t)
113+
sys.stdout.write(t)
114+
sys.stdout.flush()
81115
return False
82116

83117
return cb
@@ -657,7 +691,7 @@ def _prog():
657691

658692
reply_parts = []
659693
stop_flag = [False]
660-
streamer_cb = _make_streamer(reply_parts, stop_flag, on_first)
694+
streamer_cb = _make_streamer(reply_parts, stop_flag, on_first, thinking_filter=not reasoning)
661695

662696
kwargs = {"generation_config": gen_cfg, "streamer": streamer_cb}
663697
if image_tensors is not None:
@@ -1448,7 +1482,7 @@ def _show_progress():
14481482
progress_stop.wait(1.0)
14491483
threading.Thread(target=_show_progress, daemon=True).start()
14501484

1451-
streamer_callback = _make_streamer(reply_parts, stop_flag, on_first_token)
1485+
streamer_callback = _make_streamer(reply_parts, stop_flag, on_first_token, thinking_filter=not reasoning)
14521486

14531487
t0 = time.time()
14541488
old_handler = signal.signal(signal.SIGINT, lambda s, f: stop_flag.__setitem__(0, True))
@@ -1468,6 +1502,9 @@ def _show_progress():
14681502
signal.signal(signal.SIGINT, old_handler)
14691503
reply_text = "".join(reply_parts)
14701504

1505+
if not reasoning:
1506+
reply_text = re.sub(r'</?think>', '', reply_text).strip()
1507+
14711508
elapsed = time.time() - t0
14721509
if stop_flag[0]:
14731510
print(f"\n{TR('已中断', 'Interrupted')}")
@@ -1588,7 +1625,7 @@ def _run_translate_genai(ctx, max_tokens):
15881625
sys.stdout.flush()
15891626
reply_parts = []
15901627
stop_flag = [False]
1591-
streamer_callback = _make_streamer(reply_parts, stop_flag)
1628+
streamer_callback = _make_streamer(reply_parts, stop_flag, thinking_filter=not reasoning)
15921629

15931630
old_handler = signal.signal(signal.SIGINT, lambda s, f: stop_flag.__setitem__(0, True))
15941631
try:

0 commit comments

Comments
 (0)