From f374a558c829de4652088b46b8d5f8b7ba4a46fb Mon Sep 17 00:00:00 2001 From: Athanasios Oikonomou Date: Sun, 4 May 2025 14:06:43 +0300 Subject: [PATCH] feat: Add Documentation for Document Function and Process Method Related PR; https://github.com/open-webui/open-webui/pull/13479 --- docs/features/plugin/functions/document.mdx | 70 +++++++++++++++++++++ docs/features/plugin/functions/index.mdx | 11 ++++ 2 files changed, 81 insertions(+) create mode 100644 docs/features/plugin/functions/document.mdx diff --git a/docs/features/plugin/functions/document.mdx b/docs/features/plugin/functions/document.mdx new file mode 100644 index 00000000..ed6ac8d8 --- /dev/null +++ b/docs/features/plugin/functions/document.mdx @@ -0,0 +1,70 @@ +--- +sidebar_position: 4 +title: "๐Ÿ“„ Document Function" +--- + +# ๐Ÿ“„ Document Function: Process document prior inserting to vector database + +Welcome to the comprehensive guide on Document Functions in Open WebUI! +Document processors are a flexible and powerful **plugin system** for modifying data *before it's sent to the Vector Database. +Whether youโ€™re transforming document for better context or cleaning up for improved readability, **Document Functions** let you do it all. + +
+Example + +``` +import re +import logging +from pydantic import BaseModel, Field + +from open_webui.env import SRC_LOG_LEVELS + +log = logging.getLogger(__name__) +log.setLevel(SRC_LOG_LEVELS["MAIN"]) + +class Document: + """ + Document cleaner that removes common artifacts from imported documents. + + This processor cleans up document text by removing: + - Document banners (CONFIDENTIAL, INTERNAL, DRAFT) + - Page numbers and footers + - Excessive whitespace and line breaks + """ + + class Valves(BaseModel): + priority: int = Field(default=0, description="Processing priority (lower numbers run first)") + + def __init__(self): + self.valves = self.Valves() + + def process(self, text: str) -> str: + """ + Clean document text by removing common artifacts. + + Args: + text: Original document text + + Returns: + Cleaned document text + """ + if not text or not isinstance(text, str): + return text + + # Remove banners like "## INTERNAL ##", "**CONFIDENTIAL**", etc. + text = re.sub( + r"^\s*[#*\-]*\s*(internal|confidential|draft)\s*[#*\-]*\s*$", + "", + text, + flags=re.IGNORECASE | re.MULTILINE, + ) + + # Remove footers like "page 3 of 10" + text = re.sub(r"page\s*\d+\s*of\s*\d+", "", text, flags=re.IGNORECASE) + + # Collapse excessive empty lines + text = re.sub(r"\n\s*\n+", "\n\n", text) + + return text.strip() +``` +
\ No newline at end of file diff --git a/docs/features/plugin/functions/index.mdx b/docs/features/plugin/functions/index.mdx index 8c2feec4..86868b36 100644 --- a/docs/features/plugin/functions/index.mdx +++ b/docs/features/plugin/functions/index.mdx @@ -75,6 +75,17 @@ Buttons provide a **clean and user-friendly way** to interact with extended func Learn how to set them up in the [**Action Functions Guide**](./action.mdx). +### 4. [**Document Function** - Process Document](./document.mdx) + +An **Document Function** is used to process a document before inserting it into vector databases. These functions enable transformations or enhancements of the document content, ensuring the text is formatted, cleaned, or enriched according to the business needs before being stored or indexed. + +#### **How It Works** +1. **Definition**: A Document Function typically accepts a `text` input (the content of a document) and returns a processed version of the content (as a `str`). +2. **Pipeline Integration**: Document functions are part of a processing pipeline that can be applied to documents during their loading process. These functions can be chained, allowing multiple transformations to be applied to the document content. +3. **Dynamic Loading**: These functions are dynamically loaded and executed based on their configuration in the system. + +Learn how to set them up in the [**Document Process Functions Guide**](./document.mdx). + --- ## ๐Ÿ› ๏ธ How to Use Functions