mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-26 22:30:11 +00:00
* test(e2e): add enum values, auto-discovering label gates and secret hiding for e2e metadata * test(e2e): tag llm_translation tests with Subject metadata and record harness steps * docs(e2e): name every markerless harness test file that carries no Subject * test(e2e): keep the step discovery comprehensions to one for clause * test(e2e): declare the realtime param tuples Final
97 lines
3.6 KiB
Python
97 lines
3.6 KiB
Python
"""Live e2e: POST /v1/rerank ranks documents by relevance.
|
|
|
|
Registers Cohere and Bedrock rerank deployments at runtime and asserts the
|
|
endpoint returns scored results within the requested top_n. No official
|
|
OpenAI/Anthropic SDK covers /v1/rerank, so the call rides the shared typed
|
|
transport via ProxyClient.rerank. Migrated from
|
|
litellm-regression-tests/tests/test_inference_endpoints.py.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Final
|
|
|
|
import pytest
|
|
from e2e_config import unique_marker
|
|
from e2e_http import unwrap
|
|
from e2e_metadata import Domain, Mode, Provider, Route, Subject, meta
|
|
from lifecycle import ResourceManager
|
|
from models import LiteLLMParamsBody, RerankBody, RerankResponse
|
|
from proxy_client import ProxyClient
|
|
|
|
pytestmark = pytest.mark.e2e
|
|
|
|
DOCUMENTS = [
|
|
"Carson City is the capital city of the American state of Nevada.",
|
|
"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean.",
|
|
"Washington, D.C. is the capital of the United States.",
|
|
"Capital punishment has existed in the United States since before it was a country.",
|
|
]
|
|
COHERE_RERANK_BACKEND: Final = "cohere/rerank-v3.5"
|
|
BEDROCK_RERANK_BACKEND: Final = "bedrock/arn:aws:bedrock:us-east-1::foundation-model/cohere.rerank-v3-5:0"
|
|
QUERY = "What is the capital of the United States?"
|
|
|
|
|
|
def _assert_top_n_scored(response: RerankResponse) -> None:
|
|
assert response.results, f"/rerank returned no results: {response!r}"
|
|
assert len(response.results) <= 3, f"top_n=3 not honored: {response!r}"
|
|
assert response.results[0].relevance_score is not None, (
|
|
f"top rerank result has no relevance_score: {response!r}"
|
|
)
|
|
|
|
|
|
def _rerank_top_3(proxy: ProxyClient, key: str, model: str) -> RerankResponse:
|
|
return unwrap(
|
|
proxy.rerank(key, RerankBody(model=model, query=QUERY, documents=DOCUMENTS, top_n=3))
|
|
)
|
|
|
|
|
|
class TestRerank:
|
|
@pytest.mark.covers("llm.rerank.cohere.basic.nonstream.works")
|
|
@meta(
|
|
Subject(
|
|
domain=Domain.LLM_TRANSLATION,
|
|
route=Route.RERANK,
|
|
providers=(Provider.COHERE,),
|
|
models=(COHERE_RERANK_BACKEND,),
|
|
mode=Mode.NONSTREAM,
|
|
)
|
|
)
|
|
def test_rerank_scores_top_n(self, proxy: ProxyClient, resources: ResourceManager) -> None:
|
|
model = f"e2e-rerank-{unique_marker()}"
|
|
model_id = proxy.create_model(
|
|
model,
|
|
LiteLLMParamsBody(model=COHERE_RERANK_BACKEND, api_key="os.environ/COHERE_API_KEY"),
|
|
)
|
|
resources.defer(lambda: proxy.delete_model(model_id))
|
|
key = resources.key()
|
|
|
|
_assert_top_n_scored(_rerank_top_3(proxy, key, model))
|
|
|
|
@pytest.mark.covers("llm.rerank.bedrock.basic.nonstream.works", exercised_on=["rerank"])
|
|
@meta(
|
|
Subject(
|
|
domain=Domain.LLM_TRANSLATION,
|
|
route=Route.RERANK,
|
|
providers=(Provider.BEDROCK,),
|
|
models=(BEDROCK_RERANK_BACKEND,),
|
|
mode=Mode.NONSTREAM,
|
|
)
|
|
)
|
|
def test_bedrock_rerank_scores_top_n(
|
|
self, proxy: ProxyClient, resources: ResourceManager
|
|
) -> None:
|
|
model = f"e2e-bedrock-rerank-{unique_marker()}"
|
|
model_id = proxy.create_model(
|
|
model,
|
|
LiteLLMParamsBody(
|
|
model=BEDROCK_RERANK_BACKEND,
|
|
aws_access_key_id="os.environ/AWS_ACCESS_KEY_ID",
|
|
aws_secret_access_key="os.environ/AWS_SECRET_ACCESS_KEY",
|
|
aws_region_name="os.environ/AWS_REGION",
|
|
),
|
|
)
|
|
resources.defer(lambda: proxy.delete_model(model_id))
|
|
key = resources.key()
|
|
|
|
_assert_top_n_scored(_rerank_top_3(proxy, key, model))
|