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
28 changes: 25 additions & 3 deletions filters/warn_if_long_chat.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""
title: WarnIfLongChat
author: thiswillbeyourgithub
authors: thiswillbeyourgithubm, karilaa-dev
author_url: https://github.com/thiswillbeyourgithub/openwebui_custom_pipes_filters/
funding_url: https://github.com/thiswillbeyourgithub/openwebui_custom_pipes_filters/
version: 1.1.0
date: 2025-03-23
version: 1.2.0
date: 2025-04-07
license: GPLv3
description: A filter that adds a soft and hard limit to the number of messages in a chat.
"""
Expand All @@ -28,6 +28,10 @@ class Valves(BaseModel):
default=50,
description="Above that many messages, flat out refuse",
)
limited_models: str = Field(
default="",
description="Comma-separated list of models for which limits are enabled. Applies to all if empty.",
)
debug: bool = Field(
default=False, description="True to add emitter prints",
)
Expand All @@ -44,6 +48,12 @@ async def on_valves_updated(self):
assert self.valves.number_of_message_hard_limit > 5, "number_of_message_hard_limit has to be more than 5"
assert self.valves.number_of_message_hard_limit > self.valves.number_of_message, "number_of_message_hard_limit has to be higher than number_of_message"

# Validate limited_models format
if self.valves.limited_models:
limited_models = [model.strip() for model in self.valves.limited_models.split(",")]
if self.valves.debug:
print(f"Models with limit: {limited_models}")

# Validate exempted_users format
if self.valves.exempted_users:
exempted_users = [user.strip() for user in self.valves.exempted_users.split(',')]
Expand All @@ -56,6 +66,12 @@ async def inlet(
__user__: Optional[dict] = None,
__event_emitter__: Callable[[dict], Any] = None,
) -> dict:
# Check if models are limited
limited_models = [model.strip() for model in self.valves.limited_models.split(",") if model.strip()]
if limited_models:
if body["model"] not in limited_models:
return body

# Check if user is exempted
if __user__ and "name" in __user__:
exempted_users = [user.strip() for user in self.valves.exempted_users.split(',') if user.strip()]
Expand Down Expand Up @@ -92,6 +108,12 @@ async def log(message: str, error: bool = False):
return body

def outlet(self, body: dict, __user__: Optional[dict] = None) -> dict:
# Check if models are limited
limited_models = [model.strip() for model in self.valves.limited_models.split(",") if model.strip()]
if limited_models:
if body["model"] not in limited_models:
return body

# Check if user is exempted
if __user__ and "name" in __user__:
exempted_users = [user.strip() for user in self.valves.exempted_users.split(',') if user.strip()]
Expand Down