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

585 lines
17 KiB
Python
Raw Normal View History

2024-12-13 14:29:43 +00:00
from typing import List, Optional
2024-10-02 05:45:04 +00:00
from pydantic import BaseModel
2024-11-17 11:04:31 +00:00
from fastapi import APIRouter, Depends, HTTPException, status, Request
2024-10-04 18:11:53 +00:00
import logging
2024-10-02 05:45:04 +00:00
from open_webui.apps.webui.models.knowledge import (
Knowledges,
KnowledgeForm,
KnowledgeResponse,
2024-11-18 13:51:01 +00:00
KnowledgeUserResponse,
2024-10-02 05:45:04 +00:00
)
2024-10-03 03:42:10 +00:00
from open_webui.apps.webui.models.files import Files, FileModel
2024-10-04 05:35:43 +00:00
from open_webui.apps.retrieval.vector.connector import VECTOR_DB_CLIENT
2024-12-13 14:29:43 +00:00
from open_webui.apps.retrieval.main import BatchProcessFilesForm, process_file, ProcessFileForm, process_files_batch
2024-10-04 05:35:43 +00:00
2024-10-03 03:42:10 +00:00
2024-10-02 05:45:04 +00:00
from open_webui.constants import ERROR_MESSAGES
2024-12-13 14:29:43 +00:00
from open_webui.utils.auth import get_verified_user
2024-11-17 11:04:31 +00:00
from open_webui.utils.access_control import has_access, has_permission
2024-11-17 02:00:57 +00:00
2024-10-04 18:11:53 +00:00
from open_webui.env import SRC_LOG_LEVELS
2024-10-02 05:45:04 +00:00
2024-10-04 18:11:53 +00:00
log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["MODELS"])
2024-10-03 13:46:20 +00:00
2024-10-02 05:45:04 +00:00
router = APIRouter()
############################
2024-11-17 00:51:55 +00:00
# getKnowledgeBases
2024-10-02 05:45:04 +00:00
############################
2024-11-18 13:51:01 +00:00
@router.get("/", response_model=list[KnowledgeUserResponse])
2024-11-17 00:51:55 +00:00
async def get_knowledge(user=Depends(get_verified_user)):
knowledge_bases = []
2024-10-02 05:45:04 +00:00
2024-11-17 00:51:55 +00:00
if user.role == "admin":
knowledge_bases = Knowledges.get_knowledge_bases()
2024-10-02 05:45:04 +00:00
else:
2024-11-17 00:51:55 +00:00
knowledge_bases = Knowledges.get_knowledge_bases_by_user_id(user.id, "read")
# Get files for each knowledge base
2024-11-20 18:02:14 +00:00
knowledge_with_files = []
2024-11-17 00:51:55 +00:00
for knowledge_base in knowledge_bases:
files = []
if knowledge_base.data:
files = Files.get_file_metadatas_by_ids(
knowledge_base.data.get("file_ids", [])
)
2024-11-17 00:51:55 +00:00
# Check if all files exist
if len(files) != len(knowledge_base.data.get("file_ids", [])):
missing_files = list(
set(knowledge_base.data.get("file_ids", []))
- set([file.id for file in files])
)
2024-11-17 00:51:55 +00:00
if missing_files:
data = knowledge_base.data or {}
file_ids = data.get("file_ids", [])
2024-11-17 00:51:55 +00:00
for missing_file in missing_files:
file_ids.remove(missing_file)
data["file_ids"] = file_ids
2024-11-17 04:47:45 +00:00
Knowledges.update_knowledge_data_by_id(
id=knowledge_base.id, data=data
)
2024-10-26 08:19:17 +00:00
2024-11-17 00:51:55 +00:00
files = Files.get_file_metadatas_by_ids(file_ids)
2024-10-26 08:19:17 +00:00
2024-11-20 18:02:14 +00:00
knowledge_with_files.append(
KnowledgeUserResponse(
**knowledge_base.model_dump(),
files=files,
)
2024-11-17 00:51:55 +00:00
)
2024-11-20 18:02:14 +00:00
return knowledge_with_files
2024-11-17 00:51:55 +00:00
2024-11-18 13:51:01 +00:00
@router.get("/list", response_model=list[KnowledgeUserResponse])
2024-11-17 00:51:55 +00:00
async def get_knowledge_list(user=Depends(get_verified_user)):
knowledge_bases = []
if user.role == "admin":
knowledge_bases = Knowledges.get_knowledge_bases()
else:
knowledge_bases = Knowledges.get_knowledge_bases_by_user_id(user.id, "write")
# Get files for each knowledge base
2024-11-20 18:02:14 +00:00
knowledge_with_files = []
2024-11-17 00:51:55 +00:00
for knowledge_base in knowledge_bases:
files = []
if knowledge_base.data:
files = Files.get_file_metadatas_by_ids(
knowledge_base.data.get("file_ids", [])
)
2024-11-17 00:51:55 +00:00
# Check if all files exist
if len(files) != len(knowledge_base.data.get("file_ids", [])):
missing_files = list(
set(knowledge_base.data.get("file_ids", []))
- set([file.id for file in files])
)
if missing_files:
data = knowledge_base.data or {}
file_ids = data.get("file_ids", [])
for missing_file in missing_files:
file_ids.remove(missing_file)
data["file_ids"] = file_ids
2024-11-17 04:47:45 +00:00
Knowledges.update_knowledge_data_by_id(
id=knowledge_base.id, data=data
2024-11-17 00:51:55 +00:00
)
files = Files.get_file_metadatas_by_ids(file_ids)
2024-11-20 18:02:14 +00:00
knowledge_with_files.append(
KnowledgeUserResponse(
**knowledge_base.model_dump(),
files=files,
)
2024-11-17 00:51:55 +00:00
)
2024-11-20 18:02:14 +00:00
return knowledge_with_files
2024-10-02 05:45:04 +00:00
############################
# CreateNewKnowledge
############################
@router.post("/create", response_model=Optional[KnowledgeResponse])
2024-11-17 00:51:55 +00:00
async def create_new_knowledge(
2024-11-17 11:04:31 +00:00
request: Request, form_data: KnowledgeForm, user=Depends(get_verified_user)
2024-11-17 00:51:55 +00:00
):
2024-11-17 11:04:31 +00:00
if user.role != "admin" and not has_permission(
user.id, "workspace.knowledge", request.app.state.config.USER_PERMISSIONS
):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=ERROR_MESSAGES.UNAUTHORIZED,
)
2024-10-02 05:45:04 +00:00
knowledge = Knowledges.insert_new_knowledge(user.id, form_data)
if knowledge:
return knowledge
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.FILE_EXISTS,
)
############################
# GetKnowledgeById
############################
2024-10-03 03:42:10 +00:00
class KnowledgeFilesResponse(KnowledgeResponse):
files: list[FileModel]
@router.get("/{id}", response_model=Optional[KnowledgeFilesResponse])
2024-10-02 05:45:04 +00:00
async def get_knowledge_by_id(id: str, user=Depends(get_verified_user)):
knowledge = Knowledges.get_knowledge_by_id(id=id)
if knowledge:
2024-10-03 03:42:10 +00:00
2024-11-17 02:00:57 +00:00
if (
user.role == "admin"
or knowledge.user_id == user.id
or has_access(user.id, "read", knowledge.access_control)
):
file_ids = knowledge.data.get("file_ids", []) if knowledge.data else []
files = Files.get_files_by_ids(file_ids)
return KnowledgeFilesResponse(
**knowledge.model_dump(),
files=files,
)
2024-10-02 05:45:04 +00:00
else:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=ERROR_MESSAGES.NOT_FOUND,
)
############################
# UpdateKnowledgeById
############################
2024-10-03 13:46:20 +00:00
@router.post("/{id}/update", response_model=Optional[KnowledgeFilesResponse])
2024-10-02 05:45:04 +00:00
async def update_knowledge_by_id(
id: str,
2024-11-17 04:47:45 +00:00
form_data: KnowledgeForm,
2024-11-17 00:51:55 +00:00
user=Depends(get_verified_user),
2024-10-02 05:45:04 +00:00
):
2024-11-17 02:00:57 +00:00
knowledge = Knowledges.get_knowledge_by_id(id=id)
if not knowledge:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.NOT_FOUND,
)
if knowledge.user_id != user.id and user.role != "admin":
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
)
2024-10-02 05:45:04 +00:00
2024-11-17 02:00:57 +00:00
knowledge = Knowledges.update_knowledge_by_id(id=id, form_data=form_data)
2024-10-02 05:45:04 +00:00
if knowledge:
2024-10-03 13:46:20 +00:00
file_ids = knowledge.data.get("file_ids", []) if knowledge.data else []
files = Files.get_files_by_ids(file_ids)
return KnowledgeFilesResponse(
**knowledge.model_dump(),
files=files,
)
2024-10-02 05:45:04 +00:00
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.ID_TAKEN,
)
2024-10-03 13:46:20 +00:00
############################
# AddFileToKnowledge
############################
class KnowledgeFileIdForm(BaseModel):
file_id: str
@router.post("/{id}/file/add", response_model=Optional[KnowledgeFilesResponse])
2024-10-04 05:22:22 +00:00
def add_file_to_knowledge_by_id(
2024-10-03 13:46:20 +00:00
id: str,
form_data: KnowledgeFileIdForm,
2024-11-17 00:51:55 +00:00
user=Depends(get_verified_user),
2024-10-03 13:46:20 +00:00
):
knowledge = Knowledges.get_knowledge_by_id(id=id)
2024-11-17 02:00:57 +00:00
if not knowledge:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.NOT_FOUND,
)
if knowledge.user_id != user.id and user.role != "admin":
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
)
2024-10-03 13:46:20 +00:00
file = Files.get_file_by_id(form_data.file_id)
if not file:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.NOT_FOUND,
)
2024-10-04 05:22:22 +00:00
if not file.data:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.FILE_NOT_PROCESSED,
)
2024-10-03 13:46:20 +00:00
2024-10-04 05:35:43 +00:00
# Add content to the vector database
2024-10-04 06:06:47 +00:00
try:
process_file(ProcessFileForm(file_id=form_data.file_id, collection_name=id))
except Exception as e:
2024-10-04 23:16:16 +00:00
log.debug(e)
2024-10-05 01:22:55 +00:00
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e),
)
2024-10-04 05:35:43 +00:00
2024-10-03 13:46:20 +00:00
if knowledge:
data = knowledge.data or {}
file_ids = data.get("file_ids", [])
if form_data.file_id not in file_ids:
file_ids.append(form_data.file_id)
data["file_ids"] = file_ids
2024-11-17 10:40:47 +00:00
knowledge = Knowledges.update_knowledge_data_by_id(id=id, data=data)
2024-10-03 13:46:20 +00:00
if knowledge:
files = Files.get_files_by_ids(file_ids)
return KnowledgeFilesResponse(
**knowledge.model_dump(),
files=files,
)
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.DEFAULT("knowledge"),
)
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.DEFAULT("file_id"),
)
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.NOT_FOUND,
)
2024-10-04 07:46:32 +00:00
@router.post("/{id}/file/update", response_model=Optional[KnowledgeFilesResponse])
def update_file_from_knowledge_by_id(
id: str,
form_data: KnowledgeFileIdForm,
2024-11-17 00:51:55 +00:00
user=Depends(get_verified_user),
2024-10-04 07:46:32 +00:00
):
knowledge = Knowledges.get_knowledge_by_id(id=id)
2024-11-17 02:00:57 +00:00
if not knowledge:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.NOT_FOUND,
)
if knowledge.user_id != user.id and user.role != "admin":
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
)
2024-10-04 07:46:32 +00:00
file = Files.get_file_by_id(form_data.file_id)
if not file:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.NOT_FOUND,
)
# Remove content from the vector database
VECTOR_DB_CLIENT.delete(
collection_name=knowledge.id, filter={"file_id": form_data.file_id}
)
# Add content to the vector database
try:
process_file(ProcessFileForm(file_id=form_data.file_id, collection_name=id))
except Exception as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e),
)
if knowledge:
data = knowledge.data or {}
file_ids = data.get("file_ids", [])
files = Files.get_files_by_ids(file_ids)
return KnowledgeFilesResponse(
**knowledge.model_dump(),
files=files,
)
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.NOT_FOUND,
)
2024-10-03 13:46:20 +00:00
############################
# RemoveFileFromKnowledge
############################
@router.post("/{id}/file/remove", response_model=Optional[KnowledgeFilesResponse])
2024-10-04 05:22:22 +00:00
def remove_file_from_knowledge_by_id(
2024-10-03 13:46:20 +00:00
id: str,
form_data: KnowledgeFileIdForm,
2024-11-17 00:51:55 +00:00
user=Depends(get_verified_user),
2024-10-03 13:46:20 +00:00
):
knowledge = Knowledges.get_knowledge_by_id(id=id)
2024-11-17 02:00:57 +00:00
if not knowledge:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.NOT_FOUND,
)
if knowledge.user_id != user.id and user.role != "admin":
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
)
2024-10-03 13:46:20 +00:00
file = Files.get_file_by_id(form_data.file_id)
if not file:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.NOT_FOUND,
)
2024-10-04 05:35:43 +00:00
# Remove content from the vector database
2024-10-03 13:46:20 +00:00
VECTOR_DB_CLIENT.delete(
collection_name=knowledge.id, filter={"file_id": form_data.file_id}
)
2024-10-04 07:46:32 +00:00
result = VECTOR_DB_CLIENT.query(
collection_name=knowledge.id,
filter={"file_id": form_data.file_id},
)
2024-10-04 05:35:43 +00:00
Files.delete_file_by_id(form_data.file_id)
2024-10-03 13:46:20 +00:00
if knowledge:
data = knowledge.data or {}
file_ids = data.get("file_ids", [])
if form_data.file_id in file_ids:
file_ids.remove(form_data.file_id)
data["file_ids"] = file_ids
2024-11-17 10:40:47 +00:00
knowledge = Knowledges.update_knowledge_data_by_id(id=id, data=data)
2024-10-03 13:46:20 +00:00
if knowledge:
files = Files.get_files_by_ids(file_ids)
return KnowledgeFilesResponse(
**knowledge.model_dump(),
files=files,
)
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.DEFAULT("knowledge"),
)
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.DEFAULT("file_id"),
)
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.NOT_FOUND,
)
2024-10-05 01:44:57 +00:00
############################
2024-11-17 02:00:57 +00:00
# DeleteKnowledgeById
2024-10-05 01:44:57 +00:00
############################
2024-11-17 02:00:57 +00:00
@router.delete("/{id}/delete", response_model=bool)
async def delete_knowledge_by_id(id: str, user=Depends(get_verified_user)):
knowledge = Knowledges.get_knowledge_by_id(id=id)
if not knowledge:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.NOT_FOUND,
)
if knowledge.user_id != user.id and user.role != "admin":
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
)
2024-10-05 01:44:57 +00:00
try:
VECTOR_DB_CLIENT.delete_collection(collection_name=id)
except Exception as e:
log.debug(e)
pass
2024-11-17 02:00:57 +00:00
result = Knowledges.delete_knowledge_by_id(id=id)
return result
2024-10-05 01:44:57 +00:00
2024-10-02 05:45:04 +00:00
############################
2024-11-17 02:00:57 +00:00
# ResetKnowledgeById
2024-10-02 05:45:04 +00:00
############################
2024-11-17 02:00:57 +00:00
@router.post("/{id}/reset", response_model=Optional[KnowledgeResponse])
async def reset_knowledge_by_id(id: str, user=Depends(get_verified_user)):
knowledge = Knowledges.get_knowledge_by_id(id=id)
if not knowledge:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.NOT_FOUND,
)
if knowledge.user_id != user.id and user.role != "admin":
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
)
2024-10-04 18:11:53 +00:00
try:
VECTOR_DB_CLIENT.delete_collection(collection_name=id)
except Exception as e:
log.debug(e)
pass
2024-11-17 02:00:57 +00:00
2024-11-17 10:40:47 +00:00
knowledge = Knowledges.update_knowledge_data_by_id(id=id, data={"file_ids": []})
2024-11-17 04:47:45 +00:00
2024-11-17 02:00:57 +00:00
return knowledge
2024-12-13 14:29:43 +00:00
############################
# AddFilesToKnowledge
############################
@router.post("/{id}/files/batch/add", response_model=Optional[KnowledgeFilesResponse])
def add_files_to_knowledge_batch(
id: str,
form_data: list[KnowledgeFileIdForm],
user=Depends(get_verified_user),
):
"""
Add multiple files to a knowledge base
"""
knowledge = Knowledges.get_knowledge_by_id(id=id)
if not knowledge:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.NOT_FOUND,
)
if knowledge.user_id != user.id and user.role != "admin":
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
)
# Get files content
print(f"files/batch/add - {len(form_data)} files")
files: List[FileModel] = []
for form in form_data:
file = Files.get_file_by_id(form.file_id)
if not file:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"File {form.file_id} not found",
)
files.append(file)
# Process files
result = process_files_batch(BatchProcessFilesForm(
files=files,
collection_name=id
))
# Add successful files to knowledge base
data = knowledge.data or {}
existing_file_ids = data.get("file_ids", [])
# Only add files that were successfully processed
successful_file_ids = [r.file_id for r in result.results if r.status == "completed"]
for file_id in successful_file_ids:
if file_id not in existing_file_ids:
existing_file_ids.append(file_id)
data["file_ids"] = existing_file_ids
knowledge = Knowledges.update_knowledge_data_by_id(id=id, data=data)
# If there were any errors, include them in the response
if result.errors:
error_details = [f"{err.file_id}: {err.error}" for err in result.errors]
return KnowledgeFilesResponse(
**knowledge.model_dump(),
files=Files.get_files_by_ids(existing_file_ids),
warnings={
"message": "Some files failed to process",
"errors": error_details
}
)
return KnowledgeFilesResponse(
**knowledge.model_dump(),
files=Files.get_files_by_ids(existing_file_ids)
)