(test) custom logger - check kwargs passed all the way

This commit is contained in:
ishaan-jaff 2023-12-06 18:08:46 -08:00
parent 0295509b3b
commit 1b43c7120c

View file

@ -15,6 +15,12 @@ class MyCustomHandler(CustomLogger):
self.async_success: bool = False
self.async_failure: bool = False
self.async_completion_kwargs = None # test if the kwargs are available, for async_succcess for completion/embedding
self.async_embedding_kwargs = None
self.async_completion_kwargs_fail = None
self.async_embedding_kwargs_fail = None
def log_pre_api_call(self, model, messages, kwargs):
print(f"Pre-API Call")
@ -36,11 +42,15 @@ class MyCustomHandler(CustomLogger):
print(f"On Async success")
self.async_success = True
print("Value of async success: ", self.async_success)
print("\n kwargs: ", kwargs)
self.async_completion_kwargs = kwargs
async def async_log_failure_event(self, kwargs, response_obj, start_time, end_time):
print(f"On Async Failure")
self.async_failure = True
print("Value of async failure: ", self.async_failure)
print("\n kwargs: ", kwargs)
self.async_completion_kwargs_fail = kwargs
async def async_test_logging_fn(kwargs, completion_obj, start_time, end_time):
@ -107,15 +117,18 @@ def test_async_custom_handler():
async def test_1():
try:
response = await litellm.acompletion(
model="gpt-3.5",
messages=messages
model="gpt-3.5-turbo",
messages=messages,
api_key="test",
)
except:
pass
assert customHandler2.async_failure == False
asyncio.run(test_1())
assert customHandler2.async_failure == True, "async failure is not set to True even after failure"
assert customHandler2.async_failure == True, "async failure is not set to True even after failure"
assert customHandler2.async_completion_kwargs_fail.get("model") == "gpt-3.5-turbo"
assert len(str(customHandler2.async_completion_kwargs_fail.get("exception"))) > 10 # exppect APIError("OpenAIException - Error code: 401 - {'error': {'message': 'Incorrect API key provided: test. You can find your API key at https://platform.openai.com/account/api-keys.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}"), 'traceback_exception': 'Traceback (most recent call last):\n File "/Users/ishaanjaffer/Github/litellm/litellm/llms/openai.py", line 269, in acompletion\n response = await openai_aclient.chat.completions.create(**data)\n File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/openai/resources/chat/completions.py", line 119
print("Passed setting async failure")
async def test_2():
@ -127,7 +140,9 @@ def test_async_custom_handler():
assert customHandler2.async_success == False
asyncio.run(test_2())
assert customHandler2.async_success == True, "async success is not set to True even after success"
assert customHandler2.async_completion_kwargs.get("model") == "gpt-3.5-turbo"
print("Passed setting async success")
except Exception as e:
pytest.fail(f"Error occurred: {e}")
# test_async_custom_handler()
test_async_custom_handler()