Files
open-webui/backend/apps/webui/routers/prompts.py

91 lines
2.5 KiB
Python
Raw Normal View History

2024-08-28 00:10:27 +02:00
from typing import Optional
2024-01-02 20:51:19 -08:00
2024-08-28 00:10:27 +02:00
from apps.webui.models.prompts import PromptForm, PromptModel, Prompts
2024-01-02 20:51:19 -08:00
from constants import ERROR_MESSAGES
2024-08-28 00:10:27 +02:00
from fastapi import APIRouter, Depends, HTTPException, status
from utils.utils import get_admin_user, get_verified_user
2024-01-02 20:51:19 -08:00
router = APIRouter()
############################
# GetPrompts
############################
2024-08-14 13:46:31 +01:00
@router.get("/", response_model=list[PromptModel])
2024-06-27 11:29:59 -07:00
async def get_prompts(user=Depends(get_verified_user)):
return Prompts.get_prompts()
2024-01-02 20:51:19 -08:00
############################
# CreateNewPrompt
############################
@router.post("/create", response_model=Optional[PromptModel])
2024-06-24 09:57:08 +02:00
async def create_new_prompt(form_data: PromptForm, user=Depends(get_admin_user)):
prompt = Prompts.get_prompt_by_command(form_data.command)
2024-08-14 13:39:53 +01:00
if prompt is None:
prompt = Prompts.insert_new_prompt(user.id, form_data)
2024-01-02 21:35:47 -08:00
if prompt:
return prompt
2024-01-02 20:51:19 -08:00
raise HTTPException(
2024-01-02 21:35:47 -08:00
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.DEFAULT(),
2024-01-02 20:51:19 -08:00
)
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.COMMAND_TAKEN,
)
2024-01-02 20:51:19 -08:00
############################
# GetPromptByCommand
############################
2024-01-06 17:55:41 -08:00
@router.get("/command/{command}", response_model=Optional[PromptModel])
2024-06-27 11:29:59 -07:00
async def get_prompt_by_command(command: str, user=Depends(get_verified_user)):
prompt = Prompts.get_prompt_by_command(f"/{command}")
2024-01-02 20:51:19 -08:00
if prompt:
return prompt
else:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=ERROR_MESSAGES.NOT_FOUND,
)
############################
# UpdatePromptByCommand
############################
2024-01-06 17:55:41 -08:00
@router.post("/command/{command}/update", response_model=Optional[PromptModel])
async def update_prompt_by_command(
command: str,
form_data: PromptForm,
user=Depends(get_admin_user),
2024-01-06 17:55:41 -08:00
):
prompt = Prompts.update_prompt_by_command(f"/{command}", form_data)
2024-01-02 20:51:19 -08:00
if prompt:
return prompt
else:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
)
############################
# DeletePromptByCommand
############################
2024-01-06 17:55:41 -08:00
@router.delete("/command/{command}/delete", response_model=bool)
2024-06-24 09:57:08 +02:00
async def delete_prompt_by_command(command: str, user=Depends(get_admin_user)):
result = Prompts.delete_prompt_by_command(f"/{command}")
2024-01-02 20:51:19 -08:00
return result