enh: boilerplate

This commit is contained in:
Timothy J. Baek 2024-06-29 19:49:16 -07:00
parent 16454c0de5
commit 41c55fbb81

View File

@ -27,61 +27,73 @@
} }
let codeEditor; let codeEditor;
let boilerplate = `from pydantic import BaseModel let boilerplate = `"""
title: Example Filter
author: open-webui
author_url: https://github.com/open-webui
funding_url: https://github.com/open-webui
version: 0.1
"""
from pydantic import BaseModel, Field
from typing import Optional from typing import Optional
class Filter: class Filter:
class Valves(BaseModel): class Valves(BaseModel):
max_turns: int = 4 priority: int = Field(
default=0, description="Priority level for the filter operations."
)
max_turns: int = Field(
default=8, description="Maximum allowable conversation turns for a user."
)
pass
class UserValves(BaseModel):
max_turns: int = Field(
default=4, description="Maximum allowable conversation turns for a user."
)
pass pass
def __init__(self): def __init__(self):
# Indicates custom file handling logic. This flag helps disengage default routines in favor of custom # 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. # 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 # Alternatively, you can remove the files directly from the body in from the inlet hook
self.file_handler = True # self.file_handler = True
# Initialize 'valves' with specific configurations. Using 'Valves' instance helps encapsulate settings, # 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'. # which ensures settings are managed cohesively and not confused with operational flags like 'file_handler'.
self.valves = self.Valves(**{"max_turns": 2}) self.valves = self.Valves()
pass pass
def inlet(self, body: dict, user: Optional[dict] = None) -> dict: def inlet(self, body: dict, __user__: Optional[dict] = None) -> dict:
# Modify the request body or validate it before processing by the chat completion API. # 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. # 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. # It can also modify the request before sending it to the API.
print(f"inlet:{__name__}") print(f"inlet:{__name__}")
print(f"inlet:body:{body}") print(f"inlet:body:{body}")
print(f"inlet:user:{user}") print(f"inlet:user:{__user__}")
if user.get("role", "admin") in ["user", "admin"]: if __user__.get("role", "admin") in ["user", "admin"]:
messages = body.get("messages", []) messages = body.get("messages", [])
if len(messages) > self.valves.max_turns:
max_turns = min(__user__["valves"].max_turns, self.valves.max_turns)
if len(messages) > max_turns:
raise Exception( raise Exception(
f"Conversation turn limit exceeded. Max turns: {self.valves.max_turns}" f"Conversation turn limit exceeded. Max turns: {max_turns}"
) )
return body return body
def outlet(self, body: dict, user: Optional[dict] = None) -> dict: def outlet(self, body: dict, __user__: Optional[dict] = None) -> dict:
# Modify or analyze the response body after processing by the API. # 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 # This function is the post-processor for the API, which can be used to modify the response
# or perform additional checks and analytics. # or perform additional checks and analytics.
print(f"outlet:{__name__}") print(f"outlet:{__name__}")
print(f"outlet:body:{body}") print(f"outlet:body:{body}")
print(f"outlet:user:{user}") print(f"outlet:user:{__user__}")
messages = [
{
**message,
"content": f"{message['content']} - @@Modified from Filter Outlet",
}
for message in body.get("messages", [])
]
return {"messages": messages}
return body
`; `;
const _boilerplate = `from pydantic import BaseModel const _boilerplate = `from pydantic import BaseModel