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

111 lines
2.9 KiB
Python
Raw Normal View History

2024-01-02 20:41:37 -08:00
import time
2024-08-28 00:10:27 +02:00
from typing import Optional
2024-01-02 20:41:37 -08:00
from open_webui.apps.webui.internal.db import Base, get_db
2024-08-28 00:10:27 +02:00
from pydantic import BaseModel, ConfigDict
from sqlalchemy import BigInteger, Column, String, Text
2024-01-02 20:41:37 -08:00
####################
# Prompts DB Schema
####################
class Prompt(Base):
__tablename__ = "prompt"
2024-01-02 20:41:37 -08:00
command = Column(String, primary_key=True)
user_id = Column(String)
title = Column(Text)
content = Column(Text)
timestamp = Column(BigInteger)
2024-01-02 20:41:37 -08:00
class PromptModel(BaseModel):
command: str
user_id: str
title: str
content: str
timestamp: int # timestamp in epoch
model_config = ConfigDict(from_attributes=True)
2024-01-02 20:41:37 -08:00
####################
# Forms
####################
class PromptForm(BaseModel):
command: str
title: str
content: str
class PromptsTable:
2024-03-31 01:13:39 -07:00
def insert_new_prompt(
self, user_id: str, form_data: PromptForm
2024-03-31 01:13:39 -07:00
) -> Optional[PromptModel]:
prompt = PromptModel(
**{
"user_id": user_id,
"command": form_data.command,
"title": form_data.title,
"content": form_data.content,
"timestamp": int(time.time()),
}
)
try:
2024-07-03 23:32:39 -07:00
with get_db() as db:
result = Prompt(**prompt.dict())
db.add(result)
db.commit()
db.refresh(result)
if result:
return PromptModel.model_validate(result)
else:
return None
2024-08-28 00:10:27 +02:00
except Exception:
return None
2024-01-02 20:41:37 -08:00
def get_prompt_by_command(self, command: str) -> Optional[PromptModel]:
try:
2024-07-03 23:32:39 -07:00
with get_db() as db:
prompt = db.query(Prompt).filter_by(command=command).first()
return PromptModel.model_validate(prompt)
2024-08-14 13:38:19 +01:00
except Exception:
return None
2024-01-02 20:41:37 -08:00
2024-08-14 13:46:31 +01:00
def get_prompts(self) -> list[PromptModel]:
2024-07-03 23:32:39 -07:00
with get_db() as db:
return [
PromptModel.model_validate(prompt) for prompt in db.query(Prompt).all()
]
2024-01-02 20:41:37 -08:00
def update_prompt_by_command(
self, command: str, form_data: PromptForm
2024-03-31 01:13:39 -07:00
) -> Optional[PromptModel]:
try:
2024-07-03 23:32:39 -07: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
prompt.timestamp = int(time.time())
db.commit()
return PromptModel.model_validate(prompt)
2024-08-14 13:38:19 +01:00
except Exception:
return None
def delete_prompt_by_command(self, command: str) -> bool:
try:
2024-07-03 23:32:39 -07:00
with get_db() as db:
db.query(Prompt).filter_by(command=command).delete()
2024-07-06 08:10:58 -07:00
db.commit()
2024-07-03 23:32:39 -07:00
return True
2024-08-14 13:38:19 +01:00
except Exception:
return False
2024-01-02 20:41:37 -08:00
Prompts = PromptsTable()