From cdc49ffb127f6646a4128a6bd34bbd340694a870 Mon Sep 17 00:00:00 2001 From: Sinyo <38577585+SinyoWong@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:39:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=EF=BC=8C?= =?UTF-8?q?=E9=81=87=E5=88=B0Bad=20Case=E5=AE=89=E5=85=A8=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/providers/llm/openai/openai.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/core/providers/llm/openai/openai.py b/core/providers/llm/openai/openai.py index ceeb7e808..090b15e3e 100644 --- a/core/providers/llm/openai/openai.py +++ b/core/providers/llm/openai/openai.py @@ -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 '' in content: # 跳过深度思考内容 + 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 '' in content: is_active = False - elif '' in content: + content = content.split('')[0] # 保留标签前的有效内容 + if '' in content: is_active = True - elif is_active: + content = content.split('')[-1] # 保留标签后的有效内容 + if is_active: yield content + except Exception as e: logger.bind(tag=TAG).error(f"Error in response generation: {e}")