forked from phoenix/litellm-mirror
* 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
72 lines
2.6 KiB
Python
72 lines
2.6 KiB
Python
import json
|
|
import traceback
|
|
from datetime import datetime, timezone
|
|
|
|
import requests # type: ignore
|
|
|
|
|
|
class GreenscaleLogger:
|
|
def __init__(self):
|
|
import os
|
|
|
|
self.greenscale_api_key = os.getenv("GREENSCALE_API_KEY")
|
|
self.headers = {
|
|
"api-key": self.greenscale_api_key,
|
|
"Content-Type": "application/json",
|
|
}
|
|
self.greenscale_logging_url = os.getenv("GREENSCALE_ENDPOINT")
|
|
|
|
def log_event(self, kwargs, response_obj, start_time, end_time, print_verbose):
|
|
try:
|
|
response_json = response_obj.model_dump() if response_obj else {}
|
|
data = {
|
|
"modelId": kwargs.get("model"),
|
|
"inputTokenCount": response_json.get("usage", {}).get("prompt_tokens"),
|
|
"outputTokenCount": response_json.get("usage", {}).get(
|
|
"completion_tokens"
|
|
),
|
|
}
|
|
data["timestamp"] = datetime.now(timezone.utc).strftime(
|
|
"%Y-%m-%dT%H:%M:%SZ"
|
|
)
|
|
|
|
if type(end_time) is datetime and type(start_time) is datetime:
|
|
data["invocationLatency"] = int(
|
|
(end_time - start_time).total_seconds() * 1000
|
|
)
|
|
|
|
# Add additional metadata keys to tags
|
|
tags = []
|
|
metadata = kwargs.get("litellm_params", {}).get("metadata", {})
|
|
for key, value in metadata.items():
|
|
if key.startswith("greenscale"):
|
|
if key == "greenscale_project":
|
|
data["project"] = value
|
|
elif key == "greenscale_application":
|
|
data["application"] = value
|
|
else:
|
|
tags.append(
|
|
{"key": key.replace("greenscale_", ""), "value": str(value)}
|
|
)
|
|
|
|
data["tags"] = tags
|
|
|
|
if self.greenscale_logging_url is None:
|
|
raise Exception("Greenscale Logger Error - No logging URL found")
|
|
|
|
response = requests.post(
|
|
self.greenscale_logging_url,
|
|
headers=self.headers,
|
|
data=json.dumps(data, default=str),
|
|
)
|
|
if response.status_code != 200:
|
|
print_verbose(
|
|
f"Greenscale Logger Error - {response.text}, {response.status_code}"
|
|
)
|
|
else:
|
|
print_verbose(f"Greenscale Logger Succeeded - {response.text}")
|
|
except Exception as e:
|
|
print_verbose(
|
|
f"Greenscale Logger Error - {e}, Stack trace: {traceback.format_exc()}"
|
|
)
|
|
pass
|