Files
xiaozhi-esp32-server/main/xiaozhi-server/core/providers/llm/base.py
T
huozaimengli 6ae0af278b feat: 统一LLM错误处理并添加系统错误回复配置
在多个LLM提供者中移除try-catch块,将错误处理统一到connection.py的流处理层
添加system_error_response配置项,支持自定义系统错误时的回复内容
在意图识别和流处理中捕获异常时返回配置的错误回复,避免硬编码错误信息

Fixes #2075
2026-01-25 16:48:01 +08:00

35 lines
1.1 KiB
Python

from abc import ABC, abstractmethod
from config.logger import setup_logging
TAG = __name__
logger = setup_logging()
class LLMProviderBase(ABC):
@abstractmethod
def response(self, session_id, dialogue):
"""LLM response generator"""
pass
def response_no_stream(self, system_prompt, user_prompt, **kwargs):
# 构造对话格式
dialogue = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
]
result = ""
for part in self.response("", dialogue, **kwargs):
result += part
return result
def response_with_functions(self, session_id, dialogue, functions=None):
"""
Default implementation for function calling (streaming)
This should be overridden by providers that support function calls
Returns: generator that yields either text tokens or a special function call token
"""
# For providers that don't support functions, just return regular response
for token in self.response(session_id, dialogue):
yield token, None