open-webui/backend/open_webui/apps/webui/routers/memories.py

180 lines
4.8 KiB
Python
Raw Normal View History

2024-05-19 15:00:07 +00:00
import logging
2024-08-27 22:10:27 +00:00
from typing import Optional
2024-05-19 15:00:07 +00:00
from open_webui.apps.webui.models.memories import Memories, MemoryModel
from open_webui.config import CHROMA_CLIENT
from open_webui.env import SRC_LOG_LEVELS
2024-08-27 22:10:27 +00:00
from fastapi import APIRouter, Depends, HTTPException, Request
from pydantic import BaseModel
from open_webui.utils.utils import get_verified_user
2024-05-19 15:00:07 +00:00
log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["MODELS"])
router = APIRouter()
@router.get("/ef")
async def get_embeddings(request: Request):
return {"result": request.app.state.EMBEDDING_FUNCTION("hello world")}
############################
# GetMemories
############################
2024-08-14 12:46:31 +00:00
@router.get("/", response_model=list[MemoryModel])
async def get_memories(user=Depends(get_verified_user)):
return Memories.get_memories_by_user_id(user.id)
2024-05-19 15:00:07 +00:00
############################
# AddMemory
############################
class AddMemoryForm(BaseModel):
content: str
2024-06-15 09:35:44 +00:00
2024-06-13 01:01:50 +00:00
class MemoryUpdateModel(BaseModel):
content: Optional[str] = None
2024-05-19 15:00:07 +00:00
2024-06-15 09:35:44 +00:00
2024-05-19 15:00:07 +00:00
@router.post("/add", response_model=Optional[MemoryModel])
async def add_memory(
request: Request,
form_data: AddMemoryForm,
user=Depends(get_verified_user),
2024-05-19 15:00:07 +00:00
):
memory = Memories.insert_new_memory(user.id, form_data.content)
2024-05-19 15:00:07 +00:00
memory_embedding = request.app.state.EMBEDDING_FUNCTION(memory.content)
collection = CHROMA_CLIENT.get_or_create_collection(name=f"user-memory-{user.id}")
collection.upsert(
documents=[memory.content],
ids=[memory.id],
embeddings=[memory_embedding],
metadatas=[{"created_at": memory.created_at}],
)
return memory
############################
# QueryMemory
############################
class QueryMemoryForm(BaseModel):
content: str
2024-06-24 20:33:03 +00:00
k: Optional[int] = 1
2024-05-19 15:00:07 +00:00
2024-05-19 15:40:46 +00:00
@router.post("/query")
2024-05-19 15:14:04 +00:00
async def query_memory(
2024-05-19 15:00:07 +00:00
request: Request, form_data: QueryMemoryForm, user=Depends(get_verified_user)
):
query_embedding = request.app.state.EMBEDDING_FUNCTION(form_data.content)
collection = CHROMA_CLIENT.get_or_create_collection(name=f"user-memory-{user.id}")
results = collection.query(
query_embeddings=[query_embedding],
2024-06-24 20:33:03 +00:00
n_results=form_data.k, # how many results to return
2024-05-19 15:00:07 +00:00
)
return results
############################
# ResetMemoryFromVectorDB
############################
@router.post("/reset", response_model=bool)
2024-05-19 15:00:07 +00:00
async def reset_memory_from_vector_db(
request: Request, user=Depends(get_verified_user)
2024-05-19 15:00:07 +00:00
):
CHROMA_CLIENT.delete_collection(f"user-memory-{user.id}")
collection = CHROMA_CLIENT.get_or_create_collection(name=f"user-memory-{user.id}")
memories = Memories.get_memories_by_user_id(user.id)
2024-05-19 15:00:07 +00:00
for memory in memories:
memory_embedding = request.app.state.EMBEDDING_FUNCTION(memory.content)
collection.upsert(
documents=[memory.content],
ids=[memory.id],
embeddings=[memory_embedding],
)
return True
############################
2024-05-19 16:26:24 +00:00
# DeleteMemoriesByUserId
############################
@router.delete("/delete/user", response_model=bool)
async def delete_memory_by_user_id(user=Depends(get_verified_user)):
result = Memories.delete_memories_by_user_id(user.id)
2024-05-19 16:26:24 +00:00
if result:
try:
CHROMA_CLIENT.delete_collection(f"user-memory-{user.id}")
except Exception as e:
log.error(e)
return True
return False
############################
# UpdateMemoryById
############################
@router.post("/{memory_id}/update", response_model=Optional[MemoryModel])
async def update_memory_by_id(
memory_id: str,
request: Request,
form_data: MemoryUpdateModel,
user=Depends(get_verified_user),
):
memory = Memories.update_memory_by_id(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
2024-05-19 16:26:24 +00:00
############################
# DeleteMemoryById
2024-05-19 15:00:07 +00:00
############################
@router.delete("/{memory_id}", response_model=bool)
2024-06-24 07:57:08 +00:00
async def delete_memory_by_id(memory_id: str, user=Depends(get_verified_user)):
result = Memories.delete_memory_by_id_and_user_id(memory_id, user.id)
2024-05-19 15:46:39 +00:00
if result:
collection = CHROMA_CLIENT.get_or_create_collection(
name=f"user-memory-{user.id}"
)
2024-05-19 15:47:37 +00:00
collection.delete(ids=[memory_id])
2024-05-19 15:46:39 +00:00
return True
2024-05-19 15:47:37 +00:00
2024-05-19 15:46:39 +00:00
return False