prompts.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from typing import Optional
  2. from open_webui.apps.webui.models.prompts import PromptForm, PromptModel, Prompts
  3. from open_webui.constants import ERROR_MESSAGES
  4. from fastapi import APIRouter, Depends, HTTPException, status
  5. from open_webui.utils.utils import get_admin_user, get_verified_user
  6. router = APIRouter()
  7. ############################
  8. # GetPrompts
  9. ############################
  10. @router.get("/", response_model=list[PromptModel])
  11. async def get_prompts(user=Depends(get_verified_user)):
  12. if user.role == "admin":
  13. prompts = Prompts.get_prompts()
  14. else:
  15. prompts = Prompts.get_prompts_by_user_id(user.id, "read")
  16. return prompts
  17. @router.get("/list", response_model=list[PromptModel])
  18. async def get_prompt_list(user=Depends(get_verified_user)):
  19. if user.role == "admin":
  20. prompts = Prompts.get_prompts()
  21. else:
  22. prompts = Prompts.get_prompts_by_user_id(user.id, "write")
  23. return prompts
  24. ############################
  25. # CreateNewPrompt
  26. ############################
  27. @router.post("/create", response_model=Optional[PromptModel])
  28. async def create_new_prompt(form_data: PromptForm, user=Depends(get_verified_user)):
  29. prompt = Prompts.get_prompt_by_command(form_data.command)
  30. if prompt is None:
  31. prompt = Prompts.insert_new_prompt(user.id, form_data)
  32. if prompt:
  33. return prompt
  34. raise HTTPException(
  35. status_code=status.HTTP_400_BAD_REQUEST,
  36. detail=ERROR_MESSAGES.DEFAULT(),
  37. )
  38. raise HTTPException(
  39. status_code=status.HTTP_400_BAD_REQUEST,
  40. detail=ERROR_MESSAGES.COMMAND_TAKEN,
  41. )
  42. ############################
  43. # GetPromptByCommand
  44. ############################
  45. @router.get("/command/{command}", response_model=Optional[PromptModel])
  46. async def get_prompt_by_command(command: str, user=Depends(get_verified_user)):
  47. prompt = Prompts.get_prompt_by_command(f"/{command}")
  48. if prompt:
  49. return prompt
  50. else:
  51. raise HTTPException(
  52. status_code=status.HTTP_401_UNAUTHORIZED,
  53. detail=ERROR_MESSAGES.NOT_FOUND,
  54. )
  55. ############################
  56. # UpdatePromptByCommand
  57. ############################
  58. @router.post("/command/{command}/update", response_model=Optional[PromptModel])
  59. async def update_prompt_by_command(
  60. command: str,
  61. form_data: PromptForm,
  62. user=Depends(get_verified_user),
  63. ):
  64. prompt = Prompts.update_prompt_by_command(f"/{command}", form_data)
  65. if prompt:
  66. return prompt
  67. else:
  68. raise HTTPException(
  69. status_code=status.HTTP_401_UNAUTHORIZED,
  70. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  71. )
  72. ############################
  73. # DeletePromptByCommand
  74. ############################
  75. @router.delete("/command/{command}/delete", response_model=bool)
  76. async def delete_prompt_by_command(command: str, user=Depends(get_verified_user)):
  77. result = Prompts.delete_prompt_by_command(f"/{command}")
  78. return result