feat: add prompt caching for NativeClaudeSession

This commit is contained in:
Liang Jiaqing
2026-03-24 20:00:44 +08:00
parent f9a0aa71fa
commit 28f22c3973

View File

@@ -484,10 +484,14 @@ class NativeClaudeSession:
def raw_ask(self, messages, tools=None, system=None, model=None, temperature=0.5, max_tokens=6144): def raw_ask(self, messages, tools=None, system=None, model=None, temperature=0.5, max_tokens=6144):
"""底层API调用。yields text chunksgenerator return = list[content_block]""" """底层API调用。yields text chunksgenerator return = list[content_block]"""
model = model or self.default_model model = model or self.default_model
headers = {"x-api-key": self.api_key, "Content-Type": "application/json", "anthropic-version": "2023-06-01"} headers = {"x-api-key": self.api_key, "Content-Type": "application/json", "anthropic-version": "2023-06-01", "anthropic-beta": "prompt-caching-2024-07-31"}
payload = {"model": model, "messages": messages, "temperature": temperature, "max_tokens": max_tokens, "stream": True} payload = {"model": model, "messages": messages, "temperature": temperature, "max_tokens": max_tokens, "stream": True}
if tools: payload["tools"] = tools if tools:
if system: payload["system"] = system tools = [dict(t) for t in tools]
tools[-1]["cache_control"] = {"type": "ephemeral"}
payload["tools"] = tools
if system:
payload["system"] = [{"type": "text", "text": system, "cache_control": {"type": "ephemeral"}}]
try: try:
resp = requests.post(auto_make_url(self.api_base, "messages"), headers=headers, json=payload, stream=True, timeout=120) resp = requests.post(auto_make_url(self.api_base, "messages"), headers=headers, json=payload, stream=True, timeout=120)
if resp.status_code != 200: if resp.status_code != 200: