Skip to content

Commit e0d4c3e

Browse files
committed
refac
1 parent 65fbbf5 commit e0d4c3e

1 file changed

Lines changed: 48 additions & 22 deletions

File tree

backend/open_webui/utils/middleware.py

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
get_last_user_message_item,
9292
get_last_assistant_message,
9393
get_system_message,
94+
replace_system_message_content,
9495
prepend_to_first_user_message_content,
9596
convert_logit_bias_input_to_json,
9697
get_content_from_message,
@@ -4090,6 +4091,22 @@ async def flush_pending_delta_data(threshold: int = 0):
40904091
all_tool_call_sources = [] # Accumulated sources across all iterations
40914092
user_message = get_last_user_message(form_data["messages"])
40924093

4094+
# Check if citations are enabled for this model
4095+
citations_enabled = (
4096+
model.get("info", {}).get("meta", {}).get("capabilities") or {}
4097+
).get("citations", True)
4098+
4099+
# Save original system message so we can restore it before
4100+
# re-applying source context (prevents duplication when
4101+
# RAG_SYSTEM_CONTEXT is enabled and the template is appended
4102+
# to the system message on each iteration).
4103+
original_system_message = get_system_message(form_data["messages"])
4104+
original_system_content = (
4105+
get_content_from_message(original_system_message)
4106+
if original_system_message
4107+
else None
4108+
)
4109+
40934110
while (
40944111
len(tool_calls) > 0
40954112
and tool_call_retries < CHAT_RESPONSE_MAX_TOOL_CALL_RETRIES
@@ -4244,7 +4261,8 @@ async def flush_pending_delta_data(threshold: int = 0):
42444261

42454262
# Extract citation sources from tool results
42464263
if (
4247-
tool_function_name
4264+
citations_enabled
4265+
and tool_function_name
42484266
in [
42494267
"search_web",
42504268
"fetch_url",
@@ -4334,27 +4352,35 @@ async def flush_pending_delta_data(threshold: int = 0):
43344352
}
43354353
)
43364354

4337-
# Emit citation sources for UI display
4338-
for source in tool_call_sources:
4339-
await event_emitter({"type": "source", "data": source})
4340-
4341-
# Apply source context to messages for model
4342-
# Use metadata_only=True to avoid duplicating content
4343-
# that is already in the tool result message.
4344-
all_tool_call_sources.extend(tool_call_sources)
4345-
if all_tool_call_sources and user_message:
4346-
# Restore original user message before re-applying to avoid recursive nesting
4347-
set_last_user_message_content(
4348-
user_message, form_data["messages"]
4349-
)
4350-
form_data["messages"] = apply_source_context_to_messages(
4351-
request,
4352-
form_data["messages"],
4353-
all_tool_call_sources,
4354-
user_message,
4355-
include_content=False,
4356-
)
4357-
tool_call_sources.clear()
4355+
# Emit citation sources and apply source context to messages
4356+
if citations_enabled:
4357+
for source in tool_call_sources:
4358+
await event_emitter({"type": "source", "data": source})
4359+
4360+
# Apply source context to messages for model.
4361+
# Use include_content=False to avoid duplicating content
4362+
# that is already in the tool result message.
4363+
all_tool_call_sources.extend(tool_call_sources)
4364+
if all_tool_call_sources and user_message:
4365+
# Restore original messages before re-applying to
4366+
# avoid recursive nesting (user message) and
4367+
# duplication (system message with RAG_SYSTEM_CONTEXT).
4368+
set_last_user_message_content(
4369+
user_message, form_data["messages"]
4370+
)
4371+
if original_system_content is not None:
4372+
replace_system_message_content(
4373+
original_system_content,
4374+
form_data["messages"],
4375+
)
4376+
form_data["messages"] = apply_source_context_to_messages(
4377+
request,
4378+
form_data["messages"],
4379+
all_tool_call_sources,
4380+
user_message,
4381+
include_content=False,
4382+
)
4383+
tool_call_sources.clear()
43584384

43594385
await event_emitter(
43604386
{

0 commit comments

Comments
 (0)