Function
filter
v1.0
Markdown Image Remover
A filter to remove markdown images in chat so they don't interfere with the context. Useful for people using their own tools and functions to generate images.
Function ID
markdown_image_remover
Creator
@violet
Downloads
64+

Function Content
python
"""
title: Markdown Image Remover
author: Violet
funding_url: https://github.com/open-webui
version: 1.0
"""

import re
from typing import Callable, Any, Optional, Awaitable


def remove_markdown_images(content: str) -> str:
    return re.sub(r'!\[.*?\]\([^)]*\)', '', content)


class EventEmitter:
    def __init__(self, event_emitter: Callable[[dict], Any] = None):
        self.event_emitter = event_emitter

    async def emit(self, description="Unknown State", status="in_progress", done=False):
        if self.event_emitter:
            await self.event_emitter(
                {
                    "type": "status",
                    "data": {
                        "status": status,
                        "description": description,
                        "done": done,
                    },
                }
            )


class Filter:
    def __init__(self):
        self.output_text = ""

    async def inlet(
            self,
            body: dict,
            __event_emitter__: Callable[[Any], Awaitable[None]],
            __user__: Optional[dict] = None,
            __model__: Optional[dict] = None,
    ) -> dict:
        emitter = EventEmitter(__event_emitter__)

        await emitter.emit("Removing markdown Images")

        for i, msg in enumerate(body["messages"]):
            body["messages"][i]["content"] = remove_markdown_images(msg["content"])

        await emitter.emit(status="complete", description="Markdown images removed", done=True)

        return body