From 5b616dd34fa4c19eba5f17be04597774695e0687 Mon Sep 17 00:00:00 2001 From: Juanan Pereira Date: Thu, 9 Jan 2025 16:47:12 +0100 Subject: [PATCH 1/3] fix: Knowledge Base Detachment from Models --- backend/open_webui/routers/knowledge.py | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/backend/open_webui/routers/knowledge.py b/backend/open_webui/routers/knowledge.py index ad67cc31f..c6bc2cdee 100644 --- a/backend/open_webui/routers/knowledge.py +++ b/backend/open_webui/routers/knowledge.py @@ -25,6 +25,7 @@ from open_webui.utils.access_control import has_access, has_permission from open_webui.env import SRC_LOG_LEVELS +from open_webui.models.models import Models, ModelForm log = logging.getLogger(__name__) @@ -473,6 +474,36 @@ async def delete_knowledge_by_id(id: str, user=Depends(get_verified_user)): detail=ERROR_MESSAGES.ACCESS_PROHIBITED, ) + log.info(f"Deleting knowledge base: {id} (name: {knowledge.name})") + + # Get all models + models = Models.get_all_models() + log.info(f"Found {len(models)} models to check for knowledge base {id}") + + # Update models that reference this knowledge base + for model in models: + if model.meta and hasattr(model.meta, "knowledge"): + knowledge_list = model.meta.knowledge or [] + # Filter out the deleted knowledge base + updated_knowledge = [k for k in knowledge_list if k.get("id") != id] + + # If the knowledge list changed, update the model + if len(updated_knowledge) != len(knowledge_list): + log.info(f"Updating model {model.id} to remove knowledge base {id}") + model.meta.knowledge = updated_knowledge + # Create a ModelForm for the update + model_form = ModelForm( + id=model.id, + name=model.name, + base_model_id=model.base_model_id, + meta=model.meta, + params=model.params, + access_control=model.access_control, + is_active=model.is_active + ) + Models.update_model_by_id(model.id, model_form) + + # Clean up vector DB try: VECTOR_DB_CLIENT.delete_collection(collection_name=id) except Exception as e: From de735b3c8c125a2e06ccd857bfbe0d2375c79887 Mon Sep 17 00:00:00 2001 From: Juanan Pereira Date: Thu, 9 Jan 2025 18:41:18 +0100 Subject: [PATCH 2/3] fix format --- backend/open_webui/routers/knowledge.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/open_webui/routers/knowledge.py b/backend/open_webui/routers/knowledge.py index c6bc2cdee..e58442c50 100644 --- a/backend/open_webui/routers/knowledge.py +++ b/backend/open_webui/routers/knowledge.py @@ -486,7 +486,7 @@ async def delete_knowledge_by_id(id: str, user=Depends(get_verified_user)): knowledge_list = model.meta.knowledge or [] # Filter out the deleted knowledge base updated_knowledge = [k for k in knowledge_list if k.get("id") != id] - + # If the knowledge list changed, update the model if len(updated_knowledge) != len(knowledge_list): log.info(f"Updating model {model.id} to remove knowledge base {id}") @@ -499,7 +499,7 @@ async def delete_knowledge_by_id(id: str, user=Depends(get_verified_user)): meta=model.meta, params=model.params, access_control=model.access_control, - is_active=model.is_active + is_active=model.is_active, ) Models.update_model_by_id(model.id, model_form) From f72490093aceecd2c6888f890212ccff09370d9f Mon Sep 17 00:00:00 2001 From: Juanan Pereira Date: Thu, 9 Jan 2025 18:56:40 +0100 Subject: [PATCH 3/3] fix frontend format --- src/lib/utils/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 1d2095780..82b30b2d6 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -74,7 +74,6 @@ export const processResponseContent = (content: string) => { return content.trim(); }; - export function unescapeHtml(html: string) { const doc = new DOMParser().parseFromString(html, 'text/html'); return doc.documentElement.textContent;