|
|
|
|
|
|
|
import sys, os, time |
|
import traceback, asyncio |
|
import pytest |
|
sys.path.insert( |
|
0, os.path.abspath("../..") |
|
) |
|
import litellm |
|
from litellm import Router |
|
import concurrent |
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
|
|
model_list = [{ |
|
"model_name": "gpt-3.5-turbo", |
|
"litellm_params": { |
|
"model": "azure/chatgpt-v-2", |
|
"api_key": "bad-key", |
|
"api_version": os.getenv("AZURE_API_VERSION"), |
|
"api_base": os.getenv("AZURE_API_BASE") |
|
}, |
|
"tpm": 240000, |
|
"rpm": 1800, |
|
}, |
|
{ |
|
"model_name": "gpt-3.5-turbo", |
|
"litellm_params": { |
|
"model": "gpt-3.5-turbo", |
|
"api_key": os.getenv("OPENAI_API_KEY"), |
|
}, |
|
"tpm": 1000000, |
|
"rpm": 9000 |
|
} |
|
] |
|
|
|
kwargs = {"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Hey, how's it going?"}],} |
|
|
|
|
|
def test_multiple_deployments_sync(): |
|
import concurrent, time |
|
litellm.set_verbose=False |
|
results = [] |
|
router = Router(model_list=model_list, |
|
redis_host=os.getenv("REDIS_HOST"), |
|
redis_password=os.getenv("REDIS_PASSWORD"), |
|
redis_port=int(os.getenv("REDIS_PORT")), |
|
routing_strategy="simple-shuffle", |
|
set_verbose=True, |
|
num_retries=1) |
|
try: |
|
for _ in range(3): |
|
response = router.completion(**kwargs) |
|
results.append(response) |
|
print(results) |
|
router.reset() |
|
except Exception as e: |
|
print(f"FAILED TEST!") |
|
pytest.fail(f"An error occurred - {traceback.format_exc()}") |
|
|
|
|
|
|
|
|
|
def test_multiple_deployments_parallel(): |
|
litellm.set_verbose = False |
|
results = [] |
|
futures = {} |
|
start_time = time.time() |
|
router = Router(model_list=model_list, |
|
redis_host=os.getenv("REDIS_HOST"), |
|
redis_password=os.getenv("REDIS_PASSWORD"), |
|
redis_port=int(os.getenv("REDIS_PORT")), |
|
routing_strategy="simple-shuffle", |
|
set_verbose=True, |
|
num_retries=1) |
|
|
|
with concurrent.futures.ThreadPoolExecutor() as executor: |
|
for _ in range(5): |
|
future = executor.submit(router.completion, **kwargs) |
|
futures[future] = future |
|
|
|
|
|
while futures: |
|
done, not_done = concurrent.futures.wait(futures.values(), timeout=10, return_when=concurrent.futures.FIRST_COMPLETED) |
|
for future in done: |
|
try: |
|
result = future.result() |
|
results.append(result) |
|
del futures[future] |
|
except Exception as e: |
|
print(f"Exception: {e}; traceback: {traceback.format_exc()}") |
|
del futures[future] |
|
|
|
print(f"Remaining futures: {len(futures)}") |
|
router.reset() |
|
end_time = time.time() |
|
print(results) |
|
print(f"ELAPSED TIME: {end_time - start_time}") |
|
|
|
|
|
|
|
|
|
def test_cooldown_same_model_name(): |
|
|
|
|
|
|
|
|
|
|
|
litellm.set_verbose = False |
|
try: |
|
print("testing cooldown same model name") |
|
model_list = [ |
|
{ |
|
"model_name": "gpt-3.5-turbo", |
|
"litellm_params": { |
|
"model": "azure/chatgpt-v-2", |
|
"api_key": os.getenv("AZURE_API_KEY"), |
|
"api_version": os.getenv("AZURE_API_VERSION"), |
|
"api_base": "BAD_API_BASE", |
|
}, |
|
}, |
|
{ |
|
"model_name": "gpt-3.5-turbo", |
|
"litellm_params": { |
|
"model": "azure/chatgpt-v-2", |
|
"api_key": os.getenv("AZURE_API_KEY"), |
|
"api_version": os.getenv("AZURE_API_VERSION"), |
|
"api_base": os.getenv("AZURE_API_BASE") |
|
}, |
|
}, |
|
] |
|
|
|
router = Router( |
|
model_list=model_list, |
|
redis_host=os.getenv("REDIS_HOST"), |
|
redis_password=os.getenv("REDIS_PASSWORD"), |
|
redis_port=int(os.getenv("REDIS_PORT")), |
|
routing_strategy="simple-shuffle", |
|
set_verbose=True, |
|
num_retries=3 |
|
) |
|
|
|
response = router.completion( |
|
model="gpt-3.5-turbo", |
|
messages=[ |
|
{ |
|
"role": "user", |
|
"content": "hello this request will pass" |
|
} |
|
] |
|
) |
|
print(router.model_list) |
|
litellm_model_names = [] |
|
for model in router.model_list: |
|
litellm_model_names.append(model["litellm_params"]["model"]) |
|
print("\n litellm model names ", litellm_model_names) |
|
|
|
|
|
assert litellm_model_names[0] != litellm_model_names[1] |
|
print("\ngot response\n", response) |
|
except Exception as e: |
|
pytest.fail(f"Got unexpected exception on router! - {e}") |
|
|
|
test_cooldown_same_model_name() |
|
|