调整读取逻辑和相关测试

This commit is contained in:
Sakura-RanChen 2026-11-19 10:32:21 +08:00
parent e09df59c13
commit 7c5fe74410
2 changed files with 44 additions and 10 deletions

View file

@ -66,19 +66,29 @@ public class OpenAIStyleLLMServiceImpl implements LLMService {
/**
* Ollama的OpenAI兼容端点位于{base_url}/v1下,与xiaozhi-server侧ollama provider逻辑保持一致:
* 剥离用户误填的完整端点后缀后,仅对不含路径的纯主机地址补/v1
* 先去尾斜杠、再剥离用户误填的完整端点后缀,仅对不含路径的纯主机地址补/v1。
* 空串返回null,交由上层"配置不完整"必填校验拦截
*/
private static String normalizeOllamaBaseUrl(String baseUrl) {
if (baseUrl == null) {
return null;
}
String url = baseUrl.trim();
if (url.endsWith("/chat/completions")) {
url = url.substring(0, url.length() - "/chat/completions".length());
if (url.isEmpty()) {
return null;
}
while (url.endsWith("/")) {
url = url.substring(0, url.length() - 1);
}
if (url.endsWith("/chat/completions")) {
url = url.substring(0, url.length() - "/chat/completions".length());
while (url.endsWith("/")) {
url = url.substring(0, url.length() - 1);
}
}
if (url.isEmpty()) {
return null;
}
if (url.replaceFirst("^https?://[^/]+", "").isEmpty()) {
url = url + "/v1";
}
@ -124,6 +134,10 @@ public class OpenAIStyleLLMServiceImpl implements LLMService {
String apiKey = configJson.getStr("api_key");
if ("ollama".equalsIgnoreCase(configJson.getStr("type"))) {
baseUrl = normalizeOllamaBaseUrl(baseUrl);
// Ollama免鉴权:未填api_key时兜底为"ollama",与xiaozhi-server侧ollama.py一致
if (StringUtils.isBlank(apiKey)) {
apiKey = "ollama";
}
}
Double temperature = configJson.getDouble("temperature");
Integer maxTokens = configJson.getInt("max_tokens");
@ -222,6 +236,10 @@ public class OpenAIStyleLLMServiceImpl implements LLMService {
String apiKey = configJson.getStr("api_key");
if ("ollama".equalsIgnoreCase(configJson.getStr("type"))) {
baseUrl = normalizeOllamaBaseUrl(baseUrl);
// Ollama免鉴权:未填api_key时兜底为"ollama",与xiaozhi-server侧ollama.py一致
if (StringUtils.isBlank(apiKey)) {
apiKey = "ollama";
}
}
if (StringUtils.isBlank(baseUrl) || StringUtils.isBlank(apiKey)) {
@ -298,8 +316,10 @@ public class OpenAIStyleLLMServiceImpl implements LLMService {
String baseUrl = configJson.getStr("base_url");
String apiKey = configJson.getStr("api_key");
// Ollama免鉴权:不要求api_key
boolean ollama = "ollama".equalsIgnoreCase(configJson.getStr("type"));
return baseUrl != null && !baseUrl.trim().isEmpty() &&
apiKey != null && !apiKey.trim().isEmpty();
(ollama || (apiKey != null && !apiKey.trim().isEmpty()));
} catch (Exception e) {
log.error("检查LLM服务可用性时发生异常:", e);
return false;
@ -324,8 +344,10 @@ public class OpenAIStyleLLMServiceImpl implements LLMService {
String baseUrl = configJson.getStr("base_url");
String apiKey = configJson.getStr("api_key");
// Ollama免鉴权:不要求api_key
boolean ollama = "ollama".equalsIgnoreCase(configJson.getStr("type"));
return baseUrl != null && !baseUrl.trim().isEmpty() &&
apiKey != null && !apiKey.trim().isEmpty();
(ollama || (apiKey != null && !apiKey.trim().isEmpty()));
} catch (Exception e) {
log.error("检查LLM服务可用性时发生异常,modelId: {}", modelId, e);
return false;
@ -383,6 +405,10 @@ public class OpenAIStyleLLMServiceImpl implements LLMService {
String apiKey = configJson.getStr("api_key");
if ("ollama".equalsIgnoreCase(configJson.getStr("type"))) {
baseUrl = normalizeOllamaBaseUrl(baseUrl);
// Ollama免鉴权:未填api_key时兜底为"ollama",与xiaozhi-server侧ollama.py一致
if (StringUtils.isBlank(apiKey)) {
apiKey = "ollama";
}
}
if (StringUtils.isBlank(baseUrl) || StringUtils.isBlank(apiKey)) {

View file

@ -11,7 +11,8 @@ Live 模式(``RUN_LIVE_API_TESTS=1``):调真实 LLM provider,参考
特殊处理:
- CozeLLM 看 bot_id / user_id 而非 api_key
- Ollama 要 model_name + 本地服务可用
- Ollama(type=ollama)免鉴权:api_key 占位值 "ollama" 不算缺失,
只要求 model_name 已配置;live 模式下直连本地 Ollama 服务
- placeholder 检测("你的" / "placeholder" / "xxx" / "sk-xxx")
- 因为 LLM 是同步 generator,不能直接 asyncio.wait_for → 线程池
"""
@ -43,9 +44,19 @@ def _build_provider():
if not cfg:
pytest.skip(f"LLM provider '{name}' 配置缺失")
ptype = get_provider_type("LLM", name)
if not ptype:
pytest.skip(f"LLM '{name}' 的 type 字段缺失")
# 任何模式下:key 是占位符 → skip。真请求需要真 key;mock 模式下
# 没有真 key 跑下去也会撞真 SDK 的网络栈(openai/edge_tts)报错。
if name == "CozeLLM":
# Ollama 免鉴权供应器(比照 conftest._check_modules_ready 按 type 判断,
# provider 名是 OllamaLLM 而非 Ollama):api_key 占位 "ollama" 不算缺失,
# 只校验 model_name 后继续构造 provider。
if str(cfg.get("type", "")).lower() == "ollama":
if not cfg.get("model_name"):
pytest.skip(f"LLM '{name}' 的 model_name 未配置")
elif name == "CozeLLM":
if any(x in cfg.get("bot_id", "") for x in ["你的"]) or any(
x in cfg.get("user_id", "") for x in ["你的"]
):
@ -59,9 +70,6 @@ def _build_provider():
"真请求模式:export RUN_LIVE_API_TESTS=1 并填真 key 后再跑。"
)
ptype = get_provider_type("LLM", name)
if not ptype:
pytest.skip(f"LLM '{name}' 的 type 字段缺失")
from core.utils.llm import create_instance
return create_instance(ptype, cfg)