From d8a7e9c660a0c5115c578fd092aee70b8f2bccaf Mon Sep 17 00:00:00 2001 From: Alexander Grimm Date: Mon, 7 Apr 2025 11:33:29 +0000 Subject: [PATCH 1/2] fix: correct document citation --- backend/open_webui/utils/middleware.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index badae9906..5e6f286a0 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -897,12 +897,14 @@ async def process_chat_payload(request, form_data, user, metadata, model): # If context is not empty, insert it into the messages if len(sources) > 0: context_string = "" - for source_idx, source in enumerate(sources): + citated_file_idx = {} + for _, source in enumerate(sources, 1): if "document" in source: - for doc_idx, doc_context in enumerate(source["document"]): - context_string += ( - f'{doc_context}\n' - ) + for doc_context, doc_meta in zip(source["document"], source['metadata']): + file_id = doc_meta.get('file_id') + if file_id not in citated_file_idx: + citated_file_idx[file_id] = len(citated_file_idx) + 1 + context_string += f"{citated_file_idx[file_id]}{doc_context}\n" context_string = context_string.strip() prompt = get_last_user_message(form_data["messages"]) From 1e7a36478bc4cf36caa85f4944740fbfeef3d7e7 Mon Sep 17 00:00:00 2001 From: Alexander Grimm Date: Mon, 7 Apr 2025 14:07:27 +0200 Subject: [PATCH 2/2] ~ update to latest change --- backend/open_webui/utils/middleware.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 5e6f286a0..2c6728955 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -904,7 +904,9 @@ async def process_chat_payload(request, form_data, user, metadata, model): file_id = doc_meta.get('file_id') if file_id not in citated_file_idx: citated_file_idx[file_id] = len(citated_file_idx) + 1 - context_string += f"{citated_file_idx[file_id]}{doc_context}\n" + context_string += ( + f'{doc_context}\n' + ) context_string = context_string.strip() prompt = get_last_user_message(form_data["messages"])