mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-08 22:21:35 +00:00
* test: drop the cwd-relative sys.path.insert calls from the test suite
TQ003 stands at 1,077 across 1,058 files, and 1,015 of them are the same shape:
sys.path.insert(0, os.path.abspath("../..")) and its deeper siblings. The
argument resolves against the working directory rather than the file, so from
the repo root, where every job runs pytest, it inserts the directory two levels
above the checkout. It has never pointed at litellm. The package is installed
into the environment anyway, which is what actually makes the import work, and
what the rule's message has said all along.
Removing them leaves 1,634 imports of sys and os with no remaining reference,
and those go too, except where another test module imports the name back out of
the file. The rest of TQ003 is 62 call sites that resolve against __file__ or a
variable, which are a different question and are left alone.
Collection is identical either way: 45,871 tests and the same 51 pre-existing
collection errors before and after, and ruff reports no new undefined name.
* test: drop the duplicate imports the sys.path sweep exposed to F811
* test(pre-call-utils): restore the os import the new bedrock tests need
273 lines
8.6 KiB
Python
273 lines
8.6 KiB
Python
import json
|
|
|
|
import litellm
|
|
from litellm import completion, embedding
|
|
from litellm.llms.custom_httpx.http_handler import HTTPHandler
|
|
from unittest.mock import patch, Mock
|
|
import pytest
|
|
from typing import Optional
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def watsonx_env_vars(monkeypatch):
|
|
"""Set required WatsonX env vars so the provider passes validation.
|
|
Also clear WATSONX_ZENAPIKEY/WATSONX_TOKEN so they don't bypass the IAM token mock.
|
|
"""
|
|
monkeypatch.setenv("WATSONX_URL", "https://us-south.ml.cloud.ibm.com")
|
|
monkeypatch.setenv("WATSONX_PROJECT_ID", "test-project-id")
|
|
monkeypatch.delenv("WATSONX_ZENAPIKEY", raising=False)
|
|
monkeypatch.delenv("WATSONX_TOKEN", raising=False)
|
|
|
|
|
|
@pytest.fixture
|
|
def watsonx_chat_completion_call():
|
|
def _call(
|
|
model="watsonx/my-test-model",
|
|
messages=None,
|
|
api_key="test_api_key",
|
|
space_id: Optional[str] = None,
|
|
headers=None,
|
|
client=None,
|
|
patch_token_call=True,
|
|
):
|
|
if messages is None:
|
|
messages = [{"role": "user", "content": "Hello, how are you?"}]
|
|
if client is None:
|
|
client = HTTPHandler()
|
|
|
|
if patch_token_call:
|
|
mock_response = Mock()
|
|
mock_response.json.return_value = {
|
|
"access_token": "mock_access_token",
|
|
"expires_in": 3600,
|
|
}
|
|
mock_response.raise_for_status = Mock() # No-op to simulate no exception
|
|
|
|
with (
|
|
patch.object(client, "post") as mock_post,
|
|
patch.object(
|
|
litellm.module_level_client, "post", return_value=mock_response
|
|
) as mock_get,
|
|
):
|
|
try:
|
|
completion(
|
|
model=model,
|
|
messages=messages,
|
|
api_key=api_key,
|
|
headers=headers or {},
|
|
client=client,
|
|
space_id=space_id,
|
|
)
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
return mock_post, mock_get
|
|
else:
|
|
with patch.object(client, "post") as mock_post:
|
|
try:
|
|
completion(
|
|
model=model,
|
|
messages=messages,
|
|
api_key=api_key,
|
|
headers=headers or {},
|
|
client=client,
|
|
space_id=space_id,
|
|
)
|
|
except Exception as e:
|
|
print(e)
|
|
return mock_post, None
|
|
|
|
return _call
|
|
|
|
|
|
@pytest.fixture
|
|
def watsonx_embedding_call():
|
|
def _call(
|
|
model="watsonx/my-test-model",
|
|
input=None,
|
|
api_key="test_api_key",
|
|
space_id: Optional[str] = None,
|
|
headers=None,
|
|
client=None,
|
|
patch_token_call=True,
|
|
):
|
|
if input is None:
|
|
input = ["Hello, how are you?"]
|
|
if client is None:
|
|
client = HTTPHandler()
|
|
|
|
if patch_token_call:
|
|
mock_response = Mock()
|
|
mock_response.json.return_value = {
|
|
"access_token": "mock_access_token",
|
|
"expires_in": 3600,
|
|
}
|
|
mock_response.raise_for_status = Mock() # No-op to simulate no exception
|
|
|
|
with (
|
|
patch.object(client, "post") as mock_post,
|
|
patch.object(
|
|
litellm.module_level_client, "post", return_value=mock_response
|
|
) as mock_get,
|
|
):
|
|
try:
|
|
embedding(
|
|
model=model,
|
|
input=input,
|
|
api_key=api_key,
|
|
headers=headers or {},
|
|
client=client,
|
|
space_id=space_id,
|
|
)
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
return mock_post, mock_get
|
|
else:
|
|
with patch.object(client, "post") as mock_post:
|
|
try:
|
|
embedding(
|
|
model=model,
|
|
input=input,
|
|
api_key=api_key,
|
|
headers=headers or {},
|
|
client=client,
|
|
space_id=space_id,
|
|
)
|
|
except Exception as e:
|
|
print(e)
|
|
return mock_post, None
|
|
|
|
return _call
|
|
|
|
|
|
@pytest.mark.parametrize("with_custom_auth_header", [True, False])
|
|
def test_watsonx_custom_auth_header(
|
|
with_custom_auth_header, watsonx_chat_completion_call
|
|
):
|
|
headers = (
|
|
{"Authorization": "Bearer my-custom-auth-header"}
|
|
if with_custom_auth_header
|
|
else {}
|
|
)
|
|
|
|
mock_post, _ = watsonx_chat_completion_call(headers=headers)
|
|
|
|
assert mock_post.call_count == 1
|
|
if with_custom_auth_header:
|
|
assert (
|
|
mock_post.call_args[1]["headers"]["Authorization"]
|
|
== "Bearer my-custom-auth-header"
|
|
)
|
|
else:
|
|
assert (
|
|
mock_post.call_args[1]["headers"]["Authorization"]
|
|
== "Bearer mock_access_token"
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("env_var_key", ["WATSONX_ZENAPIKEY", "WATSONX_TOKEN"])
|
|
def test_watsonx_token_in_env_var(
|
|
monkeypatch, watsonx_chat_completion_call, env_var_key
|
|
):
|
|
monkeypatch.setenv(env_var_key, "my-custom-token")
|
|
|
|
mock_post, _ = watsonx_chat_completion_call(patch_token_call=False)
|
|
|
|
assert mock_post.call_count == 1
|
|
if env_var_key == "WATSONX_ZENAPIKEY":
|
|
assert (
|
|
mock_post.call_args[1]["headers"]["Authorization"]
|
|
== "ZenApiKey my-custom-token"
|
|
)
|
|
else:
|
|
assert (
|
|
mock_post.call_args[1]["headers"]["Authorization"]
|
|
== "Bearer my-custom-token"
|
|
)
|
|
|
|
|
|
def test_watsonx_chat_completions_endpoint(watsonx_chat_completion_call):
|
|
model = "watsonx/another-model"
|
|
messages = [{"role": "user", "content": "Test message"}]
|
|
|
|
mock_post, _ = watsonx_chat_completion_call(model=model, messages=messages)
|
|
|
|
assert mock_post.call_count == 1
|
|
assert "deployment" not in mock_post.call_args.kwargs["url"]
|
|
|
|
|
|
def test_watsonx_chat_completions_endpoint_space_id(
|
|
monkeypatch, watsonx_chat_completion_call
|
|
):
|
|
my_fake_space_id = "xxx-xxx-xxx-xxx-xxx"
|
|
monkeypatch.setenv("WATSONX_SPACE_ID", my_fake_space_id)
|
|
|
|
monkeypatch.delenv("WATSONX_PROJECT_ID", raising=False)
|
|
|
|
model = "watsonx/another-model"
|
|
messages = [{"role": "user", "content": "Test message"}]
|
|
|
|
mock_post, _ = watsonx_chat_completion_call(model=model, messages=messages)
|
|
|
|
assert mock_post.call_count == 1
|
|
assert "deployment" not in mock_post.call_args.kwargs["url"]
|
|
|
|
json_data = json.loads(mock_post.call_args.kwargs["data"])
|
|
assert my_fake_space_id == json_data["space_id"]
|
|
assert not json_data.get("project_id")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"model",
|
|
[
|
|
"watsonx/deployment/<xxxx.xxx.xxx.xxxx>",
|
|
"watsonx_text/deployment/<xxxx.xxx.xxx.xxxx>",
|
|
],
|
|
)
|
|
def test_watsonx_deployment_space_id(monkeypatch, watsonx_chat_completion_call, model):
|
|
my_fake_space_id = "xxx-xxx-xxx-xxx-xxx"
|
|
monkeypatch.setenv("WATSONX_SPACE_ID", my_fake_space_id)
|
|
|
|
mock_post, _ = watsonx_chat_completion_call(
|
|
model=model,
|
|
messages=[{"content": "Hello, how are you?", "role": "user"}],
|
|
)
|
|
|
|
assert mock_post.call_count == 1
|
|
json_data = json.loads(mock_post.call_args.kwargs["data"])
|
|
assert my_fake_space_id not in json_data
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"model",
|
|
[
|
|
"watsonx/deployment/<xxxx.xxx.xxx.xxxx>",
|
|
"watsonx_text/deployment/<xxxx.xxx.xxx.xxxx>",
|
|
],
|
|
)
|
|
def test_watsonx_deployment(watsonx_chat_completion_call, model):
|
|
messages = [{"content": "Hello, how are you?", "role": "user"}]
|
|
mock_post, _ = watsonx_chat_completion_call(
|
|
model=model,
|
|
messages=messages,
|
|
)
|
|
|
|
assert mock_post.call_count == 1
|
|
json_data = json.loads(mock_post.call_args.kwargs["data"])
|
|
|
|
# nor space_id or project_id is required by wx.ai API when inferencing deployment
|
|
assert "project_id" not in json_data and "space_id" not in json_data
|
|
|
|
|
|
def test_watsonx_deployment_space_id_embedding(monkeypatch, watsonx_embedding_call):
|
|
my_fake_space_id = "xxx-xxx-xxx-xxx-xxx"
|
|
monkeypatch.setenv("WATSONX_SPACE_ID", my_fake_space_id)
|
|
|
|
mock_post, _ = watsonx_embedding_call(model="watsonx/deployment/my-test-model")
|
|
|
|
assert mock_post.call_count == 1
|
|
json_data = json.loads(mock_post.call_args.kwargs["data"])
|
|
|
|
# nor space_id or project_id is required by wx.ai API when inferencing deployment
|
|
assert "project_id" not in json_data and "space_id" not in json_data
|