mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
fix: 修复频繁打断下生成器不会清理
This commit is contained in:
parent
8a156e99f4
commit
2a4dbdb69b
4 changed files with 131 additions and 117 deletions
|
|
@ -39,8 +39,7 @@ class LLMProvider(LLMProviderBase):
|
|||
}
|
||||
|
||||
# 发起 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()
|
||||
|
||||
|
|
|
|||
|
|
@ -50,6 +50,7 @@ class LLMProvider(LLMProviderBase):
|
|||
# 用于处理跨chunk的标签
|
||||
buffer = ""
|
||||
|
||||
try:
|
||||
for chunk in responses:
|
||||
try:
|
||||
delta = (
|
||||
|
|
@ -87,6 +88,8 @@ class LLMProvider(LLMProviderBase):
|
|||
|
||||
except Exception as e:
|
||||
logger.bind(tag=TAG).error(f"Error processing chunk: {e}")
|
||||
finally:
|
||||
responses.close()
|
||||
|
||||
def response_with_functions(self, session_id, dialogue, functions=None):
|
||||
# 如果是qwen3模型,在用户最后一条消息中添加/no_think指令
|
||||
|
|
@ -117,6 +120,7 @@ class LLMProvider(LLMProviderBase):
|
|||
is_active = True
|
||||
buffer = ""
|
||||
|
||||
try:
|
||||
for chunk in stream:
|
||||
try:
|
||||
delta = (
|
||||
|
|
@ -163,3 +167,5 @@ class LLMProvider(LLMProviderBase):
|
|||
except Exception as e:
|
||||
logger.bind(tag=TAG).error(f"Error processing function chunk: {e}")
|
||||
continue
|
||||
finally:
|
||||
stream.close()
|
||||
|
|
|
|||
|
|
@ -115,6 +115,7 @@ class LLMProvider(LLMProviderBase):
|
|||
responses = self.client.chat.completions.create(**request_params)
|
||||
|
||||
is_active = True
|
||||
try:
|
||||
for chunk in responses:
|
||||
try:
|
||||
delta = chunk.choices[0].delta if getattr(chunk, "choices", None) else None
|
||||
|
|
@ -130,6 +131,8 @@ class LLMProvider(LLMProviderBase):
|
|||
content = content.split("</think>")[-1]
|
||||
if is_active:
|
||||
yield content
|
||||
finally:
|
||||
responses.close()
|
||||
|
||||
def response_with_functions(self, session_id, dialogue, functions=None, **kwargs):
|
||||
dialogue = self.normalize_dialogue(dialogue)
|
||||
|
|
@ -157,6 +160,7 @@ class LLMProvider(LLMProviderBase):
|
|||
|
||||
stream = self.client.chat.completions.create(**request_params)
|
||||
|
||||
try:
|
||||
for chunk in stream:
|
||||
if getattr(chunk, "choices", None):
|
||||
delta = chunk.choices[0].delta
|
||||
|
|
@ -170,3 +174,5 @@ class LLMProvider(LLMProviderBase):
|
|||
f"输出 {getattr(usage_info, 'completion_tokens', '未知')},"
|
||||
f"共计 {getattr(usage_info, 'total_tokens', '未知')}"
|
||||
)
|
||||
finally:
|
||||
stream.close()
|
||||
|
|
|
|||
|
|
@ -74,6 +74,7 @@ class LLMProvider(LLMProviderBase):
|
|||
tools=functions,
|
||||
)
|
||||
|
||||
try:
|
||||
for chunk in stream:
|
||||
delta = chunk.choices[0].delta
|
||||
content = delta.content
|
||||
|
|
@ -83,3 +84,5 @@ class LLMProvider(LLMProviderBase):
|
|||
yield content, tool_calls
|
||||
elif tool_calls:
|
||||
yield None, tool_calls
|
||||
finally:
|
||||
stream.close()
|
||||
|
|
|
|||
Loading…
Reference in a new issue