open-webui/backend/apps/webui/models/memories.py

143 lines
3.6 KiB
Python
Raw Normal View History

from pydantic import BaseModel, ConfigDict
2024-05-19 15:00:07 +00:00
from typing import List, Union, Optional
from sqlalchemy import Column, String, BigInteger, Text
2024-07-04 06:32:39 +00:00
from apps.webui.internal.db import Base, get_db
2024-05-19 15:00:07 +00:00
import time
import uuid
####################
# Memory DB Schema
####################
class Memory(Base):
__tablename__ = "memory"
2024-05-19 15:00:07 +00:00
id = Column(String, primary_key=True)
user_id = Column(String)
content = Column(Text)
updated_at = Column(BigInteger)
created_at = Column(BigInteger)
2024-05-19 15:00:07 +00:00
class MemoryModel(BaseModel):
id: str
user_id: str
content: str
updated_at: int # timestamp in epoch
created_at: int # timestamp in epoch
model_config = ConfigDict(from_attributes=True)
2024-05-19 15:00:07 +00:00
####################
# Forms
####################
class MemoriesTable:
def insert_new_memory(
self,
user_id: str,
content: str,
) -> Optional[MemoryModel]:
2024-07-04 06:32:39 +00:00
with get_db() as db:
id = str(uuid.uuid4())
memory = MemoryModel(
**{
"id": id,
"user_id": user_id,
"content": content,
"created_at": int(time.time()),
"updated_at": int(time.time()),
}
)
result = Memory(**memory.model_dump())
db.add(result)
db.commit()
db.refresh(result)
if result:
return MemoryModel.model_validate(result)
else:
return None
2024-06-15 09:36:17 +00:00
def update_memory_by_id(
2024-06-13 01:01:50 +00:00
self,
id: str,
content: str,
) -> Optional[MemoryModel]:
2024-07-04 06:32:39 +00:00
with get_db() as db:
try:
db.query(Memory).filter_by(id=id).update(
{"content": content, "updated_at": int(time.time())}
)
db.commit()
return self.get_memory_by_id(id)
except:
return None
2024-05-19 15:00:07 +00:00
def get_memories(self) -> List[MemoryModel]:
2024-07-04 06:32:39 +00:00
with get_db() as db:
try:
memories = db.query(Memory).all()
return [MemoryModel.model_validate(memory) for memory in memories]
except:
return None
2024-05-19 15:00:07 +00:00
def get_memories_by_user_id(self, user_id: str) -> List[MemoryModel]:
2024-07-04 06:32:39 +00:00
with get_db() as db:
try:
memories = db.query(Memory).filter_by(user_id=user_id).all()
return [MemoryModel.model_validate(memory) for memory in memories]
except:
return None
2024-05-19 15:00:07 +00:00
def get_memory_by_id(self, id: str) -> Optional[MemoryModel]:
2024-07-04 06:32:39 +00:00
with get_db() as db:
try:
memory = db.get(Memory, id)
return MemoryModel.model_validate(memory)
except:
return None
2024-05-19 15:00:07 +00:00
def delete_memory_by_id(self, id: str) -> bool:
2024-07-04 06:32:39 +00:00
with get_db() as db:
try:
db.query(Memory).filter_by(id=id).delete()
return True
2024-05-19 15:00:07 +00:00
2024-07-04 06:32:39 +00:00
except:
return False
2024-05-19 15:00:07 +00:00
def delete_memories_by_user_id(self, user_id: str) -> bool:
2024-07-04 06:32:39 +00:00
with get_db() as db:
try:
db.query(Memory).filter_by(user_id=user_id).delete()
return True
except:
return False
2024-05-19 16:26:24 +00:00
2024-06-24 11:55:18 +00:00
def delete_memory_by_id_and_user_id(self, id: str, user_id: str) -> bool:
2024-07-04 06:32:39 +00:00
with get_db() as db:
try:
db.query(Memory).filter_by(id=id, user_id=user_id).delete()
return True
except:
return False
2024-05-19 15:00:07 +00:00
Memories = MemoriesTable()