webui / context_clip_filter.json
Nerdur's picture
Upload 21 files
02f33b7 verified
Raw
History Blame Contribute Delete
1.88 kB
[{"id":"context_clip_filter","name":"Context Clip Filter","meta":{"description":"A filter that truncates chat history to retain the latest messages while preserving the system prompt for optimal context management.","manifest":{"title":"Context Clip Filter","author":"open-webui","author_url":"https://github.com/open-webui","funding_url":"https://github.com/open-webui","version":"0.1"},"type":"filter"},"content":"\"\"\"\ntitle: Context Clip Filter\nauthor: open-webui\nauthor_url: https://github.com/open-webui\nfunding_url: https://github.com/open-webui\nversion: 0.1\n\"\"\"\n\nfrom pydantic import BaseModel, Field\nfrom typing import Optional\n\n\nclass Filter:\n class Valves(BaseModel):\n priority: int = Field(\n default=0, description=\"Priority level for the filter operations.\"\n )\n n_last_messages: int = Field(\n default=4, description=\"Number of last messages to retain.\"\n )\n pass\n\n class UserValves(BaseModel):\n pass\n\n def __init__(self):\n self.valves = self.Valves()\n pass\n\n def inlet(self, body: dict, __user__: Optional[dict] = None) -> dict:\n messages = body[\"messages\"]\n # Ensure we always keep the system prompt\n system_prompt = next(\n (message for message in messages if message.get(\"role\") == \"system\"), None\n )\n\n if system_prompt:\n messages = [\n message for message in messages if message.get(\"role\") != \"system\"\n ]\n messages = messages[-self.valves.n_last_messages :]\n messages.insert(0, system_prompt)\n else: # If no system prompt, simply truncate to the last n_last_messages\n messages = messages[-self.valves.n_last_messages :]\n\n body[\"messages\"] = messages\n return body\n"}]