memories.py 3.6 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
from sqlalchemy import Column, String, BigInteger
from sqlalchemy.orm import Session

7
from apps.webui.internal.db import Base, get_session
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60

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


class MemoriesTable:

    def insert_new_memory(
        self,
        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()),
            }
        )
61
62
63
64
65
66
67
68
69
        with get_session() as db:
            result = Memory(**memory.model_dump())
            db.add(result)
            db.commit()
            db.refresh(result)
            if result:
                return MemoryModel.model_validate(result)
            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
73
74
75
76
        self,
        id: str,
        content: str,
    ) -> Optional[MemoryModel]:
        try:
77
78
79
80
81
82
            with get_session() as db:
                db.query(Memory).filter_by(id=id).update(
                    {"content": content, "updated_at": int(time.time())}
                )
                db.commit()
                return self.get_memory_by_id(id)
Peter De-Ath's avatar
Peter De-Ath committed
83
84
        except:
            return None
Timothy J. Baek's avatar
Timothy J. Baek committed
85

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

94
    def get_memories_by_user_id(self, user_id: str) -> List[MemoryModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
95
        try:
96
97
98
            with get_session() as db:
                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
99
100
101
        except:
            return None

102
    def get_memory_by_id(self, id: str) -> Optional[MemoryModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
103
        try:
104
105
106
            with get_session() as db:
                memory = db.get(Memory, id)
                return MemoryModel.model_validate(memory)
Timothy J. Baek's avatar
Timothy J. Baek committed
107
108
109
        except:
            return None

110
    def delete_memory_by_id(self, id: str) -> bool:
Timothy J. Baek's avatar
Timothy J. Baek committed
111
        try:
112
113
            with get_session() as db:
                db.query(Memory).filter_by(id=id).delete()
Timothy J. Baek's avatar
Timothy J. Baek committed
114
115
116
117
118
            return True

        except:
            return False

119
    def delete_memories_by_user_id(self, db: Session, user_id: str) -> bool:
Timothy J. Baek's avatar
Timothy J. Baek committed
120
        try:
121
122
            with get_session() as db:
                db.query(Memory).filter_by(user_id=user_id).delete()
Timothy J. Baek's avatar
Timothy J. Baek committed
123
124
125
126
            return True
        except:
            return False

127
128
129
    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
130
        try:
131
132
            with get_session() as db:
                db.query(Memory).filter_by(id=id, user_id=user_id).delete()
Timothy J. Baek's avatar
Timothy J. Baek committed
133
134
135
136
137
            return True
        except:
            return False


138
Memories = MemoriesTable()