mirror of
https://github.com/BerriAI/litellm.git
synced 2026-08-28 05:25:59 +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
202 lines
5.7 KiB
Python
202 lines
5.7 KiB
Python
#### What this tests ####
|
|
# This tests litellm router with batch completion
|
|
|
|
import asyncio
|
|
import time
|
|
import traceback
|
|
|
|
import openai
|
|
import pytest
|
|
|
|
from collections import defaultdict
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
import httpx
|
|
from dotenv import load_dotenv
|
|
|
|
import litellm
|
|
from litellm import Router
|
|
from litellm.router import Deployment, LiteLLM_Params
|
|
from litellm.types.router import ModelInfo
|
|
|
|
load_dotenv()
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["all_responses", "fastest_response"])
|
|
@pytest.mark.asyncio
|
|
async def test_batch_completion_multiple_models(mode):
|
|
litellm.set_verbose = True
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "gpt-3.5-turbo",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "groq-llama",
|
|
"litellm_params": {
|
|
"model": "groq/openai/gpt-oss-120b",
|
|
},
|
|
},
|
|
]
|
|
)
|
|
|
|
if mode == "all_responses":
|
|
response = await router.abatch_completion(
|
|
models=["gpt-3.5-turbo", "groq-llama"],
|
|
messages=[
|
|
{"role": "user", "content": "is litellm becoming a better product ?"}
|
|
],
|
|
max_tokens=15,
|
|
)
|
|
|
|
print(response)
|
|
assert len(response) == 2
|
|
|
|
models_in_responses = []
|
|
print(f"response: {response}")
|
|
for individual_response in response:
|
|
print(f"individual_response: {individual_response}")
|
|
_model = individual_response["model"]
|
|
models_in_responses.append(_model)
|
|
|
|
# assert both models are different
|
|
assert models_in_responses[0] != models_in_responses[1]
|
|
elif mode == "fastest_response":
|
|
from openai.types.chat.chat_completion import ChatCompletion
|
|
|
|
response = await router.abatch_completion_fastest_response(
|
|
model="gpt-3.5-turbo, groq-llama",
|
|
messages=[
|
|
{"role": "user", "content": "is litellm becoming a better product ?"}
|
|
],
|
|
max_tokens=15,
|
|
)
|
|
|
|
ChatCompletion.model_validate(response.model_dump(), strict=True)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_batch_completion_fastest_response_unit_test():
|
|
"""
|
|
Unit test to confirm fastest response will always return the response which arrives earliest.
|
|
|
|
2 models -> 1 is cached, the other is a real llm api call => assert cached response always returned
|
|
"""
|
|
litellm.set_verbose = True
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-4",
|
|
"litellm_params": {
|
|
"model": "gpt-4",
|
|
},
|
|
"model_info": {"id": "1"},
|
|
},
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "gpt-3.5-turbo",
|
|
"mock_response": "This is a fake response",
|
|
},
|
|
"model_info": {"id": "2"},
|
|
},
|
|
]
|
|
)
|
|
|
|
response = await router.abatch_completion_fastest_response(
|
|
model="gpt-4, gpt-3.5-turbo",
|
|
messages=[
|
|
{"role": "user", "content": "is litellm becoming a better product ?"}
|
|
],
|
|
max_tokens=500,
|
|
)
|
|
|
|
assert response._hidden_params["model_id"] == "2"
|
|
assert response.choices[0].message.content == "This is a fake response"
|
|
print(f"response: {response}")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_batch_completion_fastest_response_streaming():
|
|
litellm.set_verbose = True
|
|
litellm._turn_on_debug()
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "gpt-3.5-turbo",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "groq-llama",
|
|
"litellm_params": {
|
|
"model": "groq/openai/gpt-oss-120b",
|
|
},
|
|
},
|
|
]
|
|
)
|
|
|
|
from openai.types.chat.chat_completion_chunk import ChatCompletionChunk
|
|
|
|
response = await router.abatch_completion_fastest_response(
|
|
model="gpt-3.5-turbo, groq-llama",
|
|
messages=[
|
|
{"role": "user", "content": "is litellm becoming a better product ?"}
|
|
],
|
|
max_tokens=15,
|
|
stream=True,
|
|
)
|
|
|
|
async for chunk in response:
|
|
ChatCompletionChunk.model_validate(chunk.model_dump(), strict=True)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_batch_completion_multiple_models_multiple_messages():
|
|
litellm.set_verbose = True
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "gpt-3.5-turbo",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "groq-llama",
|
|
"litellm_params": {
|
|
"model": "groq/openai/gpt-oss-120b",
|
|
},
|
|
},
|
|
]
|
|
)
|
|
|
|
response = await router.abatch_completion(
|
|
models=["gpt-3.5-turbo", "groq-llama"],
|
|
messages=[
|
|
[{"role": "user", "content": "is litellm becoming a better product ?"}],
|
|
[{"role": "user", "content": "who is this"}],
|
|
],
|
|
max_tokens=15,
|
|
)
|
|
|
|
print("response from batches =", response)
|
|
assert len(response) == 2
|
|
assert len(response[0]) == 2
|
|
assert isinstance(response[0][0], litellm.ModelResponse)
|
|
|
|
# models_in_responses = []
|
|
# for individual_response in response:
|
|
# _model = individual_response["model"]
|
|
# models_in_responses.append(_model)
|
|
|
|
# # assert both models are different
|
|
# assert models_in_responses[0] != models_in_responses[1]
|