mirror of
https://github.com/open-webui/pipelines
synced 2025-06-26 18:15:58 +00:00
chore
This commit is contained in:
63
examples/filters/conversation_turn_limit_filter.py
Normal file
63
examples/filters/conversation_turn_limit_filter.py
Normal file
@@ -0,0 +1,63 @@
|
||||
import os
|
||||
from typing import List, Optional
|
||||
from pydantic import BaseModel
|
||||
from schemas import OpenAIChatMessage
|
||||
import time
|
||||
|
||||
|
||||
class Pipeline:
|
||||
class Valves(BaseModel):
|
||||
# List target pipeline ids (models) that this filter will be connected to.
|
||||
# If you want to connect this filter to all pipelines, you can set pipelines to ["*"]
|
||||
pipelines: List[str] = []
|
||||
|
||||
# Assign a priority level to the filter pipeline.
|
||||
# The priority level determines the order in which the filter pipelines are executed.
|
||||
# The lower the number, the higher the priority.
|
||||
priority: int = 0
|
||||
|
||||
# Valves for conversation turn limiting
|
||||
target_user_roles: List[str] = ["user"]
|
||||
max_turns: Optional[int] = None
|
||||
|
||||
def __init__(self):
|
||||
# Pipeline filters are only compatible with Open WebUI
|
||||
# You can think of filter pipeline as a middleware that can be used to edit the form data before it is sent to the OpenAI API.
|
||||
self.type = "filter"
|
||||
|
||||
# Assign a unique identifier to the pipeline.
|
||||
# The identifier must be unique across all pipelines.
|
||||
# The identifier must be an alphanumeric string that can include underscores or hyphens. It cannot contain spaces, special characters, slashes, or backslashes.
|
||||
self.id = "conversation_turn_limit_filter_pipeline"
|
||||
self.name = "Conversation Turn Limit Filter"
|
||||
|
||||
self.valves = self.Valves(
|
||||
**{
|
||||
"pipelines": os.getenv("CONVERSATION_TURN_PIPELINES", "*").split(","),
|
||||
"max_turns": 10,
|
||||
}
|
||||
)
|
||||
|
||||
async def on_startup(self):
|
||||
# This function is called when the server is started.
|
||||
print(f"on_startup:{__name__}")
|
||||
pass
|
||||
|
||||
async def on_shutdown(self):
|
||||
# This function is called when the server is stopped.
|
||||
print(f"on_shutdown:{__name__}")
|
||||
pass
|
||||
|
||||
async def inlet(self, body: dict, user: Optional[dict] = None) -> dict:
|
||||
print(f"pipe:{__name__}")
|
||||
print(body)
|
||||
print(user)
|
||||
|
||||
if user.get("role", "admin") in self.valves.target_user_roles:
|
||||
messages = body.get("messages", [])
|
||||
if len(messages) > self.valves.max_turns:
|
||||
raise Exception(
|
||||
f"Conversation turn limit exceeded. Max turns: {self.valves.max_turns}"
|
||||
)
|
||||
|
||||
return body
|
||||
83
examples/filters/detoxify_filter_pipeline.py
Normal file
83
examples/filters/detoxify_filter_pipeline.py
Normal file
@@ -0,0 +1,83 @@
|
||||
"""
|
||||
title: Detoxify Filter Pipeline
|
||||
author: open-webui
|
||||
date: 2024-05-30
|
||||
version: 1.0
|
||||
license: MIT
|
||||
description: A pipeline for filtering out toxic messages using the Detoxify library.
|
||||
requirements: detoxify
|
||||
"""
|
||||
|
||||
from typing import List, Optional
|
||||
from schemas import OpenAIChatMessage
|
||||
from pydantic import BaseModel
|
||||
from detoxify import Detoxify
|
||||
import os
|
||||
|
||||
|
||||
class Pipeline:
|
||||
class Valves(BaseModel):
|
||||
# List target pipeline ids (models) that this filter will be connected to.
|
||||
# If you want to connect this filter to all pipelines, you can set pipelines to ["*"]
|
||||
# e.g. ["llama3:latest", "gpt-3.5-turbo"]
|
||||
pipelines: List[str] = []
|
||||
|
||||
# Assign a priority level to the filter pipeline.
|
||||
# The priority level determines the order in which the filter pipelines are executed.
|
||||
# The lower the number, the higher the priority.
|
||||
priority: int = 0
|
||||
|
||||
def __init__(self):
|
||||
# Pipeline filters are only compatible with Open WebUI
|
||||
# You can think of filter pipeline as a middleware that can be used to edit the form data before it is sent to the OpenAI API.
|
||||
self.type = "filter"
|
||||
|
||||
# Optionally, you can set the id and name of the pipeline.
|
||||
# Assign a unique identifier to the pipeline.
|
||||
# The identifier must be unique across all pipelines.
|
||||
# The identifier must be an alphanumeric string that can include underscores or hyphens. It cannot contain spaces, special characters, slashes, or backslashes.
|
||||
self.id = "detoxify_filter_pipeline"
|
||||
self.name = "Detoxify Filter"
|
||||
|
||||
# Initialize
|
||||
self.valves = self.Valves(
|
||||
**{
|
||||
"pipelines": ["*"], # Connect to all pipelines
|
||||
}
|
||||
)
|
||||
|
||||
self.model = None
|
||||
|
||||
pass
|
||||
|
||||
async def on_startup(self):
|
||||
# This function is called when the server is started.
|
||||
print(f"on_startup:{__name__}")
|
||||
|
||||
self.model = Detoxify("original")
|
||||
pass
|
||||
|
||||
async def on_shutdown(self):
|
||||
# This function is called when the server is stopped.
|
||||
print(f"on_shutdown:{__name__}")
|
||||
pass
|
||||
|
||||
async def on_valves_updated(self):
|
||||
# This function is called when the valves are updated.
|
||||
pass
|
||||
|
||||
async def inlet(self, body: dict, user: Optional[dict] = None) -> dict:
|
||||
# This filter is applied to the form data before it is sent to the OpenAI API.
|
||||
print(f"inlet:{__name__}")
|
||||
|
||||
print(body)
|
||||
user_message = body["messages"][-1]["content"]
|
||||
|
||||
# Filter out toxic messages
|
||||
toxicity = self.model.predict(user_message)
|
||||
print(toxicity)
|
||||
|
||||
if toxicity["toxicity"] > 0.5:
|
||||
raise Exception("Toxic message detected")
|
||||
|
||||
return body
|
||||
103
examples/filters/langfuse_filter_pipeline.py
Normal file
103
examples/filters/langfuse_filter_pipeline.py
Normal file
@@ -0,0 +1,103 @@
|
||||
"""
|
||||
title: Langfuse Filter Pipeline
|
||||
author: open-webui
|
||||
date: 2024-05-30
|
||||
version: 1.0
|
||||
license: MIT
|
||||
description: A filter pipeline that uses Langfuse.
|
||||
requirements: langfuse
|
||||
"""
|
||||
|
||||
from typing import List, Optional
|
||||
from schemas import OpenAIChatMessage
|
||||
import os
|
||||
|
||||
|
||||
from pydantic import BaseModel
|
||||
from langfuse import Langfuse
|
||||
|
||||
|
||||
class Pipeline:
|
||||
|
||||
class Valves(BaseModel):
|
||||
# List target pipeline ids (models) that this filter will be connected to.
|
||||
# If you want to connect this filter to all pipelines, you can set pipelines to ["*"]
|
||||
# e.g. ["llama3:latest", "gpt-3.5-turbo"]
|
||||
pipelines: List[str] = []
|
||||
|
||||
# Assign a priority level to the filter pipeline.
|
||||
# The priority level determines the order in which the filter pipelines are executed.
|
||||
# The lower the number, the higher the priority.
|
||||
priority: int = 0
|
||||
|
||||
# Valves
|
||||
secret_key: str
|
||||
public_key: str
|
||||
host: str
|
||||
|
||||
def __init__(self):
|
||||
# Pipeline filters are only compatible with Open WebUI
|
||||
# You can think of filter pipeline as a middleware that can be used to edit the form data before it is sent to the OpenAI API.
|
||||
self.type = "filter"
|
||||
|
||||
# Optionally, you can set the id and name of the pipeline.
|
||||
# Assign a unique identifier to the pipeline.
|
||||
# The identifier must be unique across all pipelines.
|
||||
# The identifier must be an alphanumeric string that can include underscores or hyphens. It cannot contain spaces, special characters, slashes, or backslashes.
|
||||
self.id = "langfuse_filter_pipeline"
|
||||
self.name = "Langfuse Filter"
|
||||
|
||||
# Initialize
|
||||
self.valves = self.Valves(
|
||||
**{
|
||||
"pipelines": ["*"], # Connect to all pipelines
|
||||
"secret_key": os.getenv("LANGFUSE_SECRET_KEY"),
|
||||
"public_key": os.getenv("LANGFUSE_PUBLIC_KEY"),
|
||||
"host": os.getenv("LANGFUSE_HOST", "https://cloud.langfuse.com"),
|
||||
}
|
||||
)
|
||||
|
||||
self.langfuse = None
|
||||
pass
|
||||
|
||||
async def on_startup(self):
|
||||
# This function is called when the server is started.
|
||||
print(f"on_startup:{__name__}")
|
||||
self.set_langfuse()
|
||||
pass
|
||||
|
||||
async def on_shutdown(self):
|
||||
# This function is called when the server is stopped.
|
||||
print(f"on_shutdown:{__name__}")
|
||||
self.langfuse.flush()
|
||||
pass
|
||||
|
||||
async def on_valves_updated(self):
|
||||
# This function is called when the valves are updated.
|
||||
|
||||
self.set_langfuse()
|
||||
pass
|
||||
|
||||
def set_langfuse(self):
|
||||
self.langfuse = Langfuse(
|
||||
secret_key=self.valves.secret_key,
|
||||
public_key=self.valves.public_key,
|
||||
host=self.valves.host,
|
||||
debug=False,
|
||||
)
|
||||
self.langfuse.auth_check()
|
||||
|
||||
async def inlet(self, body: dict, user: Optional[dict] = None) -> dict:
|
||||
print(f"inlet:{__name__}")
|
||||
|
||||
trace = self.langfuse.trace(
|
||||
name=f"filter:{__name__}",
|
||||
input=body,
|
||||
user_id=user["id"],
|
||||
metadata={"name": user["name"]},
|
||||
session_id=body["chat_id"],
|
||||
)
|
||||
|
||||
print(trace.get_trace_url())
|
||||
|
||||
return body
|
||||
141
examples/filters/libretranslate_filter_pipeline.py
Normal file
141
examples/filters/libretranslate_filter_pipeline.py
Normal file
@@ -0,0 +1,141 @@
|
||||
from typing import List, Optional
|
||||
from schemas import OpenAIChatMessage
|
||||
from pydantic import BaseModel
|
||||
import requests
|
||||
import os
|
||||
|
||||
from utils.main import get_last_user_message, get_last_assistant_message
|
||||
|
||||
|
||||
class Pipeline:
|
||||
|
||||
class Valves(BaseModel):
|
||||
# List target pipeline ids (models) that this filter will be connected to.
|
||||
# If you want to connect this filter to all pipelines, you can set pipelines to ["*"]
|
||||
# e.g. ["llama3:latest", "gpt-3.5-turbo"]
|
||||
pipelines: List[str] = []
|
||||
|
||||
# Assign a priority level to the filter pipeline.
|
||||
# The priority level determines the order in which the filter pipelines are executed.
|
||||
# The lower the number, the higher the priority.
|
||||
priority: int = 0
|
||||
|
||||
# Valves
|
||||
libretranslate_url: str
|
||||
|
||||
# Source and target languages
|
||||
# User message will be translated from source_user to target_user
|
||||
source_user: Optional[str] = "auto"
|
||||
target_user: Optional[str] = "en"
|
||||
|
||||
# Assistant languages
|
||||
# Assistant message will be translated from source_assistant to target_assistant
|
||||
source_assistant: Optional[str] = "en"
|
||||
target_assistant: Optional[str] = "es"
|
||||
|
||||
def __init__(self):
|
||||
# Pipeline filters are only compatible with Open WebUI
|
||||
# You can think of filter pipeline as a middleware that can be used to edit the form data before it is sent to the OpenAI API.
|
||||
self.type = "filter"
|
||||
|
||||
# Optionally, you can set the id and name of the pipeline.
|
||||
# Assign a unique identifier to the pipeline.
|
||||
# The identifier must be unique across all pipelines.
|
||||
# The identifier must be an alphanumeric string that can include underscores or hyphens. It cannot contain spaces, special characters, slashes, or backslashes.
|
||||
self.id = "libretranslate_filter_pipeline"
|
||||
self.name = "LibreTranslate Filter"
|
||||
|
||||
# Initialize
|
||||
self.valves = self.Valves(
|
||||
**{
|
||||
"pipelines": ["*"], # Connect to all pipelines
|
||||
"libretranslate_url": os.getenv(
|
||||
"LIBRETRANSLATE_API_BASE_URL", "http://localhost:5000"
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
pass
|
||||
|
||||
async def on_startup(self):
|
||||
# This function is called when the server is started.
|
||||
print(f"on_startup:{__name__}")
|
||||
pass
|
||||
|
||||
async def on_shutdown(self):
|
||||
# This function is called when the server is stopped.
|
||||
print(f"on_shutdown:{__name__}")
|
||||
pass
|
||||
|
||||
async def on_valves_updated(self):
|
||||
# This function is called when the valves are updated.
|
||||
pass
|
||||
|
||||
def translate(self, text: str, source: str, target: str) -> str:
|
||||
payload = {
|
||||
"q": text,
|
||||
"source": source,
|
||||
"target": target,
|
||||
}
|
||||
|
||||
try:
|
||||
r = requests.post(
|
||||
f"{self.valves.libretranslate_url}/translate", json=payload
|
||||
)
|
||||
r.raise_for_status()
|
||||
|
||||
data = r.json()
|
||||
return data["translatedText"]
|
||||
except Exception as e:
|
||||
print(f"Error translating text: {e}")
|
||||
return text
|
||||
|
||||
async def inlet(self, body: dict, user: Optional[dict] = None) -> dict:
|
||||
print(f"inlet:{__name__}")
|
||||
|
||||
messages = body["messages"]
|
||||
user_message = get_last_user_message(messages)
|
||||
|
||||
print(f"User message: {user_message}")
|
||||
|
||||
# Translate user message
|
||||
translated_user_message = self.translate(
|
||||
user_message,
|
||||
self.valves.source_user,
|
||||
self.valves.target_user,
|
||||
)
|
||||
|
||||
print(f"Translated user message: {translated_user_message}")
|
||||
|
||||
for message in reversed(messages):
|
||||
if message["role"] == "user":
|
||||
message["content"] = translated_user_message
|
||||
break
|
||||
|
||||
body = {**body, "messages": messages}
|
||||
return body
|
||||
|
||||
async def outlet(self, body: dict, user: Optional[dict] = None) -> dict:
|
||||
print(f"outlet:{__name__}")
|
||||
|
||||
messages = body["messages"]
|
||||
assistant_message = get_last_assistant_message(messages)
|
||||
|
||||
print(f"Assistant message: {assistant_message}")
|
||||
|
||||
# Translate assistant message
|
||||
translated_assistant_message = self.translate(
|
||||
assistant_message,
|
||||
self.valves.source_assistant,
|
||||
self.valves.target_assistant,
|
||||
)
|
||||
|
||||
print(f"Translated assistant message: {translated_assistant_message}")
|
||||
|
||||
for message in reversed(messages):
|
||||
if message["role"] == "assistant":
|
||||
message["content"] = translated_assistant_message
|
||||
break
|
||||
|
||||
body = {**body, "messages": messages}
|
||||
return body
|
||||
126
examples/filters/rate_limit_filter_pipeline.py
Normal file
126
examples/filters/rate_limit_filter_pipeline.py
Normal file
@@ -0,0 +1,126 @@
|
||||
import os
|
||||
from typing import List, Optional
|
||||
from pydantic import BaseModel
|
||||
from schemas import OpenAIChatMessage
|
||||
import time
|
||||
|
||||
|
||||
class Pipeline:
|
||||
class Valves(BaseModel):
|
||||
# List target pipeline ids (models) that this filter will be connected to.
|
||||
# If you want to connect this filter to all pipelines, you can set pipelines to ["*"]
|
||||
pipelines: List[str] = []
|
||||
|
||||
# Assign a priority level to the filter pipeline.
|
||||
# The priority level determines the order in which the filter pipelines are executed.
|
||||
# The lower the number, the higher the priority.
|
||||
priority: int = 0
|
||||
|
||||
# Valves for rate limiting
|
||||
requests_per_minute: Optional[int] = None
|
||||
requests_per_hour: Optional[int] = None
|
||||
sliding_window_limit: Optional[int] = None
|
||||
sliding_window_minutes: Optional[int] = None
|
||||
|
||||
def __init__(self):
|
||||
# Pipeline filters are only compatible with Open WebUI
|
||||
# You can think of filter pipeline as a middleware that can be used to edit the form data before it is sent to the OpenAI API.
|
||||
self.type = "filter"
|
||||
|
||||
# Assign a unique identifier to the pipeline.
|
||||
# The identifier must be unique across all pipelines.
|
||||
# The identifier must be an alphanumeric string that can include underscores or hyphens. It cannot contain spaces, special characters, slashes, or backslashes.
|
||||
self.id = "rate_limit_filter_pipeline"
|
||||
self.name = "Rate Limit Filter"
|
||||
|
||||
# Initialize rate limits
|
||||
self.valves = self.Valves(
|
||||
**{
|
||||
"pipelines": os.getenv("RATE_LIMIT_PIPELINES", "*").split(","),
|
||||
"requests_per_minute": int(
|
||||
os.getenv("RATE_LIMIT_REQUESTS_PER_MINUTE", 10)
|
||||
),
|
||||
"requests_per_hour": int(
|
||||
os.getenv("RATE_LIMIT_REQUESTS_PER_HOUR", 1000)
|
||||
),
|
||||
"sliding_window_limit": int(
|
||||
os.getenv("RATE_LIMIT_SLIDING_WINDOW_LIMIT", 100)
|
||||
),
|
||||
"sliding_window_minutes": int(
|
||||
os.getenv("RATE_LIMIT_SLIDING_WINDOW_MINUTES", 15)
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
# Tracking data - user_id -> (timestamps of requests)
|
||||
self.user_requests = {}
|
||||
|
||||
async def on_startup(self):
|
||||
# This function is called when the server is started.
|
||||
print(f"on_startup:{__name__}")
|
||||
pass
|
||||
|
||||
async def on_shutdown(self):
|
||||
# This function is called when the server is stopped.
|
||||
print(f"on_shutdown:{__name__}")
|
||||
pass
|
||||
|
||||
def prune_requests(self, user_id: str):
|
||||
"""Prune old requests that are outside of the sliding window period."""
|
||||
now = time.time()
|
||||
if user_id in self.user_requests:
|
||||
self.user_requests[user_id] = [
|
||||
req
|
||||
for req in self.user_requests[user_id]
|
||||
if (
|
||||
(self.valves.requests_per_minute is not None and now - req < 60)
|
||||
or (self.valves.requests_per_hour is not None and now - req < 3600)
|
||||
or (
|
||||
self.valves.sliding_window_limit is not None
|
||||
and now - req < self.valves.sliding_window_minutes * 60
|
||||
)
|
||||
)
|
||||
]
|
||||
|
||||
def log_request(self, user_id: str):
|
||||
"""Log a new request for a user."""
|
||||
now = time.time()
|
||||
if user_id not in self.user_requests:
|
||||
self.user_requests[user_id] = []
|
||||
self.user_requests[user_id].append(now)
|
||||
|
||||
def rate_limited(self, user_id: str) -> bool:
|
||||
"""Check if a user is rate limited."""
|
||||
self.prune_requests(user_id)
|
||||
|
||||
user_reqs = self.user_requests.get(user_id, [])
|
||||
|
||||
if self.valves.requests_per_minute is not None:
|
||||
requests_last_minute = sum(1 for req in user_reqs if time.time() - req < 60)
|
||||
if requests_last_minute >= self.valves.requests_per_minute:
|
||||
return True
|
||||
|
||||
if self.valves.requests_per_hour is not None:
|
||||
requests_last_hour = sum(1 for req in user_reqs if time.time() - req < 3600)
|
||||
if requests_last_hour >= self.valves.requests_per_hour:
|
||||
return True
|
||||
|
||||
if self.valves.sliding_window_limit is not None:
|
||||
requests_in_window = len(user_reqs)
|
||||
if requests_in_window >= self.valves.sliding_window_limit:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
async def inlet(self, body: dict, user: Optional[dict] = None) -> dict:
|
||||
print(f"pipe:{__name__}")
|
||||
print(body)
|
||||
print(user)
|
||||
|
||||
if user.get("role", "admin") == "user":
|
||||
user_id = user["id"] if user and "id" in user else "default_user"
|
||||
if self.rate_limited(user_id):
|
||||
raise Exception("Rate limit exceeded. Please try again later.")
|
||||
|
||||
self.log_request(user_id)
|
||||
return body
|
||||
Reference in New Issue
Block a user