feat: Add Documentation for Document Function and Process Method

Related PR; https://github.com/open-webui/open-webui/pull/13479
This commit is contained in:
Athanasios Oikonomou 2025-05-04 14:06:43 +03:00 committed by Athanasios Oikonomou
parent a164415416
commit f374a558c8
2 changed files with 81 additions and 0 deletions

View File

@ -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 youre transforming document for better context or cleaning up for improved readability, **Document Functions** let you do it all.
<details>
<summary>Example</summary>
```
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()
```
</details>

View File

@ -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