fix: 修复频繁打断下生成器不会清理

This commit is contained in:
Sakura-RanChen
2026-04-22 11:24:43 +08:00
parent 8a156e99f4
commit 2a4dbdb69b
4 changed files with 131 additions and 117 deletions
@@ -39,8 +39,7 @@ class LLMProvider(LLMProviderBase):
} }
# 发起 POST 请求 # 发起 POST 请求
response = requests.post(self.api_url, json=payload, headers=headers) with requests.post(self.api_url, json=payload, headers=headers) as response:
# 检查请求是否成功 # 检查请求是否成功
response.raise_for_status() response.raise_for_status()
@@ -50,6 +50,7 @@ class LLMProvider(LLMProviderBase):
# 用于处理跨chunk的标签 # 用于处理跨chunk的标签
buffer = "" buffer = ""
try:
for chunk in responses: for chunk in responses:
try: try:
delta = ( delta = (
@@ -87,6 +88,8 @@ class LLMProvider(LLMProviderBase):
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"Error processing chunk: {e}") logger.bind(tag=TAG).error(f"Error processing chunk: {e}")
finally:
responses.close()
def response_with_functions(self, session_id, dialogue, functions=None): def response_with_functions(self, session_id, dialogue, functions=None):
# 如果是qwen3模型,在用户最后一条消息中添加/no_think指令 # 如果是qwen3模型,在用户最后一条消息中添加/no_think指令
@@ -117,6 +120,7 @@ class LLMProvider(LLMProviderBase):
is_active = True is_active = True
buffer = "" buffer = ""
try:
for chunk in stream: for chunk in stream:
try: try:
delta = ( delta = (
@@ -163,3 +167,5 @@ class LLMProvider(LLMProviderBase):
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"Error processing function chunk: {e}") logger.bind(tag=TAG).error(f"Error processing function chunk: {e}")
continue continue
finally:
stream.close()
@@ -115,6 +115,7 @@ class LLMProvider(LLMProviderBase):
responses = self.client.chat.completions.create(**request_params) responses = self.client.chat.completions.create(**request_params)
is_active = True is_active = True
try:
for chunk in responses: for chunk in responses:
try: try:
delta = chunk.choices[0].delta if getattr(chunk, "choices", None) else None delta = chunk.choices[0].delta if getattr(chunk, "choices", None) else None
@@ -130,6 +131,8 @@ class LLMProvider(LLMProviderBase):
content = content.split("</think>")[-1] content = content.split("</think>")[-1]
if is_active: if is_active:
yield content yield content
finally:
responses.close()
def response_with_functions(self, session_id, dialogue, functions=None, **kwargs): def response_with_functions(self, session_id, dialogue, functions=None, **kwargs):
dialogue = self.normalize_dialogue(dialogue) dialogue = self.normalize_dialogue(dialogue)
@@ -157,6 +160,7 @@ class LLMProvider(LLMProviderBase):
stream = self.client.chat.completions.create(**request_params) stream = self.client.chat.completions.create(**request_params)
try:
for chunk in stream: for chunk in stream:
if getattr(chunk, "choices", None): if getattr(chunk, "choices", None):
delta = chunk.choices[0].delta delta = chunk.choices[0].delta
@@ -170,3 +174,5 @@ class LLMProvider(LLMProviderBase):
f"输出 {getattr(usage_info, 'completion_tokens', '未知')}" f"输出 {getattr(usage_info, 'completion_tokens', '未知')}"
f"共计 {getattr(usage_info, 'total_tokens', '未知')}" f"共计 {getattr(usage_info, 'total_tokens', '未知')}"
) )
finally:
stream.close()
@@ -74,6 +74,7 @@ class LLMProvider(LLMProviderBase):
tools=functions, tools=functions,
) )
try:
for chunk in stream: for chunk in stream:
delta = chunk.choices[0].delta delta = chunk.choices[0].delta
content = delta.content content = delta.content
@@ -83,3 +84,5 @@ class LLMProvider(LLMProviderBase):
yield content, tool_calls yield content, tool_calls
elif tool_calls: elif tool_calls:
yield None, tool_calls yield None, tool_calls
finally:
stream.close()