fix(utils.py): better exception raising if logging object is not able to get set

This commit is contained in:
Krrish Dholakia 2023-11-06 06:34:16 -08:00
parent 2014e69e99
commit c55db28b6f
3 changed files with 34 additions and 19 deletions

View file

@ -14,7 +14,7 @@ class LangFuseLogger:
try: try:
from langfuse import Langfuse from langfuse import Langfuse
except Exception as e: except Exception as e:
raise Exception("\033[91mLangfuse not installed, try running 'pip install langfuse' to fix this error\033[0m", e) raise Exception(f"\033[91mLangfuse not installed, try running 'pip install langfuse' to fix this error: {e}\033[0m")
# Instance variables # Instance variables
self.secret_key = os.getenv("LANGFUSE_SECRET_KEY") self.secret_key = os.getenv("LANGFUSE_SECRET_KEY")
self.public_key = os.getenv("LANGFUSE_PUBLIC_KEY") self.public_key = os.getenv("LANGFUSE_PUBLIC_KEY")

View file

@ -1,7 +1,8 @@
import sys import sys
import os import os
import io import io, asyncio
import logging
logging.basicConfig(level=logging.DEBUG)
sys.path.insert(0, os.path.abspath('../..')) sys.path.insert(0, os.path.abspath('../..'))
from litellm import completion from litellm import completion
@ -11,7 +12,18 @@ litellm.success_callback = ["langfuse"]
# litellm.set_verbose = True # litellm.set_verbose = True
import time import time
def test_langfuse_logging_async():
async def _test_langfuse():
await litellm.acompletion(
model="gpt-3.5-turbo",
prompt="This is a test",
max_tokens=1000,
temperature=0.7,
)
response = asyncio.run(_test_langfuse())
print(f"response: {response}")
test_langfuse_logging_async()
def test_langfuse_logging(): def test_langfuse_logging():
try: try:
@ -27,7 +39,7 @@ def test_langfuse_logging():
except Exception as e: except Exception as e:
print(e) print(e)
test_langfuse_logging() # test_langfuse_logging()
def test_langfuse_logging_custom_generation_name(): def test_langfuse_logging_custom_generation_name():
try: try:
@ -47,7 +59,7 @@ def test_langfuse_logging_custom_generation_name():
except Exception as e: except Exception as e:
print(e) print(e)
test_langfuse_logging_custom_generation_name() # test_langfuse_logging_custom_generation_name()
def test_langfuse_logging_function_calling(): def test_langfuse_logging_function_calling():
function1 = [ function1 = [
@ -80,7 +92,7 @@ def test_langfuse_logging_function_calling():
except Exception as e: except Exception as e:
print(e) print(e)
test_langfuse_logging_function_calling() # test_langfuse_logging_function_calling()

View file

@ -835,8 +835,9 @@ def client(original_function):
logging_obj = Logging(model=model, messages=messages, stream=stream, litellm_call_id=kwargs["litellm_call_id"], function_id=function_id, call_type=call_type, start_time=start_time) logging_obj = Logging(model=model, messages=messages, stream=stream, litellm_call_id=kwargs["litellm_call_id"], function_id=function_id, call_type=call_type, start_time=start_time)
return logging_obj return logging_obj
except Exception as e: # DO NOT BLOCK running the function because of this except Exception as e: # DO NOT BLOCK running the function because of this
print_verbose(f"[Non-Blocking] {traceback.format_exc()}; args - {args}; kwargs - {kwargs}") import logging
pass logging.debug(f"[Non-Blocking] {traceback.format_exc()}; args - {args}; kwargs - {kwargs}")
raise e
def crash_reporting(*args, **kwargs): def crash_reporting(*args, **kwargs):
if litellm.telemetry: if litellm.telemetry:
@ -860,6 +861,7 @@ def client(original_function):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
start_time = datetime.datetime.now() start_time = datetime.datetime.now()
result = None result = None
logging_obj = None
# only set litellm_call_id if its not in kwargs # only set litellm_call_id if its not in kwargs
if "litellm_call_id" not in kwargs: if "litellm_call_id" not in kwargs:
@ -948,6 +950,7 @@ def client(original_function):
crash_reporting(*args, **kwargs, exception=traceback_exception) crash_reporting(*args, **kwargs, exception=traceback_exception)
end_time = datetime.datetime.now() end_time = datetime.datetime.now()
# LOG FAILURE - handle streaming failure logging in the _next_ object, remove `handle_failure` once it's deprecated # LOG FAILURE - handle streaming failure logging in the _next_ object, remove `handle_failure` once it's deprecated
if logging_obj:
threading.Thread(target=logging_obj.failure_handler, args=(e, traceback_exception, start_time, end_time)).start() threading.Thread(target=logging_obj.failure_handler, args=(e, traceback_exception, start_time, end_time)).start()
my_thread = threading.Thread( my_thread = threading.Thread(
target=handle_failure, target=handle_failure,