This commit is contained in:
limdeng 2026-10-27 10:07:43 +08:00 • committed by GitHub
commit f3a4e965bc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 48 additions and 7 deletions

View file

@ -61,7 +61,7 @@ class DashScopeRerankConfig(BaseRerankConfig):
Reference: https://help.aliyun.com/zh/model-studio/text-rerank-api
Targets DashScope's qwen3-rerank model. Request fields: model, query,
documents, top_n, return_documents. Response: results[].index,
documents, top_n, return_documents, instruct. Response: results[].index,
results[].relevance_score, optionally results[].document.text (when
return_documents=true), plus a top-level usage.total_tokens counter.
"""
@ -116,7 +116,7 @@ class DashScopeRerankConfig(BaseRerankConfig):
}
def get_supported_cohere_rerank_params(self, model: str) -> list:
return ["query", "documents", "top_n", "return_documents"]
return ["query", "documents", "top_n", "return_documents", "instruction"]
def map_cohere_rerank_params(
self,
@ -133,8 +133,6 @@ class DashScopeRerankConfig(BaseRerankConfig):
max_tokens_per_doc: int | None = None,
instruction: str | None = None,
) -> dict:
# qwen3-rerank accepts query/documents/top_n/return_documents. The
# rest (rank_fields, max_*_per_doc) are silently dropped.
params: Final[OptionalRerankParams] = OptionalRerankParams(
query=query,
documents=documents,
@ -143,12 +141,14 @@ class DashScopeRerankConfig(BaseRerankConfig):
params["top_n"] = top_n
if return_documents is not None:
params["return_documents"] = return_documents
if instruction is not None:
params["instruction"] = instruction
return dict(params)
def transform_rerank_request(
self,
model: str,
optional_rerank_params: dict,
optional_rerank_params: dict[str, object],
headers: dict,
litellm_params: dict | None = None,
) -> dict:
@ -166,7 +166,10 @@ class DashScopeRerankConfig(BaseRerankConfig):
request["top_n"] = optional_rerank_params["top_n"]
if optional_rerank_params.get("return_documents") is not None:
request["return_documents"] = optional_rerank_params["return_documents"]
return request
instruction: Final = optional_rerank_params.get("instruction")
if instruction is None:
return request
return dict(request, instruct=_STR.validate_python(instruction))
def transform_rerank_response(
self,

View file

@ -3,19 +3,22 @@ Unit tests for DashScope rerank transformation.
"""
import json
from typing import Final
from unittest.mock import MagicMock
import httpx
import pytest
from pydantic import ValidationError
from litellm import LlmProviders
from litellm.llms.dashscope.common_utils import DashScopeError
from litellm.llms.dashscope.rerank.transformation import (
DEFAULT_RERANK_URL,
DashScopeRerankConfig,
)
from litellm.rerank_api.rerank_utils import get_optional_rerank_params
from litellm.types.rerank import RerankResponse
from litellm.utils import ProviderConfigManager
class TestDashScopeRerankURL:
@ -112,8 +115,43 @@ class TestDashScopeRerankRequest:
"documents",
"top_n",
"return_documents",
"instruction",
]
@pytest.mark.parametrize(
"provider",
[LlmProviders.DASHSCOPE, LlmProviders.QWENCLOUD, LlmProviders.QWEN_AI_PLATFORM],
)
@pytest.mark.parametrize("instruction", [None, "", "Retrieve semantically similar text."])
def test_instruction_reaches_provider_request(
self, provider: LlmProviders, instruction: str | None
) -> None:
config: Final = ProviderConfigManager.get_provider_rerank_config(
model="qwen3-rerank", provider=provider, api_base=None, present_version_params=[]
)
assert config is not None
params: Final = get_optional_rerank_params(
rerank_provider_config=config,
model="qwen3-rerank",
drop_params=False,
query="How do I reset my password?",
documents=["How can I change my password?", "Open settings and choose Reset password."],
top_n=1,
return_documents=False,
instruction=instruction,
)
body: Final = config.transform_rerank_request(
model="qwen3-rerank", optional_rerank_params=params, headers={}
)
assert body == {
"model": "qwen3-rerank",
"query": "How do I reset my password?",
"documents": ["How can I change my password?", "Open settings and choose Reset password."],
"top_n": 1,
"return_documents": False,
**({"instruct": instruction} if instruction is not None else {}),
}
def test_map_params_drops_unsupported(self):
# qwen3-rerank accepts query/documents/top_n/return_documents.
# rank_fields and max_*_per_doc are silently dropped.