mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-07 11:28:35 -05:00
[PR #12971] [MERGED] fix: Align backend <source> tag indexing with frontend citation grouping #46115
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/12971
Author: @tth37
Created: 4/17/2025
Status: ✅ Merged
Merged: 4/18/2025
Merged by: @tjbck
Base:
dev← Head:fix_source_indexing📝 Commits (1)
79bde6ffix: Align backend tag indexing with frontend citation grouping📊 Changes
2 files changed (+11 additions, -6 deletions)
View changed files
📝
backend/open_webui/utils/middleware.py(+10 -6)📝
src/lib/components/chat/Messages/Citations.svelte(+1 -0)📄 Description
Related Issue and PR:
#12811 #12562
Problem
The current backend logic for generating citation indexes within
<source>tags in the context string, leads to inconsistencies with how the frontend groups and display citations:file_idin their metadata. The original code usedfile_idas the key for indexing, causing all web results (wherefile_idisNone) to map to the same index, even though they represent distinct sources. This directly causes issue #12811file_id. This results in generated text like[1][2][3]where the frontend might only display clickable citations for[1][2], leaving subsequent numbers as plain text because they refer to indices not uniquely represented in the frontend's grouped view.Root Cause
The backend used an internal identifier (
file_id) to create unique sequential indices for each document chunk. However, the frontend aggregates citation sources based on a different logic:metadata?.source ?? source?.source?.id ?? 'N/A'.c5636ff68c/src/lib/components/chat/Messages/Citations.svelte (L47-L57)This mismatch causes the mapping between the backend-generated
<source id="...">tags and the frontend's citation display to break.Solution
This PR modifies the backend context generation logic to use the exact same identifier logic as the frontend for creating the citation indices within the
<source>tags.Specifically, the identifier used to group sources and assign the
idattribute in<source id="{index}">is now calculated using:doc_meta.get("source", None) or source.get("source", {}).get("id", None) or "N/A"This ensures that:
<source id="...">index in the backend-generated context.[1],[2], etc.) referenced in the model's generation will directly correspond to the citation blocks rendered by the frontend.This alignment will hopefully ensure citation references function correctly.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.