router- initialize alerting

This commit is contained in:
Ishaan Jaff 2024-05-07 18:03:04 -07:00
parent 5fd3b12d34
commit c08352a0ce
2 changed files with 27 additions and 5 deletions

View file

@ -44,6 +44,7 @@ from litellm.types.router import (
updateDeployment, updateDeployment,
updateLiteLLMParams, updateLiteLLMParams,
RetryPolicy, RetryPolicy,
AlertingConfig,
) )
from litellm.integrations.custom_logger import CustomLogger from litellm.integrations.custom_logger import CustomLogger
@ -103,6 +104,7 @@ class Router:
] = "simple-shuffle", ] = "simple-shuffle",
routing_strategy_args: dict = {}, # just for latency-based routing routing_strategy_args: dict = {}, # just for latency-based routing
semaphore: Optional[asyncio.Semaphore] = None, semaphore: Optional[asyncio.Semaphore] = None,
alerting_config: Optional[AlertingConfig] = None,
) -> None: ) -> None:
""" """
Initialize the Router class with the given parameters for caching, reliability, and routing strategy. Initialize the Router class with the given parameters for caching, reliability, and routing strategy.
@ -131,7 +133,7 @@ class Router:
cooldown_time (float): Time to cooldown a deployment after failure in seconds. Defaults to 1. cooldown_time (float): Time to cooldown a deployment after failure in seconds. Defaults to 1.
routing_strategy (Literal["simple-shuffle", "least-busy", "usage-based-routing", "latency-based-routing", "cost-based-routing"]): Routing strategy. Defaults to "simple-shuffle". routing_strategy (Literal["simple-shuffle", "least-busy", "usage-based-routing", "latency-based-routing", "cost-based-routing"]): Routing strategy. Defaults to "simple-shuffle".
routing_strategy_args (dict): Additional args for latency-based routing. Defaults to {}. routing_strategy_args (dict): Additional args for latency-based routing. Defaults to {}.
alerting_config (AlertingConfig): Slack alerting configuration. Defaults to None.
Returns: Returns:
Router: An instance of the litellm.Router class. Router: An instance of the litellm.Router class.
@ -316,6 +318,9 @@ class Router:
self.model_group_retry_policy: Optional[Dict[str, RetryPolicy]] = ( self.model_group_retry_policy: Optional[Dict[str, RetryPolicy]] = (
model_group_retry_policy model_group_retry_policy
) )
self.alerting_config: Optional[AlertingConfig] = alerting_config
if self.alerting_config is not None:
self._initialize_alerting()
def routing_strategy_init(self, routing_strategy: str, routing_strategy_args: dict): def routing_strategy_init(self, routing_strategy: str, routing_strategy_args: dict):
if routing_strategy == "least-busy": if routing_strategy == "least-busy":
@ -3320,6 +3325,23 @@ class Router:
): ):
return retry_policy.ContentPolicyViolationErrorRetries return retry_policy.ContentPolicyViolationErrorRetries
def _initialize_alerting(self):
from litellm.integrations.slack_alerting import SlackAlerting
router_alerting_config: AlertingConfig = self.alerting_config
_slack_alerting_logger = SlackAlerting(
alerting_threshold=router_alerting_config.alerting_threshold,
alerting=["slack"],
default_webhook_url=router_alerting_config.webhook_url,
)
litellm.callbacks.append(_slack_alerting_logger)
litellm.success_callback.append(
_slack_alerting_logger.response_taking_too_long_callback
)
print("\033[94m\nInitialized Alerting for litellm.Router\033[0m\n") # noqa
def flush_cache(self): def flush_cache(self):
litellm.cache = None litellm.cache = None
self.cache.flush_cache() self.cache.flush_cache()

View file

@ -348,7 +348,7 @@ class RetryPolicy(BaseModel):
InternalServerErrorRetries: Optional[int] = None InternalServerErrorRetries: Optional[int] = None
class RouterAlerting(BaseModel): class AlertingConfig(BaseModel):
""" """
Use this configure alerting for the router. Receive alerts on the following events Use this configure alerting for the router. Receive alerts on the following events
- LLM API Exceptions - LLM API Exceptions
@ -356,9 +356,9 @@ class RouterAlerting(BaseModel):
- LLM Requests Hanging - LLM Requests Hanging
Args: Args:
webhook_url: Optional[str] = None - webhook url for alerting webhook_url: str - webhook url for alerting, slack provides a webhook url to send alerts to
alerting_threshold: Optional[float] = None - threhshold for slow / hanging llm responses (in seconds) alerting_threshold: Optional[float] = None - threhshold for slow / hanging llm responses (in seconds)
""" """
webhook_url: Optional[str] = None webhook_url: str
alerting_threshold: Optional[float] = None alerting_threshold: Optional[float] = 300