prompts.py 2.84 KB
Newer Older
1
2
from pydantic import BaseModel, ConfigDict
from typing import List, Optional
Timothy J. Baek's avatar
Timothy J. Baek committed
3
4
import time

5
from sqlalchemy import String, Column, BigInteger, Text
Timothy J. Baek's avatar
Timothy J. Baek committed
6

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
7
from apps.webui.internal.db import Base, get_db
Timothy J. Baek's avatar
Timothy J. Baek committed
8
9
10
11
12
13
14
15

import json

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


16
17
class Prompt(Base):
    __tablename__ = "prompt"
Timothy J. Baek's avatar
Timothy J. Baek committed
18

19
20
    command = Column(String, primary_key=True)
    user_id = Column(String)
21
22
    title = Column(Text)
    content = Column(Text)
23
    timestamp = Column(BigInteger)
Timothy J. Baek's avatar
Timothy J. Baek committed
24
25
26
27
28
29
30
31
32


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

33
34
    model_config = ConfigDict(from_attributes=True)

Timothy J. Baek's avatar
Timothy J. Baek committed
35
36
37
38
39
40
41
42
43
44
45
46
47

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


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


class PromptsTable:
48

Timothy J. Baek's avatar
Timothy J. Baek committed
49
    def insert_new_prompt(
50
        self, user_id: str, form_data: PromptForm
Timothy J. Baek's avatar
Timothy J. Baek committed
51
    ) -> Optional[PromptModel]:
52
53
54
55
56
57
58
59
60
61
62
        prompt = PromptModel(
            **{
                "user_id": user_id,
                "command": form_data.command,
                "title": form_data.title,
                "content": form_data.content,
                "timestamp": int(time.time()),
            }
        )

        try:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
63
64
65
66
67
68
69
70
71
72
            with get_db() as db:

                result = Prompt(**prompt.dict())
                db.add(result)
                db.commit()
                db.refresh(result)
                if result:
                    return PromptModel.model_validate(result)
                else:
                    return None
73
74
        except Exception as e:
            return None
Timothy J. Baek's avatar
Timothy J. Baek committed
75

76
    def get_prompt_by_command(self, command: str) -> Optional[PromptModel]:
77
        try:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
78
79
80
81
            with get_db() as db:

                prompt = db.query(Prompt).filter_by(command=command).first()
                return PromptModel.model_validate(prompt)
82
83
        except:
            return None
Timothy J. Baek's avatar
Timothy J. Baek committed
84

85
    def get_prompts(self) -> List[PromptModel]:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
86
87
88
89
90
        with get_db() as db:

            return [
                PromptModel.model_validate(prompt) for prompt in db.query(Prompt).all()
            ]
Timothy J. Baek's avatar
Timothy J. Baek committed
91
92

    def update_prompt_by_command(
93
        self, command: str, form_data: PromptForm
Timothy J. Baek's avatar
Timothy J. Baek committed
94
    ) -> Optional[PromptModel]:
95
        try:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
96
97
98
99
100
101
102
103
            with get_db() as db:

                prompt = db.query(Prompt).filter_by(command=command).first()
                prompt.title = form_data.title
                prompt.content = form_data.content
                prompt.timestamp = int(time.time())
                db.commit()
                return PromptModel.model_validate(prompt)
104
105
        except:
            return None
106
107

    def delete_prompt_by_command(self, command: str) -> bool:
108
        try:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
109
110
111
112
            with get_db() as db:

                db.query(Prompt).filter_by(command=command).delete()
                return True
113
114
        except:
            return False
Timothy J. Baek's avatar
Timothy J. Baek committed
115
116


117
Prompts = PromptsTable()