feat: Filter can modify the chat records ultimately stored in the database #5683

Closed
opened 2025-11-11 16:29:15 -06:00 by GiteaMirror · 1 comment
Owner

Originally created by @0721-Day on GitHub (Jul 2, 2025).

Check Existing Issues

  • I have searched the existing issues and discussions.

Problem Description

I created a Filter to generate random numbers within the chat.
It works normally, but before each request, I need to go through all the chat records to replace this random value.
It cannot be stored in the database, and I haven't found a better solution
Perhaps "Pipe" can achieve this function, but if we insist on using its proxy for every model, I believe it would be counterproductive.
It could also be that I am not very familiar with the project, and I am unable to find a reasonable solution

async def inlet(self, body: dict, __event_emitter__) -> dict:
        # This is where you manipulate user inputs.

        chat_id = body["metadata"]["chat_id"]

        random.seed(chat_id)

        __RANDOM__ = r"\{\{RANDOM\#\[([^\]]+)\]\#(\d+)\}\}"

        def replace_random(match):
            items = [item.strip() for item in match.group(1).split(",")]
            count = int(match.group(2))
            selected = random.sample(items, min(count, len(items)))
            return ", ".join(selected)

        for i, msg in enumerate(body["messages"]):
            if i >= 10:
                break
            if "content" in msg and isinstance(msg["content"],str):
                msg["content"] = re.sub(__RANDOM__, replace_random, msg["content"])

        return body

Desired Solution you'd like

There are optional parameters or other methods to directly modify the original content

Alternatives Considered

No response

Additional Context

No response

Originally created by @0721-Day on GitHub (Jul 2, 2025). ### Check Existing Issues - [x] I have searched the existing issues and discussions. ### Problem Description I created a Filter to generate random numbers within the chat. It works normally, but before each request, I need to go through all the chat records to replace this random value. It cannot be stored in the database, and I haven't found a better solution Perhaps "Pipe" can achieve this function, but if we insist on using its proxy for every model, I believe it would be counterproductive. It could also be that I am not very familiar with the project, and I am unable to find a reasonable solution ``` async def inlet(self, body: dict, __event_emitter__) -> dict: # This is where you manipulate user inputs. chat_id = body["metadata"]["chat_id"] random.seed(chat_id) __RANDOM__ = r"\{\{RANDOM\#\[([^\]]+)\]\#(\d+)\}\}" def replace_random(match): items = [item.strip() for item in match.group(1).split(",")] count = int(match.group(2)) selected = random.sample(items, min(count, len(items))) return ", ".join(selected) for i, msg in enumerate(body["messages"]): if i >= 10: break if "content" in msg and isinstance(msg["content"],str): msg["content"] = re.sub(__RANDOM__, replace_random, msg["content"]) return body ``` ### Desired Solution you'd like There are optional parameters or other methods to directly modify the original content ### Alternatives Considered _No response_ ### Additional Context _No response_
Author
Owner

@tjbck commented on GitHub (Jul 2, 2025):

Not entirely sure what you meant here, could you elaborate a bit more?

@tjbck commented on GitHub (Jul 2, 2025): Not entirely sure what you meant here, could you elaborate a bit more?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#5683