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

191 lines
4.8 KiB
Python
Raw Normal View History

2024-09-10 01:27:50 +00:00
from fastapi import APIRouter, Depends, HTTPException, Request
from pydantic import BaseModel
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
2024-09-27 23:28:45 +00:00
from open_webui.apps.retrieval.vector.connector import VECTOR_DB_CLIENT
from open_webui.utils.utils import get_verified_user
2024-09-10 01:27:50 +00:00
from open_webui.env import SRC_LOG_LEVELS
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
2024-09-10 03:37:06 +00:00
VECTOR_DB_CLIENT.upsert(
collection_name=f"user-memory-{user.id}",
items=[
{
"id": memory.id,
"text": memory.content,
"vector": request.app.state.EMBEDDING_FUNCTION(memory.content),
"metadata": {"created_at": memory.created_at},
}
],
2024-05-19 15:00:07 +00:00
)
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)
):
2024-09-10 03:37:06 +00:00
results = VECTOR_DB_CLIENT.search(
2024-09-16 05:14:17 +00:00
collection_name=f"user-memory-{user.id}",
2024-09-10 03:37:06 +00:00
vectors=[request.app.state.EMBEDDING_FUNCTION(form_data.content)],
limit=form_data.k,
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
):
2024-09-10 01:27:50 +00:00
VECTOR_DB_CLIENT.delete_collection(f"user-memory-{user.id}")
2024-05-19 15:00:07 +00:00
memories = Memories.get_memories_by_user_id(user.id)
2024-09-10 03:37:06 +00:00
VECTOR_DB_CLIENT.upsert(
collection_name=f"user-memory-{user.id}",
items=[
{
"id": memory.id,
"text": memory.content,
"vector": request.app.state.EMBEDDING_FUNCTION(memory.content),
"metadata": {
"created_at": memory.created_at,
"updated_at": memory.updated_at,
},
}
for memory in memories
],
)
2024-05-19 15:00:07 +00:00
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:
2024-09-10 01:27:50 +00:00
VECTOR_DB_CLIENT.delete_collection(f"user-memory-{user.id}")
2024-05-19 16:26:24 +00:00
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:
2024-09-10 03:37:06 +00:00
VECTOR_DB_CLIENT.upsert(
collection_name=f"user-memory-{user.id}",
items=[
{
"id": memory.id,
"text": memory.content,
"vector": request.app.state.EMBEDDING_FUNCTION(memory.content),
"metadata": {
"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:
2024-09-10 03:37:06 +00:00
VECTOR_DB_CLIENT.delete(
collection_name=f"user-memory-{user.id}", 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