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

138 lines
3.7 KiB
Python
Raw Permalink Normal View History

2024-05-19 15:00:07 +00:00
import time
import uuid
2024-08-27 22:10:27 +00:00
from typing import Optional
2024-12-10 08:54:13 +00:00
from open_webui.internal.db import Base, get_db
2024-08-27 22:10:27 +00:00
from pydantic import BaseModel, ConfigDict
from sqlalchemy import BigInteger, Column, String, Text
2024-05-19 15:00:07 +00:00
####################
# 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)
2024-08-14 12:38:19 +00:00
except Exception:
2024-07-04 06:32:39 +00:00
return None
2024-05-19 15:00:07 +00:00
2024-08-14 12:46:31 +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]
2024-08-14 12:38:19 +00:00
except Exception:
2024-07-04 06:32:39 +00:00
return None
2024-05-19 15:00:07 +00:00
2024-08-14 12:46:31 +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]
2024-08-14 12:38:19 +00:00
except Exception:
2024-07-04 06:32:39 +00:00
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)
2024-08-14 12:38:19 +00:00
except Exception:
2024-07-04 06:32:39 +00:00
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()
2024-07-06 15:10:58 +00:00
db.commit()
2024-07-04 06:32:39 +00:00
return True
2024-05-19 15:00:07 +00:00
2024-08-14 12:38:19 +00:00
except Exception:
2024-07-04 06:32:39 +00:00
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()
2024-07-06 15:10:58 +00:00
db.commit()
2024-07-04 06:32:39 +00:00
return True
2024-08-14 12:38:19 +00:00
except Exception:
2024-07-04 06:32:39 +00:00
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()
2024-07-06 15:10:58 +00:00
db.commit()
2024-07-04 06:32:39 +00:00
return True
2024-08-14 12:38:19 +00:00
except Exception:
2024-07-04 06:32:39 +00:00
return False
2024-05-19 15:00:07 +00:00
Memories = MemoriesTable()