mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-18 10:00:40 +08:00
fix: support both Bailian Rerank API formats based on URL endpoint (#7250)
* fix: support both Bailian Rerank API formats based on URL endpoint
阿里云百炼有两个不同的 rerank API 端点:
- /compatible-api/v1/reranks: 使用扁平请求格式 {model, query, documents}
- /api/v1/services/rerank/...: 需要 input 包装 {model, input: {...}}
之前代码只根据模型名判断格式,导致 qwen3-rerank + compatible-api 组合失败。
修复内容:
- _build_payload(): 根据 URL 是否含 'compatible-api' 决定请求格式
- _parse_results(): 根据 URL 判断响应中 results 的位置
Fixes #7161
* refactor: reduce duplication in bailian rerank payload and results handling
- Extract params building outside the if-else branch
- Add back empty results warning log
- Simplify error handling variable assignment
* fix: simplify bailian rerank payload to use model-based logic only
qwen3-rerank always uses flat format regardless of API endpoint.
Other models (gte-rerank-v2, etc.) use input wrapper format.
This simplifies the logic and correctly handles all model/URL combinations.
Tested: qwen3-rerank accepts both formats, gte-rerank-v2 only supports input wrapper.
---------
Co-authored-by: root <root@localhost.localdomain>
Co-authored-by: Fix Bot <fix@example.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import os
|
||||
from typing import Any
|
||||
|
||||
import aiohttp
|
||||
|
||||
@@ -88,8 +89,6 @@ class BailianRerankProvider(RerankProvider):
|
||||
normalized_model = self.model.strip().lower()
|
||||
normalized_top_n = top_n if top_n is not None and top_n > 0 else None
|
||||
|
||||
# qwen3-rerank follows a model-specific payload:
|
||||
# query/documents/top_n/instruct should be at the top level.
|
||||
if normalized_model == self.QWEN3_RERANK_MODEL:
|
||||
payload = {
|
||||
"model": self.model,
|
||||
@@ -107,8 +106,7 @@ class BailianRerankProvider(RerankProvider):
|
||||
)
|
||||
return payload
|
||||
|
||||
base = {"model": self.model, "input": {"query": query, "documents": documents}}
|
||||
|
||||
payload_input = {"query": query, "documents": documents}
|
||||
params = {
|
||||
k: v
|
||||
for k, v in [
|
||||
@@ -118,6 +116,7 @@ class BailianRerankProvider(RerankProvider):
|
||||
if v is not None
|
||||
}
|
||||
|
||||
base: dict[str, Any] = {"model": self.model, "input": payload_input}
|
||||
if params:
|
||||
base["parameters"] = params
|
||||
|
||||
@@ -136,14 +135,23 @@ class BailianRerankProvider(RerankProvider):
|
||||
BailianAPIError: API返回错误
|
||||
KeyError: 结果缺少必要字段
|
||||
"""
|
||||
# 检查响应状态
|
||||
if data.get("code", "200") != "200":
|
||||
raise BailianAPIError(
|
||||
f"百炼 API 错误: {data.get('code')} – {data.get('message', '')}"
|
||||
)
|
||||
is_compatible_api = "compatible-api" in self.base_url
|
||||
|
||||
if is_compatible_api:
|
||||
code = data.get("code")
|
||||
if code:
|
||||
raise BailianAPIError(
|
||||
f"百炼 API 错误: {code} – {data.get('message', '')}"
|
||||
)
|
||||
results = data.get("results", [])
|
||||
else:
|
||||
code = data.get("code", "200")
|
||||
if code != "200":
|
||||
raise BailianAPIError(
|
||||
f"百炼 API 错误: {code} – {data.get('message', '')}"
|
||||
)
|
||||
results = data.get("output", {}).get("results", [])
|
||||
|
||||
# 兼容旧版 API (output.results) 和新版 compatible API (results)
|
||||
results = (data.get("output") or {}).get("results") or data.get("results") or []
|
||||
if not results:
|
||||
logger.warning(f"百炼 Rerank 返回空结果: {data}")
|
||||
return []
|
||||
|
||||
Reference in New Issue
Block a user