Function
filter
v0.1
Thinking Filter
Removes a block of thoughts when chatting with ThoughtStream-4B. Badly, but still.
Function ID
thinking_filter
Creator
@trollek
Downloads
175+

Function Content
python
"""
title: Thinking Filter
author: TrolleK
author_url: https://github.com/hosteren & https://huggingface.co/trollek
version: 0.1
"""
import re
from pydantic import BaseModel, Field
from typing import Optional


class Filter:
    class Valves(BaseModel):
        priority: int = Field(
            default=0, description="Priority level for the filter operations."
        )
        start_thought_token: str = Field(
            default="<\|thought_start\|>", description="The start of thougt token."
        )
        end_thought_token: str = Field(
            default="<\|thought_end\|>", description="The end of thought token."
		)
        pass

    def __init__(self):
        # Indicates custom file handling logic. This flag helps disengage default routines in favor of custom
        # implementations, informing the WebUI to defer file-related operations to designated methods within this class.
        # Alternatively, you can remove the files directly from the body in from the inlet hook
        # self.file_handler = True

        # Initialize 'valves' with specific configurations. Using 'Valves' instance helps encapsulate settings,
        # which ensures settings are managed cohesively and not confused with operational flags like 'file_handler'.
        self.valves = self.Valves()
        self.pattern = re.compile(rf'{self.valves.start_thought_token}(.*?){self.valves.end_thought_token}')
        pass

    def remove_thought(self, text: str) -> str:
        # Remove thought markers from text and return the modified text.
        # This function is used to remove thought markers from a string of text, which are typically used in chat applications to indicate that a message contains a thought or an idea.
        return self.pattern.sub('', text)

    def inlet(self, body: dict, __user__: Optional[dict] = None) -> dict:
        # Modify the request body or validate it before processing by the chat completion API.
        # This function is the pre-processor for the API where various checks on the input can be performed.
        # It can also modify the request before sending it to the API.
        print(f"inlet:{__name__}")
        print(f"inlet:body:{body}")
        print(f"inlet:user:{__user__}")

        return body

    def outlet(self, body: dict, __user__: Optional[dict] = None) -> dict:
        # Modify or analyze the response body after processing by the API.
        # This function is the post-processor for the API, which can be used to modify the response
        # or perform additional checks and analytics.
        print(f"outlet:{__name__}")
        print(f"outlet:body:{body}")
        print(f"outlet:user:{__user__}")

        last_message = body["messages"][-1]["content"]
        if self.pattern.search(last_message):
            body["messages"][-1]["content"] = self.remove_thought(last_message)
        return body