Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions astrbot/core/provider/sources/anthropic_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"Anthropic Claude API 提供商适配器",
)
class ProviderAnthropic(Provider):
_PROMPT_CACHE_CONTROL = {"type": "ephemeral"}

@staticmethod
def _ensure_usable_response(
llm_response: LLMResponse,
Expand Down Expand Up @@ -479,6 +481,16 @@ def _normalize_tool_choice(tool_choice) -> dict:
logger.warning(f"未知的 tool_choice 值: {tool_choice},已回退为 'auto'")
return {"type": "auto"}

@classmethod
def _apply_explicit_prompt_cache_breakpoints(cls, payloads: dict) -> None:
system_blocks = payloads.get("system")
if not isinstance(system_blocks, list) or not system_blocks:
return

last_block = system_blocks[-1]
if isinstance(last_block, dict) and "cache_control" not in last_block:
last_block["cache_control"] = dict(cls._PROMPT_CACHE_CONTROL)

async def _query(
self,
payloads: dict,
Expand All @@ -497,6 +509,7 @@ async def _query(

if "max_tokens" not in payloads:
payloads["max_tokens"] = 65536
self._apply_explicit_prompt_cache_breakpoints(payloads)
self._apply_thinking_config(payloads)
self._sanitize_assistant_messages(payloads)

Expand Down Expand Up @@ -598,6 +611,7 @@ async def _query_stream(

if "max_tokens" not in payloads:
payloads["max_tokens"] = 65536
self._apply_explicit_prompt_cache_breakpoints(payloads)
self._apply_thinking_config(payloads)
self._sanitize_assistant_messages(payloads)
Comment on lines 612 to 616

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Remove this duplicated payload preparation block as it is now fully handled by the _prepare_query_payload helper method.

References
  1. When implementing similar functionality for different cases, refactor the logic into a shared helper function to avoid code duplication.


Expand Down
Loading