Selaa lähdekoodia

fix: support Alibaba DashScope rerank endpoint /v1/reranks

DashScope uses /v1/reranks (plural) instead of /v1/rerank (Jina/Cohere).
Detect dashscope in base_url to pick the correct endpoint path.
summer_tears 1 kuukausi sitten
vanhempi
sitoutus
4ee1552a64

+ 7 - 0
webnovel-writer/scripts/data_modules/api_client.py

@@ -274,6 +274,13 @@ class RerankAPIClient:
         """构建请求 URL"""
         base_url = self.config.rerank_base_url.rstrip("/")
         if self.config.rerank_api_type == "openai":
+            # 阿里云 DashScope: /v1/reranks (复数)
+            if "dashscope" in base_url:
+                if not base_url.endswith("/reranks"):
+                    if base_url.endswith("/v1"):
+                        return f"{base_url}/reranks"
+                    return f"{base_url}/v1/reranks"
+                return base_url
             # Jina/Cohere 兼容: /v1/rerank
             if not base_url.endswith("/rerank"):
                 if base_url.endswith("/v1"):

+ 35 - 0
webnovel-writer/scripts/data_modules/tests/test_api_client.py

@@ -292,6 +292,41 @@ async def test_embedding_exception_and_close(tmp_path, monkeypatch):
     assert session.closed is True
 
 
+def test_rerank_build_url(tmp_path):
+    config = DataModulesConfig.from_project_root(tmp_path)
+    config.rerank_api_type = "openai"
+
+    # Jina/Cohere: bare URL
+    config.rerank_base_url = "https://api.jina.ai"
+    client = RerankAPIClient(config)
+    assert client._build_url() == "https://api.jina.ai/v1/rerank"
+
+    # Jina/Cohere: URL ending with /v1
+    config.rerank_base_url = "https://api.jina.ai/v1"
+    assert client._build_url() == "https://api.jina.ai/v1/rerank"
+
+    # Jina/Cohere: URL already ending with /rerank
+    config.rerank_base_url = "https://api.jina.ai/v1/rerank"
+    assert client._build_url() == "https://api.jina.ai/v1/rerank"
+
+    # DashScope: bare URL
+    config.rerank_base_url = "https://dashscope.aliyuncs.com"
+    assert client._build_url() == "https://dashscope.aliyuncs.com/v1/reranks"
+
+    # DashScope: URL ending with /v1
+    config.rerank_base_url = "https://dashscope.aliyuncs.com/v1"
+    assert client._build_url() == "https://dashscope.aliyuncs.com/v1/reranks"
+
+    # DashScope: URL already ending with /reranks
+    config.rerank_base_url = "https://dashscope.aliyuncs.com/v1/reranks"
+    assert client._build_url() == "https://dashscope.aliyuncs.com/v1/reranks"
+
+    # Modal: passthrough
+    config.rerank_api_type = "modal"
+    config.rerank_base_url = "https://modal.example.com/rerank"
+    assert client._build_url() == "https://modal.example.com/rerank"
+
+
 def test_rerank_headers_payload_and_stats(tmp_path, capsys):
     config = DataModulesConfig.from_project_root(tmp_path)
     config.rerank_api_key = "rk-test"