mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-15 09:12:18 +00:00
Merge 7ada3370df into 60fc121ce8
This commit is contained in:
commit
7090ee32d0
3 changed files with 86 additions and 6 deletions
|
|
@ -11,11 +11,12 @@ import time
|
|||
import uuid
|
||||
from collections.abc import AsyncIterator, Iterator, Mapping, Sequence
|
||||
from types import MappingProxyType
|
||||
from typing import TYPE_CHECKING, Any, Final
|
||||
from typing import TYPE_CHECKING, Any, Final, cast # noqa: TID251 # response_format is a plain JSON object
|
||||
|
||||
import httpx
|
||||
from pydantic import TypeAdapter
|
||||
|
||||
import litellm
|
||||
from litellm._logging import verbose_logger
|
||||
from litellm.litellm_core_utils.url_utils import join_api_base
|
||||
from litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMException
|
||||
|
|
@ -63,6 +64,13 @@ class GigaChatError(BaseLLMException):
|
|||
"""GigaChat API error."""
|
||||
|
||||
|
||||
def _is_supported_response_format(value: object) -> bool:
|
||||
if not isinstance(value, Mapping):
|
||||
return False
|
||||
response_format: Final = cast("dict[str, object]", value) # cast-ok: the provider sends a JSON object
|
||||
return response_format.get("type") in (None, "text", "json_schema")
|
||||
|
||||
|
||||
class GigaChatConfig(BaseConfig):
|
||||
"""
|
||||
Configuration class for GigaChat API.
|
||||
|
|
@ -153,7 +161,6 @@ class GigaChatConfig(BaseConfig):
|
|||
"top_p",
|
||||
"max_tokens",
|
||||
"max_completion_tokens",
|
||||
"stop",
|
||||
"tools",
|
||||
"tool_choice",
|
||||
"functions",
|
||||
|
|
@ -182,9 +189,6 @@ class GigaChatConfig(BaseConfig):
|
|||
optional_params["top_p"] = value
|
||||
elif param in ("max_tokens", "max_completion_tokens"):
|
||||
optional_params["max_tokens"] = value
|
||||
elif param == "stop":
|
||||
# GigaChat doesn't support stop sequences
|
||||
pass
|
||||
elif param == "tools":
|
||||
# Convert tools to functions format
|
||||
if isinstance(value, Sequence):
|
||||
|
|
@ -223,6 +227,19 @@ class GigaChatConfig(BaseConfig):
|
|||
]
|
||||
optional_params["function_call"] = {"name": schema_name}
|
||||
optional_params["_structured_output"] = True
|
||||
elif not _is_supported_response_format(value): # pyright: ignore[reportUnknownArgumentType] # value
|
||||
from litellm.utils import UnsupportedParamsError
|
||||
|
||||
if litellm.drop_params is not True and drop_params is not True:
|
||||
raise UnsupportedParamsError(
|
||||
status_code=400,
|
||||
message=(
|
||||
"GigaChat does not support response_format="
|
||||
f"{value!r}. Use response_format={{'type': 'json_schema', "
|
||||
"'json_schema': {...}}} instead, or set `drop_params=True` "
|
||||
"to drop it."
|
||||
),
|
||||
)
|
||||
|
||||
return optional_params
|
||||
|
||||
|
|
|
|||
|
|
@ -174,7 +174,6 @@ class TestGetSupportedOpenAiParams:
|
|||
"top_p",
|
||||
"max_tokens",
|
||||
"max_completion_tokens",
|
||||
"stop",
|
||||
"tools",
|
||||
"tool_choice",
|
||||
"functions",
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import importlib
|
|||
import pytest
|
||||
|
||||
import litellm
|
||||
from litellm.utils import UnsupportedParamsError, get_optional_params
|
||||
from tests._vcr_conftest_common import install_live_call_probe, record_vcr_outcome
|
||||
|
||||
|
||||
|
|
@ -195,6 +196,69 @@ class TestGigaChatParamsTransformation:
|
|||
|
||||
return GigaChatConfig()
|
||||
|
||||
|
||||
def test_stop_raises_unsupported_params_error(self):
|
||||
with pytest.raises(UnsupportedParamsError):
|
||||
get_optional_params(
|
||||
model="GigaChat-Pro",
|
||||
custom_llm_provider="gigachat",
|
||||
stop=["\n\n"],
|
||||
request_timeout=10,
|
||||
num_retries=0,
|
||||
)
|
||||
|
||||
def test_stop_dropped_with_drop_params_true(self):
|
||||
result = get_optional_params(
|
||||
model="GigaChat-Pro",
|
||||
custom_llm_provider="gigachat",
|
||||
stop=["\n\n"],
|
||||
request_timeout=10,
|
||||
num_retries=0,
|
||||
drop_params=True,
|
||||
)
|
||||
assert "stop" not in result
|
||||
|
||||
def test_response_format_json_object_raises_unsupported(self, config):
|
||||
with pytest.raises(UnsupportedParamsError):
|
||||
config.map_openai_params(
|
||||
non_default_params={"response_format": {"type": "json_object"}},
|
||||
optional_params={},
|
||||
model="GigaChat",
|
||||
drop_params=False,
|
||||
)
|
||||
|
||||
def test_response_format_json_object_dropped_with_drop_params(self, config):
|
||||
result = config.map_openai_params(
|
||||
non_default_params={"response_format": {"type": "json_object"}},
|
||||
optional_params={},
|
||||
model="GigaChat",
|
||||
drop_params=True,
|
||||
)
|
||||
assert "functions" not in result
|
||||
assert "function_call" not in result
|
||||
|
||||
def test_response_format_json_object_dropped_with_global_drop_params(self, config, monkeypatch):
|
||||
monkeypatch.setattr(litellm, "drop_params", True, raising=False)
|
||||
result = config.map_openai_params(
|
||||
non_default_params={"response_format": {"type": "json_object"}},
|
||||
optional_params={},
|
||||
model="GigaChat",
|
||||
drop_params=False,
|
||||
)
|
||||
assert "functions" not in result
|
||||
assert "function_call" not in result
|
||||
|
||||
def test_response_format_json_object_raises_when_global_drop_params_off(self, config, monkeypatch):
|
||||
monkeypatch.setattr(litellm, "drop_params", False, raising=False)
|
||||
with pytest.raises(UnsupportedParamsError):
|
||||
config.map_openai_params(
|
||||
non_default_params={"response_format": {"type": "json_object"}},
|
||||
optional_params={},
|
||||
model="GigaChat",
|
||||
drop_params=False,
|
||||
)
|
||||
|
||||
|
||||
def test_temperature_zero_becomes_top_p_zero(self, config):
|
||||
"""temperature=0 should become top_p=0"""
|
||||
params = {"temperature": 0}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue