mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-01 02:02:20 +00:00
refactor: clean up fresh tech debt from 2026-09-28 (#43674)
* refactor: clean up fresh tech debt from 2026-09-28 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor: group leaderboard rows in one pass and wrap docstring at 120 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
parent
85dc7cb62e
commit
7f95b5f361
6 changed files with 46 additions and 32 deletions
|
|
@ -39,10 +39,10 @@ class _ScanKwargs(TypedDict):
|
|||
server_name: ReadOnly[str]
|
||||
mcp_rate_limit_server_name: ReadOnly[str]
|
||||
user_api_key_auth: ReadOnly[UserAPIKeyAuth | None]
|
||||
user_api_key_user_id: ReadOnly[object]
|
||||
user_api_key_team_id: ReadOnly[object]
|
||||
user_api_key_end_user_id: ReadOnly[object]
|
||||
user_api_key_hash: ReadOnly[object]
|
||||
user_api_key_user_id: ReadOnly[str | None]
|
||||
user_api_key_team_id: ReadOnly[str | None]
|
||||
user_api_key_end_user_id: ReadOnly[str | None]
|
||||
user_api_key_hash: ReadOnly[str | None]
|
||||
headers: ReadOnly[Mapping[str, str]]
|
||||
mcp_tool_description: ReadOnly[str]
|
||||
mcp_input_schema: ReadOnly[Mapping[str, object]]
|
||||
|
|
@ -208,10 +208,10 @@ async def _guarded_catalog_entry(
|
|||
"server_name": server.name,
|
||||
"mcp_rate_limit_server_name": server.alias or server.server_name or server.name,
|
||||
"user_api_key_auth": user_api_key_auth,
|
||||
"user_api_key_user_id": getattr(user_api_key_auth, "user_id", None),
|
||||
"user_api_key_team_id": getattr(user_api_key_auth, "team_id", None),
|
||||
"user_api_key_end_user_id": getattr(user_api_key_auth, "end_user_id", None),
|
||||
"user_api_key_hash": getattr(user_api_key_auth, "api_key", None),
|
||||
"user_api_key_user_id": user_api_key_auth.user_id if user_api_key_auth else None,
|
||||
"user_api_key_team_id": user_api_key_auth.team_id if user_api_key_auth else None,
|
||||
"user_api_key_end_user_id": user_api_key_auth.end_user_id if user_api_key_auth else None,
|
||||
"user_api_key_hash": user_api_key_auth.api_key if user_api_key_auth else None,
|
||||
"headers": logging_safe_mcp_headers(raw_headers),
|
||||
"mcp_tool_description": tool.description or "",
|
||||
"mcp_input_schema": tool.input_schema,
|
||||
|
|
|
|||
|
|
@ -591,10 +591,9 @@ def _is_cost_explicitly_configured(model: str, llm_router: "Router") -> bool:
|
|||
|
||||
The group is resolved through ``Router.get_model_list()``, the same resolution
|
||||
``get_model_group_info()`` applies when the caller reads the cost a few lines earlier, so the
|
||||
two lookups cannot disagree: names defined in ``Router.model_group_alias`` are not
|
||||
``model_name`` entries in ``Router.model_list``, and scanning that list by exact name reported
|
||||
every aliased group as unconfigured. It also reaches a deployment that prices itself through
|
||||
its ``model_info`` block, whose entry lands in the cost map under the deployment id.
|
||||
two lookups cannot disagree, including for names defined in ``Router.model_group_alias``.
|
||||
It also reaches a deployment that prices itself through its ``model_info`` block, whose entry
|
||||
lands in the cost map under the deployment id.
|
||||
"""
|
||||
for deployment in llm_router.get_model_list(model_name=model) or ():
|
||||
model_id = (deployment.get("model_info") or _EMPTY_COST_ENTRY).get("id")
|
||||
|
|
|
|||
|
|
@ -22,12 +22,16 @@ def model_usage_task_type(request_tags: str) -> str:
|
|||
tags: Final = _TAGS.validate_json(request_tags)
|
||||
except ValidationError:
|
||||
return MODEL_INSIGHTS_DEFAULT_TASK
|
||||
for tag in tags:
|
||||
if isinstance(tag, str) and tag.startswith(MODEL_INSIGHTS_TASK_TAG_PREFIX):
|
||||
task = tag.removeprefix(MODEL_INSIGHTS_TASK_TAG_PREFIX)
|
||||
if task in load_model_insight_tasks():
|
||||
return task
|
||||
return MODEL_INSIGHTS_DEFAULT_TASK
|
||||
return next(
|
||||
(
|
||||
task
|
||||
for tag in tags
|
||||
if isinstance(tag, str)
|
||||
and tag.startswith(MODEL_INSIGHTS_TASK_TAG_PREFIX)
|
||||
and (task := tag.removeprefix(MODEL_INSIGHTS_TASK_TAG_PREFIX)) in load_model_insight_tasks()
|
||||
),
|
||||
MODEL_INSIGHTS_DEFAULT_TASK,
|
||||
)
|
||||
|
||||
|
||||
def _is_internal_call(metadata: str) -> bool:
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import functools
|
||||
import itertools
|
||||
from collections.abc import Mapping
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
from typing import Annotated, Final
|
||||
|
|
@ -111,14 +113,20 @@ def _daily_metric(row: _GroupedDaily) -> ModelInsightDailyMetric:
|
|||
|
||||
def _summarize_tasks(rows: list[_GroupedTask], metric: ModelInsightsMetric) -> list[ModelInsightTaskSummary]:
|
||||
catalog: Final = load_model_insight_tasks()
|
||||
totals: Final[dict[str, float]] = {}
|
||||
leaders: Final[dict[str, _GroupedTask]] = {}
|
||||
for row in rows:
|
||||
value = _rank_value(row, metric)
|
||||
totals[row.task_type] = totals.get(row.task_type, 0.0) + value
|
||||
leader = leaders.get(row.task_type)
|
||||
if leader is None or value > _rank_value(leader, metric):
|
||||
leaders[row.task_type] = row
|
||||
first_seen: Final = {task: index for index, task in enumerate(dict.fromkeys(row.task_type for row in rows))}
|
||||
by_task: Final = {
|
||||
task: tuple(group)
|
||||
for task, group in itertools.groupby(
|
||||
sorted(rows, key=lambda row: first_seen[row.task_type]), key=lambda row: row.task_type
|
||||
)
|
||||
}
|
||||
totals: Final = {
|
||||
task: functools.reduce(lambda total, row: total + _rank_value(row, metric), task_rows, 0.0)
|
||||
for task, task_rows in by_task.items()
|
||||
}
|
||||
leaders: Final = {
|
||||
task: max(task_rows, key=lambda row: _rank_value(row, metric)) for task, task_rows in by_task.items()
|
||||
}
|
||||
grand: Final = sum(totals.values())
|
||||
return [
|
||||
ModelInsightTaskSummary(
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ from dataclasses import dataclass
|
|||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Final,
|
||||
Literal,
|
||||
Protocol,
|
||||
cast, # noqa: TID251 # bounded compatibility calls into legacy Python integrations
|
||||
)
|
||||
|
|
@ -231,6 +232,10 @@ def defer_success(logger: LoggingSurface, pending: object) -> None:
|
|||
setattr(logger, "_native_pending_logging", pending)
|
||||
|
||||
|
||||
def _cache_hit(logger: LoggingSurface) -> Literal[True] | None:
|
||||
return True if logger.model_call_details.get("cache_hit") is True else None
|
||||
|
||||
|
||||
def sync_success_for_async_call(
|
||||
logger: LoggingSurface, response: object, start: datetime.datetime, end: datetime.datetime
|
||||
) -> None:
|
||||
|
|
@ -238,7 +243,7 @@ def sync_success_for_async_call(
|
|||
result=response,
|
||||
start_time=start,
|
||||
end_time=end,
|
||||
cache_hit=True if logger.model_call_details.get("cache_hit") is True else None,
|
||||
cache_hit=_cache_hit(logger),
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -265,16 +270,14 @@ def submit_success(logger: LoggingSurface, response: object, start: datetime.dat
|
|||
response,
|
||||
start,
|
||||
end,
|
||||
cache_hit=True if logger.model_call_details.get("cache_hit") is True else None,
|
||||
cache_hit=_cache_hit(logger),
|
||||
)
|
||||
|
||||
|
||||
def async_success_handler(
|
||||
logger: LoggingSurface, response: object, start: datetime.datetime, end: datetime.datetime
|
||||
) -> Coroutine[object, object, None]:
|
||||
return logger.async_success_handler(
|
||||
response, start, end, cache_hit=True if logger.model_call_details.get("cache_hit") is True else None
|
||||
)
|
||||
return logger.async_success_handler(response, start, end, cache_hit=_cache_hit(logger))
|
||||
|
||||
|
||||
def enqueue_logging(coroutine: Coroutine[object, object, None]) -> None:
|
||||
|
|
|
|||
|
|
@ -122,7 +122,7 @@ def test_model_insights_scopes_daily_to_ranked_deployments() -> None:
|
|||
def _task_rows() -> list[dict[str, object]]:
|
||||
def row(task: str, group: str, requests: str, spend: float) -> dict[str, object]:
|
||||
base = _grouped_row(task_type=task, model_group=group, model=group, custom_llm_provider="openai")
|
||||
base["_sum"].update({"request_count": requests, "spend": spend}) # type: ignore[union-attr]
|
||||
base["_sum"].update({"request_count": requests, "spend": spend})
|
||||
return base
|
||||
|
||||
return [
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue