From c9ee89467e94ecd885201fc24a65cb386efddd75 Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Fri, 30 Oct 2026 23:47:17 +0100 Subject: [PATCH] feat: {{USER_ROLE}} prompt variable for the signed-in user's role (#33441) Prompts could use the user's name and email but not their role, so role-aware system prompts needed a custom Filter function. Model system prompts, the user's own system prompt, saved prompts used with a slash command and chat requests through the API now replace {{USER_ROLE}} with the role, such as admin or user. Requested in #4765. --- backend/open_webui/utils/task.py | 2 ++ src/lib/components/channel/MessageInput.svelte | 8 ++++++++ src/lib/components/chat/MessageInput.svelte | 8 ++++++++ 3 files changed, 18 insertions(+) diff --git a/backend/open_webui/utils/task.py b/backend/open_webui/utils/task.py index 66f596c5c1..66f4fc32c0 100644 --- a/backend/open_webui/utils/task.py +++ b/backend/open_webui/utils/task.py @@ -73,6 +73,7 @@ async def prompt_template(template: str, user: Optional[Any] = None) -> str: USER_VARIABLES = { 'name': str(user.get('name')), 'email': str(user.get('email')), + 'role': str(user.get('role')), 'location': str(user_info.get('location')), 'bio': str(user.get('bio')), 'gender': str(user.get('gender')), @@ -96,6 +97,7 @@ async def prompt_template(template: str, user: Optional[Any] = None) -> str: template = template.replace('{{USER_NAME}}', USER_VARIABLES.get('name', 'Unknown')) template = template.replace('{{USER_EMAIL}}', USER_VARIABLES.get('email', 'Unknown')) + template = template.replace('{{USER_ROLE}}', USER_VARIABLES.get('role', 'Unknown')) template = template.replace('{{USER_BIO}}', USER_VARIABLES.get('bio', 'Unknown')) template = template.replace('{{USER_GENDER}}', USER_VARIABLES.get('gender', 'Unknown')) template = template.replace('{{USER_BIRTH_DATE}}', USER_VARIABLES.get('birth_date', 'Unknown')) diff --git a/src/lib/components/channel/MessageInput.svelte b/src/lib/components/channel/MessageInput.svelte index c901e556a5..7c03f71047 100644 --- a/src/lib/components/channel/MessageInput.svelte +++ b/src/lib/components/channel/MessageInput.svelte @@ -182,6 +182,14 @@ } } + if (text.includes('{{USER_ROLE}}')) { + const role = sessionUser?.role || ''; + + if (role) { + text = text.replaceAll('{{USER_ROLE}}', role); + } + } + if (text.includes('{{USER_BIO}}')) { const bio = sessionUser?.bio || ''; diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index bafec0644a..eaac2b59e0 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -352,6 +352,14 @@ } } + if (text.includes('{{USER_ROLE}}')) { + const role = sessionUser?.role || ''; + + if (role) { + text = text.replaceAll('{{USER_ROLE}}', role); + } + } + if (text.includes('{{USER_BIO}}')) { const bio = sessionUser?.bio || '';