|
|
|
|
|
import sys, os, time, inspect, asyncio, traceback |
|
from datetime import datetime |
|
import pytest |
|
|
|
sys.path.insert(0, os.path.abspath("../..")) |
|
from typing import Optional, Literal, List |
|
from litellm import Router, Cache |
|
import litellm |
|
from litellm.integrations.custom_logger import CustomLogger |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CompletionCustomHandler( |
|
CustomLogger |
|
): |
|
""" |
|
The set of expected inputs to a custom handler for a |
|
""" |
|
|
|
|
|
def __init__(self): |
|
self.errors = [] |
|
self.states: Optional[ |
|
List[ |
|
Literal[ |
|
"sync_pre_api_call", |
|
"async_pre_api_call", |
|
"post_api_call", |
|
"sync_stream", |
|
"async_stream", |
|
"sync_success", |
|
"async_success", |
|
"sync_failure", |
|
"async_failure", |
|
] |
|
] |
|
] = [] |
|
|
|
def log_pre_api_call(self, model, messages, kwargs): |
|
try: |
|
print(f"received kwargs in pre-input: {kwargs}") |
|
self.states.append("sync_pre_api_call") |
|
|
|
assert isinstance(model, str) |
|
|
|
assert isinstance(messages, list) |
|
|
|
assert isinstance(kwargs["model"], str) |
|
assert isinstance(kwargs["messages"], list) |
|
assert isinstance(kwargs["optional_params"], dict) |
|
assert isinstance(kwargs["litellm_params"], dict) |
|
assert isinstance(kwargs["start_time"], (datetime, type(None))) |
|
assert isinstance(kwargs["stream"], bool) |
|
assert isinstance(kwargs["user"], (str, type(None))) |
|
|
|
assert isinstance(kwargs["litellm_params"]["metadata"], dict) |
|
assert isinstance(kwargs["litellm_params"]["metadata"]["model_group"], str) |
|
assert isinstance(kwargs["litellm_params"]["metadata"]["deployment"], str) |
|
assert isinstance(kwargs["litellm_params"]["model_info"], dict) |
|
assert isinstance(kwargs["litellm_params"]["model_info"]["id"], str) |
|
assert isinstance( |
|
kwargs["litellm_params"]["proxy_server_request"], (str, type(None)) |
|
) |
|
assert isinstance( |
|
kwargs["litellm_params"]["preset_cache_key"], (str, type(None)) |
|
) |
|
assert isinstance(kwargs["litellm_params"]["stream_response"], dict) |
|
except Exception as e: |
|
print(f"Assertion Error: {traceback.format_exc()}") |
|
self.errors.append(traceback.format_exc()) |
|
|
|
def log_post_api_call(self, kwargs, response_obj, start_time, end_time): |
|
try: |
|
self.states.append("post_api_call") |
|
|
|
assert isinstance(start_time, datetime) |
|
|
|
assert end_time == None |
|
|
|
assert response_obj == None |
|
|
|
assert isinstance(kwargs["model"], str) |
|
assert isinstance(kwargs["messages"], list) |
|
assert isinstance(kwargs["optional_params"], dict) |
|
assert isinstance(kwargs["litellm_params"], dict) |
|
assert isinstance(kwargs["start_time"], (datetime, type(None))) |
|
assert isinstance(kwargs["stream"], bool) |
|
assert isinstance(kwargs["user"], (str, type(None))) |
|
assert isinstance(kwargs["input"], (list, dict, str)) |
|
assert isinstance(kwargs["api_key"], (str, type(None))) |
|
assert ( |
|
isinstance( |
|
kwargs["original_response"], (str, litellm.CustomStreamWrapper) |
|
) |
|
or inspect.iscoroutine(kwargs["original_response"]) |
|
or inspect.isasyncgen(kwargs["original_response"]) |
|
) |
|
assert isinstance(kwargs["additional_args"], (dict, type(None))) |
|
assert isinstance(kwargs["log_event_type"], str) |
|
|
|
assert isinstance(kwargs["litellm_params"]["metadata"], dict) |
|
assert isinstance(kwargs["litellm_params"]["metadata"]["model_group"], str) |
|
assert isinstance(kwargs["litellm_params"]["metadata"]["deployment"], str) |
|
assert isinstance(kwargs["litellm_params"]["model_info"], dict) |
|
assert isinstance(kwargs["litellm_params"]["model_info"]["id"], str) |
|
assert isinstance( |
|
kwargs["litellm_params"]["proxy_server_request"], (str, type(None)) |
|
) |
|
assert isinstance( |
|
kwargs["litellm_params"]["preset_cache_key"], (str, type(None)) |
|
) |
|
assert isinstance(kwargs["litellm_params"]["stream_response"], dict) |
|
except: |
|
print(f"Assertion Error: {traceback.format_exc()}") |
|
self.errors.append(traceback.format_exc()) |
|
|
|
async def async_log_stream_event(self, kwargs, response_obj, start_time, end_time): |
|
try: |
|
self.states.append("async_stream") |
|
|
|
assert isinstance(start_time, datetime) |
|
|
|
assert isinstance(end_time, datetime) |
|
|
|
assert isinstance(response_obj, litellm.ModelResponse) |
|
|
|
assert isinstance(kwargs["model"], str) |
|
assert isinstance(kwargs["messages"], list) and isinstance( |
|
kwargs["messages"][0], dict |
|
) |
|
assert isinstance(kwargs["optional_params"], dict) |
|
assert isinstance(kwargs["litellm_params"], dict) |
|
assert isinstance(kwargs["start_time"], (datetime, type(None))) |
|
assert isinstance(kwargs["stream"], bool) |
|
assert isinstance(kwargs["user"], (str, type(None))) |
|
assert ( |
|
isinstance(kwargs["input"], list) |
|
and isinstance(kwargs["input"][0], dict) |
|
) or isinstance(kwargs["input"], (dict, str)) |
|
assert isinstance(kwargs["api_key"], (str, type(None))) |
|
assert ( |
|
isinstance( |
|
kwargs["original_response"], (str, litellm.CustomStreamWrapper) |
|
) |
|
or inspect.isasyncgen(kwargs["original_response"]) |
|
or inspect.iscoroutine(kwargs["original_response"]) |
|
) |
|
assert isinstance(kwargs["additional_args"], (dict, type(None))) |
|
assert isinstance(kwargs["log_event_type"], str) |
|
except: |
|
print(f"Assertion Error: {traceback.format_exc()}") |
|
self.errors.append(traceback.format_exc()) |
|
|
|
def log_success_event(self, kwargs, response_obj, start_time, end_time): |
|
try: |
|
self.states.append("sync_success") |
|
|
|
assert isinstance(start_time, datetime) |
|
|
|
assert isinstance(end_time, datetime) |
|
|
|
assert isinstance(response_obj, litellm.ModelResponse) |
|
|
|
assert isinstance(kwargs["model"], str) |
|
assert isinstance(kwargs["messages"], list) and isinstance( |
|
kwargs["messages"][0], dict |
|
) |
|
assert isinstance(kwargs["optional_params"], dict) |
|
assert isinstance(kwargs["litellm_params"], dict) |
|
assert isinstance(kwargs["start_time"], (datetime, type(None))) |
|
assert isinstance(kwargs["stream"], bool) |
|
assert isinstance(kwargs["user"], (str, type(None))) |
|
assert ( |
|
isinstance(kwargs["input"], list) |
|
and isinstance(kwargs["input"][0], dict) |
|
) or isinstance(kwargs["input"], (dict, str)) |
|
assert isinstance(kwargs["api_key"], (str, type(None))) |
|
assert isinstance( |
|
kwargs["original_response"], (str, litellm.CustomStreamWrapper) |
|
) |
|
assert isinstance(kwargs["additional_args"], (dict, type(None))) |
|
assert isinstance(kwargs["log_event_type"], str) |
|
assert kwargs["cache_hit"] is None or isinstance(kwargs["cache_hit"], bool) |
|
except: |
|
print(f"Assertion Error: {traceback.format_exc()}") |
|
self.errors.append(traceback.format_exc()) |
|
|
|
def log_failure_event(self, kwargs, response_obj, start_time, end_time): |
|
try: |
|
self.states.append("sync_failure") |
|
|
|
assert isinstance(start_time, datetime) |
|
|
|
assert isinstance(end_time, datetime) |
|
|
|
assert response_obj == None |
|
|
|
assert isinstance(kwargs["model"], str) |
|
assert isinstance(kwargs["messages"], list) and isinstance( |
|
kwargs["messages"][0], dict |
|
) |
|
assert isinstance(kwargs["optional_params"], dict) |
|
assert isinstance(kwargs["litellm_params"], dict) |
|
assert isinstance(kwargs["start_time"], (datetime, type(None))) |
|
assert isinstance(kwargs["stream"], bool) |
|
assert isinstance(kwargs["user"], (str, type(None))) |
|
assert ( |
|
isinstance(kwargs["input"], list) |
|
and isinstance(kwargs["input"][0], dict) |
|
) or isinstance(kwargs["input"], (dict, str)) |
|
assert isinstance(kwargs["api_key"], (str, type(None))) |
|
assert ( |
|
isinstance( |
|
kwargs["original_response"], (str, litellm.CustomStreamWrapper) |
|
) |
|
or kwargs["original_response"] == None |
|
) |
|
assert isinstance(kwargs["additional_args"], (dict, type(None))) |
|
assert isinstance(kwargs["log_event_type"], str) |
|
except: |
|
print(f"Assertion Error: {traceback.format_exc()}") |
|
self.errors.append(traceback.format_exc()) |
|
|
|
async def async_log_pre_api_call(self, model, messages, kwargs): |
|
try: |
|
""" |
|
No-op. |
|
Not implemented yet. |
|
""" |
|
pass |
|
except Exception as e: |
|
print(f"Assertion Error: {traceback.format_exc()}") |
|
self.errors.append(traceback.format_exc()) |
|
|
|
async def async_log_success_event(self, kwargs, response_obj, start_time, end_time): |
|
try: |
|
self.states.append("async_success") |
|
|
|
assert isinstance(start_time, datetime) |
|
|
|
assert isinstance(end_time, datetime) |
|
|
|
assert isinstance( |
|
response_obj, (litellm.ModelResponse, litellm.EmbeddingResponse) |
|
) |
|
|
|
assert isinstance(kwargs["model"], str) |
|
assert isinstance(kwargs["messages"], list) |
|
assert isinstance(kwargs["optional_params"], dict) |
|
assert isinstance(kwargs["litellm_params"], dict) |
|
assert isinstance(kwargs["start_time"], (datetime, type(None))) |
|
assert isinstance(kwargs["stream"], bool) |
|
assert isinstance(kwargs["user"], (str, type(None))) |
|
assert isinstance(kwargs["input"], (list, dict, str)) |
|
assert isinstance(kwargs["api_key"], (str, type(None))) |
|
assert ( |
|
isinstance( |
|
kwargs["original_response"], (str, litellm.CustomStreamWrapper) |
|
) |
|
or inspect.isasyncgen(kwargs["original_response"]) |
|
or inspect.iscoroutine(kwargs["original_response"]) |
|
) |
|
assert isinstance(kwargs["additional_args"], (dict, type(None))) |
|
assert isinstance(kwargs["log_event_type"], str) |
|
assert kwargs["cache_hit"] is None or isinstance(kwargs["cache_hit"], bool) |
|
|
|
assert isinstance(kwargs["litellm_params"]["metadata"], dict) |
|
assert isinstance(kwargs["litellm_params"]["metadata"]["model_group"], str) |
|
assert isinstance(kwargs["litellm_params"]["metadata"]["deployment"], str) |
|
assert isinstance(kwargs["litellm_params"]["model_info"], dict) |
|
assert isinstance(kwargs["litellm_params"]["model_info"]["id"], str) |
|
assert isinstance( |
|
kwargs["litellm_params"]["proxy_server_request"], (str, type(None)) |
|
) |
|
assert isinstance( |
|
kwargs["litellm_params"]["preset_cache_key"], (str, type(None)) |
|
) |
|
assert isinstance(kwargs["litellm_params"]["stream_response"], dict) |
|
except: |
|
print(f"Assertion Error: {traceback.format_exc()}") |
|
self.errors.append(traceback.format_exc()) |
|
|
|
async def async_log_failure_event(self, kwargs, response_obj, start_time, end_time): |
|
try: |
|
print(f"received original response: {kwargs['original_response']}") |
|
self.states.append("async_failure") |
|
|
|
assert isinstance(start_time, datetime) |
|
|
|
assert isinstance(end_time, datetime) |
|
|
|
assert response_obj == None |
|
|
|
assert isinstance(kwargs["model"], str) |
|
assert isinstance(kwargs["messages"], list) |
|
assert isinstance(kwargs["optional_params"], dict) |
|
assert isinstance(kwargs["litellm_params"], dict) |
|
assert isinstance(kwargs["start_time"], (datetime, type(None))) |
|
assert isinstance(kwargs["stream"], bool) |
|
assert isinstance(kwargs["user"], (str, type(None))) |
|
assert isinstance(kwargs["input"], (list, str, dict)) |
|
assert isinstance(kwargs["api_key"], (str, type(None))) |
|
assert ( |
|
isinstance( |
|
kwargs["original_response"], (str, litellm.CustomStreamWrapper) |
|
) |
|
or inspect.isasyncgen(kwargs["original_response"]) |
|
or inspect.iscoroutine(kwargs["original_response"]) |
|
or kwargs["original_response"] == None |
|
) |
|
assert isinstance(kwargs["additional_args"], (dict, type(None))) |
|
assert isinstance(kwargs["log_event_type"], str) |
|
except: |
|
print(f"Assertion Error: {traceback.format_exc()}") |
|
self.errors.append(traceback.format_exc()) |
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio |
|
async def test_async_chat_azure(): |
|
try: |
|
customHandler_completion_azure_router = CompletionCustomHandler() |
|
customHandler_streaming_azure_router = CompletionCustomHandler() |
|
customHandler_failure = CompletionCustomHandler() |
|
litellm.callbacks = [customHandler_completion_azure_router] |
|
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": os.getenv("AZURE_API_BASE"), |
|
}, |
|
"tpm": 240000, |
|
"rpm": 1800, |
|
}, |
|
] |
|
router = Router(model_list=model_list) |
|
response = await router.acompletion( |
|
model="gpt-3.5-turbo", |
|
messages=[{"role": "user", "content": "Hi π - i'm openai"}], |
|
) |
|
await asyncio.sleep(2) |
|
assert len(customHandler_completion_azure_router.errors) == 0 |
|
assert ( |
|
len(customHandler_completion_azure_router.states) == 3 |
|
) |
|
|
|
litellm.callbacks = [customHandler_streaming_azure_router] |
|
router2 = Router(model_list=model_list) |
|
response = await router2.acompletion( |
|
model="gpt-3.5-turbo", |
|
messages=[{"role": "user", "content": "Hi π - i'm openai"}], |
|
stream=True, |
|
) |
|
async for chunk in response: |
|
print(f"async azure router chunk: {chunk}") |
|
continue |
|
await asyncio.sleep(1) |
|
print(f"customHandler.states: {customHandler_streaming_azure_router.states}") |
|
assert len(customHandler_streaming_azure_router.errors) == 0 |
|
assert ( |
|
len(customHandler_streaming_azure_router.states) >= 4 |
|
) |
|
|
|
model_list = [ |
|
{ |
|
"model_name": "gpt-3.5-turbo", |
|
"litellm_params": { |
|
"model": "azure/chatgpt-v-2", |
|
"api_key": "my-bad-key", |
|
"api_version": os.getenv("AZURE_API_VERSION"), |
|
"api_base": os.getenv("AZURE_API_BASE"), |
|
}, |
|
"tpm": 240000, |
|
"rpm": 1800, |
|
}, |
|
] |
|
litellm.callbacks = [customHandler_failure] |
|
router3 = Router(model_list=model_list) |
|
try: |
|
response = await router3.acompletion( |
|
model="gpt-3.5-turbo", |
|
messages=[{"role": "user", "content": "Hi π - i'm openai"}], |
|
) |
|
print(f"response in router3 acompletion: {response}") |
|
except: |
|
pass |
|
await asyncio.sleep(1) |
|
print(f"customHandler.states: {customHandler_failure.states}") |
|
assert len(customHandler_failure.errors) == 0 |
|
assert len(customHandler_failure.states) == 3 |
|
assert "async_failure" in customHandler_failure.states |
|
except Exception as e: |
|
print(f"Assertion Error: {traceback.format_exc()}") |
|
pytest.fail(f"An exception occurred - {str(e)}") |
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio |
|
async def test_async_embedding_azure(): |
|
try: |
|
customHandler = CompletionCustomHandler() |
|
customHandler_failure = CompletionCustomHandler() |
|
litellm.callbacks = [customHandler] |
|
model_list = [ |
|
{ |
|
"model_name": "azure-embedding-model", |
|
"litellm_params": { |
|
"model": "azure/azure-embedding-model", |
|
"api_key": os.getenv("AZURE_API_KEY"), |
|
"api_version": os.getenv("AZURE_API_VERSION"), |
|
"api_base": os.getenv("AZURE_API_BASE"), |
|
}, |
|
"tpm": 240000, |
|
"rpm": 1800, |
|
}, |
|
] |
|
router = Router(model_list=model_list) |
|
response = await router.aembedding( |
|
model="azure-embedding-model", input=["hello from litellm!"] |
|
) |
|
await asyncio.sleep(2) |
|
assert len(customHandler.errors) == 0 |
|
assert len(customHandler.states) == 3 |
|
|
|
model_list = [ |
|
{ |
|
"model_name": "azure-embedding-model", |
|
"litellm_params": { |
|
"model": "azure/azure-embedding-model", |
|
"api_key": "my-bad-key", |
|
"api_version": os.getenv("AZURE_API_VERSION"), |
|
"api_base": os.getenv("AZURE_API_BASE"), |
|
}, |
|
"tpm": 240000, |
|
"rpm": 1800, |
|
}, |
|
] |
|
litellm.callbacks = [customHandler_failure] |
|
router3 = Router(model_list=model_list) |
|
try: |
|
response = await router3.aembedding( |
|
model="azure-embedding-model", input=["hello from litellm!"] |
|
) |
|
print(f"response in router3 aembedding: {response}") |
|
except: |
|
pass |
|
await asyncio.sleep(1) |
|
print(f"customHandler.states: {customHandler_failure.states}") |
|
assert len(customHandler_failure.errors) == 0 |
|
assert len(customHandler_failure.states) == 3 |
|
assert "async_failure" in customHandler_failure.states |
|
except Exception as e: |
|
print(f"Assertion Error: {traceback.format_exc()}") |
|
pytest.fail(f"An exception occurred - {str(e)}") |
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio |
|
async def test_async_chat_azure_with_fallbacks(): |
|
try: |
|
customHandler_fallbacks = CompletionCustomHandler() |
|
litellm.callbacks = [customHandler_fallbacks] |
|
|
|
model_list = [ |
|
{ |
|
"model_name": "gpt-3.5-turbo", |
|
"litellm_params": { |
|
"model": "azure/chatgpt-v-2", |
|
"api_key": "my-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-16k", |
|
"litellm_params": { |
|
"model": "gpt-3.5-turbo-16k", |
|
}, |
|
"tpm": 240000, |
|
"rpm": 1800, |
|
}, |
|
] |
|
router = Router(model_list=model_list, fallbacks=[{"gpt-3.5-turbo": ["gpt-3.5-turbo-16k"]}]) |
|
response = await router.acompletion( |
|
model="gpt-3.5-turbo", |
|
messages=[{"role": "user", "content": "Hi π - i'm openai"}], |
|
) |
|
await asyncio.sleep(2) |
|
print(f"customHandler_fallbacks.states: {customHandler_fallbacks.states}") |
|
assert len(customHandler_fallbacks.errors) == 0 |
|
assert ( |
|
len(customHandler_fallbacks.states) == 6 |
|
) |
|
litellm.callbacks = [] |
|
except Exception as e: |
|
print(f"Assertion Error: {traceback.format_exc()}") |
|
pytest.fail(f"An exception occurred - {str(e)}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio |
|
async def test_async_completion_azure_caching(): |
|
customHandler_caching = CompletionCustomHandler() |
|
litellm.cache = Cache( |
|
type="redis", |
|
host=os.environ["REDIS_HOST"], |
|
port=os.environ["REDIS_PORT"], |
|
password=os.environ["REDIS_PASSWORD"], |
|
) |
|
litellm.callbacks = [customHandler_caching] |
|
unique_time = time.time() |
|
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": os.getenv("AZURE_API_BASE"), |
|
}, |
|
"tpm": 240000, |
|
"rpm": 1800, |
|
}, |
|
{ |
|
"model_name": "gpt-3.5-turbo-16k", |
|
"litellm_params": { |
|
"model": "gpt-3.5-turbo-16k", |
|
}, |
|
"tpm": 240000, |
|
"rpm": 1800, |
|
}, |
|
] |
|
router = Router(model_list=model_list) |
|
response1 = await router.acompletion( |
|
model="gpt-3.5-turbo", |
|
messages=[{"role": "user", "content": f"Hi π - i'm async azure {unique_time}"}], |
|
caching=True, |
|
) |
|
await asyncio.sleep(1) |
|
print(f"customHandler_caching.states pre-cache hit: {customHandler_caching.states}") |
|
response2 = await router.acompletion( |
|
model="gpt-3.5-turbo", |
|
messages=[{"role": "user", "content": f"Hi π - i'm async azure {unique_time}"}], |
|
caching=True, |
|
) |
|
await asyncio.sleep(1) |
|
print( |
|
f"customHandler_caching.states post-cache hit: {customHandler_caching.states}" |
|
) |
|
assert len(customHandler_caching.errors) == 0 |
|
assert len(customHandler_caching.states) == 4 |
|
|