prompts.py 3 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, get_session
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, user_id: str, form_data: PromptForm
Timothy J. Baek's avatar
Timothy J. Baek committed
52
    ) -> Optional[PromptModel]:
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
        with get_session() as db:
            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())
                db.add(result)
                db.commit()
                db.refresh(result)
                if result:
                    return PromptModel.model_validate(result)
                else:
                    return None
            except Exception as e:
Timothy J. Baek's avatar
Timothy J. Baek committed
74
75
                return None

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

84
85
    def get_prompts(self) -> List[PromptModel]:
        with get_session() as db:
86
87
88
            return [
                PromptModel.model_validate(prompt) for prompt in db.query(Prompt).all()
            ]
Timothy J. Baek's avatar
Timothy J. Baek committed
89
90

    def update_prompt_by_command(
91
        self, command: str, form_data: PromptForm
Timothy J. Baek's avatar
Timothy J. Baek committed
92
    ) -> Optional[PromptModel]:
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
        with get_session() as db:
            try:
                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 prompt
                # return self.get_prompt_by_command(command)
            except:
                return None

    def delete_prompt_by_command(self, command: str) -> bool:
        with get_session() as db:
            try:
                db.query(Prompt).filter_by(command=command).delete()
                return True
            except:
                return False
Timothy J. Baek's avatar
Timothy J. Baek committed
112
113


114
Prompts = PromptsTable()