From 6c3e6710ef2e6b4fcb84f98810a033f73e54ce5e Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 18 Nov 2024 05:51:01 -0800 Subject: [PATCH] enh: knowledge author info --- .../open_webui/apps/webui/models/knowledge.py | 28 +++++++++++-------- .../apps/webui/routers/knowledge.py | 5 ++-- src/lib/components/workspace/Knowledge.svelte | 28 +++++++++++++------ src/lib/components/workspace/Models.svelte | 5 +++- src/lib/utils/index.ts | 1 + 5 files changed, 44 insertions(+), 23 deletions(-) diff --git a/backend/open_webui/apps/webui/models/knowledge.py b/backend/open_webui/apps/webui/models/knowledge.py index 2d0e33f1b..16eae7767 100644 --- a/backend/open_webui/apps/webui/models/knowledge.py +++ b/backend/open_webui/apps/webui/models/knowledge.py @@ -8,6 +8,7 @@ from open_webui.apps.webui.internal.db import Base, get_db from open_webui.env import SRC_LOG_LEVELS from open_webui.apps.webui.models.files import FileMetadataResponse +from open_webui.apps.webui.models.users import Users, UserResponse from pydantic import BaseModel, ConfigDict @@ -79,17 +80,15 @@ class KnowledgeModel(BaseModel): #################### -class KnowledgeResponse(BaseModel): - id: str - name: str - description: str - data: Optional[dict] = None - meta: Optional[dict] = None +class KnowledgeUserModel(KnowledgeModel): + user: Optional[UserResponse] = None - access_control: Optional[dict] = None - created_at: int # timestamp in epoch - updated_at: int # timestamp in epoch +class KnowledgeResponse(KnowledgeModel): + files: Optional[list[FileMetadataResponse | dict]] = None + + +class KnowledgeUserResponse(KnowledgeUserModel): files: Optional[list[FileMetadataResponse | dict]] = None @@ -127,10 +126,15 @@ class KnowledgeTable: except Exception: return None - def get_knowledge_bases(self) -> list[KnowledgeModel]: + def get_knowledge_bases(self) -> list[KnowledgeUserModel]: with get_db() as db: return [ - KnowledgeModel.model_validate(knowledge) + KnowledgeUserModel.model_validate( + { + **KnowledgeModel.model_validate(knowledge).model_dump(), + "user": Users.get_user_by_id(knowledge.user_id).model_dump(), + } + ) for knowledge in db.query(Knowledge) .order_by(Knowledge.updated_at.desc()) .all() @@ -138,7 +142,7 @@ class KnowledgeTable: def get_knowledge_bases_by_user_id( self, user_id: str, permission: str = "write" - ) -> list[KnowledgeModel]: + ) -> list[KnowledgeUserModel]: knowledge_bases = self.get_knowledge_bases() return [ knowledge_base diff --git a/backend/open_webui/apps/webui/routers/knowledge.py b/backend/open_webui/apps/webui/routers/knowledge.py index 966e82960..30fc5d9d9 100644 --- a/backend/open_webui/apps/webui/routers/knowledge.py +++ b/backend/open_webui/apps/webui/routers/knowledge.py @@ -8,6 +8,7 @@ from open_webui.apps.webui.models.knowledge import ( Knowledges, KnowledgeForm, KnowledgeResponse, + KnowledgeUserResponse, ) from open_webui.apps.webui.models.files import Files, FileModel from open_webui.apps.retrieval.vector.connector import VECTOR_DB_CLIENT @@ -32,7 +33,7 @@ router = APIRouter() ############################ -@router.get("/", response_model=list[KnowledgeResponse]) +@router.get("/", response_model=list[KnowledgeUserResponse]) async def get_knowledge(user=Depends(get_verified_user)): knowledge_bases = [] @@ -77,7 +78,7 @@ async def get_knowledge(user=Depends(get_verified_user)): return knowledge_bases -@router.get("/list", response_model=list[KnowledgeResponse]) +@router.get("/list", response_model=list[KnowledgeUserResponse]) async def get_knowledge_list(user=Depends(get_verified_user)): knowledge_bases = [] diff --git a/src/lib/components/workspace/Knowledge.svelte b/src/lib/components/workspace/Knowledge.svelte index e550239df..b2fc94614 100644 --- a/src/lib/components/workspace/Knowledge.svelte +++ b/src/lib/components/workspace/Knowledge.svelte @@ -24,6 +24,8 @@ import Search from '../icons/Search.svelte'; import Plus from '../icons/Plus.svelte'; import Spinner from '../common/Spinner.svelte'; + import { capitalizeFirstLetter } from '$lib/utils'; + import Tooltip from '../common/Tooltip.svelte'; let loaded = false; @@ -137,7 +139,11 @@ >
-
{item.name}
+ {#if item?.meta?.document} + + {:else} + + {/if}
+
{item.name}
+
{item.description}
-
-
- {#if item?.meta?.document} - - {:else} - - {/if} +
+
+ + {$i18n.t('By {{name}}', { + name: capitalizeFirstLetter(item?.user?.name ?? item?.user?.email) + })} +
{$i18n.t('Updated')} diff --git a/src/lib/components/workspace/Models.svelte b/src/lib/components/workspace/Models.svelte index d48eadb94..678bdb86f 100644 --- a/src/lib/components/workspace/Models.svelte +++ b/src/lib/components/workspace/Models.svelte @@ -32,6 +32,7 @@ import ChevronRight from '../icons/ChevronRight.svelte'; import Switch from '../common/Switch.svelte'; import Spinner from '../common/Spinner.svelte'; + import { capitalizeFirstLetter } from '$lib/utils'; let shiftKey = false; @@ -288,7 +289,9 @@
- By {model?.user?.name ?? model?.user?.email} + {$i18n.t('By {{name}}', { + name: capitalizeFirstLetter(model?.user?.name ?? model?.user?.email) + })}
diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index bc47d124d..42f1f3f7b 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -8,6 +8,7 @@ import { TTS_RESPONSE_SPLIT } from '$lib/types'; // Helper functions ////////////////////////// + export const replaceTokens = (content, char, user) => { const charToken = /{{char}}/gi; const userToken = /{{user}}/gi;