open-webui/backend/open_webui/apps/webui/models/prompts.py

155 lines
4.4 KiB
Python
Raw Normal View History

2024-01-03 04:41:37 +00:00
import time
2024-08-27 22:10:27 +00:00
from typing import Optional
2024-01-03 04:41:37 +00:00
from open_webui.apps.webui.internal.db import Base, get_db
2024-11-18 14:19:34 +00:00
from open_webui.apps.webui.models.users import Users, UserResponse
2024-11-15 09:29:07 +00:00
2024-08-27 22:10:27 +00:00
from pydantic import BaseModel, ConfigDict
2024-11-15 02:57:25 +00:00
from sqlalchemy import BigInteger, Column, String, Text, JSON
2024-01-03 04:41:37 +00:00
2024-11-17 01:09:15 +00:00
from open_webui.utils.access_control import has_access
2024-01-03 04:41:37 +00:00
####################
# Prompts DB Schema
####################
class Prompt(Base):
__tablename__ = "prompt"
2024-01-03 04:41:37 +00:00
command = Column(String, primary_key=True)
user_id = Column(String)
title = Column(Text)
content = Column(Text)
timestamp = Column(BigInteger)
2024-01-03 04:41:37 +00:00
2024-11-15 02:57:25 +00:00
access_control = Column(JSON, nullable=True) # Controls data access levels.
2024-11-15 04:13:43 +00:00
# Defines access control rules for this entry.
# - `None`: Public access, available to all users with the "user" role.
# - `{}`: Private access, restricted exclusively to the owner.
# - Custom permissions: Specific access control for reading and writing;
# Can specify group or user-level restrictions:
# {
# "read": {
# "group_ids": ["group_id1", "group_id2"],
# "user_ids": ["user_id1", "user_id2"]
# },
# "write": {
# "group_ids": ["group_id1", "group_id2"],
# "user_ids": ["user_id1", "user_id2"]
# }
# }
2024-11-15 02:57:25 +00:00
2024-01-03 04:41:37 +00:00
class PromptModel(BaseModel):
command: str
user_id: str
title: str
content: str
timestamp: int # timestamp in epoch
2024-11-15 04:13:43 +00:00
access_control: Optional[dict] = None
model_config = ConfigDict(from_attributes=True)
2024-01-03 04:41:37 +00:00
####################
# Forms
####################
2024-11-18 14:19:34 +00:00
class PromptUserResponse(PromptModel):
user: Optional[UserResponse] = None
2024-01-03 04:41:37 +00:00
class PromptForm(BaseModel):
command: str
title: str
content: str
2024-11-17 01:49:13 +00:00
access_control: Optional[dict] = None
2024-01-03 04:41:37 +00:00
class PromptsTable:
2024-03-31 08:13:39 +00:00
def insert_new_prompt(
self, user_id: str, form_data: PromptForm
2024-03-31 08:13:39 +00:00
) -> Optional[PromptModel]:
prompt = PromptModel(
**{
"user_id": user_id,
2024-11-17 04:52:57 +00:00
**form_data.model_dump(),
"timestamp": int(time.time()),
}
)
try:
2024-07-04 06:32:39 +00:00
with get_db() as db:
2024-11-17 04:52:57 +00:00
result = Prompt(**prompt.model_dump())
2024-07-04 06:32:39 +00:00
db.add(result)
db.commit()
db.refresh(result)
if result:
return PromptModel.model_validate(result)
else:
return None
2024-08-27 22:10:27 +00:00
except Exception:
return None
2024-01-03 04:41:37 +00:00
def get_prompt_by_command(self, command: str) -> Optional[PromptModel]:
try:
2024-07-04 06:32:39 +00:00
with get_db() as db:
prompt = db.query(Prompt).filter_by(command=command).first()
return PromptModel.model_validate(prompt)
2024-08-14 12:38:19 +00:00
except Exception:
return None
2024-01-03 04:41:37 +00:00
2024-11-18 14:19:34 +00:00
def get_prompts(self) -> list[PromptUserResponse]:
2024-07-04 06:32:39 +00:00
with get_db() as db:
return [
2024-11-18 14:19:34 +00:00
PromptUserResponse.model_validate(
{
**PromptModel.model_validate(prompt).model_dump(),
"user": Users.get_user_by_id(prompt.user_id).model_dump(),
}
)
for prompt in db.query(Prompt).all()
2024-07-04 06:32:39 +00:00
]
2024-01-03 04:41:37 +00:00
2024-11-15 09:29:07 +00:00
def get_prompts_by_user_id(
self, user_id: str, permission: str = "write"
2024-11-18 14:19:34 +00:00
) -> list[PromptUserResponse]:
2024-11-15 09:29:07 +00:00
prompts = self.get_prompts()
2024-11-17 01:09:15 +00:00
return [
prompt
for prompt in prompts
if prompt.user_id == user_id
or has_access(user_id, permission, prompt.access_control)
]
2024-11-15 09:29:07 +00:00
2024-01-03 04:41:37 +00:00
def update_prompt_by_command(
self, command: str, form_data: PromptForm
2024-03-31 08:13:39 +00:00
) -> Optional[PromptModel]:
try:
2024-07-04 06:32:39 +00:00
with get_db() as db:
prompt = db.query(Prompt).filter_by(command=command).first()
prompt.title = form_data.title
prompt.content = form_data.content
2024-11-17 01:49:13 +00:00
prompt.access_control = form_data.access_control
2024-07-04 06:32:39 +00:00
prompt.timestamp = int(time.time())
db.commit()
return PromptModel.model_validate(prompt)
2024-08-14 12:38:19 +00:00
except Exception:
return None
def delete_prompt_by_command(self, command: str) -> bool:
try:
2024-07-04 06:32:39 +00:00
with get_db() as db:
db.query(Prompt).filter_by(command=command).delete()
2024-07-06 15:10:58 +00:00
db.commit()
2024-07-04 06:32:39 +00:00
return True
2024-08-14 12:38:19 +00:00
except Exception:
return False
2024-01-03 04:41:37 +00:00
Prompts = PromptsTable()