mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-01 01:18:34 +00:00
* refactor(typing): clear basedpyright Any errors (wave 1) * refactor(typing): clear basedpyright Any errors (wave 2) * refactor(typing): clear basedpyright Any errors (wave 3) * refactor(typing): clear basedpyright Any errors (wave 4) * refactor(typing): clear basedpyright Any errors (wave 5) * refactor(typing): clear basedpyright Any errors (wave 6) * fix(typing): keep the cached-plan fallback's direct query_first calls so span naming resolves * fix(logging): keep callbacks running when litellm_params is a params model should_run_callback and the sync success and failure handlers read litellm_params through any object with a get method again, as they did before this branch, so a logging hook that returns a GenericLiteLLMParams no longer stops sync logging for that request Adds behavior tests for the changed lines codecov listed as not run * fix: read the regional uplift for every data residency again and reuse one dict TypeAdapter The typed lookup only read the eu and us multipliers, so a deployment that set regional_processing_uplift_multiplier_fedramp stopped applying it. /model/update and the model write paths now share one module-level TypeAdapter instead of building one per call. * fix: bill regional uplift multipliers float() accepts, bytes included * fix: raise the in operator's own error from one shared json_contains, and name the bad uplift type
144 lines
4.8 KiB
Python
144 lines
4.8 KiB
Python
"""
|
|
Functions for sending Email Alerts
|
|
"""
|
|
|
|
import os
|
|
from collections.abc import Awaitable, Mapping, Sequence
|
|
from typing import TYPE_CHECKING, Final
|
|
|
|
from litellm._logging import verbose_logger, verbose_proxy_logger
|
|
from litellm.repositories.team_repository import TeamRepository
|
|
from litellm.types.integrations.slack_alerting import WebhookEvent
|
|
|
|
if TYPE_CHECKING:
|
|
from litellm.proxy.utils import PrismaClient
|
|
|
|
# we use this for the email header, please send a test email if you change this. verify it looks good on email
|
|
LITELLM_LOGO_URL: Final = "https://litellm-listing.s3.amazonaws.com/litellm_logo.png"
|
|
LITELLM_SUPPORT_CONTACT: Final = "support@berri.ai"
|
|
|
|
|
|
def _query_raw_rows(
|
|
client: "PrismaClient", query: str, *args: object
|
|
) -> Awaitable[Sequence[Mapping[str, object]] | None]:
|
|
from litellm.proxy.utils import query_raw_rows
|
|
|
|
return query_raw_rows(client.db, query, *args)
|
|
|
|
|
|
async def get_all_team_member_emails(team_id: str | None = None) -> list:
|
|
verbose_logger.debug("Email Alerting: Getting all team members for team_id=%s", team_id)
|
|
if team_id is None:
|
|
return []
|
|
from litellm.proxy.proxy_server import prisma_client
|
|
|
|
if prisma_client is None:
|
|
raise Exception("Not connected to DB!")
|
|
|
|
team_row: Final = await TeamRepository(prisma_client).table.find_unique(
|
|
where={
|
|
"team_id": team_id,
|
|
}
|
|
)
|
|
|
|
if team_row is None:
|
|
return []
|
|
|
|
_team_members: Final = team_row.members_with_roles
|
|
verbose_logger.debug(
|
|
"Email Alerting: Got team members for team_id=%s Team Members: %s",
|
|
team_id,
|
|
_team_members,
|
|
)
|
|
_team_member_user_ids: Final[list[str]] = []
|
|
for member in _team_members:
|
|
if member and isinstance(member, dict):
|
|
_user_id = member.get("user_id")
|
|
if _user_id and isinstance(_user_id, str):
|
|
_team_member_user_ids.append(_user_id)
|
|
|
|
sql_query: Final = """
|
|
SELECT user_email
|
|
FROM "LiteLLM_UserTable"
|
|
WHERE user_id = ANY($1::TEXT[]);
|
|
"""
|
|
|
|
_result: Final = await _query_raw_rows(prisma_client, sql_query, _team_member_user_ids)
|
|
|
|
verbose_logger.debug("Email Alerting: Got all Emails for team, emails=%s", _result)
|
|
|
|
if _result is None:
|
|
return []
|
|
|
|
emails: Final = [
|
|
user.get("user_email")
|
|
for user in _result
|
|
if user and isinstance(user, dict) and user.get("user_email", None) is not None
|
|
]
|
|
return emails
|
|
|
|
|
|
async def send_team_budget_alert(webhook_event: WebhookEvent) -> bool:
|
|
"""
|
|
Send an Email Alert to All Team Members when the Team Budget is crossed
|
|
Returns -> True if sent, False if not.
|
|
"""
|
|
from litellm.proxy.utils import send_email
|
|
|
|
_team_id: Final = webhook_event.team_id
|
|
team_alias: Final = webhook_event.team_alias
|
|
verbose_logger.debug("Email Alerting: Sending Team Budget Alert for team=%s", team_alias)
|
|
|
|
email_logo_url = os.getenv("SMTP_SENDER_LOGO", os.getenv("EMAIL_LOGO_URL", None))
|
|
email_support_contact = os.getenv("EMAIL_SUPPORT_CONTACT", None)
|
|
|
|
# await self._check_if_using_premium_email_feature(
|
|
# premium_user, email_logo_url, email_support_contact
|
|
# )
|
|
|
|
if email_logo_url is None:
|
|
email_logo_url = LITELLM_LOGO_URL
|
|
if email_support_contact is None:
|
|
email_support_contact = LITELLM_SUPPORT_CONTACT
|
|
recipient_emails: Final = await get_all_team_member_emails(_team_id)
|
|
recipient_emails_str: Final[str] = ",".join(recipient_emails)
|
|
verbose_logger.debug("Email Alerting: Sending team budget alert to %s", recipient_emails_str)
|
|
|
|
event_name: Final = webhook_event.event_message
|
|
max_budget: Final = webhook_event.max_budget
|
|
email_html_content = "Alert from LiteLLM Server"
|
|
|
|
if recipient_emails_str is None:
|
|
verbose_proxy_logger.warning(
|
|
"Email Alerting: Trying to send email alert to no recipient, got recipient_emails=%s",
|
|
recipient_emails_str,
|
|
)
|
|
|
|
email_html_content = f"""
|
|
<img src="{email_logo_url}" alt="LiteLLM Logo" width="150" height="50" /> <br/><br/><br/>
|
|
|
|
Budget Crossed for Team <b> {team_alias} </b> <br/> <br/>
|
|
|
|
Your Teams LLM API usage has crossed it's <b> budget of ${max_budget} </b>, current spend is <b>${webhook_event.spend}</b><br /> <br />
|
|
|
|
API requests will be rejected until either (a) you increase your budget or (b) your budget gets reset <br /> <br />
|
|
|
|
If you have any questions, please send an email to {email_support_contact} <br /> <br />
|
|
|
|
Best, <br />
|
|
The LiteLLM team <br />
|
|
"""
|
|
|
|
email_event: Final = {
|
|
"to": recipient_emails_str,
|
|
"subject": f"LiteLLM {event_name} for Team {team_alias}",
|
|
"html": email_html_content,
|
|
}
|
|
|
|
await send_email(
|
|
receiver_email=email_event["to"],
|
|
subject=email_event["subject"],
|
|
html=email_event["html"],
|
|
)
|
|
|
|
return False
|