From 86f33de9f36a33fdc97370b1bc661300d41ca3b9 Mon Sep 17 00:00:00 2001 From: Athanasios Oikonomou Date: Thu, 18 Sep 2025 10:14:43 +0300 Subject: [PATCH] feat: support per-model RAG template override Add `rag_template` as a configurable parameter in model settings, allowing each model to define its own RAG template instead of always using the global default. - Middleware now selects the model-specific RAG template if provided. - Model editor UI updated to allow editing and saving `rag_template`. - Fallback to global `RAG_TEMPLATE` remains when no override is set. --- backend/open_webui/utils/middleware.py | 7 +++++- .../workspace/Models/ModelEditor.svelte | 23 ++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index b300bfa8d3..8bd401866d 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -1170,10 +1170,15 @@ async def process_chat_payload(request, form_data, user, metadata, model): if prompt is None: raise Exception("No user message found") + model_rag_template = ( + model.get("info", {}).get("params", {}).get("rag_template", "") + or request.app.state.config.RAG_TEMPLATE + ) + if context_string != "": form_data["messages"] = add_or_update_user_message( rag_template( - request.app.state.config.RAG_TEMPLATE, + model_rag_template, context_string, prompt, ), diff --git a/src/lib/components/workspace/Models/ModelEditor.svelte b/src/lib/components/workspace/Models/ModelEditor.svelte index c846414d4b..1774cee808 100644 --- a/src/lib/components/workspace/Models/ModelEditor.svelte +++ b/src/lib/components/workspace/Models/ModelEditor.svelte @@ -63,6 +63,7 @@ } let system = ''; + let rag_template = ''; let info = { id: '', base_model_id: null, @@ -74,12 +75,14 @@ tags: [] }, params: { - system: '' + system: '', + rag_template: '' } }; - let params = { - system: '' + let params: { + system: ''; + rag_template: ''; }; let knowledge = []; @@ -203,6 +206,7 @@ } info.params.system = system.trim() === '' ? null : system; + info.params.rag_template = rag_template.trim() === '' ? null : rag_template; info.params.stop = params.stop ? params.stop.split(',').filter((s) => s.trim()) : null; Object.keys(info.params).forEach((key) => { if (info.params[key] === '' || info.params[key] === null) { @@ -250,6 +254,7 @@ } system = model?.params?.system ?? ''; + rag_template = model?.params?.rag_template ?? ''; params = { ...params, ...model?.params }; params.stop = params?.stop @@ -618,6 +623,18 @@ +
+
{$i18n.t('RAG Template')}
+
+