prompts.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. return Prompts.get_prompts()
  13. ############################
  14. # CreateNewPrompt
  15. ############################
  16. @router.post("/create", response_model=Optional[PromptModel])
  17. async def create_new_prompt(form_data: PromptForm, user=Depends(get_admin_user)):
  18. prompt = Prompts.get_prompt_by_command(form_data.command)
  19. if prompt is None:
  20. prompt = Prompts.insert_new_prompt(user.id, form_data)
  21. if prompt:
  22. return prompt
  23. raise HTTPException(
  24. status_code=status.HTTP_400_BAD_REQUEST,
  25. detail=ERROR_MESSAGES.DEFAULT(),
  26. )
  27. raise HTTPException(
  28. status_code=status.HTTP_400_BAD_REQUEST,
  29. detail=ERROR_MESSAGES.COMMAND_TAKEN,
  30. )
  31. ############################
  32. # GetPromptByCommand
  33. ############################
  34. @router.get("/command/{command}", response_model=Optional[PromptModel])
  35. async def get_prompt_by_command(command: str, user=Depends(get_verified_user)):
  36. prompt = Prompts.get_prompt_by_command(f"/{command}")
  37. if prompt:
  38. return prompt
  39. else:
  40. raise HTTPException(
  41. status_code=status.HTTP_401_UNAUTHORIZED,
  42. detail=ERROR_MESSAGES.NOT_FOUND,
  43. )
  44. ############################
  45. # UpdatePromptByCommand
  46. ############################
  47. @router.post("/command/{command}/update", response_model=Optional[PromptModel])
  48. async def update_prompt_by_command(
  49. command: str,
  50. form_data: PromptForm,
  51. user=Depends(get_admin_user),
  52. ):
  53. prompt = Prompts.update_prompt_by_command(f"/{command}", form_data)
  54. if prompt:
  55. return prompt
  56. else:
  57. raise HTTPException(
  58. status_code=status.HTTP_401_UNAUTHORIZED,
  59. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  60. )
  61. ############################
  62. # DeletePromptByCommand
  63. ############################
  64. @router.delete("/command/{command}/delete", response_model=bool)
  65. async def delete_prompt_by_command(command: str, user=Depends(get_admin_user)):
  66. result = Prompts.delete_prompt_by_command(f"/{command}")
  67. return result