mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
Merge 23f306c9e7 into 8b1910c95a
This commit is contained in:
commit
9eec930a89
2 changed files with 46 additions and 1 deletions
|
|
@ -45,6 +45,13 @@ def _thought_signature(provider_specific_fields: object) -> str | None:
|
|||
return signature if isinstance(signature, str) else None
|
||||
|
||||
|
||||
def _matched_stop_sequence(provider_specific_fields: object) -> str | None:
|
||||
"""The stop string vLLM reports in a choice's ``stop_reason``; an int there is a stop token id, not a sequence."""
|
||||
fields: Final = _as_string_mapping(provider_specific_fields)
|
||||
stop_reason: Final = fields.get("stop_reason") if fields is not None else None
|
||||
return stop_reason if isinstance(stop_reason, str) and stop_reason else None
|
||||
|
||||
|
||||
def _compaction_blocks(provider_specific_fields: object) -> tuple[Mapping[str, object], ...]:
|
||||
fields: Final = _as_string_mapping(provider_specific_fields)
|
||||
raw_blocks: Final = fields.get("compaction_blocks") if fields is not None else None
|
||||
|
|
@ -1592,11 +1599,18 @@ class LiteLLMAnthropicMessagesAdapter:
|
|||
translated_finish_reason: Final = self._translate_openai_finish_reason_to_anthropic(
|
||||
openai_finish_reason=openai_finish_reason
|
||||
)
|
||||
matched_stop_sequence: Final = (
|
||||
_matched_stop_sequence(_optional_attr(response.choices[0], "provider_specific_fields"))
|
||||
if response.choices and openai_finish_reason == "stop"
|
||||
else None
|
||||
)
|
||||
anthropic_finish_reason: Final = (
|
||||
"compaction"
|
||||
if len(anthropic_content) == 1 and anthropic_content[0].get("type") == "compaction"
|
||||
else "refusal"
|
||||
if refusal_text is not None and translated_finish_reason != "max_tokens"
|
||||
else "stop_sequence"
|
||||
if matched_stop_sequence is not None
|
||||
else translated_finish_reason
|
||||
)
|
||||
# extract usage
|
||||
|
|
@ -1628,7 +1642,7 @@ class LiteLLMAnthropicMessagesAdapter:
|
|||
type="message",
|
||||
role="assistant",
|
||||
model=response.model or "unknown-model",
|
||||
stop_sequence=None,
|
||||
stop_sequence=matched_stop_sequence if anthropic_finish_reason == "stop_sequence" else None,
|
||||
usage=anthropic_usage,
|
||||
content=anthropic_content,
|
||||
stop_reason=anthropic_finish_reason,
|
||||
|
|
|
|||
|
|
@ -165,6 +165,37 @@ def test_translate_chat_refusal_finish_reason_to_anthropic_response():
|
|||
assert result["stop_reason"] == "refusal"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("provider_specific_fields", "expected_stop_reason", "expected_stop_sequence"),
|
||||
[
|
||||
({"stop_reason": ", 7"}, "stop_sequence", ", 7"),
|
||||
({"stop_reason": 151645}, "end_turn", None),
|
||||
(None, "end_turn", None),
|
||||
],
|
||||
ids=["matched_stop_string", "stop_token_id", "no_stop_reason"],
|
||||
)
|
||||
def test_translate_vllm_matched_stop_string_to_anthropic_stop_sequence(
|
||||
provider_specific_fields: dict[str, object] | None,
|
||||
expected_stop_reason: str,
|
||||
expected_stop_sequence: str | None,
|
||||
) -> None:
|
||||
response: Final = ModelResponse(
|
||||
choices=[
|
||||
Choices(
|
||||
finish_reason="stop",
|
||||
message=Message(content="1, 2, 3, 4, 5, 6", role="assistant"),
|
||||
provider_specific_fields=provider_specific_fields,
|
||||
)
|
||||
],
|
||||
usage=Usage(prompt_tokens=1, completion_tokens=1, total_tokens=2),
|
||||
)
|
||||
|
||||
result: Final = LiteLLMAnthropicMessagesAdapter().translate_openai_response_to_anthropic(response)
|
||||
|
||||
assert result["stop_reason"] == expected_stop_reason
|
||||
assert result["stop_sequence"] == expected_stop_sequence
|
||||
|
||||
|
||||
def test_translate_streaming_openai_chunk_to_anthropic_content_block():
|
||||
choices = [
|
||||
StreamingChoices(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue