mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-11 08:38:24 +00:00
* test: migrate local_testing SDK and provider tests out of build_and_test * test: cover sagemaker region, hf template and nova paths end to end Region rows now go through litellm.completion on the sagemaker/ handler that the legacy tests exercised, the prompt template test mocks the Hugging Face tokenizer config instead of switching to custom_prompt_dict, streaming tests assert role, finish reason and body, and Nova single turn and streaming run through litellm.completion with a recorded request body * test: tighten migrated integration tests for local OpenAI routes, timeouts and air-gapped boot Split the catch-all providers migration file by behavior. The fake endpoint route tests now hit the owned upstream directly, as the legacy harness tests did. LM Studio asserts the outbound key, path and stripped model. The embedding timeout test asserts the proxy maps a deployment timeout to a 408 and forwards the read timeout to the provider client, instead of timing out the test client The Azure streaming tool-call test also asserts the second identical stream is served from the Redis cache with the same reassembled tool call The Docker network test always skipped in CI because the security job has no litellm/litellm image and no egress to pull one, and it tested a published image rather than the checkout. It now boots the checkout proxy with every outbound HTTP connection refused and asserts readiness, the bundled cost map fallback and that the cost map host is the only egress attempted * test: tighten the migrated e2e rows for local_testing SDK and provider tests Drop the duplicated Anthropic covers marker and the Anthropic cell wrongly claimed by the Bedrock and Vertex cache tests. Keep the file-wide Anthropic model as it is on main. Send the 2023-06-01 version header to Anthropic through the deployment's extra_headers so the header actually reaches the provider, and mark the creation-token case 1h then 5m like the legacy test did Reuse the Bedrock file's own registration for the pydantic response test, swap the hand-built PNGs for the shared cat.jpg fixture, assert the SageMaker Nova image answer names the cat and that top_logprobs=3 is honored, and register the SageMaker Nova opt-in markers the same way as the other opt-in markers * test: restore lost assertions in migrated test_main tests Restore the role alternation check in test_completion_anthropic_hanging, the embedding and streaming response-header checks, the logit_bias drop check and the gemini system and safety settings checks. Add an azure 429 retry-after test for test_exception_with_headers, trigger the router fallback with mock_timeout like the legacy test, and stop the mock timeout tests sleeping 3s * test: restore lost legacy assertions in migrated vertex ai unit tests * test: tighten migrated cost, streaming and embedding tests and scope the hosted mock guard * test: strengthen migrated anthropic, azure, bedrock and cohere provider tests Assert full request bodies and usage instead of presence checks, restore the dummy-tool and custom_llm_provider assertions the legacy tests made, add the async and per-model variants the legacy parametrization covered, split streamed tool arguments across deltas, and move the Azure chat streaming tests to the azure.py mirror file * test: tighten migrated llms provider tests and cover legacy intents lost in the move Strengthen the migrated fireworks, gemini, mistral, together, openai text completion, openrouter, perplexity and openai error tests so they assert what the legacy tests proved (cost, provider tag, usage-only-on-last-chunk, function_call rebuild, reasoning param, sync/async/stream error fields). Replace the huggingface and jina tests that doubled litellm code with respx end-to-end tests. Add the tests the pre-existing mapped tests never covered: cohere 400 mapping, token-array text completion, hosted_vllm/llamafile/lm_studio env api base embeddings, watsonx embedding auth, ollama ssl_verify client and ollama vision prompt * test: cover residual legacy paths for anthropic tool streams, watsonx async stream, ollama http images and caller async embedding clients * test: restore single request assertions in the deep_infra and azure_ai embedding tests * test: drop a no-op streaming cost flag from the openai streaming cost test * test: cover residual legacy-test lines in caching, main, utils, replicate, vertex and bedrock invoke * test: cover streamed audio assembly, choice membership, untranslated responses events and the stream log guard * test: pin cost rates in unit tests, use sdk for cohere e2e, drop vertex llama e2e * test: send the vertex maps e2e request through the openai sdk * test: drop the local_testing jobs that select no tests after the migrations * test: wait for the streamed cache entry and keep custom Mistral rates test-local * test: keep untouched Mistral test lines as they were --------- Co-authored-by: yuneng <yuneng@berri.ai>
632 lines
20 KiB
Python
632 lines
20 KiB
Python
from unittest.mock import AsyncMock, Mock
|
|
|
|
import httpx
|
|
import pytest
|
|
import respx
|
|
|
|
import litellm
|
|
from litellm.llms.predibase.chat.handler import PredibaseChatCompletion
|
|
from litellm.llms.predibase.chat.transformation import PredibaseConfig
|
|
from litellm.llms.predibase.common_utils import PredibaseError
|
|
from litellm.utils import Choices, Message, ModelResponse
|
|
|
|
|
|
def _build_model_response() -> ModelResponse:
|
|
return ModelResponse(
|
|
choices=[
|
|
Choices(
|
|
finish_reason=None,
|
|
index=0,
|
|
message=Message(role="assistant", content=""),
|
|
)
|
|
]
|
|
)
|
|
|
|
|
|
def test_predibase_transform_request_non_stream():
|
|
config = PredibaseConfig()
|
|
request_data = config.transform_request(
|
|
model="predibase-model",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
optional_params={"temperature": 0.2},
|
|
litellm_params={},
|
|
headers={},
|
|
)
|
|
|
|
assert request_data["inputs"]
|
|
assert request_data["parameters"]["temperature"] == 0.2
|
|
assert request_data["parameters"]["details"] is True
|
|
assert "stream" not in request_data["parameters"]
|
|
|
|
|
|
def test_predibase_transform_request_custom_prompt(monkeypatch):
|
|
config = PredibaseConfig()
|
|
|
|
monkeypatch.setattr(
|
|
"litellm.llms.predibase.chat.transformation.custom_prompt",
|
|
lambda **kwargs: "custom-prompt",
|
|
)
|
|
|
|
request_data = config.transform_request(
|
|
model="predibase-model",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
optional_params={},
|
|
litellm_params={
|
|
"custom_prompt_dict": {
|
|
"predibase-model": {
|
|
"roles": {},
|
|
"initial_prompt_value": "",
|
|
"final_prompt_value": "",
|
|
}
|
|
}
|
|
},
|
|
headers={},
|
|
)
|
|
|
|
assert request_data["inputs"] == "custom-prompt"
|
|
|
|
|
|
def test_predibase_get_complete_url_stream_and_non_stream():
|
|
config = PredibaseConfig()
|
|
litellm_params = {"predibase_tenant_id": "tenant-123"}
|
|
|
|
non_stream_url = config.get_complete_url(
|
|
api_base="https://serving.example.com",
|
|
api_key="test-key",
|
|
model="predibase-model",
|
|
optional_params={"stream": False},
|
|
litellm_params=litellm_params,
|
|
)
|
|
stream_url = config.get_complete_url(
|
|
api_base="https://serving.example.com",
|
|
api_key="test-key",
|
|
model="predibase-model",
|
|
optional_params={"stream": True},
|
|
litellm_params=litellm_params,
|
|
)
|
|
|
|
assert non_stream_url.endswith("/generate")
|
|
assert stream_url.endswith("/generate_stream")
|
|
|
|
|
|
def test_predibase_get_complete_url_missing_tenant_id():
|
|
config = PredibaseConfig()
|
|
|
|
with pytest.raises(ValueError, match="Missing Predibase Tenant ID"):
|
|
config.get_complete_url(
|
|
api_base=None,
|
|
api_key="test-key",
|
|
model="predibase-model",
|
|
optional_params={},
|
|
litellm_params={},
|
|
)
|
|
|
|
|
|
def test_predibase_get_complete_url_with_tenant_id_key():
|
|
config = PredibaseConfig()
|
|
|
|
url = config.get_complete_url(
|
|
api_base="https://serving.example.com",
|
|
api_key="test-key",
|
|
model="predibase-model",
|
|
optional_params={},
|
|
litellm_params={"tenant_id": "tenant-xyz"},
|
|
)
|
|
|
|
assert "tenant-xyz" in url
|
|
assert url.endswith("/generate")
|
|
|
|
|
|
def test_predibase_authentication_error_does_not_expose_the_api_key(respx_mock: respx.MockRouter) -> None:
|
|
route = respx_mock.post(
|
|
"https://serving.app.predibase.com/c4768f95/deployments/v2/llms/llama-3-8b-instruct/generate"
|
|
).mock(return_value=httpx.Response(401, json={"message": "invalid API key"}))
|
|
|
|
with pytest.raises(litellm.AuthenticationError) as exc_info:
|
|
litellm.completion(
|
|
model="predibase/llama-3-8b-instruct",
|
|
messages=[{"role": "user", "content": "What is the meaning of life?"}],
|
|
predibase_tenant_id="c4768f95",
|
|
api_key="hf-rawapikey",
|
|
max_retries=0,
|
|
)
|
|
|
|
assert "hf-rawapikey" not in str(exc_info.value)
|
|
assert route.calls.last.request.headers["Authorization"] == "Bearer hf-rawapikey"
|
|
|
|
|
|
def test_predibase_transform_response_success_best_of(monkeypatch):
|
|
config = PredibaseConfig()
|
|
logging_obj = Mock()
|
|
encoding = Mock()
|
|
encoding.encode.return_value = [1, 2, 3]
|
|
monkeypatch.setattr("litellm.token_counter", lambda messages: 5)
|
|
|
|
raw_response = httpx.Response(
|
|
status_code=200,
|
|
json={
|
|
"generated_text": "<|assistant|>primary-output</s>",
|
|
"details": {
|
|
"finish_reason": "eos_token",
|
|
"tokens": [{"logprob": -0.2}, {"logprob": None}],
|
|
"best_of_sequences": [
|
|
{
|
|
"generated_text": "<s>secondary-output</s>",
|
|
"finish_reason": "length",
|
|
"tokens": [{"logprob": -0.5}],
|
|
}
|
|
],
|
|
},
|
|
},
|
|
headers={"x-request-id": "req-123"},
|
|
)
|
|
|
|
result = config.transform_response(
|
|
model="predibase-model",
|
|
raw_response=raw_response,
|
|
model_response=_build_model_response(),
|
|
logging_obj=logging_obj,
|
|
request_data={"inputs": "hello", "parameters": {}},
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
optional_params={"best_of": 2},
|
|
litellm_params={},
|
|
encoding=encoding,
|
|
api_key="test-key",
|
|
)
|
|
|
|
assert result.choices[0].message.content == "primary-output"
|
|
assert len(result.choices) == 2
|
|
assert result.choices[1].message.content == "secondary-output"
|
|
assert result.usage.prompt_tokens == 5
|
|
assert result.usage.completion_tokens == 3
|
|
assert (
|
|
result._hidden_params["additional_headers"]["llm_provider-x-request-id"]
|
|
== "req-123"
|
|
)
|
|
|
|
|
|
def test_predibase_transform_response_invalid_json():
|
|
config = PredibaseConfig()
|
|
|
|
with pytest.raises(PredibaseError) as exc:
|
|
config.transform_response(
|
|
model="predibase-model",
|
|
raw_response=httpx.Response(status_code=200, content=b"not-json"),
|
|
model_response=_build_model_response(),
|
|
logging_obj=Mock(),
|
|
request_data={},
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
optional_params={},
|
|
litellm_params={},
|
|
encoding=Mock(),
|
|
api_key="test-key",
|
|
)
|
|
|
|
assert exc.value.status_code == 422
|
|
|
|
|
|
def test_predibase_transform_response_error_field():
|
|
config = PredibaseConfig()
|
|
|
|
with pytest.raises(PredibaseError) as exc:
|
|
config.transform_response(
|
|
model="predibase-model",
|
|
raw_response=httpx.Response(
|
|
status_code=400, json={"error": "invalid request"}
|
|
),
|
|
model_response=_build_model_response(),
|
|
logging_obj=Mock(),
|
|
request_data={},
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
optional_params={},
|
|
litellm_params={},
|
|
encoding=Mock(),
|
|
api_key="test-key",
|
|
)
|
|
|
|
assert exc.value.status_code == 400
|
|
|
|
|
|
def test_predibase_transform_response_missing_generated_text():
|
|
config = PredibaseConfig()
|
|
|
|
with pytest.raises(PredibaseError, match="'generated_text' is not a key"):
|
|
config.transform_response(
|
|
model="predibase-model",
|
|
raw_response=httpx.Response(status_code=200, json={"details": {}}),
|
|
model_response=_build_model_response(),
|
|
logging_obj=Mock(),
|
|
request_data={},
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
optional_params={},
|
|
litellm_params={},
|
|
encoding=Mock(),
|
|
api_key="test-key",
|
|
)
|
|
|
|
|
|
def test_predibase_transform_response_non_dict_payload():
|
|
config = PredibaseConfig()
|
|
raw_response = Mock()
|
|
raw_response.text = "[]"
|
|
raw_response.status_code = 200
|
|
raw_response.headers = {}
|
|
raw_response.json.return_value = []
|
|
|
|
with pytest.raises(PredibaseError, match="'completion_response' is not a dictionary"):
|
|
config.transform_response(
|
|
model="predibase-model",
|
|
raw_response=raw_response,
|
|
model_response=_build_model_response(),
|
|
logging_obj=Mock(),
|
|
request_data={},
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
optional_params={},
|
|
litellm_params={},
|
|
encoding=Mock(),
|
|
api_key="test-key",
|
|
)
|
|
|
|
|
|
def test_predibase_transform_response_best_of_with_empty_generated_text(monkeypatch):
|
|
config = PredibaseConfig()
|
|
logging_obj = Mock()
|
|
encoding = Mock()
|
|
encoding.encode.return_value = [1]
|
|
monkeypatch.setattr("litellm.token_counter", lambda messages: 1)
|
|
|
|
raw_response = httpx.Response(
|
|
status_code=200,
|
|
json={
|
|
"generated_text": "primary-output",
|
|
"details": {
|
|
"finish_reason": "stop",
|
|
"tokens": [],
|
|
"best_of_sequences": [
|
|
{
|
|
"generated_text": "",
|
|
"finish_reason": "length",
|
|
"tokens": [],
|
|
}
|
|
],
|
|
},
|
|
},
|
|
)
|
|
|
|
result = config.transform_response(
|
|
model="predibase-model",
|
|
raw_response=raw_response,
|
|
model_response=_build_model_response(),
|
|
logging_obj=logging_obj,
|
|
request_data={"inputs": "hello", "parameters": {}},
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
optional_params={"best_of": 2},
|
|
litellm_params={},
|
|
encoding=encoding,
|
|
api_key="test-key",
|
|
)
|
|
|
|
assert len(result.choices) == 2
|
|
assert result.choices[1].message.content is None
|
|
|
|
|
|
def test_predibase_transform_response_best_of_from_request_data(monkeypatch):
|
|
config = PredibaseConfig()
|
|
logging_obj = Mock()
|
|
encoding = Mock()
|
|
encoding.encode.return_value = [1]
|
|
monkeypatch.setattr("litellm.token_counter", lambda messages: 1)
|
|
|
|
raw_response = httpx.Response(
|
|
status_code=200,
|
|
json={
|
|
"generated_text": "primary-output",
|
|
"details": {
|
|
"finish_reason": "stop",
|
|
"tokens": [],
|
|
"best_of_sequences": [
|
|
{
|
|
"generated_text": "secondary-output",
|
|
"finish_reason": "length",
|
|
"tokens": [],
|
|
}
|
|
],
|
|
},
|
|
},
|
|
)
|
|
|
|
result = config.transform_response(
|
|
model="predibase-model",
|
|
raw_response=raw_response,
|
|
model_response=_build_model_response(),
|
|
logging_obj=logging_obj,
|
|
request_data={"inputs": "hello", "parameters": {"best_of": 2}},
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
optional_params={},
|
|
litellm_params={},
|
|
encoding=encoding,
|
|
api_key="test-key",
|
|
)
|
|
|
|
assert len(result.choices) == 2
|
|
assert result.choices[1].message.content == "secondary-output"
|
|
|
|
|
|
def test_predibase_transform_response_best_of_invalid_value_falls_back(monkeypatch):
|
|
config = PredibaseConfig()
|
|
logging_obj = Mock()
|
|
encoding = Mock()
|
|
encoding.encode.return_value = [1]
|
|
monkeypatch.setattr("litellm.token_counter", lambda messages: 1)
|
|
|
|
raw_response = httpx.Response(
|
|
status_code=200,
|
|
json={
|
|
"generated_text": "primary-output",
|
|
"details": {
|
|
"finish_reason": "stop",
|
|
"tokens": [],
|
|
"best_of_sequences": [
|
|
{
|
|
"generated_text": "secondary-output",
|
|
"finish_reason": "length",
|
|
"tokens": [],
|
|
}
|
|
],
|
|
},
|
|
},
|
|
)
|
|
|
|
result = config.transform_response(
|
|
model="predibase-model",
|
|
raw_response=raw_response,
|
|
model_response=_build_model_response(),
|
|
logging_obj=logging_obj,
|
|
request_data={"inputs": "hello", "parameters": {}},
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
optional_params={"best_of": "invalid-int"},
|
|
litellm_params={},
|
|
encoding=encoding,
|
|
api_key="test-key",
|
|
)
|
|
|
|
# Invalid best_of should safely fall back to 0 and not append extra choices.
|
|
assert len(result.choices) == 1
|
|
assert result.choices[0].message.content == "primary-output"
|
|
|
|
|
|
def test_predibase_transform_response_empty_output_sets_completion_tokens_zero(monkeypatch):
|
|
config = PredibaseConfig()
|
|
logging_obj = Mock()
|
|
encoding = Mock()
|
|
monkeypatch.setattr("litellm.token_counter", lambda messages: 3)
|
|
|
|
raw_response = httpx.Response(
|
|
status_code=200,
|
|
json={"generated_text": "", "details": {"tokens": [], "finish_reason": "stop"}},
|
|
)
|
|
|
|
result = config.transform_response(
|
|
model="predibase-model",
|
|
raw_response=raw_response,
|
|
model_response=_build_model_response(),
|
|
logging_obj=logging_obj,
|
|
request_data={"inputs": "hello", "parameters": {}},
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
optional_params={},
|
|
litellm_params={},
|
|
encoding=encoding,
|
|
api_key="test-key",
|
|
)
|
|
|
|
assert result.usage.prompt_tokens == 3
|
|
assert result.usage.completion_tokens == 0
|
|
|
|
|
|
def test_predibase_get_complete_url_uses_env_base_url(monkeypatch):
|
|
config = PredibaseConfig()
|
|
monkeypatch.setenv("PREDIBASE_API_BASE", "https://env.predibase.com")
|
|
|
|
url = config.get_complete_url(
|
|
api_base=None,
|
|
api_key="test-key",
|
|
model="predibase-model",
|
|
optional_params={},
|
|
litellm_params={"predibase_tenant_id": "tenant-123"},
|
|
)
|
|
|
|
assert url.startswith("https://env.predibase.com/tenant-123/")
|
|
|
|
|
|
def test_predibase_transform_response_usage_fallbacks(monkeypatch):
|
|
config = PredibaseConfig()
|
|
logging_obj = Mock()
|
|
encoding = Mock()
|
|
encoding.encode.side_effect = RuntimeError("encoding failure")
|
|
monkeypatch.setattr(
|
|
"litellm.token_counter", lambda messages: (_ for _ in ()).throw(RuntimeError())
|
|
)
|
|
|
|
raw_response = httpx.Response(
|
|
status_code=200,
|
|
json={"generated_text": "ok", "details": {"tokens": [], "finish_reason": "stop"}},
|
|
)
|
|
|
|
result = config.transform_response(
|
|
model="predibase-model",
|
|
raw_response=raw_response,
|
|
model_response=_build_model_response(),
|
|
logging_obj=logging_obj,
|
|
request_data={"inputs": "hello", "parameters": {}},
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
optional_params={},
|
|
litellm_params={},
|
|
encoding=encoding,
|
|
api_key="test-key",
|
|
)
|
|
|
|
assert result.usage.prompt_tokens == 0
|
|
assert result.usage.completion_tokens == 0
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_predibase_async_completion_uses_default_config_when_none(monkeypatch):
|
|
handler = PredibaseChatCompletion()
|
|
mock_response = httpx.Response(status_code=200, json={"generated_text": "ok"})
|
|
|
|
async_handler = Mock()
|
|
async_handler.post = AsyncMock(return_value=mock_response)
|
|
monkeypatch.setattr(
|
|
"litellm.llms.predibase.chat.handler.get_async_httpx_client",
|
|
lambda **kwargs: async_handler,
|
|
)
|
|
|
|
default_config = Mock()
|
|
default_config.transform_response.return_value = _build_model_response()
|
|
monkeypatch.setattr("litellm.PredibaseConfig", lambda: default_config)
|
|
|
|
result = await handler.async_completion(
|
|
model="predibase-model",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
api_base="https://serving.example.com/x/generate",
|
|
model_response=_build_model_response(),
|
|
print_verbose=Mock(),
|
|
encoding=Mock(),
|
|
api_key="test-key",
|
|
logging_obj=Mock(),
|
|
stream=False,
|
|
data={"inputs": "hello", "parameters": {}},
|
|
optional_params={},
|
|
timeout=10,
|
|
litellm_params={},
|
|
headers={"Authorization": "Bearer test"},
|
|
)
|
|
|
|
assert result is default_config.transform_response.return_value
|
|
default_config.transform_response.assert_called_once()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_predibase_async_completion_uses_passed_config(monkeypatch):
|
|
handler = PredibaseChatCompletion()
|
|
mock_response = httpx.Response(status_code=200, json={"generated_text": "ok"})
|
|
|
|
async_handler = Mock()
|
|
async_handler.post = AsyncMock(return_value=mock_response)
|
|
monkeypatch.setattr(
|
|
"litellm.llms.predibase.chat.handler.get_async_httpx_client",
|
|
lambda **kwargs: async_handler,
|
|
)
|
|
|
|
passed_config = Mock()
|
|
passed_config.transform_response.return_value = _build_model_response()
|
|
|
|
result = await handler.async_completion(
|
|
model="predibase-model",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
api_base="https://serving.example.com/x/generate",
|
|
model_response=_build_model_response(),
|
|
print_verbose=Mock(),
|
|
encoding=Mock(),
|
|
api_key="test-key",
|
|
logging_obj=Mock(),
|
|
stream=False,
|
|
data={"inputs": "hello", "parameters": {}},
|
|
optional_params={},
|
|
timeout=10,
|
|
litellm_params={},
|
|
headers={"Authorization": "Bearer test"},
|
|
predibase_config=passed_config,
|
|
)
|
|
|
|
assert result is passed_config.transform_response.return_value
|
|
passed_config.transform_response.assert_called_once()
|
|
|
|
|
|
def test_predibase_completion_sync_returns_transform_response(monkeypatch):
|
|
handler = PredibaseChatCompletion()
|
|
expected = _build_model_response()
|
|
|
|
def fake_validate_environment(self, **kwargs):
|
|
return {"Authorization": "Bearer test"}
|
|
|
|
def fake_get_complete_url(self, **kwargs):
|
|
return "https://serving.example.com/tenant/deployments/v2/llms/model/generate"
|
|
|
|
def fake_transform_request(self, **kwargs):
|
|
return {"inputs": "hello", "parameters": {}}
|
|
|
|
def fake_transform_response(self, **kwargs):
|
|
return expected
|
|
|
|
monkeypatch.setattr(PredibaseConfig, "validate_environment", fake_validate_environment)
|
|
monkeypatch.setattr(PredibaseConfig, "get_complete_url", fake_get_complete_url)
|
|
monkeypatch.setattr(PredibaseConfig, "transform_request", fake_transform_request)
|
|
monkeypatch.setattr(PredibaseConfig, "transform_response", fake_transform_response)
|
|
monkeypatch.setattr(
|
|
"litellm.module_level_client.post",
|
|
lambda *args, **kwargs: httpx.Response(status_code=200, json={"generated_text": "ok"}),
|
|
)
|
|
|
|
result = handler.completion(
|
|
model="predibase-model",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
api_base="https://serving.example.com",
|
|
custom_prompt_dict={},
|
|
model_response=_build_model_response(),
|
|
print_verbose=Mock(),
|
|
encoding=Mock(),
|
|
api_key="test-key",
|
|
logging_obj=Mock(),
|
|
optional_params={},
|
|
litellm_params={},
|
|
tenant_id="tenant-123",
|
|
timeout=10,
|
|
acompletion=False,
|
|
)
|
|
|
|
assert result is expected
|
|
|
|
|
|
def test_predibase_completion_passes_existing_config_to_async_completion(monkeypatch):
|
|
handler = PredibaseChatCompletion()
|
|
captured = {}
|
|
|
|
def fake_validate_environment(self, **kwargs):
|
|
captured["config_instance"] = self
|
|
return {"Authorization": "Bearer test"}
|
|
|
|
def fake_get_complete_url(self, **kwargs):
|
|
return "https://serving.example.com/tenant/deployments/v2/llms/model/generate"
|
|
|
|
def fake_transform_request(self, **kwargs):
|
|
return {"inputs": "hello", "parameters": {}}
|
|
|
|
def fake_async_completion(**kwargs):
|
|
captured["async_kwargs"] = kwargs
|
|
return "async-result"
|
|
|
|
monkeypatch.setattr(PredibaseConfig, "validate_environment", fake_validate_environment)
|
|
monkeypatch.setattr(PredibaseConfig, "get_complete_url", fake_get_complete_url)
|
|
monkeypatch.setattr(PredibaseConfig, "transform_request", fake_transform_request)
|
|
monkeypatch.setattr(handler, "async_completion", fake_async_completion)
|
|
|
|
result = handler.completion(
|
|
model="predibase-model",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
api_base="https://serving.example.com",
|
|
custom_prompt_dict={},
|
|
model_response=_build_model_response(),
|
|
print_verbose=Mock(),
|
|
encoding=Mock(),
|
|
api_key="test-key",
|
|
logging_obj=Mock(),
|
|
optional_params={},
|
|
litellm_params={},
|
|
tenant_id="tenant-123",
|
|
timeout=10,
|
|
acompletion=True,
|
|
)
|
|
|
|
assert result == "async-result"
|
|
assert captured["async_kwargs"]["predibase_config"] is captured["config_instance"]
|