优化代码,遇到Bad Case安全处理

This commit is contained in:
Sinyo 2025-02-18 10:39:11 +08:00 committed by GitHub
parent d8195394aa
commit cdc49ffb12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -25,17 +25,25 @@ class LLMProvider(LLMProviderBase):
messages=dialogue,
stream=True
)
is_active = True
for chunk in responses:
# 检查是否存在有效的choice且content不为空
delta = chunk.choices[0].delta if chunk.choices else None
content = getattr(delta, 'content', '') if delta else ''
if content: # 仅在content非空时生成
if '<think>' in content: # 跳过<think>深度思考内容
try:
# 检查是否存在有效的choice且content不为空
delta = chunk.choices[0].delta if getattr(chunk, 'choices', None) else None
content = delta.content if hasattr(delta, 'content') else ''
except IndexError:
content = ''
if content:
# 处理标签跨多个chunk的情况
if '<think>' in content:
is_active = False
elif '</think>' in content:
content = content.split('<think>')[0] # 保留标签前的有效内容
if '</think>' in content:
is_active = True
elif is_active:
content = content.split('</think>')[-1] # 保留标签后的有效内容
if is_active:
yield content
except Exception as e:
logger.bind(tag=TAG).error(f"Error in response generation: {e}")