Skip to content

Commit 54ce441

Browse files
Copilotowndev
andcommitted
Add per-user system prompt support from Personalization settings
Co-authored-by: owndev <69784886+owndev@users.noreply.github.com>
1 parent 71f774d commit 54ce441

2 files changed

Lines changed: 96 additions & 32 deletions

File tree

docs/google-gemini-integration.md

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,36 +251,68 @@ To use this filter, ensure it's enabled in your Open WebUI configuration. Then,
251251

252252
Native tool calling is enabled/disabled via the standard 'Function calling' Open Web UI toggle.
253253

254-
## Default System Prompt
254+
## System Prompt Hierarchy
255255

256-
The Google Gemini pipeline supports a configurable default system prompt that is applied to all chats. This is useful when you want to consistently apply certain behaviors or instructions to all Gemini models without having to configure each model individually.
256+
The Google Gemini pipeline supports a hierarchical system prompt configuration that combines multiple sources. This allows for flexible customization at different levels: global defaults, per-user personalization, and per-chat customization.
257+
258+
### Prompt Sources (in order of combination)
259+
260+
1. **Default System Prompt** (`GOOGLE_DEFAULT_SYSTEM_PROMPT`): Global default applied to all chats, configurable via environment variable or Admin UI valves.
261+
262+
2. **Per-User System Prompt** (User Personalization): Each user can set their own system prompt in Open WebUI's Settings > Personalization. This is stored in `user.info.system` and is automatically included.
263+
264+
3. **Chat-Level System Prompt**: The system message defined in the model settings or passed with individual chat messages.
257265

258266
### How It Works
259267

260-
- **Default Only**: If only `GOOGLE_DEFAULT_SYSTEM_PROMPT` is set and no user-defined system prompt exists, the default prompt is used as the system instruction.
261-
- **User Only**: If only a user-defined system prompt exists (from model settings), it is used as-is.
262-
- **Both**: If both are set, the default system prompt is **prepended** to the user-defined prompt, separated by a blank line. This allows you to have base instructions that apply to all chats while still allowing model-specific customization.
268+
All available prompts are combined in order, separated by blank lines:
269+
270+
```
271+
{Default System Prompt}
272+
273+
{Per-User Personalization Prompt}
274+
275+
{Chat-Level System Prompt}
276+
```
277+
278+
If only one prompt source is set, it is used as-is without any additional formatting.
263279

264280
### Configuration
265281

266-
Set via environment variable:
282+
**Environment Variable:**
267283

268284
```bash
269285
# Default system prompt applied to all chats
270-
# If a user-defined system prompt exists, this is prepended to it
286+
# Combined with per-user and chat-level prompts if they exist
271287
GOOGLE_DEFAULT_SYSTEM_PROMPT="You are a helpful AI assistant. Always be concise and accurate."
272288
```
273289

274290
Or configure through the pipeline valves in Open WebUI's Admin panel.
275291

292+
**Per-User Personalization:**
293+
294+
Users can set their personalized system prompt in Open WebUI:
295+
1. Go to Settings > Personalization
296+
2. Enter your preferred system prompt in the "System Prompt" field
297+
3. Save settings
298+
299+
This prompt will be automatically applied to all your Gemini chats, combined with any default and chat-level prompts.
300+
276301
### Example
277302

278-
If your default system prompt is:
303+
If your configuration is:
304+
305+
**Default system prompt (`GOOGLE_DEFAULT_SYSTEM_PROMPT`):**
279306
```
280307
You are a helpful AI assistant.
281308
```
282309

283-
And your model-specific system prompt is:
310+
**Per-user personalization prompt (Settings > Personalization):**
311+
```
312+
My name is John. I prefer detailed explanations.
313+
```
314+
315+
**Chat-level system prompt (model settings):**
284316
```
285317
Always respond in formal English.
286318
```
@@ -289,6 +321,8 @@ The combined system prompt sent to Gemini will be:
289321
```
290322
You are a helpful AI assistant.
291323
324+
My name is John. I prefer detailed explanations.
325+
292326
Always respond in formal English.
293327
```
294328

pipelines/google/google_gemini.py

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
author_url: https://github.com/owndev/
55
project_url: https://github.com/owndev/Open-WebUI-Functions
66
funding_url: https://github.com/sponsors/owndev
7-
version: 1.8.1
7+
version: 1.8.2
88
required_open_webui_version: 0.6.26
99
license: Apache License 2.0
1010
description: Highly optimized Google Gemini pipeline with advanced image generation capabilities, intelligent compression, and streamlined processing workflows.
@@ -34,6 +34,7 @@
3434
- Flexible upload fallback options and optimization controls
3535
- Configurable thinking levels (low/high) for Gemini 3 models
3636
- Configurable thinking budgets (0-32768 tokens) for Gemini 2.5 models
37+
- Hierarchical system prompts (default, per-user personalization, per-chat)
3738
"""
3839

3940
import os
@@ -289,38 +290,67 @@ def _deduplicate_images(self, images: List[Dict[str, Any]]) -> List[Dict[str, An
289290
result.append(part)
290291
return result
291292

293+
def _get_user_personalization_prompt(self) -> Optional[str]:
294+
"""Get the per-user system prompt from user settings (Personalization).
295+
296+
In Open WebUI, users can configure a personalized system prompt
297+
in Settings > Personalization. This is stored in user.info.system.
298+
299+
Returns:
300+
The user's personalized system prompt or None if not set
301+
"""
302+
if not hasattr(self, "user") or self.user is None:
303+
return None
304+
305+
try:
306+
user_info = self.user.info
307+
if user_info and isinstance(user_info, dict):
308+
system_prompt = user_info.get("system")
309+
if system_prompt and isinstance(system_prompt, str):
310+
return system_prompt.strip() or None
311+
except Exception as e:
312+
self.log.debug(f"Could not retrieve user personalization prompt: {e}")
313+
314+
return None
315+
292316
def _combine_system_prompts(
293-
self, user_system_prompt: Optional[str]
317+
self, chat_system_prompt: Optional[str]
294318
) -> Optional[str]:
295-
"""Combine default system prompt with user-defined system prompt.
319+
"""Combine default, per-user, and chat-level system prompts.
296320
297-
If DEFAULT_SYSTEM_PROMPT is set and user_system_prompt exists,
298-
the default is prepended to the user's prompt.
299-
If only DEFAULT_SYSTEM_PROMPT is set, it is used as the system prompt.
300-
If only user_system_prompt is set, it is used as-is.
321+
Prompt hierarchy (all prompts are combined if set):
322+
1. DEFAULT_SYSTEM_PROMPT (environment/valve setting)
323+
2. Per-user system prompt (from user.info.system - Personalization settings)
324+
3. Chat-level system prompt (from messages in this request)
301325
302326
Args:
303-
user_system_prompt: The user-defined system prompt from messages (may be None)
327+
chat_system_prompt: The chat-level system prompt from messages (may be None)
304328
305329
Returns:
306-
Combined system prompt or None if neither is set
330+
Combined system prompt or None if none are set
307331
"""
308332
default_prompt = self.valves.DEFAULT_SYSTEM_PROMPT.strip()
309-
user_prompt = user_system_prompt.strip() if user_system_prompt else ""
333+
user_personalization = self._get_user_personalization_prompt() or ""
334+
chat_prompt = chat_system_prompt.strip() if chat_system_prompt else ""
310335

311-
if default_prompt and user_prompt:
312-
combined = f"{default_prompt}\n\n{user_prompt}"
313-
self.log.debug(
314-
f"Combined system prompts: default ({len(default_prompt)} chars) + "
315-
f"user ({len(user_prompt)} chars) = {len(combined)} chars"
316-
)
317-
return combined
318-
elif default_prompt:
319-
self.log.debug(f"Using default system prompt ({len(default_prompt)} chars)")
320-
return default_prompt
321-
elif user_prompt:
322-
return user_prompt
323-
return None
336+
prompts = [p for p in [default_prompt, user_personalization, chat_prompt] if p]
337+
338+
if not prompts:
339+
return None
340+
341+
if len(prompts) == 1:
342+
self.log.debug(f"Using single system prompt ({len(prompts[0])} chars)")
343+
return prompts[0]
344+
345+
combined = "\n\n".join(prompts)
346+
self.log.debug(
347+
f"Combined system prompts: "
348+
f"default={len(default_prompt) if default_prompt else 0}, "
349+
f"user_personalization={len(user_personalization) if user_personalization else 0}, "
350+
f"chat={len(chat_prompt) if chat_prompt else 0} -> "
351+
f"total={len(combined)} chars"
352+
)
353+
return combined
324354

325355
def _apply_order_and_limit(
326356
self,

0 commit comments

Comments
 (0)