feat: 统一LLM错误处理并添加系统错误回复配置

在多个LLM提供者中移除try-catch块,将错误处理统一到connection.py的流处理层
添加system_error_response配置项,支持自定义系统错误时的回复内容
在意图识别和流处理中捕获异常时返回配置的错误回复,避免硬编码错误信息

Fixes #2075
This commit is contained in:
huozaimengli
2026-01-25 16:48:01 +08:00
parent 275102f5b7
commit 6ae0af278b
13 changed files with 542 additions and 551 deletions
@@ -2,11 +2,14 @@ from typing import List, Dict
from ..base import IntentProviderBase
from plugins_func.functions.play_music import initialize_music_handler
from config.logger import setup_logging
from core.utils.util import get_system_error_response
import re
import json
import hashlib
import time
TAG = __name__
logger = setup_logging()
@@ -115,12 +118,16 @@ class IntentProvider(IntentProviderBase):
return prompt
def replyResult(self, text: str, original_text: str):
llm_result = self.llm.response_no_stream(
system_prompt=text,
user_prompt="请根据以上内容,像人类一样说话的口吻回复用户,要求简洁,请直接返回结果。用户现在说:"
+ original_text,
)
return llm_result
try:
llm_result = self.llm.response_no_stream(
system_prompt=text,
user_prompt="请根据以上内容,像人类一样说话的口吻回复用户,要求简洁,请直接返回结果。用户现在说:"
+ original_text,
)
return llm_result
except Exception as e:
logger.bind(tag=TAG).error(f"Error in generating reply result: {e}")
return get_system_error_response(self.config)
async def detect_intent(self, conn, dialogue_history: List[Dict], text: str) -> str:
if not self.llm:
@@ -194,9 +201,13 @@ class IntentProvider(IntentProviderBase):
llm_start_time = time.time()
logger.bind(tag=TAG).debug(f"开始LLM意图识别调用, 模型: {model_info}")
intent = self.llm.response_no_stream(
system_prompt=prompt_music, user_prompt=user_prompt
)
try:
intent = self.llm.response_no_stream(
system_prompt=prompt_music, user_prompt=user_prompt
)
except Exception as e:
logger.bind(tag=TAG).error(f"Error in intent detection LLM call: {e}")
return '{"function_call": {"name": "continue_chat"}}'
# 记录LLM调用完成时间
llm_time = time.time() - llm_start_time