prompts.py 2.7 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
6
from sqlalchemy import String, Column, BigInteger
from sqlalchemy.orm import Session
Timothy J. Baek's avatar
Timothy J. Baek committed
7

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

import json

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


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

20
21
22
23
24
    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
25
26
27
28
29
30
31
32
33


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

34
35
    model_config = ConfigDict(from_attributes=True)

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

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


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


class PromptsTable:
49

Timothy J. Baek's avatar
Timothy J. Baek committed
50
    def insert_new_prompt(
51
        self, db: Session, user_id: str, form_data: PromptForm
Timothy J. Baek's avatar
Timothy J. Baek committed
52
    ) -> Optional[PromptModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
53
54
55
56
57
58
59
        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
60
61
            }
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
62
63

        try:
64
65
66
67
            result = Prompt(**prompt.dict())
            db.add(result)
            db.commit()
            db.refresh(result)
Timothy J. Baek's avatar
Timothy J. Baek committed
68
            if result:
69
                return PromptModel.model_validate(result)
Timothy J. Baek's avatar
Timothy J. Baek committed
70
71
            else:
                return None
72
        except Exception as e:
Timothy J. Baek's avatar
Timothy J. Baek committed
73
74
            return None

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

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

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

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


108
Prompts = PromptsTable()