mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(anthropic): keep the type-discipline budget flat
Build the candidate indices as a tuple, take the already-marked messages as a Container with a frozenset default, and say why the one remaining set is mutable.
This commit is contained in:
parent
13bacab837
commit
9a16dd008a
1 changed files with 5 additions and 6 deletions
|
|
@ -917,7 +917,7 @@ class AnthropicCacheControlHook(CustomPromptManagement):
|
|||
messages, answered_tool_ids=client_answers
|
||||
)
|
||||
|
||||
taken: set[int] = set()
|
||||
taken: set[int] = set() # mutable-ok: the messages this request has already marked
|
||||
limit_reached = False
|
||||
for point in points:
|
||||
if used_blocks >= max_blocks:
|
||||
|
|
@ -959,7 +959,7 @@ class AnthropicCacheControlHook(CustomPromptManagement):
|
|||
|
||||
@staticmethod
|
||||
def _resolve_target_indices(
|
||||
point: CacheControlMessageInjectionPoint, messages: list[AllMessageValues], taken: set[int] | None = None
|
||||
point: CacheControlMessageInjectionPoint, messages: list[AllMessageValues], taken: Container[int] = frozenset()
|
||||
) -> list[int]:
|
||||
"""Resolve which message indices an injection point targets.
|
||||
|
||||
|
|
@ -968,7 +968,6 @@ class AnthropicCacheControlHook(CustomPromptManagement):
|
|||
point turns two configured breakpoints into one, and the four exist so that a
|
||||
prefix which stops matching at one can still match at an earlier one.
|
||||
"""
|
||||
already_marked: Final = taken or set()
|
||||
_targetted_index: Final[int | str | None] = point.get("index", None)
|
||||
targetted_index: int | None = None
|
||||
if isinstance(_targetted_index, str):
|
||||
|
|
@ -983,11 +982,11 @@ class AnthropicCacheControlHook(CustomPromptManagement):
|
|||
# that role's turns, so {role: assistant, index: -1} is the last assistant
|
||||
# turn rather than the last message.
|
||||
targetted_role: Final = point.get("role", None)
|
||||
candidates: Final = [
|
||||
candidates: Final = tuple(
|
||||
index
|
||||
for index, message in enumerate(messages)
|
||||
if targetted_role is None or message.get("role") == targetted_role
|
||||
]
|
||||
)
|
||||
|
||||
# Case 1: Target by role alone
|
||||
if targetted_index is None:
|
||||
|
|
@ -1016,7 +1015,7 @@ class AnthropicCacheControlHook(CustomPromptManagement):
|
|||
position = targetted_index
|
||||
while position >= 0:
|
||||
index = candidates[position]
|
||||
if index not in already_marked and _message_accepts_cache_control(messages[index]):
|
||||
if index not in taken and _message_accepts_cache_control(messages[index]):
|
||||
return [index]
|
||||
position -= 1
|
||||
return []
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue