fix: 修复DEFAULT_phrases拼写错误导致JS不执行
This commit is contained in:
@@ -268,8 +268,7 @@ class LLMService:
|
||||
|
||||
thinking_prefix = agent_config.get('thinking_prefix', '')
|
||||
thinking_suffix = agent_config.get('thinking_suffix', '')
|
||||
in_thinking = False
|
||||
thinking_buffer = ""
|
||||
buffer = "" # 用于累积和检测思考部分
|
||||
|
||||
async with httpx.AsyncClient(timeout=60.0) as client:
|
||||
async with client.stream("POST", url, headers=headers, json=payload) as response:
|
||||
@@ -284,35 +283,46 @@ class LLMService:
|
||||
delta = data['choices'][0].get('delta', {})
|
||||
if 'content' in delta:
|
||||
text = delta['content']
|
||||
buffer += text
|
||||
|
||||
# 检测思考部分
|
||||
if thinking_prefix and thinking_suffix:
|
||||
for char in text:
|
||||
if in_thinking:
|
||||
thinking_buffer += char
|
||||
# 检查是否结束思考
|
||||
if thinking_buffer.endswith(thinking_suffix):
|
||||
thinking_content = thinking_buffer[:-len(thinking_suffix)]
|
||||
yield {"type": "thinking", "text": thinking_content}
|
||||
in_thinking = False
|
||||
thinking_buffer = ""
|
||||
# 检测思考部分(简化逻辑)
|
||||
if thinking_prefix and thinking_suffix and thinking_prefix in buffer:
|
||||
# 尝试解析思考部分
|
||||
try:
|
||||
start_idx = buffer.find(thinking_prefix)
|
||||
if start_idx >= 0:
|
||||
# 找到思考开始,继续找结束
|
||||
end_idx = buffer.find(thinking_suffix, start_idx)
|
||||
if end_idx > start_idx:
|
||||
# 思考部分完整,发送思考然后发送内容
|
||||
thinking = buffer[start_idx + len(thinking_prefix):end_idx]
|
||||
yield {"type": "thinking", "text": thinking}
|
||||
# 发送思考后的内容
|
||||
remaining = buffer[end_idx + len(thinking_suffix):]
|
||||
if remaining:
|
||||
yield {"type": "content", "text": remaining}
|
||||
buffer = ""
|
||||
else:
|
||||
# 检查是否接近结束
|
||||
suffix_len = len(thinking_suffix)
|
||||
if len(thinking_buffer) >= suffix_len:
|
||||
yield {"type": "thinking", "text": thinking_buffer[-suffix_len:]}
|
||||
# 思考部分还没结束,先发送之前的内容
|
||||
if start_idx > 0:
|
||||
yield {"type": "content", "text": buffer[:start_idx]}
|
||||
# 等待更多数据来完成思考部分
|
||||
buffer = buffer[start_idx:]
|
||||
else:
|
||||
if char == thinking_prefix[0]:
|
||||
# 可能开始思考
|
||||
thinking_buffer = char
|
||||
if len(thinking_prefix) == 1:
|
||||
in_thinking = True
|
||||
else:
|
||||
yield {"type": "content", "text": char}
|
||||
# 没有思考标记,直接发送内容
|
||||
yield {"type": "content", "text": text}
|
||||
buffer = ""
|
||||
except:
|
||||
yield {"type": "content", "text": text}
|
||||
else:
|
||||
# 没有思考标记配置,直接发送内容
|
||||
yield {"type": "content", "text": text}
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
|
||||
# 处理剩余buffer
|
||||
if buffer:
|
||||
yield {"type": "content", "text": buffer}
|
||||
|
||||
|
||||
# 全局实例
|
||||
|
||||
Reference in New Issue
Block a user