forked from phoenix/litellm-mirror
updating helper functions with a try-except
This commit is contained in:
parent
2d75db70ad
commit
42bf542ba6
10 changed files with 123 additions and 109 deletions
|
@ -1,6 +1,6 @@
|
||||||
Metadata-Version: 2.1
|
Metadata-Version: 2.1
|
||||||
Name: litellm
|
Name: litellm
|
||||||
Version: 0.1.2
|
Version: 0.1.207
|
||||||
Summary: Library to easily interface with LLM API providers
|
Summary: Library to easily interface with LLM API providers
|
||||||
Author: BerriAI
|
Author: BerriAI
|
||||||
License-File: LICENSE
|
License-File: LICENSE
|
||||||
|
|
|
@ -3,6 +3,8 @@ README.md
|
||||||
setup.py
|
setup.py
|
||||||
litellm/__init__.py
|
litellm/__init__.py
|
||||||
litellm/main.py
|
litellm/main.py
|
||||||
|
litellm/timeout.py
|
||||||
|
litellm/utils.py
|
||||||
litellm.egg-info/PKG-INFO
|
litellm.egg-info/PKG-INFO
|
||||||
litellm.egg-info/SOURCES.txt
|
litellm.egg-info/SOURCES.txt
|
||||||
litellm.egg-info/dependency_links.txt
|
litellm.egg-info/dependency_links.txt
|
||||||
|
|
|
@ -1,2 +1,7 @@
|
||||||
openai
|
openai
|
||||||
cohere
|
cohere
|
||||||
|
pytest
|
||||||
|
anthropic
|
||||||
|
replicate
|
||||||
|
python-dotenv
|
||||||
|
openai[datalib]
|
||||||
|
|
Binary file not shown.
|
@ -10,8 +10,6 @@ import random
|
||||||
####### ENVIRONMENT VARIABLES ###################
|
####### ENVIRONMENT VARIABLES ###################
|
||||||
dotenv.load_dotenv() # Loading env variables using dotenv
|
dotenv.load_dotenv() # Loading env variables using dotenv
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_optional_params(
|
def get_optional_params(
|
||||||
# 12 optional params
|
# 12 optional params
|
||||||
functions = [],
|
functions = [],
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
221
litellm/utils.py
221
litellm/utils.py
|
@ -101,130 +101,139 @@ def client(original_function):
|
||||||
####### HELPER FUNCTIONS ################
|
####### HELPER FUNCTIONS ################
|
||||||
def set_callbacks(callback_list):
|
def set_callbacks(callback_list):
|
||||||
global sentry_sdk_instance, capture_exception, add_breadcrumb, posthog, slack_app, alerts_channel
|
global sentry_sdk_instance, capture_exception, add_breadcrumb, posthog, slack_app, alerts_channel
|
||||||
for callback in callback_list:
|
try:
|
||||||
if callback == "sentry":
|
for callback in callback_list:
|
||||||
try:
|
if callback == "sentry":
|
||||||
import sentry_sdk
|
try:
|
||||||
except ImportError:
|
import sentry_sdk
|
||||||
print_verbose("Package 'sentry_sdk' is missing. Installing it...")
|
except ImportError:
|
||||||
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'sentry_sdk'])
|
print_verbose("Package 'sentry_sdk' is missing. Installing it...")
|
||||||
import sentry_sdk
|
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'sentry_sdk'])
|
||||||
sentry_sdk_instance = sentry_sdk
|
import sentry_sdk
|
||||||
sentry_sdk_instance.init(dsn=os.environ.get("SENTRY_API_URL"), traces_sample_rate=float(os.environ.get("SENTRY_API_TRACE_RATE")))
|
sentry_sdk_instance = sentry_sdk
|
||||||
capture_exception = sentry_sdk_instance.capture_exception
|
sentry_sdk_instance.init(dsn=os.environ.get("SENTRY_API_URL"), traces_sample_rate=float(os.environ.get("SENTRY_API_TRACE_RATE")))
|
||||||
add_breadcrumb = sentry_sdk_instance.add_breadcrumb
|
capture_exception = sentry_sdk_instance.capture_exception
|
||||||
elif callback == "posthog":
|
add_breadcrumb = sentry_sdk_instance.add_breadcrumb
|
||||||
try:
|
elif callback == "posthog":
|
||||||
from posthog import Posthog
|
try:
|
||||||
except ImportError:
|
from posthog import Posthog
|
||||||
print_verbose("Package 'posthog' is missing. Installing it...")
|
except ImportError:
|
||||||
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'posthog'])
|
print_verbose("Package 'posthog' is missing. Installing it...")
|
||||||
from posthog import Posthog
|
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'posthog'])
|
||||||
posthog = Posthog(
|
from posthog import Posthog
|
||||||
project_api_key=os.environ.get("POSTHOG_API_KEY"),
|
posthog = Posthog(
|
||||||
host=os.environ.get("POSTHOG_API_URL"))
|
project_api_key=os.environ.get("POSTHOG_API_KEY"),
|
||||||
elif callback == "slack":
|
host=os.environ.get("POSTHOG_API_URL"))
|
||||||
try:
|
elif callback == "slack":
|
||||||
from slack_bolt import App
|
try:
|
||||||
except ImportError:
|
from slack_bolt import App
|
||||||
print_verbose("Package 'slack_bolt' is missing. Installing it...")
|
except ImportError:
|
||||||
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'slack_bolt'])
|
print_verbose("Package 'slack_bolt' is missing. Installing it...")
|
||||||
from slack_bolt import App
|
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'slack_bolt'])
|
||||||
slack_app = App(
|
from slack_bolt import App
|
||||||
token=os.environ.get("SLACK_API_TOKEN"),
|
slack_app = App(
|
||||||
signing_secret=os.environ.get("SLACK_API_SECRET")
|
token=os.environ.get("SLACK_API_TOKEN"),
|
||||||
)
|
signing_secret=os.environ.get("SLACK_API_SECRET")
|
||||||
alerts_channel = os.environ["SLACK_API_CHANNEL"]
|
)
|
||||||
print_verbose(f"Initialized Slack App: {slack_app}")
|
alerts_channel = os.environ["SLACK_API_CHANNEL"]
|
||||||
|
print_verbose(f"Initialized Slack App: {slack_app}")
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def handle_failure(exception, traceback_exception, args, kwargs):
|
def handle_failure(exception, traceback_exception, args, kwargs):
|
||||||
global sentry_sdk_instance, capture_exception, add_breadcrumb, posthog, slack_app, alerts_channel
|
global sentry_sdk_instance, capture_exception, add_breadcrumb, posthog, slack_app, alerts_channel
|
||||||
print_verbose(f"handle_failure args: {args}")
|
try:
|
||||||
print_verbose(f"handle_failure kwargs: {kwargs}")
|
print_verbose(f"handle_failure args: {args}")
|
||||||
|
print_verbose(f"handle_failure kwargs: {kwargs}")
|
||||||
|
|
||||||
|
success_handler = additional_details.pop("success_handler", None)
|
||||||
|
failure_handler = additional_details.pop("failure_handler", None)
|
||||||
|
|
||||||
|
additional_details["Event_Name"] = additional_details.pop("failed_event_name", "litellm.failed_query")
|
||||||
|
print_verbose(f"self.failure_callback: {litellm.failure_callback}")
|
||||||
|
|
||||||
|
print_verbose(f"additional_details: {additional_details}")
|
||||||
|
for callback in litellm.failure_callback:
|
||||||
|
try:
|
||||||
|
if callback == "slack":
|
||||||
|
slack_msg = ""
|
||||||
|
if len(kwargs) > 0:
|
||||||
|
for key in kwargs:
|
||||||
|
slack_msg += f"{key}: {kwargs[key]}\n"
|
||||||
|
if len(args) > 0:
|
||||||
|
for i, arg in enumerate(args):
|
||||||
|
slack_msg += f"LiteLLM_Args_{str(i)}: {arg}"
|
||||||
|
for detail in additional_details:
|
||||||
|
slack_msg += f"{detail}: {additional_details[detail]}\n"
|
||||||
|
slack_msg += f"Traceback: {traceback_exception}"
|
||||||
|
slack_app.client.chat_postMessage(channel=alerts_channel, text=slack_msg)
|
||||||
|
elif callback == "sentry":
|
||||||
|
capture_exception(exception)
|
||||||
|
elif callback == "posthog":
|
||||||
|
print_verbose(f"inside posthog, additional_details: {len(additional_details.keys())}")
|
||||||
|
ph_obj = {}
|
||||||
|
if len(kwargs) > 0:
|
||||||
|
ph_obj = kwargs
|
||||||
|
if len(args) > 0:
|
||||||
|
for i, arg in enumerate(args):
|
||||||
|
ph_obj["litellm_args_" + str(i)] = arg
|
||||||
|
for detail in additional_details:
|
||||||
|
ph_obj[detail] = additional_details[detail]
|
||||||
|
event_name = additional_details["Event_Name"]
|
||||||
|
print_verbose(f"ph_obj: {ph_obj}")
|
||||||
|
print_verbose(f"PostHog Event Name: {event_name}")
|
||||||
|
if "user_id" in additional_details:
|
||||||
|
posthog.capture(additional_details["user_id"], event_name, ph_obj)
|
||||||
|
else: # PostHog calls require a unique id to identify a user - https://posthog.com/docs/libraries/python
|
||||||
|
unique_id = str(uuid.uuid4())
|
||||||
|
posthog.capture(unique_id, event_name)
|
||||||
|
print_verbose(f"successfully logged to PostHog!")
|
||||||
|
except:
|
||||||
|
print_verbose(f"Error Occurred while logging failure: {traceback.format_exc()}")
|
||||||
|
pass
|
||||||
|
|
||||||
|
if failure_handler and callable(failure_handler):
|
||||||
|
call_details = {
|
||||||
|
"exception": exception,
|
||||||
|
"additional_details": additional_details
|
||||||
|
}
|
||||||
|
failure_handler(call_details)
|
||||||
|
pass
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def handle_success(*args, **kwargs):
|
||||||
|
try:
|
||||||
success_handler = additional_details.pop("success_handler", None)
|
success_handler = additional_details.pop("success_handler", None)
|
||||||
failure_handler = additional_details.pop("failure_handler", None)
|
failure_handler = additional_details.pop("failure_handler", None)
|
||||||
|
additional_details["Event_Name"] = additional_details.pop("successful_event_name", "litellm.succes_query")
|
||||||
additional_details["Event_Name"] = additional_details.pop("failed_event_name", "litellm.failed_query")
|
for callback in litellm.success_callback:
|
||||||
print_verbose(f"self.failure_callback: {litellm.failure_callback}")
|
|
||||||
|
|
||||||
print_verbose(f"additional_details: {additional_details}")
|
|
||||||
for callback in litellm.failure_callback:
|
|
||||||
try:
|
try:
|
||||||
if callback == "slack":
|
if callback == "posthog":
|
||||||
slack_msg = ""
|
|
||||||
if len(kwargs) > 0:
|
|
||||||
for key in kwargs:
|
|
||||||
slack_msg += f"{key}: {kwargs[key]}\n"
|
|
||||||
if len(args) > 0:
|
|
||||||
for i, arg in enumerate(args):
|
|
||||||
slack_msg += f"LiteLLM_Args_{str(i)}: {arg}"
|
|
||||||
for detail in additional_details:
|
|
||||||
slack_msg += f"{detail}: {additional_details[detail]}\n"
|
|
||||||
slack_msg += f"Traceback: {traceback_exception}"
|
|
||||||
slack_app.client.chat_postMessage(channel=alerts_channel, text=slack_msg)
|
|
||||||
elif callback == "sentry":
|
|
||||||
capture_exception(exception)
|
|
||||||
elif callback == "posthog":
|
|
||||||
print_verbose(f"inside posthog, additional_details: {len(additional_details.keys())}")
|
|
||||||
ph_obj = {}
|
ph_obj = {}
|
||||||
if len(kwargs) > 0:
|
|
||||||
ph_obj = kwargs
|
|
||||||
if len(args) > 0:
|
|
||||||
for i, arg in enumerate(args):
|
|
||||||
ph_obj["litellm_args_" + str(i)] = arg
|
|
||||||
for detail in additional_details:
|
for detail in additional_details:
|
||||||
ph_obj[detail] = additional_details[detail]
|
ph_obj[detail] = additional_details[detail]
|
||||||
event_name = additional_details["Event_Name"]
|
event_name = additional_details["Event_Name"]
|
||||||
print_verbose(f"ph_obj: {ph_obj}")
|
|
||||||
print_verbose(f"PostHog Event Name: {event_name}")
|
|
||||||
if "user_id" in additional_details:
|
if "user_id" in additional_details:
|
||||||
posthog.capture(additional_details["user_id"], event_name, ph_obj)
|
posthog.capture(additional_details["user_id"], event_name, ph_obj)
|
||||||
else: # PostHog calls require a unique id to identify a user - https://posthog.com/docs/libraries/python
|
else: # PostHog calls require a unique id to identify a user - https://posthog.com/docs/libraries/python
|
||||||
unique_id = str(uuid.uuid4())
|
unique_id = str(uuid.uuid4())
|
||||||
posthog.capture(unique_id, event_name)
|
posthog.capture(unique_id, event_name, ph_obj)
|
||||||
print_verbose(f"successfully logged to PostHog!")
|
pass
|
||||||
|
elif callback == "slack":
|
||||||
|
slack_msg = ""
|
||||||
|
for detail in additional_details:
|
||||||
|
slack_msg += f"{detail}: {additional_details[detail]}\n"
|
||||||
|
slack_app.client.chat_postMessage(channel=alerts_channel, text=slack_msg)
|
||||||
except:
|
except:
|
||||||
print_verbose(f"Error Occurred while logging failure: {traceback.format_exc()}")
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if failure_handler and callable(failure_handler):
|
|
||||||
call_details = {
|
|
||||||
"exception": exception,
|
|
||||||
"additional_details": additional_details
|
|
||||||
}
|
|
||||||
failure_handler(call_details)
|
|
||||||
pass
|
|
||||||
|
|
||||||
def handle_success(*args, **kwargs):
|
if success_handler and callable(success_handler):
|
||||||
success_handler = additional_details.pop("success_handler", None)
|
success_handler(args, kwargs)
|
||||||
failure_handler = additional_details.pop("failure_handler", None)
|
pass
|
||||||
additional_details["Event_Name"] = additional_details.pop("successful_event_name", "litellm.succes_query")
|
except:
|
||||||
for callback in litellm.success_callback:
|
pass
|
||||||
try:
|
|
||||||
if callback == "posthog":
|
|
||||||
ph_obj = {}
|
|
||||||
for detail in additional_details:
|
|
||||||
ph_obj[detail] = additional_details[detail]
|
|
||||||
event_name = additional_details["Event_Name"]
|
|
||||||
if "user_id" in additional_details:
|
|
||||||
posthog.capture(additional_details["user_id"], event_name, ph_obj)
|
|
||||||
else: # PostHog calls require a unique id to identify a user - https://posthog.com/docs/libraries/python
|
|
||||||
unique_id = str(uuid.uuid4())
|
|
||||||
posthog.capture(unique_id, event_name, ph_obj)
|
|
||||||
pass
|
|
||||||
elif callback == "slack":
|
|
||||||
slack_msg = ""
|
|
||||||
for detail in additional_details:
|
|
||||||
slack_msg += f"{detail}: {additional_details[detail]}\n"
|
|
||||||
slack_app.client.chat_postMessage(channel=alerts_channel, text=slack_msg)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if success_handler and callable(success_handler):
|
|
||||||
success_handler(args, kwargs)
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def exception_type(model, original_exception):
|
def exception_type(model, original_exception):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue