mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 03:04:13 +00:00
Litellm ruff linting enforcement (#5992)
* ci(config.yml): add a 'check_code_quality' step Addresses https://github.com/BerriAI/litellm/issues/5991 * ci(config.yml): check why circle ci doesn't pick up this test * ci(config.yml): fix to run 'check_code_quality' tests * fix(__init__.py): fix unprotected import * fix(__init__.py): don't remove unused imports * build(ruff.toml): update ruff.toml to ignore unused imports * fix: fix: ruff + pyright - fix linting + type-checking errors * fix: fix linting errors * fix(lago.py): fix module init error * fix: fix linting errors * ci(config.yml): cd into correct dir for checks * fix(proxy_server.py): fix linting error * fix(utils.py): fix bare except causes ruff linting errors * fix: ruff - fix remaining linting errors * fix(clickhouse.py): use standard logging object * fix(__init__.py): fix unprotected import * fix: ruff - fix linting errors * fix: fix linting errors * ci(config.yml): cleanup code qa step (formatting handled in local_testing) * fix(_health_endpoints.py): fix ruff linting errors * ci(config.yml): just use ruff in check_code_quality pipeline for now * build(custom_guardrail.py): include missing file * style(embedding_handler.py): fix ruff check
This commit is contained in:
parent
3fc4ae0d65
commit
d57be47b0f
263 changed files with 1687 additions and 3320 deletions
|
@ -1,6 +1,10 @@
|
|||
import requests, traceback, json, os
|
||||
import json
|
||||
import os
|
||||
import traceback
|
||||
import types
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
class LiteDebugger:
|
||||
user_email = None
|
||||
|
@ -17,23 +21,17 @@ class LiteDebugger:
|
|||
email or os.getenv("LITELLM_TOKEN") or os.getenv("LITELLM_EMAIL")
|
||||
)
|
||||
if (
|
||||
self.user_email == None
|
||||
self.user_email is None
|
||||
): # if users are trying to use_client=True but token not set
|
||||
raise ValueError(
|
||||
"litellm.use_client = True but no token or email passed. Please set it in litellm.token"
|
||||
)
|
||||
self.dashboard_url = "https://admin.litellm.ai/" + self.user_email
|
||||
try:
|
||||
print(
|
||||
f"\033[92mHere's your LiteLLM Dashboard 👉 \033[94m\033[4m{self.dashboard_url}\033[0m"
|
||||
)
|
||||
except:
|
||||
print(f"Here's your LiteLLM Dashboard 👉 {self.dashboard_url}")
|
||||
if self.user_email == None:
|
||||
if self.user_email is None:
|
||||
raise ValueError(
|
||||
"[Non-Blocking Error] LiteLLMDebugger: Missing LITELLM_TOKEN. Set it in your environment. Eg.: os.environ['LITELLM_TOKEN']= <your_email>"
|
||||
)
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
raise ValueError(
|
||||
"[Non-Blocking Error] LiteLLMDebugger: Missing LITELLM_TOKEN. Set it in your environment. Eg.: os.environ['LITELLM_TOKEN']= <your_email>"
|
||||
)
|
||||
|
@ -49,123 +47,18 @@ class LiteDebugger:
|
|||
litellm_params,
|
||||
optional_params,
|
||||
):
|
||||
print_verbose(
|
||||
f"LiteDebugger: Pre-API Call Logging for call id {litellm_call_id}"
|
||||
)
|
||||
try:
|
||||
print_verbose(
|
||||
f"LiteLLMDebugger: Logging - Enters input logging function for model {model}"
|
||||
)
|
||||
|
||||
def remove_key_value(dictionary, key):
|
||||
new_dict = dictionary.copy() # Create a copy of the original dictionary
|
||||
new_dict.pop(key) # Remove the specified key-value pair from the copy
|
||||
return new_dict
|
||||
|
||||
updated_litellm_params = remove_key_value(litellm_params, "logger_fn")
|
||||
|
||||
if call_type == "embedding":
|
||||
for (
|
||||
message
|
||||
) in (
|
||||
messages
|
||||
): # assuming the input is a list as required by the embedding function
|
||||
litellm_data_obj = {
|
||||
"model": model,
|
||||
"messages": [{"role": "user", "content": message}],
|
||||
"end_user": end_user,
|
||||
"status": "initiated",
|
||||
"litellm_call_id": litellm_call_id,
|
||||
"user_email": self.user_email,
|
||||
"litellm_params": updated_litellm_params,
|
||||
"optional_params": optional_params,
|
||||
}
|
||||
print_verbose(
|
||||
f"LiteLLMDebugger: Logging - logged data obj {litellm_data_obj}"
|
||||
)
|
||||
response = requests.post(
|
||||
url=self.api_url,
|
||||
headers={"content-type": "application/json"},
|
||||
data=json.dumps(litellm_data_obj),
|
||||
)
|
||||
print_verbose(f"LiteDebugger: embedding api response - {response.text}")
|
||||
elif call_type == "completion":
|
||||
litellm_data_obj = {
|
||||
"model": model,
|
||||
"messages": messages
|
||||
if isinstance(messages, list)
|
||||
else [{"role": "user", "content": messages}],
|
||||
"end_user": end_user,
|
||||
"status": "initiated",
|
||||
"litellm_call_id": litellm_call_id,
|
||||
"user_email": self.user_email,
|
||||
"litellm_params": updated_litellm_params,
|
||||
"optional_params": optional_params,
|
||||
}
|
||||
print_verbose(
|
||||
f"LiteLLMDebugger: Logging - logged data obj {litellm_data_obj}"
|
||||
)
|
||||
response = requests.post(
|
||||
url=self.api_url,
|
||||
headers={"content-type": "application/json"},
|
||||
data=json.dumps(litellm_data_obj),
|
||||
)
|
||||
print_verbose(
|
||||
f"LiteDebugger: completion api response - {response.text}"
|
||||
)
|
||||
except:
|
||||
print_verbose(
|
||||
f"[Non-Blocking Error] LiteDebugger: Logging Error - {traceback.format_exc()}"
|
||||
)
|
||||
pass
|
||||
"""
|
||||
This integration is not implemented yet.
|
||||
"""
|
||||
return
|
||||
|
||||
def post_call_log_event(
|
||||
self, original_response, litellm_call_id, print_verbose, call_type, stream
|
||||
):
|
||||
print_verbose(
|
||||
f"LiteDebugger: Post-API Call Logging for call id {litellm_call_id}"
|
||||
)
|
||||
try:
|
||||
if call_type == "embedding":
|
||||
litellm_data_obj = {
|
||||
"status": "received",
|
||||
"additional_details": {
|
||||
"original_response": str(
|
||||
original_response["data"][0]["embedding"][:5]
|
||||
)
|
||||
}, # don't store the entire vector
|
||||
"litellm_call_id": litellm_call_id,
|
||||
"user_email": self.user_email,
|
||||
}
|
||||
elif call_type == "completion" and not stream:
|
||||
litellm_data_obj = {
|
||||
"status": "received",
|
||||
"additional_details": {"original_response": original_response},
|
||||
"litellm_call_id": litellm_call_id,
|
||||
"user_email": self.user_email,
|
||||
}
|
||||
elif call_type == "completion" and stream:
|
||||
litellm_data_obj = {
|
||||
"status": "received",
|
||||
"additional_details": {
|
||||
"original_response": "Streamed response"
|
||||
if isinstance(original_response, types.GeneratorType)
|
||||
else original_response
|
||||
},
|
||||
"litellm_call_id": litellm_call_id,
|
||||
"user_email": self.user_email,
|
||||
}
|
||||
print_verbose(f"litedebugger post-call data object - {litellm_data_obj}")
|
||||
response = requests.post(
|
||||
url=self.api_url,
|
||||
headers={"content-type": "application/json"},
|
||||
data=json.dumps(litellm_data_obj),
|
||||
)
|
||||
print_verbose(f"LiteDebugger: api response - {response.text}")
|
||||
except:
|
||||
print_verbose(
|
||||
f"[Non-Blocking Error] LiteDebugger: Logging Error - {traceback.format_exc()}"
|
||||
)
|
||||
"""
|
||||
This integration is not implemented yet.
|
||||
"""
|
||||
return
|
||||
|
||||
def log_event(
|
||||
self,
|
||||
|
@ -178,85 +71,7 @@ class LiteDebugger:
|
|||
call_type,
|
||||
stream=False,
|
||||
):
|
||||
print_verbose(
|
||||
f"LiteDebugger: Success/Failure Call Logging for call id {litellm_call_id}"
|
||||
)
|
||||
try:
|
||||
print_verbose(
|
||||
f"LiteLLMDebugger: Success/Failure Logging - Enters handler logging function for function {call_type} and stream set to {stream} with response object {response_obj}"
|
||||
)
|
||||
total_cost = 0 # [TODO] implement cost tracking
|
||||
response_time = (end_time - start_time).total_seconds()
|
||||
if call_type == "completion" and stream == False:
|
||||
litellm_data_obj = {
|
||||
"response_time": response_time,
|
||||
"total_cost": total_cost,
|
||||
"response": response_obj["choices"][0]["message"]["content"],
|
||||
"litellm_call_id": litellm_call_id,
|
||||
"status": "success",
|
||||
}
|
||||
print_verbose(
|
||||
f"LiteDebugger: Logging - final data object: {litellm_data_obj}"
|
||||
)
|
||||
response = requests.post(
|
||||
url=self.api_url,
|
||||
headers={"content-type": "application/json"},
|
||||
data=json.dumps(litellm_data_obj),
|
||||
)
|
||||
elif call_type == "embedding":
|
||||
litellm_data_obj = {
|
||||
"response_time": response_time,
|
||||
"total_cost": total_cost,
|
||||
"response": str(response_obj["data"][0]["embedding"][:5]),
|
||||
"litellm_call_id": litellm_call_id,
|
||||
"status": "success",
|
||||
}
|
||||
response = requests.post(
|
||||
url=self.api_url,
|
||||
headers={"content-type": "application/json"},
|
||||
data=json.dumps(litellm_data_obj),
|
||||
)
|
||||
elif call_type == "completion" and stream == True:
|
||||
if len(response_obj["content"]) > 0: # don't log the empty strings
|
||||
litellm_data_obj = {
|
||||
"response_time": response_time,
|
||||
"total_cost": total_cost,
|
||||
"response": response_obj["content"],
|
||||
"litellm_call_id": litellm_call_id,
|
||||
"status": "success",
|
||||
}
|
||||
print_verbose(
|
||||
f"LiteDebugger: Logging - final data object: {litellm_data_obj}"
|
||||
)
|
||||
response = requests.post(
|
||||
url=self.api_url,
|
||||
headers={"content-type": "application/json"},
|
||||
data=json.dumps(litellm_data_obj),
|
||||
)
|
||||
elif "error" in response_obj:
|
||||
if "Unable to map your input to a model." in response_obj["error"]:
|
||||
total_cost = 0
|
||||
litellm_data_obj = {
|
||||
"response_time": response_time,
|
||||
"model": response_obj["model"],
|
||||
"total_cost": total_cost,
|
||||
"error": response_obj["error"],
|
||||
"end_user": end_user,
|
||||
"litellm_call_id": litellm_call_id,
|
||||
"status": "failure",
|
||||
"user_email": self.user_email,
|
||||
}
|
||||
print_verbose(
|
||||
f"LiteDebugger: Logging - final data object: {litellm_data_obj}"
|
||||
)
|
||||
response = requests.post(
|
||||
url=self.api_url,
|
||||
headers={"content-type": "application/json"},
|
||||
data=json.dumps(litellm_data_obj),
|
||||
)
|
||||
print_verbose(f"LiteDebugger: api response - {response.text}")
|
||||
except:
|
||||
print_verbose(
|
||||
f"[Non-Blocking Error] LiteDebugger: Logging Error - {traceback.format_exc()}"
|
||||
)
|
||||
pass
|
||||
"""
|
||||
This integration is not implemented yet.
|
||||
"""
|
||||
return
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue