prompts.py 2.77 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
4
5
6
7
8
9
from pydantic import BaseModel
from peewee import *
from playhouse.shortcuts import model_to_dict
from typing import List, Union, Optional
import time

from utils.utils import decode_token
from utils.misc import get_gravatar_url

10
from apps.webui.internal.db import DB
Timothy J. Baek's avatar
Timothy J. Baek committed
11
12
13
14
15
16
17
18
19
20
21

import json

####################
# Prompts DB Schema
####################


class Prompt(Model):
    command = CharField(unique=True)
    user_id = CharField()
22
    title = TextField()
Timothy J. Baek's avatar
Timothy J. Baek committed
23
    content = TextField()
24
    timestamp = BigIntegerField()
Timothy J. Baek's avatar
Timothy J. Baek committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

    class Meta:
        database = DB


class PromptModel(BaseModel):
    command: str
    user_id: str
    title: str
    content: str
    timestamp: int  # timestamp in epoch


####################
# Forms
####################


class PromptForm(BaseModel):
    command: str
    title: str
    content: str


class PromptsTable:
50

Timothy J. Baek's avatar
Timothy J. Baek committed
51
52
53
54
    def __init__(self, db):
        self.db = db
        self.db.create_tables([Prompt])

Timothy J. Baek's avatar
Timothy J. Baek committed
55
56
57
    def insert_new_prompt(
        self, user_id: str, form_data: PromptForm
    ) -> Optional[PromptModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
58
59
60
61
62
63
64
        prompt = PromptModel(
            **{
                "user_id": user_id,
                "command": form_data.command,
                "title": form_data.title,
                "content": form_data.content,
                "timestamp": int(time.time()),
Timothy J. Baek's avatar
Timothy J. Baek committed
65
66
            }
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

        try:
            result = Prompt.create(**prompt.model_dump())
            if result:
                return prompt
            else:
                return None
        except:
            return None

    def get_prompt_by_command(self, command: str) -> Optional[PromptModel]:
        try:
            prompt = Prompt.get(Prompt.command == command)
            return PromptModel(**model_to_dict(prompt))
        except:
            return None

    def get_prompts(self) -> List[PromptModel]:
        return [
Timothy J. Baek's avatar
Timothy J. Baek committed
86
87
            PromptModel(**model_to_dict(prompt))
            for prompt in Prompt.select()
Timothy J. Baek's avatar
Timothy J. Baek committed
88
89
90
91
            # .limit(limit).offset(skip)
        ]

    def update_prompt_by_command(
Timothy J. Baek's avatar
Timothy J. Baek committed
92
93
        self, command: str, form_data: PromptForm
    ) -> Optional[PromptModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
        try:
            query = Prompt.update(
                title=form_data.title,
                content=form_data.content,
                timestamp=int(time.time()),
            ).where(Prompt.command == command)

            query.execute()

            prompt = Prompt.get(Prompt.command == command)
            return PromptModel(**model_to_dict(prompt))
        except:
            return None

    def delete_prompt_by_command(self, command: str) -> bool:
        try:
            query = Prompt.delete().where((Prompt.command == command))
            query.execute()  # Remove the rows, return number of rows removed.

            return True
        except:
            return False


Prompts = PromptsTable(DB)