mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
优化代码,遇到Bad Case安全处理
This commit is contained in:
parent
d8195394aa
commit
cdc49ffb12
1 changed files with 15 additions and 7 deletions
|
|
@ -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}")
|
||||
|
|
|
|||
Loading…
Reference in a new issue