Skip to content

Commit d664922

Browse files
Classic298tjbck
andauthored
Avoid loading full chat JSON blob for pinned/archived/shared list endpoints (open-webui#21591)
Co-authored-by: Tim Baek <tim@openwebui.com>
1 parent 9950cc8 commit d664922

2 files changed

Lines changed: 47 additions & 26 deletions

File tree

backend/open_webui/models/chats.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ def get_archived_chat_list_by_user_id(
714714
skip: int = 0,
715715
limit: int = 50,
716716
db: Optional[Session] = None,
717-
) -> list[ChatModel]:
717+
) -> list[ChatTitleIdResponse]:
718718

719719
with get_db_context(db) as db:
720720
query = db.query(Chat).filter_by(user_id=user_id, archived=True)
@@ -740,13 +740,27 @@ def get_archived_chat_list_by_user_id(
740740
else:
741741
query = query.order_by(Chat.updated_at.desc())
742742

743+
query = query.with_entities(
744+
Chat.id, Chat.title, Chat.updated_at, Chat.created_at
745+
)
746+
743747
if skip:
744748
query = query.offset(skip)
745749
if limit:
746750
query = query.limit(limit)
747751

748752
all_chats = query.all()
749-
return [ChatModel.model_validate(chat) for chat in all_chats]
753+
return [
754+
ChatTitleIdResponse.model_validate(
755+
{
756+
"id": chat[0],
757+
"title": chat[1],
758+
"updated_at": chat[2],
759+
"created_at": chat[3],
760+
}
761+
)
762+
for chat in all_chats
763+
]
750764

751765
def get_shared_chat_list_by_user_id(
752766
self,
@@ -802,12 +816,14 @@ def get_shared_chat_list_by_user_id(
802816

803817
all_chats = query.all()
804818
return [
805-
SharedChatResponse(
806-
id=chat[0],
807-
title=chat[1],
808-
share_id=chat[2],
809-
updated_at=chat[3],
810-
created_at=chat[4],
819+
SharedChatResponse.model_validate(
820+
{
821+
"id": chat[0],
822+
"title": chat[1],
823+
"share_id": chat[2],
824+
"updated_at": chat[3],
825+
"created_at": chat[4],
826+
}
811827
)
812828
for chat in all_chats
813829
]
@@ -1017,14 +1033,27 @@ def get_chats_by_user_id(
10171033

10181034
def get_pinned_chats_by_user_id(
10191035
self, user_id: str, db: Optional[Session] = None
1020-
) -> list[ChatModel]:
1036+
) -> list[ChatTitleIdResponse]:
10211037
with get_db_context(db) as db:
10221038
all_chats = (
10231039
db.query(Chat)
10241040
.filter_by(user_id=user_id, pinned=True, archived=False)
10251041
.order_by(Chat.updated_at.desc())
1042+
.with_entities(
1043+
Chat.id, Chat.title, Chat.updated_at, Chat.created_at
1044+
)
10261045
)
1027-
return [ChatModel.model_validate(chat) for chat in all_chats]
1046+
return [
1047+
ChatTitleIdResponse.model_validate(
1048+
{
1049+
"id": chat[0],
1050+
"title": chat[1],
1051+
"updated_at": chat[2],
1052+
"created_at": chat[3],
1053+
}
1054+
)
1055+
for chat in all_chats
1056+
]
10281057

10291058
def get_archived_chats_by_user_id(
10301059
self, user_id: str, db: Optional[Session] = None

backend/open_webui/routers/chats.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -723,10 +723,7 @@ async def get_chat_list_by_folder_id(
723723
async def get_user_pinned_chats(
724724
user=Depends(get_verified_user), db: Session = Depends(get_session)
725725
):
726-
return [
727-
ChatTitleIdResponse(**chat.model_dump())
728-
for chat in Chats.get_pinned_chats_by_user_id(user.id, db=db)
729-
]
726+
return Chats.get_pinned_chats_by_user_id(user.id, db=db)
730727

731728

732729
############################
@@ -821,18 +818,13 @@ async def get_archived_session_user_chat_list(
821818
if direction:
822819
filter["direction"] = direction
823820

824-
chat_list = [
825-
ChatTitleIdResponse(**chat.model_dump())
826-
for chat in Chats.get_archived_chat_list_by_user_id(
827-
user.id,
828-
filter=filter,
829-
skip=skip,
830-
limit=limit,
831-
db=db,
832-
)
833-
]
834-
835-
return chat_list
821+
return Chats.get_archived_chat_list_by_user_id(
822+
user.id,
823+
filter=filter,
824+
skip=skip,
825+
limit=limit,
826+
db=db,
827+
)
836828

837829

838830
############################

0 commit comments

Comments
 (0)