From 9bcbb6cc8b3201b91b846bd5286d06ac46ad7a0b Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Fri, 24 May 2024 14:29:00 -0700 Subject: [PATCH] feat - send send_email_alert_using_smtp --- litellm/integrations/slack_alerting.py | 58 ++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/litellm/integrations/slack_alerting.py b/litellm/integrations/slack_alerting.py index a4ff0620ad..f8c0d0b5fd 100644 --- a/litellm/integrations/slack_alerting.py +++ b/litellm/integrations/slack_alerting.py @@ -781,6 +781,57 @@ Model Info: return False + async def send_email_alert_using_smtp(self, webhook_event: WebhookEvent) -> bool: + """ + Sends structured Email alert to an SMTP server + + Currently only implemented for budget alerts + + Returns -> True if sent, False if not. + """ + from litellm.proxy.utils import send_email + + event_name = webhook_event.event_message + recipient_email = webhook_event.user_email + user_name = webhook_event.user_id + max_budget = webhook_event.max_budget + email_html_content = "Alert from LiteLLM Server" + + if webhook_event.event == "budget_crossed": + email_html_content = f""" +

LiteLLM

+ +

Hi {user_name},
+ + Your LLM API usage this month has reached your account's monthly budget of ${max_budget}

+ + API requests will be rejected until either (a) you increase your monthly budget or (b) your monthly usage resets at the beginning of the next calendar month.

+ + If you have any questions, please send an email to support@berri.ai

+ + Best,
+ The LiteLLM team
+ """ + + payload = webhook_event.model_dump_json() + email_event = { + "from": "support@alerts.litellm.ai", + "to": recipient_email, + "subject": event_name, + "html": email_html_content, + } + headers = {"Content-type": "application/json"} + + response = await send_email( + sender_name=email_event["from"], + sender_email=email_event["from"], + receiver_email=email_event["to"], + subject=email_event["subject"], + html=email_event["html"], + ) + + return False + async def send_alert( self, message: str, @@ -823,6 +874,13 @@ Model Info: ): await self.send_webhook_alert(webhook_event=user_info) + if ( + "email" in self.alerting + and alert_type == "budget_alerts" + and user_info is not None + ): + await self.send_email_alert_using_smtp(webhook_event=user_info) + if "slack" not in self.alerting: return