mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
dify / fastgpt / homeassistant 三个 provider 用 requests 发请求时都没传
timeout。requests 不传 timeout 就是无限等待,上游不响应时请求会一直挂着,
设备侧表现为「服务无响应」,服务端对应的线程也一直被占着不放:
#1229 与 dify 智能体对话时始终在说话中但是一直无响应
gemini 的代理探测 requests.get 同样没有 timeout,它在 provider 构造阶段
被调用,代理不可达时会直接卡住连接建立。
改动:
- core/providers/llm/base:新增 LLMProviderBase.resolve_timeout,
统一解析模型配置里的 timeout / connect_timeout,默认 60s / 10s
- dify / fastgpt / homeassistant:请求带上超时
- gemini:代理探测加 10s 超时
顺带修掉 dify 流式解析的两个问题:
- mode 配成不支持的值时 request_json 根本没赋值,直接抛 UnboundLocalError,
现在改成明确报出「不支持的 dify 模式」并列出可用值
- SSE 里出现一条解析不了的数据时 json.loads 抛异常,整段回复直接中断,
现在跳过这一条继续往下读
69 lines
2.5 KiB
Python
69 lines
2.5 KiB
Python
from abc import ABC, abstractmethod
|
||
from config.logger import setup_logging
|
||
|
||
TAG = __name__
|
||
logger = setup_logging()
|
||
|
||
class LLMProviderBase(ABC):
|
||
# provider 发起 HTTP 请求时的默认超时(秒)
|
||
DEFAULT_HTTP_TIMEOUT = 60
|
||
DEFAULT_CONNECT_TIMEOUT = 10
|
||
|
||
@classmethod
|
||
def resolve_timeout(cls, config, default=None):
|
||
"""解析 provider 配置里的 HTTP 超时,返回 (连接超时, 读取超时)
|
||
|
||
配置写在模型配置的 JSON 里:
|
||
timeout: 60 连接和读取都用这个值
|
||
connect_timeout: 10 连接超时单独设置
|
||
|
||
之前不少 provider 用 requests 发请求时都没传 timeout,上游不响应时
|
||
请求会一直挂着,设备侧表现为「服务无响应」,服务端对应的线程也一直被
|
||
占着不放。这里统一给一个上限,取值非法时回退到默认值。
|
||
"""
|
||
if not isinstance(config, dict):
|
||
config = {}
|
||
|
||
def _positive(value, fallback):
|
||
try:
|
||
value = float(value)
|
||
except (TypeError, ValueError):
|
||
return fallback
|
||
if value <= 0:
|
||
return fallback
|
||
return value
|
||
|
||
fallback = float(default if default is not None else cls.DEFAULT_HTTP_TIMEOUT)
|
||
read_timeout = _positive(config.get("timeout"), fallback)
|
||
connect_timeout = _positive(
|
||
config.get("connect_timeout"), float(cls.DEFAULT_CONNECT_TIMEOUT)
|
||
)
|
||
return (connect_timeout, read_timeout)
|
||
|
||
@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
|
||
|