当前文件是 constants.py
,它位于 open-webui\backend\open_webui\constants.py
。以下是该文件的功能详细描述:
constants.py
这是一个 常量定义文件,主要用于定义系统中使用的各种 消息模板、错误提示、任务类型 等字符串常量。
通过将这些内容集中管理,使得:
class MESSAGES(str, Enum):
DEFAULT = lambda msg="": f"{msg if msg else ''}"
MODEL_ADDED = lambda model="": f"The model '{model}' has been added successfully."
MODEL_DELETED = lambda model="": f"The model '{model}' has been deleted successfully."
MODEL_ADDED("llama3")
→ "The model 'llama3' has been added successfully."
MODEL_DELETED("gpt-4o")
→ "The model 'gpt-4o' has been deleted successfully."
class WEBHOOK_MESSAGES(str, Enum):
DEFAULT = lambda msg="": f"{msg if msg else ''}"
USER_SIGNUP = lambda username="": (
f"New user signed up: {username}" if username else "New user signed up"
)
USER_SIGNUP("john_doe")
→ "New user signed up: john_doe"
这是整个项目中最核心的错误消息集合,分为多个类别:
DEFAULT = lambda err="": f'{"Something went wrong :/" if err == "" else "[ERROR: " + str(err) + "]"}'
ENV_VAR_NOT_FOUND = "Required environment variable not found. Terminating now."
CREATE_USER_ERROR = "Oops! Something went wrong while creating your account..."
EMAIL_TAKEN = "Uh-oh! This email is already registered..."
PASSWORD_TOO_LONG = "Uh-oh! The password you entered is too long..."
COMMAND_TAKEN = "Uh-oh! This command is already registered..."
FILE_EXISTS = "Uh-oh! This file is already registered..."
ID_TAKEN = "Uh-oh! This id is already registered..."
INVALID_TOKEN = "Your session has expired or the token is invalid..."
INVALID_CRED = "The email or password provided is incorrect..."
INVALID_EMAIL_FORMAT = "The email format you entered is invalid..."
UNAUTHORIZED = "401 Unauthorized"
ACCESS_PROHIBITED = "You do not have permission to access this resource..."
ACTION_PROHIBITED = "The requested action has been restricted..."
FILE_NOT_SUPPORTED = "Oops! It seems like the file format you're trying to upload is not supported..."
PANDOC_NOT_INSTALLED = "Pandoc is not installed on the server..."
INCORRECT_FORMAT = lambda err="": f"Invalid format. Please use the correct format{err}"
MODEL_NOT_FOUND = lambda name="": f"Model '{name}' was not found"
OLLAMA_NOT_FOUND = "WebUI could not connect to Ollama"
OPENAI_NOT_FOUND = lambda name="": "OpenAI API was not found"
RATE_LIMIT_EXCEEDED = "API rate limit exceeded"
MALICIOUS = "Unusual activities detected, please try again in a few minutes."
DB_NOT_SQLITE = "This feature is only available when running with SQLite databases."
EXISTING_USERS = "You can't turn off authentication because there are existing users..."
class TASKS(str, Enum):
DEFAULT = lambda task="": f"{task if task else 'generation'}"
TITLE_GENERATION = "title_generation"
TAGS_GENERATION = "tags_generation"
EMOJI_GENERATION = "emoji_generation"
QUERY_GENERATION = "query_generation"
IMAGE_PROMPT_GENERATION = "image_prompt_generation"
AUTOCOMPLETE_GENERATION = "autocomplete_generation"
FUNCTION_CALLING = "function_calling"
MOA_RESPONSE_GENERATION = "moa_response_generation"
TITLE_GENERATION
表示“生成标题”任务。类别 | 说明 | 示例 |
---|---|---|
MESSAGES |
通用操作成功提示 | "Model deleted successfully" |
WEBHOOK_MESSAGES |
Webhook 通知消息 | "New user signed up: john_doe" |
ERROR_MESSAGES |
所有错误提示信息 | "Email already taken" , "File too large" |
TASKS |
任务类型标识符 | "title_generation" , "function_calling" |
当你尝试添加一个已存在的模型时,会返回:
ERROR_MESSAGES.MODEL_ID_TAKEN
# 输出: "Uh-oh! This model id is already registered..."
当用户注册新账号时,会触发 Webhook 消息:
WEBHOOK_MESSAGES.USER_SIGNUP("alice")
# 输出: "New user signed up: alice"
当调用不存在的模型时,会抛出:
ERROR_MESSAGES.MODEL_NOT_FOUND("my-model")
# 输出: "Model 'my-model' was not found"
当需要生成图像提示时,会使用:
TASKS.IMAGE_PROMPT_GENERATION
# 输出: "image_prompt_generation"
你可以根据业务需求,在 .env
或其他配置文件中启用/禁用某些功能,并在对应模块中引用这些常量来统一输出格式。例如:
from open_webui.constants import ERROR_MESSAGES
if not user:
raise Exception(ERROR_MESSAGES.USER_NOT_FOUND)
这有助于构建一致且易维护的错误提示系统。
Enum
和 lambda
函数,可以动态生成带参数的消息。str
,可以直接像字符串一样使用。这个文件是项目的“语言中枢”,负责统一输出所有用户可见的提示、错误、任务类型等信息,是构建友好交互体验的重要基础组件。