Skip to content

Commit

Permalink
Merge pull request #3872 from BerriAI/litellm_make_email_alerts_free
Browse files Browse the repository at this point in the history
[Feat] Make Email alerting Free Tier, but customizing emails enterprise
  • Loading branch information
ishaan-jaff committed May 28, 2024
2 parents e1b46d4 + 8d0bd9d commit 4b0a8ff
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
12 changes: 6 additions & 6 deletions docs/my-website/docs/proxy/email.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ import Image from '@theme/IdealImage';

# ✨ 📧 Email Notifications

:::info

This is an Enterprise only feature [Get in touch with us for a Free Trial](https://calendly.com/d/4mp-gd3-k5k/litellm-1-1-onboarding-chat)

:::

Send an Email to your users when:
- A Proxy API Key is created for them
- Their API Key crosses it's Budget
Expand Down Expand Up @@ -38,6 +32,12 @@ That's it ! start your proxy

## Customizing Email Branding

:::info

Customizing Email Branding is an Enterprise Feature [Get in touch with us for a Free Trial](https://calendly.com/d/4mp-gd3-k5k/litellm-1-1-onboarding-chat)

:::

LiteLLM allows you to customize the:
- Logo on the Email
- Email support contact
Expand Down
49 changes: 33 additions & 16 deletions litellm/integrations/slack_alerting.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ class ProviderRegionOutageModel(BaseOutageModel):

# 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 = "https://litellm-listing.s3.amazonaws.com/litellm_logo.png"
EMAIL_LOGO_URL = os.getenv(
"SMTP_SENDER_LOGO", "https://litellm-listing.s3.amazonaws.com/litellm_logo.png"
)
EMAIL_SUPPORT_CONTACT = os.getenv("EMAIL_SUPPORT_CONTACT", "support@berri.ai")


class LiteLLMBase(BaseModel):
Expand Down Expand Up @@ -1147,21 +1143,34 @@ async def send_webhook_alert(self, webhook_event: WebhookEvent) -> bool:

return False

async def _check_if_using_premium_email_feature(
self,
premium_user: bool,
email_logo_url: Optional[str] = None,
email_support_contact: Optional[str] = None,
):
from litellm.proxy.proxy_server import premium_user
from litellm.proxy.proxy_server import CommonProxyErrors

if premium_user is not True:
if email_logo_url is not None or email_support_contact is not None:
raise ValueError(
f"Trying to Customize Email Alerting\n {CommonProxyErrors.not_premium_user.value}"
)

async def send_key_created_email(self, webhook_event: WebhookEvent) -> bool:
from litellm.proxy.utils import send_email

if self.alerting is None or "email" not in self.alerting:
# do nothing if user does not want email alerts
return False
from litellm.proxy.proxy_server import premium_user, prisma_client

# make sure this is a premium user
from litellm.proxy.proxy_server import premium_user
from litellm.proxy.proxy_server import CommonProxyErrors, prisma_client

if premium_user != True:
raise Exception(
f"Trying to use Email Alerting on key creation\n {CommonProxyErrors.not_premium_user.value}"
)
email_logo_url = os.getenv("SMTP_SENDER_LOGO", 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
)

event_name = webhook_event.event_message
recipient_email = webhook_event.user_email
Expand All @@ -1188,7 +1197,7 @@ async def send_key_created_email(self, webhook_event: WebhookEvent) -> bool:
"Trying to send email alert to no recipient", extra=webhook_event.dict()
)
email_html_content = f"""
<img src="{EMAIL_LOGO_URL}" alt="LiteLLM Logo" width="150" height="50" />
<img src="{email_logo_url}" alt="LiteLLM Logo" width="150" height="50" />
<p> Hi {recipient_email}, <br/>
Expand Down Expand Up @@ -1223,7 +1232,7 @@ async def send_key_created_email(self, webhook_event: WebhookEvent) -> bool:
</pre>
If you have any questions, please send an email to {EMAIL_SUPPORT_CONTACT} <br /> <br />
If you have any questions, please send an email to {email_support_contact} <br /> <br />
Best, <br />
The LiteLLM team <br />
Expand Down Expand Up @@ -1254,6 +1263,14 @@ async def send_email_alert_using_smtp(self, webhook_event: WebhookEvent) -> bool
"""
from litellm.proxy.utils import send_email

from litellm.proxy.proxy_server import premium_user, prisma_client

email_logo_url = os.getenv("SMTP_SENDER_LOGO", 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
)

event_name = webhook_event.event_message
recipient_email = webhook_event.user_email
user_name = webhook_event.user_id
Expand All @@ -1266,15 +1283,15 @@ async def send_email_alert_using_smtp(self, webhook_event: WebhookEvent) -> bool

if webhook_event.event == "budget_crossed":
email_html_content = f"""
<img src="{EMAIL_LOGO_URL}" alt="LiteLLM Logo" width="150" height="50" />
<img src="{email_logo_url}" alt="LiteLLM Logo" width="150" height="50" />
<p> Hi {user_name}, <br/>
Your LLM API usage this month has reached your account's <b> monthly budget of ${max_budget} </b> <br /> <br />
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. <br /> <br />
If you have any questions, please send an email to {EMAIL_SUPPORT_CONTACT} <br /> <br />
If you have any questions, please send an email to {email_support_contact} <br /> <br />
Best, <br />
The LiteLLM team <br />
Expand Down
11 changes: 2 additions & 9 deletions litellm/proxy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1850,20 +1850,13 @@ async def send_email(receiver_email, subject, html):
from litellm.proxy.proxy_server import premium_user
from litellm.proxy.proxy_server import CommonProxyErrors

# Check if user is premium - This is an Enterprise only Feature
if premium_user != True:
raise Exception(
f"Trying to use Email Alerting\n {CommonProxyErrors.not_premium_user.value}"
)
# Done Checking

smtp_host = os.getenv("SMTP_HOST")
smtp_port = os.getenv("SMTP_PORT", 587) # default to port 587
smtp_port = int(os.getenv("SMTP_PORT", "587")) # default to port 587
smtp_username = os.getenv("SMTP_USERNAME")
smtp_password = os.getenv("SMTP_PASSWORD")
sender_email = os.getenv("SMTP_SENDER_EMAIL", None)
if sender_email is None:
raise Exception("Trying to use SMTP, but SMTP_SENDER_EMAIL is not set")
raise ValueError("Trying to use SMTP, but SMTP_SENDER_EMAIL is not set")

## EMAIL SETUP ##
email_message = MIMEMultipart()
Expand Down

0 comments on commit 4b0a8ff

Please sign in to comment.