mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-25 18:54:30 +00:00
This commit is contained in:
parent
bdad9961e3
commit
d640bc0a00
6 changed files with 135 additions and 14 deletions
|
@ -1,8 +1,10 @@
|
|||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
import respx
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
sys.path.insert(
|
||||
|
@ -259,3 +261,84 @@ def test_bedrock_latency_optimized_inference():
|
|||
mock_post.assert_called_once()
|
||||
json_data = json.loads(mock_post.call_args.kwargs["data"])
|
||||
assert json_data["performanceConfig"]["latency"] == "optimized"
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def set_openrouter_api_key():
|
||||
original_api_key = os.environ.get("OPENROUTER_API_KEY")
|
||||
os.environ["OPENROUTER_API_KEY"] = "fake-key-for-testing"
|
||||
yield
|
||||
if original_api_key is not None:
|
||||
os.environ["OPENROUTER_API_KEY"] = original_api_key
|
||||
else:
|
||||
del os.environ["OPENROUTER_API_KEY"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_extra_body_with_fallback(respx_mock: respx.MockRouter, set_openrouter_api_key):
|
||||
"""
|
||||
test regression for https://github.com/BerriAI/litellm/issues/8425.
|
||||
|
||||
This was perhaps a wider issue with the acompletion function not passing kwargs such as extra_body correctly when fallbacks are specified.
|
||||
"""
|
||||
# Set up test parameters
|
||||
model = "openrouter/deepseek/deepseek-chat"
|
||||
messages = [{"role": "user", "content": "Hello, world!"}]
|
||||
extra_body = {
|
||||
"provider": {
|
||||
"order": ["DeepSeek"],
|
||||
"allow_fallbacks": False,
|
||||
"require_parameters": True
|
||||
}
|
||||
}
|
||||
fallbacks = [
|
||||
{
|
||||
"model": "openrouter/google/gemini-flash-1.5-8b"
|
||||
}
|
||||
]
|
||||
|
||||
respx_mock.post("https://openrouter.ai/api/v1/chat/completions").respond(
|
||||
json={
|
||||
"id": "chatcmpl-123",
|
||||
"object": "chat.completion",
|
||||
"created": 1677652288,
|
||||
"model": model,
|
||||
"choices": [
|
||||
{
|
||||
"index": 0,
|
||||
"message": {
|
||||
"role": "assistant",
|
||||
"content": "Hello from mocked response!",
|
||||
},
|
||||
"finish_reason": "stop",
|
||||
}
|
||||
],
|
||||
"usage": {"prompt_tokens": 9, "completion_tokens": 12, "total_tokens": 21},
|
||||
}
|
||||
)
|
||||
|
||||
response = await litellm.acompletion(
|
||||
model=model,
|
||||
messages=messages,
|
||||
extra_body=extra_body,
|
||||
fallbacks=fallbacks,
|
||||
api_key="fake-openrouter-api-key",
|
||||
)
|
||||
|
||||
# Get the request from the mock
|
||||
request: httpx.Request = respx_mock.calls[0].request
|
||||
request_body = request.read()
|
||||
request_body = json.loads(request_body)
|
||||
|
||||
# Verify basic parameters
|
||||
assert request_body["model"] == "deepseek/deepseek-chat"
|
||||
assert request_body["messages"] == messages
|
||||
|
||||
# Verify the extra_body parameters remain under the provider key
|
||||
assert request_body["provider"]["order"] == ["DeepSeek"]
|
||||
assert request_body["provider"]["allow_fallbacks"] is False
|
||||
assert request_body["provider"]["require_parameters"] is True
|
||||
|
||||
# Verify the response
|
||||
assert response is not None
|
||||
assert response.choices[0].message.content == "Hello from mocked response!"
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue