mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-01 01:18:34 +00:00
* test: migrate selected llm translation contracts * test: migrate anthropic completion translation tests * test: migrate bedrock and cohere translation tests * test: tighten bedrock translation migration * test: migrate azure and openai model translation tests * test: migrate openai and small-provider translation tests * test: migrate gemini and vertex translation tests * test: migrate optional params and rerank translation tests * test: migrate huggingface, json and openrouter translation tests * test: fix openrouter integration expectations * test: migrate databricks, xai, cometapi and fireworks translation tests * test: migrate ovhcloud, prompt and small provider translation tests * test: split reasoning effort grid into unit and e2e * test: migrate router and realtime guardrail translation tests * test: migrate google interactions translation tests * test: migrate a2a, evals, skills and hosted vllm translation tests * test: remove remaining llm translation suite * test: tighten migrated llm translation unit tests and land missing rows Restores rows the first pass dropped or weakened (bedrock agentcore and openwebui thinking stream, anthropic prefix, citations and tool_choice, azure agents handler, huggingface bare api_base, optional params num_retries, alternating roles, rerank response type), replaces truthiness and call-count checks with whole-value assertions, and moves tests to the mirror file of the code they cover * test: move cohere, databricks, fireworks and gemini rows to mirror files over mocked HTTP Restores three TestCohereV2Transform methods the first pass nested out of collection, replaces direct transform calls with respx-backed completion calls, and drops the databricks and fireworks files that sat outside their mirror paths * test: close lost-intent gaps found in review of the llm translation migration Points the hosted-mock guard at the triton test's new integration path, restores exact rerank result assertions for infinity, drops the reasoning e2e fallback that passed for any multi-line answer, and adds the mixtral invoke system prompt variants, eval deletion, and async pdf url download that the legacy suite exercised * test: assert provider acceptance in the reasoning effort e2e The live half of the split grid proves each route accepts reasoning_effort. claude-fable-5-1 on the direct route answers without surfacing reasoning content, so a reasoning-presence check either fails on head or needs a fallback that passes for any answer. Wire shape stays covered by the unit grid * test: cover async and stream-end paths the legacy translation suite exercised Adds async variants for bedrock converse file and thinking content, bedrock embedding timeout and region resolution, interaction stream termination and success logging, xai reasoning token folding, and bytes chunks with a prefix before data:. Also scopes the vertex safety settings global in test_vertex_safety_settings with monkeypatch so it stops leaking into gemini request bodies on the same worker * test: type the new llm translation e2e calls and mark moved fixture dirs as packages * test: name the reasoning effort e2e models as module constants * test: assert reasoning content, register the new gemini coverage ids and give the guardrail config e2e a unique name * test: block the prompt the shared guardrail denies and drop the reasoning presence check * test: drop the duplicate litellm import left by the main sync --------- Co-authored-by: yuneng <yuneng@berri.ai>
254 lines
9.1 KiB
Python
254 lines
9.1 KiB
Python
import datetime
|
|
import json
|
|
from collections.abc import Iterator
|
|
from typing import Final
|
|
|
|
import httpx
|
|
import pytest
|
|
import respx
|
|
|
|
import litellm
|
|
from litellm.caching.llm_caching_handler import LLMClientCache
|
|
from litellm.litellm_core_utils.litellm_logging import Logging
|
|
from litellm.llms.cohere.embed.transformation import CohereEmbeddingConfig
|
|
from litellm.types.utils import EmbeddingResponse
|
|
|
|
COHERE_V2_EMBED_URL: Final = "https://api.cohere.ai/v2/embed"
|
|
|
|
|
|
@pytest.fixture
|
|
def _cohere_httpx_transport(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:
|
|
client_cache: Final = LLMClientCache()
|
|
monkeypatch.setattr(litellm, "in_memory_llm_clients_cache", client_cache)
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
monkeypatch.setattr(litellm, "force_ipv4", False)
|
|
yield
|
|
client_cache.flush_cache()
|
|
|
|
|
|
def _cohere_v2_embed_response(vectors: list[list[float]], texts: list[str]) -> dict[str, object]:
|
|
return {
|
|
"id": "da6e531f-83f4-4bcb-8e9a-f1a2c6e7a1b3",
|
|
"embeddings": {"float": vectors},
|
|
"texts": texts,
|
|
"meta": {"api_version": {"version": "2"}, "billed_units": {"input_tokens": 9}},
|
|
"response_type": "embeddings_by_type",
|
|
}
|
|
|
|
|
|
async def _embed(sync_mode: bool, **kwargs: object) -> EmbeddingResponse:
|
|
if sync_mode:
|
|
return litellm.embedding(model="cohere/embed-v4.0", api_key="cohere-test-key", **kwargs)
|
|
return await litellm.aembedding(model="cohere/embed-v4.0", api_key="cohere-test-key", **kwargs)
|
|
|
|
|
|
def _cohere_embedding_logging_obj() -> Logging:
|
|
return Logging(
|
|
model="embed-v4.0",
|
|
messages=["first", "second"],
|
|
stream=False,
|
|
call_type="embedding",
|
|
start_time=datetime.datetime(2025, 1, 1, tzinfo=datetime.timezone.utc),
|
|
litellm_call_id="cohere-embedding-test-call",
|
|
function_id="cohere-embedding-test-function",
|
|
)
|
|
|
|
|
|
def test_cohere_embed_v4_request_maps_input_options() -> None:
|
|
config: Final = CohereEmbeddingConfig()
|
|
|
|
request: Final = config.transform_embedding_request(
|
|
model="embed-v4.0",
|
|
input=["first", "second"],
|
|
optional_params={
|
|
"input_type": "search_document",
|
|
"embedding_types": ["float"],
|
|
"output_dimension": 256,
|
|
},
|
|
headers={},
|
|
)
|
|
|
|
assert request == {
|
|
"model": "embed-v4.0",
|
|
"texts": ["first", "second"],
|
|
"input_type": "search_document",
|
|
"embedding_types": ["float"],
|
|
"output_dimension": 256,
|
|
}
|
|
|
|
|
|
def test_cohere_embed_v4_request_routes_base64_images() -> None:
|
|
config: Final = CohereEmbeddingConfig()
|
|
|
|
request: Final = config.transform_embedding_request(
|
|
model="embed-v4.0",
|
|
input=["data:image/png;base64,aGVsbG8="],
|
|
optional_params={"input_type": "search_document"},
|
|
headers={},
|
|
)
|
|
|
|
assert request == {
|
|
"model": "embed-v4.0",
|
|
"images": ["data:image/png;base64,aGVsbG8="],
|
|
"input_type": "search_document",
|
|
}
|
|
|
|
|
|
def test_cohere_embed_v4_response_parses_multiple_embeddings() -> None:
|
|
config: Final = CohereEmbeddingConfig()
|
|
model_response: Final = EmbeddingResponse()
|
|
raw_response: Final = httpx.Response(
|
|
200,
|
|
json={
|
|
"embeddings": {
|
|
"float": [[0.1, 0.2], [0.3, 0.4]],
|
|
"int8": [[1, 2], [3, 4]],
|
|
},
|
|
"meta": {"billed_units": {"input_tokens": 6}},
|
|
},
|
|
)
|
|
|
|
response: Final = config.transform_embedding_response(
|
|
model="embed-v4.0",
|
|
raw_response=raw_response,
|
|
model_response=model_response,
|
|
logging_obj=_cohere_embedding_logging_obj(),
|
|
api_key="test-api-key",
|
|
request_data={"texts": ["first", "second"]},
|
|
optional_params={"embedding_types": ["float", "int8"]},
|
|
litellm_params={},
|
|
)
|
|
|
|
assert response.object == "list"
|
|
assert response.model == "embed-v4.0"
|
|
assert [item["embedding"] for item in response.data] == [
|
|
[0.1, 0.2],
|
|
[0.3, 0.4],
|
|
[1, 2],
|
|
[3, 4],
|
|
]
|
|
assert [item["index"] for item in response.data] == [0, 1, 0, 1]
|
|
assert response.usage.prompt_tokens == 6
|
|
assert response.usage.total_tokens == 6
|
|
|
|
|
|
@pytest.mark.usefixtures("_cohere_httpx_transport")
|
|
@pytest.mark.respx(assert_all_called=True)
|
|
@pytest.mark.parametrize("sync_mode", [True, False])
|
|
async def test_cohere_embed_v4_multiple_texts(respx_mock: respx.MockRouter, sync_mode: bool) -> None:
|
|
texts: Final = [
|
|
"The quick brown fox jumps over the lazy dog",
|
|
"Machine learning is transforming the world",
|
|
"Python is a versatile programming language",
|
|
"Natural language processing enables human-computer interaction",
|
|
]
|
|
vectors: Final = [[0.011, -0.022], [0.033, -0.044], [0.055, -0.066], [0.077, -0.088]]
|
|
route: Final = respx_mock.post(COHERE_V2_EMBED_URL).mock(
|
|
return_value=httpx.Response(200, json=_cohere_v2_embed_response(vectors, texts))
|
|
)
|
|
|
|
response: Final = await _embed(sync_mode, input=texts, input_type="search_document")
|
|
|
|
assert json.loads(route.calls.last.request.content) == {
|
|
"model": "embed-v4.0",
|
|
"texts": texts,
|
|
"input_type": "search_document",
|
|
}
|
|
assert response.model == "embed-v4.0"
|
|
assert response.data == [
|
|
{"object": "embedding", "index": index, "embedding": vector} for index, vector in enumerate(vectors)
|
|
]
|
|
assert response.usage.prompt_tokens == 9
|
|
|
|
|
|
@pytest.mark.usefixtures("_cohere_httpx_transport")
|
|
@pytest.mark.respx(assert_all_called=True)
|
|
@pytest.mark.parametrize("input_type", ["search_document", "search_query", "classification", "clustering"])
|
|
async def test_cohere_embed_v4_input_types(respx_mock: respx.MockRouter, input_type: str) -> None:
|
|
text: Final = f"Test text for {input_type}"
|
|
route: Final = respx_mock.post(COHERE_V2_EMBED_URL).mock(
|
|
return_value=httpx.Response(200, json=_cohere_v2_embed_response([[0.5, -0.5]], [text]))
|
|
)
|
|
|
|
await _embed(False, input=[text], input_type=input_type)
|
|
|
|
assert json.loads(route.calls.last.request.content) == {
|
|
"model": "embed-v4.0",
|
|
"texts": [text],
|
|
"input_type": input_type,
|
|
}
|
|
|
|
|
|
@pytest.mark.usefixtures("_cohere_httpx_transport")
|
|
@pytest.mark.respx(assert_all_called=True)
|
|
async def test_cohere_embed_v4_encoding_format(respx_mock: respx.MockRouter) -> None:
|
|
route: Final = respx_mock.post(COHERE_V2_EMBED_URL).mock(
|
|
return_value=httpx.Response(200, json=_cohere_v2_embed_response([[0.25, -0.75, 0.5]], ["Test encoding format"]))
|
|
)
|
|
|
|
response: Final = await _embed(True, input=["Test encoding format"], encoding_format="float")
|
|
|
|
assert json.loads(route.calls.last.request.content) == {
|
|
"model": "embed-v4.0",
|
|
"texts": ["Test encoding format"],
|
|
"input_type": "search_document",
|
|
"embedding_types": ["float"],
|
|
}
|
|
assert response.data == [{"object": "embedding", "index": 0, "embedding": [0.25, -0.75, 0.5]}]
|
|
|
|
|
|
@pytest.mark.usefixtures("_cohere_httpx_transport")
|
|
@pytest.mark.respx(assert_all_called=True)
|
|
async def test_cohere_embed_v4_with_optional_params(respx_mock: respx.MockRouter) -> None:
|
|
vector: Final = [round(index * 0.001, 3) for index in range(256)]
|
|
route: Final = respx_mock.post(COHERE_V2_EMBED_URL).mock(
|
|
return_value=httpx.Response(200, json=_cohere_v2_embed_response([vector], ["Test with optional parameters"]))
|
|
)
|
|
|
|
response: Final = await _embed(
|
|
True,
|
|
input=["Test with optional parameters"],
|
|
input_type="search_query",
|
|
dimensions=256,
|
|
encoding_format="float",
|
|
)
|
|
|
|
assert json.loads(route.calls.last.request.content) == {
|
|
"model": "embed-v4.0",
|
|
"texts": ["Test with optional parameters"],
|
|
"input_type": "search_query",
|
|
"embedding_types": ["float"],
|
|
"output_dimension": 256,
|
|
}
|
|
assert response.data == [{"object": "embedding", "index": 0, "embedding": vector}]
|
|
|
|
|
|
@pytest.mark.usefixtures("_cohere_httpx_transport")
|
|
@pytest.mark.respx(assert_all_called=True)
|
|
@pytest.mark.parametrize("sync_mode", [True, False])
|
|
async def test_cohere_embed_v4_image_embedding(respx_mock: respx.MockRouter, sync_mode: bool) -> None:
|
|
image: Final = (
|
|
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC"
|
|
)
|
|
route: Final = respx_mock.post(COHERE_V2_EMBED_URL).mock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "5807ee2e-0cda-445a-9ec8-864c60a06606",
|
|
"embeddings": {"float": [[0.125, -0.375]]},
|
|
"texts": [],
|
|
"images": [{"width": 1, "height": 1, "format": "image/png", "bit_depth": 8}],
|
|
"meta": {"api_version": {"version": "2"}, "billed_units": {"images": 1}},
|
|
"response_type": "embeddings_by_type",
|
|
},
|
|
)
|
|
)
|
|
|
|
response: Final = await _embed(sync_mode, input=[image], input_type="image")
|
|
|
|
assert json.loads(route.calls.last.request.content) == {
|
|
"model": "embed-v4.0",
|
|
"images": [image],
|
|
"input_type": "image",
|
|
}
|
|
assert response.data == [{"object": "embedding", "index": 0, "embedding": [0.125, -0.375]}]
|