From 91d9d59717b1cd57e5e39054652ec442f3920faa Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Thu, 20 Jun 2024 14:32:52 -0700 Subject: [PATCH] docs - routing --- docs/my-website/docs/routing.md | 123 ++++++++++++++++++++++++++++++-- 1 file changed, 119 insertions(+), 4 deletions(-) diff --git a/docs/my-website/docs/routing.md b/docs/my-website/docs/routing.md index 63fac94562b..fd4fb865885 100644 --- a/docs/my-website/docs/routing.md +++ b/docs/my-website/docs/routing.md @@ -95,7 +95,7 @@ print(response) - `router.image_generation()` - completion calls in OpenAI `/v1/images/generations` endpoint format - `router.aimage_generation()` - async image generation calls -## Advanced - Routing Strategies +## Advanced - Routing Strategies ⭐️ #### Routing Strategies - Weighted Pick, Rate Limit Aware, Least Busy, Latency Based, Cost Based Router provides 4 strategies for routing your calls across multiple deployments: @@ -262,7 +262,7 @@ if response is not None: ) ``` -### Set Time Window +#### Set Time Window Set time window for how far back to consider when averaging latency for a deployment. @@ -278,7 +278,7 @@ router_settings: routing_strategy_args: {"ttl": 10} ``` -### Set Lowest Latency Buffer +#### Set Lowest Latency Buffer Set a buffer within which deployments are candidates for making calls to. @@ -468,6 +468,122 @@ asyncio.run(router_acompletion()) ``` + + + +**Plugin a custom routing strategy to select deployments** + + +Step 1. Define your custom routing strategy + +```python + +from litellm.router import CustomRoutingStrategyBase +class CustomRoutingStrategy(CustomRoutingStrategyBase): + async def async_get_available_deployment( + self, + model: str, + messages: Optional[List[Dict[str, str]]] = None, + input: Optional[Union[str, List]] = None, + specific_deployment: Optional[bool] = False, + request_kwargs: Optional[Dict] = None, + ): + """ + Asynchronously retrieves the available deployment based on the given parameters. + + Args: + model (str): The name of the model. + messages (Optional[List[Dict[str, str]]], optional): The list of messages for a given request. Defaults to None. + input (Optional[Union[str, List]], optional): The input for a given embedding request. Defaults to None. + specific_deployment (Optional[bool], optional): Whether to retrieve a specific deployment. Defaults to False. + request_kwargs (Optional[Dict], optional): Additional request keyword arguments. Defaults to None. + + Returns: + Returns an element from litellm.router.model_list + + """ + print("In CUSTOM async get available deployment") + model_list = router.model_list + print("router model list=", model_list) + for model in model_list: + if isinstance(model, dict): + if model["litellm_params"]["model"] == "openai/very-special-endpoint": + return model + pass + + def get_available_deployment( + self, + model: str, + messages: Optional[List[Dict[str, str]]] = None, + input: Optional[Union[str, List]] = None, + specific_deployment: Optional[bool] = False, + request_kwargs: Optional[Dict] = None, + ): + """ + Synchronously retrieves the available deployment based on the given parameters. + + Args: + model (str): The name of the model. + messages (Optional[List[Dict[str, str]]], optional): The list of messages for a given request. Defaults to None. + input (Optional[Union[str, List]], optional): The input for a given embedding request. Defaults to None. + specific_deployment (Optional[bool], optional): Whether to retrieve a specific deployment. Defaults to False. + request_kwargs (Optional[Dict], optional): Additional request keyword arguments. Defaults to None. + + Returns: + Returns an element from litellm.router.model_list + + """ + pass +``` + +Step 2. Initialize Router with custom routing strategy +```python +from litellm import Router + +router = Router( + model_list=[ + { + "model_name": "azure-model", + "litellm_params": { + "model": "openai/very-special-endpoint", + "api_base": "https://exampleopenaiendpoint-production.up.railway.app/", # If you are Krrish, this is OpenAI Endpoint3 on our Railway endpoint :) + "api_key": "fake-key", + }, + "model_info": {"id": "very-special-endpoint"}, + }, + { + "model_name": "azure-model", + "litellm_params": { + "model": "openai/fast-endpoint", + "api_base": "https://exampleopenaiendpoint-production.up.railway.app/", + "api_key": "fake-key", + }, + "model_info": {"id": "fast-endpoint"}, + }, + ], + set_verbose=True, + debug_level="DEBUG", + timeout=1, +) # type: ignore + +router.set_custom_routing_strategy(CustomRoutingStrategy()) # 👈 Set your routing strategy here +``` + +Step 3. Test your routing strategy. Expect your custom routing strategy to be called when running `router.acompletion` requests +```python +for _ in range(10): + response = await router.acompletion( + model="azure-model", messages=[{"role": "user", "content": "hello"}] + ) + print(response) + _picked_model_id = response._hidden_params["model_id"] + print("picked model=", _picked_model_id) +``` + + + + + Picks a deployment based on the lowest cost @@ -563,7 +679,6 @@ asyncio.run(router_acompletion()) ``` - ## Basic Reliability