prompts.py 2.75 KB
Newer Older
1
2
3
4
5
6
7
8
from fastapi import Depends, FastAPI, HTTPException, status
from datetime import datetime, timedelta
from typing import List, Union, Optional

from fastapi import APIRouter
from pydantic import BaseModel
import json

9
from apps.webui.internal.db import get_db
10
from apps.webui.models.prompts import Prompts, PromptForm, PromptModel
11

12
from utils.utils import get_current_user, get_admin_user
13
14
15
16
17
18
19
20
21
22
from constants import ERROR_MESSAGES

router = APIRouter()

############################
# GetPrompts
############################


@router.get("/", response_model=List[PromptModel])
23
24
async def get_prompts(user=Depends(get_current_user), db=Depends(get_db)):
    return Prompts.get_prompts(db)
25
26
27
28
29
30
31
32


############################
# CreateNewPrompt
############################


@router.post("/create", response_model=Optional[PromptModel])
33
34
35
36
async def create_new_prompt(
    form_data: PromptForm, user=Depends(get_admin_user), db=Depends(get_db)
):
    prompt = Prompts.get_prompt_by_command(db, form_data.command)
Timothy J. Baek's avatar
Timothy J. Baek committed
37
    if prompt == None:
38
        prompt = Prompts.insert_new_prompt(db, user.id, form_data)
Timothy J. Baek's avatar
Timothy J. Baek committed
39
40
41

        if prompt:
            return prompt
42
        raise HTTPException(
Timothy J. Baek's avatar
Timothy J. Baek committed
43
            status_code=status.HTTP_400_BAD_REQUEST,
44
            detail=ERROR_MESSAGES.DEFAULT(),
45
        )
46
47
48
49
    raise HTTPException(
        status_code=status.HTTP_400_BAD_REQUEST,
        detail=ERROR_MESSAGES.COMMAND_TAKEN,
    )
50
51
52
53
54
55
56


############################
# GetPromptByCommand
############################


57
@router.get("/command/{command}", response_model=Optional[PromptModel])
58
59
60
61
async def get_prompt_by_command(
    command: str, user=Depends(get_current_user), db=Depends(get_db)
):
    prompt = Prompts.get_prompt_by_command(db, f"/{command}")
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

    if prompt:
        return prompt
    else:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail=ERROR_MESSAGES.NOT_FOUND,
        )


############################
# UpdatePromptByCommand
############################


77
78
@router.post("/command/{command}/update", response_model=Optional[PromptModel])
async def update_prompt_by_command(
79
80
81
82
    command: str,
    form_data: PromptForm,
    user=Depends(get_admin_user),
    db=Depends(get_db),
83
):
84
    prompt = Prompts.update_prompt_by_command(db, f"/{command}", form_data)
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    if prompt:
        return prompt
    else:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
        )


############################
# DeletePromptByCommand
############################


99
@router.delete("/command/{command}/delete", response_model=bool)
100
101
102
103
async def delete_prompt_by_command(
    command: str, user=Depends(get_admin_user), db=Depends(get_db)
):
    result = Prompts.delete_prompt_by_command(db, f"/{command}")
104
    return result