feat: first version of prompt caching
This commit is contained in:
33
anthropic_prompt_caching.py
Normal file
33
anthropic_prompt_caching.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
class Filter:
|
||||||
|
class Valves(BaseModel):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.valves = self.Valves()
|
||||||
|
self.toggle = True # IMPORTANT: This creates a switch UI in Open WebUI
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def inlet(
|
||||||
|
self, body: dict, __event_emitter__, __user__: Optional[dict] = None
|
||||||
|
) -> dict:
|
||||||
|
await __event_emitter__(
|
||||||
|
{
|
||||||
|
"type": "status",
|
||||||
|
"data": {
|
||||||
|
"description": "Prompt Caching Enabled!",
|
||||||
|
"done": True,
|
||||||
|
"hidden": False,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
print("Prompt Caching Enabled!")
|
||||||
|
last_message_item = body["messages"][-1]
|
||||||
|
# 如果 content 是字符串
|
||||||
|
if isinstance(last_message_item["content"], str):
|
||||||
|
last_message_item['content'] = [{"type": "text", "text": last_message_item["content"], "cache_control": {"type": "ephemeral"}}]
|
||||||
|
else:
|
||||||
|
print("Prompt Caching Failed: unsupported content type")
|
||||||
|
return body
|
Reference in New Issue
Block a user