"host/online_compile/CMakeLists.txt" did not exist on "1264925422920f24b3bb4fa34f178e31a23c97b5"
memories.py 3.27 KB
Newer Older
1
from pydantic import BaseModel, ConfigDict
Timothy J. Baek's avatar
Timothy J. Baek committed
2
3
from typing import List, Union, Optional

4
5
6
7
from sqlalchemy import Column, String, BigInteger
from sqlalchemy.orm import Session

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

import time
import uuid

####################
# Memory DB Schema
####################


18
19
class Memory(Base):
    __tablename__ = "memory"
Timothy J. Baek's avatar
Timothy J. Baek committed
20

21
22
23
24
25
    id = Column(String, primary_key=True)
    user_id = Column(String)
    content = Column(String)
    updated_at = Column(BigInteger)
    created_at = Column(BigInteger)
Timothy J. Baek's avatar
Timothy J. Baek committed
26
27
28
29
30
31
32
33
34


class MemoryModel(BaseModel):
    id: str
    user_id: str
    content: str
    updated_at: int  # timestamp in epoch
    created_at: int  # timestamp in epoch

35
36
    model_config = ConfigDict(from_attributes=True)

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

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


class MemoriesTable:

    def insert_new_memory(
        self,
47
        db: Session,
Timothy J. Baek's avatar
Timothy J. Baek committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
        user_id: str,
        content: str,
    ) -> Optional[MemoryModel]:
        id = str(uuid.uuid4())

        memory = MemoryModel(
            **{
                "id": id,
                "user_id": user_id,
                "content": content,
                "created_at": int(time.time()),
                "updated_at": int(time.time()),
            }
        )
62
63
64
65
        result = Memory(**memory.dict())
        db.add(result)
        db.commit()
        db.refresh(result)
Timothy J. Baek's avatar
Timothy J. Baek committed
66
        if result:
67
            return MemoryModel.model_validate(result)
Timothy J. Baek's avatar
Timothy J. Baek committed
68
69
        else:
            return None
Timothy J. Baek's avatar
Timothy J. Baek committed
70

71
    def update_memory_by_id(
Peter De-Ath's avatar
Peter De-Ath committed
72
        self,
73
        db: Session,
Peter De-Ath's avatar
Peter De-Ath committed
74
75
76
77
        id: str,
        content: str,
    ) -> Optional[MemoryModel]:
        try:
78
79
80
81
            db.query(Memory).filter_by(id=id).update(
                {"content": content, "updated_at": int(time.time())}
            )
            return self.get_memory_by_id(db, id)
Peter De-Ath's avatar
Peter De-Ath committed
82
83
        except:
            return None
Timothy J. Baek's avatar
Timothy J. Baek committed
84

85
    def get_memories(self, db: Session) -> List[MemoryModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
86
        try:
87
88
            memories = db.query(Memory).all()
            return [MemoryModel.model_validate(memory) for memory in memories]
Timothy J. Baek's avatar
Timothy J. Baek committed
89
90
91
        except:
            return None

92
    def get_memories_by_user_id(self, db: Session, user_id: str) -> List[MemoryModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
93
        try:
94
95
            memories = db.query(Memory).filter_by(user_id=user_id).all()
            return [MemoryModel.model_validate(memory) for memory in memories]
Timothy J. Baek's avatar
Timothy J. Baek committed
96
97
98
        except:
            return None

99
    def get_memory_by_id(self, db: Session, id: str) -> Optional[MemoryModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
100
        try:
101
102
            memory = db.get(Memory, id)
            return MemoryModel.model_validate(memory)
Timothy J. Baek's avatar
Timothy J. Baek committed
103
104
105
        except:
            return None

106
    def delete_memory_by_id(self, db: Session, id: str) -> bool:
Timothy J. Baek's avatar
Timothy J. Baek committed
107
        try:
108
            db.query(Memory).filter_by(id=id).delete()
Timothy J. Baek's avatar
Timothy J. Baek committed
109
110
111
112
113
            return True

        except:
            return False

114
    def delete_memories_by_user_id(self, db: Session, user_id: str) -> bool:
Timothy J. Baek's avatar
Timothy J. Baek committed
115
        try:
116
            db.query(Memory).filter_by(user_id=user_id).delete()
Timothy J. Baek's avatar
Timothy J. Baek committed
117
118
119
120
            return True
        except:
            return False

121
122
123
    def delete_memory_by_id_and_user_id(
        self, db: Session, id: str, user_id: str
    ) -> bool:
Timothy J. Baek's avatar
Timothy J. Baek committed
124
        try:
125
            db.query(Memory).filter_by(id=id, user_id=user_id).delete()
Timothy J. Baek's avatar
Timothy J. Baek committed
126
127
128
129
130
            return True
        except:
            return False


131
Memories = MemoriesTable()