"llm/patches/02-cudaleaks.diff" did not exist on "ace2cdf1c627178a78dea7c7a415ed92abfaed6f"
memories.py 3.17 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
from sqlalchemy import Column, String, BigInteger

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

import time
import uuid

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


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

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


class MemoryModel(BaseModel):
    id: str
    user_id: str
    content: str
    updated_at: int  # timestamp in epoch
    created_at: 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
48
49
50
51
52
53
54
55
56
57
58

####################
# 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()),
            }
        )
59
60
61
62
63
64
65
66
        result = Memory(**memory.model_dump())
        Session.add(result)
        Session.commit()
        Session.refresh(result)
        if result:
            return MemoryModel.model_validate(result)
        else:
            return None
Timothy J. Baek's avatar
Timothy J. Baek committed
67

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

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

89
    def get_memories_by_user_id(self, user_id: str) -> List[MemoryModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
90
        try:
91
92
            memories = Session.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
93
94
95
        except:
            return None

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

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

        except:
            return False

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

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


128
Memories = MemoriesTable()