mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 11:14:04 +00:00
* fix(anthropic/chat/transformation.py): support anthropic disable_parallel_tool_use param Fixes https://github.com/BerriAI/litellm/issues/6456 * feat(anthropic/chat/transformation.py): support anthropic computer tool use Closes https://github.com/BerriAI/litellm/issues/6427 * fix(vertex_ai/common_utils.py): parse out '$schema' when calling vertex ai Fixes issue when trying to call vertex from vercel sdk * fix(main.py): add 'extra_headers' support for azure on all translation endpoints Fixes https://github.com/BerriAI/litellm/issues/6465 * fix: fix linting errors * fix(transformation.py): handle no beta headers for anthropic * test: cleanup test * fix: fix linting error * fix: fix linting errors * fix: fix linting errors * fix(transformation.py): handle dummy tool call * fix(main.py): fix linting error * fix(azure.py): pass required param * LiteLLM Minor Fixes & Improvements (10/24/2024) (#6441) * fix(azure.py): handle /openai/deployment in azure api base * fix(factory.py): fix faulty anthropic tool result translation check Fixes https://github.com/BerriAI/litellm/issues/6422 * fix(gpt_transformation.py): add support for parallel_tool_calls to azure Fixes https://github.com/BerriAI/litellm/issues/6440 * fix(factory.py): support anthropic prompt caching for tool results * fix(vertex_ai/common_utils): don't pop non-null required field Fixes https://github.com/BerriAI/litellm/issues/6426 * feat(vertex_ai.py): support code_execution tool call for vertex ai + gemini Closes https://github.com/BerriAI/litellm/issues/6434 * build(model_prices_and_context_window.json): Add 'supports_assistant_prefill' for bedrock claude-3-5-sonnet v2 models Closes https://github.com/BerriAI/litellm/issues/6437 * fix(types/utils.py): fix linting * test: update test to include required fields * test: fix test * test: handle flaky test * test: remove e2e test - hitting gemini rate limits * Litellm dev 10 26 2024 (#6472) * docs(exception_mapping.md): add missing exception types Fixes https://github.com/Aider-AI/aider/issues/2120#issuecomment-2438971183 * fix(main.py): register custom model pricing with specific key Ensure custom model pricing is registered to the specific model+provider key combination * test: make testing more robust for custom pricing * fix(redis_cache.py): instrument otel logging for sync redis calls ensures complete coverage for all redis cache calls * (Testing) Add unit testing for DualCache - ensure in memory cache is used when expected (#6471) * test test_dual_cache_get_set * unit testing for dual cache * fix async_set_cache_sadd * test_dual_cache_local_only * redis otel tracing + async support for latency routing (#6452) * docs(exception_mapping.md): add missing exception types Fixes https://github.com/Aider-AI/aider/issues/2120#issuecomment-2438971183 * fix(main.py): register custom model pricing with specific key Ensure custom model pricing is registered to the specific model+provider key combination * test: make testing more robust for custom pricing * fix(redis_cache.py): instrument otel logging for sync redis calls ensures complete coverage for all redis cache calls * refactor: pass parent_otel_span for redis caching calls in router allows for more observability into what calls are causing latency issues * test: update tests with new params * refactor: ensure e2e otel tracing for router * refactor(router.py): add more otel tracing acrosss router catch all latency issues for router requests * fix: fix linting error * fix(router.py): fix linting error * fix: fix test * test: fix tests * fix(dual_cache.py): pass ttl to redis cache * fix: fix param * fix(dual_cache.py): set default value for parent_otel_span * fix(transformation.py): support 'response_format' for anthropic calls * fix(transformation.py): check for cache_control inside 'function' block * fix: fix linting error * fix: fix linting errors --------- Co-authored-by: Ishaan Jaff <ishaanjaffer0324@gmail.com>
171 lines
6 KiB
Python
171 lines
6 KiB
Python
"""
|
|
Wrapper around router cache. Meant to handle model cooldown logic
|
|
"""
|
|
|
|
import json
|
|
import time
|
|
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, TypedDict
|
|
|
|
from litellm import verbose_logger
|
|
from litellm.caching.caching import Cache, DualCache
|
|
from litellm.caching.in_memory_cache import InMemoryCache
|
|
|
|
if TYPE_CHECKING:
|
|
from opentelemetry.trace import Span as _Span
|
|
|
|
Span = _Span
|
|
else:
|
|
Span = Any
|
|
|
|
|
|
class CooldownCacheValue(TypedDict):
|
|
exception_received: str
|
|
status_code: str
|
|
timestamp: float
|
|
cooldown_time: float
|
|
|
|
|
|
class CooldownCache:
|
|
def __init__(self, cache: DualCache, default_cooldown_time: float):
|
|
self.cache = cache
|
|
self.default_cooldown_time = default_cooldown_time
|
|
self.in_memory_cache = InMemoryCache()
|
|
|
|
def _common_add_cooldown_logic(
|
|
self, model_id: str, original_exception, exception_status, cooldown_time: float
|
|
) -> Tuple[str, CooldownCacheValue]:
|
|
try:
|
|
current_time = time.time()
|
|
cooldown_key = f"deployment:{model_id}:cooldown"
|
|
|
|
# Store the cooldown information for the deployment separately
|
|
cooldown_data = CooldownCacheValue(
|
|
exception_received=str(original_exception),
|
|
status_code=str(exception_status),
|
|
timestamp=current_time,
|
|
cooldown_time=cooldown_time,
|
|
)
|
|
|
|
return cooldown_key, cooldown_data
|
|
except Exception as e:
|
|
verbose_logger.error(
|
|
"CooldownCache::_common_add_cooldown_logic - Exception occurred - {}".format(
|
|
str(e)
|
|
)
|
|
)
|
|
raise e
|
|
|
|
def add_deployment_to_cooldown(
|
|
self,
|
|
model_id: str,
|
|
original_exception: Exception,
|
|
exception_status: int,
|
|
cooldown_time: Optional[float],
|
|
):
|
|
try:
|
|
_cooldown_time = cooldown_time or self.default_cooldown_time
|
|
cooldown_key, cooldown_data = self._common_add_cooldown_logic(
|
|
model_id=model_id,
|
|
original_exception=original_exception,
|
|
exception_status=exception_status,
|
|
cooldown_time=_cooldown_time,
|
|
)
|
|
|
|
# Set the cache with a TTL equal to the cooldown time
|
|
self.cache.set_cache(
|
|
value=cooldown_data,
|
|
key=cooldown_key,
|
|
ttl=_cooldown_time,
|
|
)
|
|
except Exception as e:
|
|
verbose_logger.error(
|
|
"CooldownCache::add_deployment_to_cooldown - Exception occurred - {}".format(
|
|
str(e)
|
|
)
|
|
)
|
|
raise e
|
|
|
|
@staticmethod
|
|
def get_cooldown_cache_key(model_id: str) -> str:
|
|
return f"deployment:{model_id}:cooldown"
|
|
|
|
async def async_get_active_cooldowns(
|
|
self, model_ids: List[str], parent_otel_span: Optional[Span]
|
|
) -> List[Tuple[str, CooldownCacheValue]]:
|
|
# Generate the keys for the deployments
|
|
keys = [
|
|
CooldownCache.get_cooldown_cache_key(model_id) for model_id in model_ids
|
|
]
|
|
|
|
# Retrieve the values for the keys using mget
|
|
## more likely to be none if no models ratelimited. So just check redis every 1s
|
|
## each redis call adds ~100ms latency.
|
|
|
|
## check in memory cache first
|
|
results = await self.cache.async_batch_get_cache(
|
|
keys=keys, parent_otel_span=parent_otel_span
|
|
)
|
|
active_cooldowns: List[Tuple[str, CooldownCacheValue]] = []
|
|
|
|
if results is None:
|
|
return active_cooldowns
|
|
|
|
# Process the results
|
|
for model_id, result in zip(model_ids, results):
|
|
if result and isinstance(result, dict):
|
|
cooldown_cache_value = CooldownCacheValue(**result) # type: ignore
|
|
active_cooldowns.append((model_id, cooldown_cache_value))
|
|
|
|
return active_cooldowns
|
|
|
|
def get_active_cooldowns(
|
|
self, model_ids: List[str], parent_otel_span: Optional[Span]
|
|
) -> List[Tuple[str, CooldownCacheValue]]:
|
|
# Generate the keys for the deployments
|
|
keys = [f"deployment:{model_id}:cooldown" for model_id in model_ids]
|
|
# Retrieve the values for the keys using mget
|
|
results = (
|
|
self.cache.batch_get_cache(keys=keys, parent_otel_span=parent_otel_span)
|
|
or []
|
|
)
|
|
|
|
active_cooldowns = []
|
|
# Process the results
|
|
for model_id, result in zip(model_ids, results):
|
|
if result and isinstance(result, dict):
|
|
cooldown_cache_value = CooldownCacheValue(**result) # type: ignore
|
|
active_cooldowns.append((model_id, cooldown_cache_value))
|
|
|
|
return active_cooldowns
|
|
|
|
def get_min_cooldown(
|
|
self, model_ids: List[str], parent_otel_span: Optional[Span]
|
|
) -> float:
|
|
"""Return min cooldown time required for a group of model id's."""
|
|
|
|
# Generate the keys for the deployments
|
|
keys = [f"deployment:{model_id}:cooldown" for model_id in model_ids]
|
|
|
|
# Retrieve the values for the keys using mget
|
|
results = (
|
|
self.cache.batch_get_cache(keys=keys, parent_otel_span=parent_otel_span)
|
|
or []
|
|
)
|
|
|
|
min_cooldown_time: Optional[float] = None
|
|
# Process the results
|
|
for model_id, result in zip(model_ids, results):
|
|
if result and isinstance(result, dict):
|
|
cooldown_cache_value = CooldownCacheValue(**result) # type: ignore
|
|
if min_cooldown_time is None:
|
|
min_cooldown_time = cooldown_cache_value["cooldown_time"]
|
|
elif cooldown_cache_value["cooldown_time"] < min_cooldown_time:
|
|
min_cooldown_time = cooldown_cache_value["cooldown_time"]
|
|
|
|
return min_cooldown_time or self.default_cooldown_time
|
|
|
|
|
|
# Usage example:
|
|
# cooldown_cache = CooldownCache(cache=your_cache_instance, cooldown_time=your_cooldown_time)
|
|
# cooldown_cache.add_deployment_to_cooldown(deployment, original_exception, exception_status)
|
|
# active_cooldowns = cooldown_cache.get_active_cooldowns()
|