prompts.py 2.64 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
Timothy J. Baek's avatar
Timothy J. Baek committed
6

7
from apps.webui.internal.db import Base, Session
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
21
22
23
    command = Column(String, primary_key=True)
    user_id = Column(String)
    title = Column(String)
    content = Column(String)
    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
63
64
65
66
67
68
69
        prompt = PromptModel(
            **{
                "user_id": user_id,
                "command": form_data.command,
                "title": form_data.title,
                "content": form_data.content,
                "timestamp": int(time.time()),
            }
        )

        try:
            result = Prompt(**prompt.dict())
            Session.add(result)
            Session.commit()
            Session.refresh(result)
            if result:
                return PromptModel.model_validate(result)
            else:
Timothy J. Baek's avatar
Timothy J. Baek committed
70
                return None
71
72
        except Exception as e:
            return None
Timothy J. Baek's avatar
Timothy J. Baek committed
73

74
    def get_prompt_by_command(self, command: str) -> Optional[PromptModel]:
75
76
77
78
79
        try:
            prompt = Session.query(Prompt).filter_by(command=command).first()
            return PromptModel.model_validate(prompt)
        except:
            return None
Timothy J. Baek's avatar
Timothy J. Baek committed
80

81
    def get_prompts(self) -> List[PromptModel]:
82
83
84
        return [
            PromptModel.model_validate(prompt) for prompt in Session.query(Prompt).all()
        ]
Timothy J. Baek's avatar
Timothy J. Baek committed
85
86

    def update_prompt_by_command(
87
        self, command: str, form_data: PromptForm
Timothy J. Baek's avatar
Timothy J. Baek committed
88
    ) -> Optional[PromptModel]:
89
90
91
92
93
94
95
96
97
        try:
            prompt = Session.query(Prompt).filter_by(command=command).first()
            prompt.title = form_data.title
            prompt.content = form_data.content
            prompt.timestamp = int(time.time())
            Session.commit()
            return PromptModel.model_validate(prompt)
        except:
            return None
98
99

    def delete_prompt_by_command(self, command: str) -> bool:
100
101
102
103
104
        try:
            Session.query(Prompt).filter_by(command=command).delete()
            return True
        except:
            return False
Timothy J. Baek's avatar
Timothy J. Baek committed
105
106


107
Prompts = PromptsTable()