fix: inject full context knowledge into system message for KV prefix caching (#20317)

* Update middleware.py

* Update middleware.py

* env var

* address

* upd
This commit is contained in:
Classic298 authored and GitHub committed 2026-01-05 23:58:53 +04:00
1 parent cd5a38a694
commit 713a65ee31
2 files changed
+18

No files matched your search

+4
View File
@@ -356,6 +356,10 @@ ENABLE_REALTIME_CHAT_SAVE = (
ENABLE_QUERIES_CACHE = os.environ.get("ENABLE_QUERIES_CACHE", "False").lower() == "true" ENABLE_QUERIES_CACHE = os.environ.get("ENABLE_QUERIES_CACHE", "False").lower() == "true"
RAG_SYSTEM_CONTEXT = (
os.environ.get("RAG_SYSTEM_CONTEXT", "False").lower() == "true"
)
#################################### ####################################
# REDIS # REDIS
#################################### ####################################
+14
View File
@@ -119,6 +119,7 @@ from open_webui.env import (
BYPASS_MODEL_ACCESS_CONTROL, BYPASS_MODEL_ACCESS_CONTROL,
ENABLE_REALTIME_CHAT_SAVE, ENABLE_REALTIME_CHAT_SAVE,
ENABLE_QUERIES_CACHE, ENABLE_QUERIES_CACHE,
RAG_SYSTEM_CONTEXT,
) )
from open_webui.constants import TASKS from open_webui.constants import TASKS
@@ -1622,6 +1623,19 @@ async def process_chat_payload(request, form_data, user, metadata, model):
raise Exception("No user message found") raise Exception("No user message found")
if context_string != "": if context_string != "":
if RAG_SYSTEM_CONTEXT:
# Inject into system message for KV prefix caching
form_data["messages"] = add_or_update_system_message(
rag_template(
request.app.state.config.RAG_TEMPLATE,
context_string,
prompt,
),
form_data["messages"],
append=True,
)
else:
# Inject into user message
form_data["messages"] = add_or_update_user_message( form_data["messages"] = add_or_update_user_message(
rag_template( rag_template(
request.app.state.config.RAG_TEMPLATE, request.app.state.config.RAG_TEMPLATE,