prompts.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import time
  2. from typing import Optional
  3. from open_webui.apps.webui.internal.db import Base, get_db
  4. from pydantic import BaseModel, ConfigDict
  5. from sqlalchemy import BigInteger, Column, String, Text, JSON
  6. ####################
  7. # Prompts DB Schema
  8. ####################
  9. class Prompt(Base):
  10. __tablename__ = "prompt"
  11. command = Column(String, primary_key=True)
  12. user_id = Column(String)
  13. title = Column(Text)
  14. content = Column(Text)
  15. timestamp = Column(BigInteger)
  16. access_control = Column(JSON, nullable=True) # Controls data access levels.
  17. # Defines access control rules for this entry.
  18. # - `None`: Public access, available to all users with the "user" role.
  19. # - `{}`: Private access, restricted exclusively to the owner.
  20. # - Custom permissions: Specific access control for reading and writing;
  21. # Can specify group or user-level restrictions:
  22. # {
  23. # "read": {
  24. # "group_ids": ["group_id1", "group_id2"],
  25. # "user_ids": ["user_id1", "user_id2"]
  26. # },
  27. # "write": {
  28. # "group_ids": ["group_id1", "group_id2"],
  29. # "user_ids": ["user_id1", "user_id2"]
  30. # }
  31. # }
  32. class PromptModel(BaseModel):
  33. command: str
  34. user_id: str
  35. title: str
  36. content: str
  37. timestamp: int # timestamp in epoch
  38. access_control: Optional[dict] = None
  39. model_config = ConfigDict(from_attributes=True)
  40. ####################
  41. # Forms
  42. ####################
  43. class PromptForm(BaseModel):
  44. command: str
  45. title: str
  46. content: str
  47. class PromptsTable:
  48. def insert_new_prompt(
  49. self, user_id: str, form_data: PromptForm
  50. ) -> Optional[PromptModel]:
  51. prompt = PromptModel(
  52. **{
  53. "user_id": user_id,
  54. "command": form_data.command,
  55. "title": form_data.title,
  56. "content": form_data.content,
  57. "timestamp": int(time.time()),
  58. }
  59. )
  60. try:
  61. with get_db() as db:
  62. result = Prompt(**prompt.dict())
  63. db.add(result)
  64. db.commit()
  65. db.refresh(result)
  66. if result:
  67. return PromptModel.model_validate(result)
  68. else:
  69. return None
  70. except Exception:
  71. return None
  72. def get_prompt_by_command(self, command: str) -> Optional[PromptModel]:
  73. try:
  74. with get_db() as db:
  75. prompt = db.query(Prompt).filter_by(command=command).first()
  76. return PromptModel.model_validate(prompt)
  77. except Exception:
  78. return None
  79. def get_prompts(self) -> list[PromptModel]:
  80. with get_db() as db:
  81. return [
  82. PromptModel.model_validate(prompt) for prompt in db.query(Prompt).all()
  83. ]
  84. def update_prompt_by_command(
  85. self, command: str, form_data: PromptForm
  86. ) -> Optional[PromptModel]:
  87. try:
  88. with get_db() as db:
  89. prompt = db.query(Prompt).filter_by(command=command).first()
  90. prompt.title = form_data.title
  91. prompt.content = form_data.content
  92. prompt.timestamp = int(time.time())
  93. db.commit()
  94. return PromptModel.model_validate(prompt)
  95. except Exception:
  96. return None
  97. def delete_prompt_by_command(self, command: str) -> bool:
  98. try:
  99. with get_db() as db:
  100. db.query(Prompt).filter_by(command=command).delete()
  101. db.commit()
  102. return True
  103. except Exception:
  104. return False
  105. Prompts = PromptsTable()