mirror of
https://github.com/open-webui/open-webui.git
synced 2025-12-15 19:37:47 +01:00
enh: ability to edit memories
This commit is contained in:
@@ -64,6 +64,20 @@ class MemoriesTable:
|
||||
return memory
|
||||
else:
|
||||
return None
|
||||
|
||||
def update_memory(
|
||||
self,
|
||||
id: str,
|
||||
content: str,
|
||||
) -> Optional[MemoryModel]:
|
||||
try:
|
||||
memory = Memory.get(Memory.id == id)
|
||||
memory.content = content
|
||||
memory.updated_at = int(time.time())
|
||||
memory.save()
|
||||
return MemoryModel(**model_to_dict(memory))
|
||||
except:
|
||||
return None
|
||||
|
||||
def get_memories(self) -> List[MemoryModel]:
|
||||
try:
|
||||
|
||||
@@ -43,6 +43,8 @@ async def get_memories(user=Depends(get_verified_user)):
|
||||
class AddMemoryForm(BaseModel):
|
||||
content: str
|
||||
|
||||
class MemoryUpdateModel(BaseModel):
|
||||
content: Optional[str] = None
|
||||
|
||||
@router.post("/add", response_model=Optional[MemoryModel])
|
||||
async def add_memory(
|
||||
@@ -62,6 +64,27 @@ async def add_memory(
|
||||
return memory
|
||||
|
||||
|
||||
@router.patch("/{memory_id}", response_model=Optional[MemoryModel])
|
||||
async def update_memory(
|
||||
memory_id: str, request: Request, form_data: MemoryUpdateModel, user=Depends(get_verified_user)
|
||||
):
|
||||
memory = Memories.update_memory(memory_id, form_data.content)
|
||||
if memory is None:
|
||||
raise HTTPException(status_code=404, detail="Memory not found")
|
||||
|
||||
if form_data.content is not None:
|
||||
memory_embedding = request.app.state.EMBEDDING_FUNCTION(form_data.content)
|
||||
collection = CHROMA_CLIENT.get_or_create_collection(name=f"user-memory-{user.id}")
|
||||
collection.upsert(
|
||||
documents=[form_data.content],
|
||||
ids=[memory.id],
|
||||
embeddings=[memory_embedding],
|
||||
metadatas=[{"created_at": memory.created_at, "updated_at": memory.updated_at}],
|
||||
)
|
||||
|
||||
return memory
|
||||
|
||||
|
||||
############################
|
||||
# QueryMemory
|
||||
############################
|
||||
|
||||
Reference in New Issue
Block a user