Merge pull request #20593 from ryan-crabbe/perf/reuse-litellm-params
perf: reuse LiteLLM_Params
This commit is contained in:
commit
d8dda93bba
@ -5562,7 +5562,7 @@ class Router:
|
||||
return
|
||||
else:
|
||||
deployment_model_info = self.get_router_model_info(
|
||||
deployment=deployment_info.model_dump(),
|
||||
deployment=deployment_info,
|
||||
received_model_name=model_group,
|
||||
)
|
||||
# get tpm/rpm from deployment info
|
||||
@ -6836,7 +6836,7 @@ class Router:
|
||||
|
||||
@overload
|
||||
def get_router_model_info(
|
||||
self, deployment: dict, received_model_name: str, id: None = None
|
||||
self, deployment: Union[dict, "Deployment"], received_model_name: str, id: None = None
|
||||
) -> ModelMapInfo:
|
||||
pass
|
||||
|
||||
@ -6848,7 +6848,7 @@ class Router:
|
||||
|
||||
def get_router_model_info(
|
||||
self,
|
||||
deployment: Optional[dict],
|
||||
deployment: Optional[Union[dict, "Deployment"]],
|
||||
received_model_name: str,
|
||||
id: Optional[str] = None,
|
||||
) -> ModelMapInfo:
|
||||
@ -6868,7 +6868,7 @@ class Router:
|
||||
if id is not None:
|
||||
_deployment = self.get_deployment(model_id=id)
|
||||
if _deployment is not None:
|
||||
deployment = _deployment.model_dump(exclude_none=True)
|
||||
deployment = _deployment
|
||||
|
||||
if deployment is None:
|
||||
raise ValueError("Deployment not found")
|
||||
@ -6880,10 +6880,22 @@ class Router:
|
||||
|
||||
model = base_model
|
||||
|
||||
## GET PROVIDER
|
||||
## GET PROVIDER - reuse LiteLLM_Params if already constructed
|
||||
litellm_params_data = deployment.get("litellm_params")
|
||||
litellm_params: LiteLLM_Params
|
||||
if isinstance(litellm_params_data, LiteLLM_Params):
|
||||
litellm_params = litellm_params_data
|
||||
elif isinstance(litellm_params_data, dict) and "model" in litellm_params_data:
|
||||
litellm_params = LiteLLM_Params(**litellm_params_data)
|
||||
else:
|
||||
raise ValueError(
|
||||
f"Deployment missing valid litellm_params. "
|
||||
f"Got: {type(litellm_params_data).__name__}, "
|
||||
f"deployment_id: {deployment.get('model_info', {}).get('id', 'unknown')}"
|
||||
)
|
||||
_model, custom_llm_provider, _, _ = litellm.get_llm_provider(
|
||||
model=deployment.get("litellm_params", {}).get("model", ""),
|
||||
litellm_params=LiteLLM_Params(**deployment.get("litellm_params", {})),
|
||||
model=litellm_params.model,
|
||||
litellm_params=litellm_params,
|
||||
)
|
||||
|
||||
## SET MODEL TO 'model=' - if base_model is None + not azure
|
||||
|
||||
@ -2198,3 +2198,33 @@ def test_get_valid_args():
|
||||
# Verify it contains keyword-only arguments too
|
||||
# These are common Router.__init__ parameters
|
||||
assert "assistants_config" in valid_args or "search_tools" in valid_args
|
||||
|
||||
|
||||
def test_get_router_model_info_with_deployment_object():
|
||||
"""Test get_router_model_info accepts Deployment object directly and reuses LiteLLM_Params"""
|
||||
router = Router(
|
||||
model_list=[
|
||||
{
|
||||
"model_name": "gpt-4",
|
||||
"litellm_params": {"model": "gpt-4", "api_key": "test-key"},
|
||||
"model_info": {"id": "test-id"},
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
# Get the Deployment object (not dict)
|
||||
deployment = router.get_deployment(model_id="test-id")
|
||||
assert deployment is not None
|
||||
assert isinstance(deployment, Deployment)
|
||||
assert isinstance(deployment.litellm_params, LiteLLM_Params)
|
||||
|
||||
# Pass Deployment directly (not .model_dump()) - this exercises the isinstance check
|
||||
# that reuses the existing LiteLLM_Params instead of reconstructing it
|
||||
model_info = router.get_router_model_info(
|
||||
deployment=deployment,
|
||||
received_model_name="gpt-4",
|
||||
)
|
||||
|
||||
# Verify we got valid model info back
|
||||
assert model_info is not None
|
||||
assert isinstance(model_info, dict)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user