Skip to content

Commit dca2f94

Browse files
Copilotowndev
andcommitted
Fix model system prompt location (__model__["info"]["system"]) and user prompt priority (chat controls first)
Co-authored-by: owndev <69784886+owndev@users.noreply.github.com>
1 parent f39e444 commit dca2f94

2 files changed

Lines changed: 31 additions & 31 deletions

File tree

docs/google-gemini-integration.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,13 @@ The Google Gemini pipeline supports a hierarchical system prompt configuration t
259259

260260
1. **Default System Prompt** (`GOOGLE_DEFAULT_SYSTEM_PROMPT`): Global default applied to all chats, configurable via environment variable or Admin UI valves.
261261

262-
2. **Model System Prompt**: The system prompt configured in the model settings (Admin > Models > Select Model > System Prompt). This is passed via `__metadata__["chat"]["params"]["system"]`.
262+
2. **Model System Prompt**: The system prompt configured in the model settings (Admin > Models > Select Model > System Prompt). This is accessed via `__model__["info"]["system"]`.
263263

264264
3. **User System Prompt**: The user's personalized system prompt from either:
265-
- **User Settings** (Settings > Personalization): Stored in `settings.ui.system`
266265
- **Chat Controls**: The system message passed with individual chat messages
266+
- **User Settings** (Settings > Personalization): Stored in `settings.ui.system`
267267

268-
Note: User settings take precedence over chat controls if both are set.
268+
Note: Chat controls take precedence over user settings if both are set.
269269

270270
### How It Works
271271

@@ -310,7 +310,7 @@ Users can set their personalized system prompt in Open WebUI:
310310
2. Enter your preferred system prompt in the "System Prompt" field
311311
3. Save settings
312312

313-
This prompt will be automatically applied to all your Gemini chats, combined with any default and model prompts.
313+
Alternatively, users can override the system prompt per-chat using chat controls. If both are set, the chat controls value takes precedence.
314314

315315
### Example
316316

@@ -326,7 +326,7 @@ You are a helpful AI assistant.
326326
You specialize in Python programming.
327327
```
328328

329-
**User system prompt (Settings > Personalization OR chat controls):**
329+
**User system prompt (chat controls OR Settings > Personalization):**
330330
```
331331
My name is John. I prefer detailed explanations.
332332
```

pipelines/google/google_gemini.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -321,31 +321,29 @@ def _get_user_personalization_prompt(
321321
return None
322322

323323
def _get_model_system_prompt(
324-
self, __metadata__: Optional[dict] = None
324+
self, __model__: Optional[dict] = None
325325
) -> Optional[str]:
326326
"""Get the model's system prompt from model settings.
327327
328328
In Open WebUI, each model can have its own system prompt configured
329-
in Admin > Models > Select Model > System Prompt. This is typically
330-
passed via __metadata__["chat"]["params"]["system"].
329+
in Admin > Models > Select Model > System Prompt. This is accessed
330+
via __model__["info"]["system"].
331331
332332
Args:
333-
__metadata__: The metadata dict passed to the pipe method
333+
__model__: The model dict passed to the pipe method
334334
335335
Returns:
336336
The model's system prompt or None if not set
337337
"""
338-
if __metadata__ is None:
338+
if __model__ is None:
339339
return None
340340

341341
try:
342-
chat = __metadata__.get("chat")
343-
if chat and isinstance(chat, dict):
344-
params = chat.get("params")
345-
if params and isinstance(params, dict):
346-
system_prompt = params.get("system")
347-
if system_prompt and isinstance(system_prompt, str):
348-
return system_prompt.strip() or None
342+
info = __model__.get("info")
343+
if info and isinstance(info, dict):
344+
system_prompt = info.get("system")
345+
if system_prompt and isinstance(system_prompt, str):
346+
return system_prompt.strip() or None
349347
except Exception as e:
350348
self.log.debug(f"Could not retrieve model system prompt: {e}")
351349

@@ -355,30 +353,30 @@ def _combine_system_prompts(
355353
self,
356354
chat_system_prompt: Optional[str],
357355
__user__: Optional[dict] = None,
358-
__metadata__: Optional[dict] = None,
356+
__model__: Optional[dict] = None,
359357
) -> Optional[str]:
360358
"""Combine default, model, and user system prompts.
361359
362360
Prompt hierarchy (all prompts are combined if set):
363361
1. DEFAULT_SYSTEM_PROMPT (environment/valve setting)
364-
2. Model system prompt (from model settings - __metadata__["chat"]["params"]["system"])
365-
3. User system prompt (from user settings OR chat controls/messages)
362+
2. Model system prompt (from model settings - __model__["info"]["system"])
363+
3. User system prompt (from chat controls OR user settings - chat controls take precedence)
366364
367365
Args:
368366
chat_system_prompt: The chat-level system prompt from messages (may be None)
369367
__user__: The user dict passed to the pipe method
370-
__metadata__: The metadata dict passed to the pipe method
368+
__model__: The model dict passed to the pipe method
371369
372370
Returns:
373371
Combined system prompt or None if none are set
374372
"""
375373
default_prompt = self.valves.DEFAULT_SYSTEM_PROMPT.strip() or None
376-
model_prompt = self._get_model_system_prompt(__metadata__)
374+
model_prompt = self._get_model_system_prompt(__model__)
377375
user_personalization = self._get_user_personalization_prompt(__user__)
378376
chat_prompt = chat_system_prompt.strip() if chat_system_prompt else None
379377

380-
# User prompt = user personalization OR chat prompt (user settings take precedence)
381-
user_prompt = user_personalization or chat_prompt
378+
# User prompt = chat controls OR user settings (chat controls take precedence if both are set)
379+
user_prompt = chat_prompt or user_personalization
382380

383381
prompts = [p for p in [default_prompt, model_prompt, user_prompt] if p]
384382

@@ -485,7 +483,7 @@ async def _build_image_generation_contents(
485483
messages: List[Dict[str, Any]],
486484
__event_emitter__: Callable,
487485
__user__: Optional[dict] = None,
488-
__metadata__: Optional[dict] = None,
486+
__model__: Optional[dict] = None,
489487
) -> Tuple[List[Dict[str, Any]], Optional[str]]:
490488
"""Construct the contents payload for image-capable models.
491489
@@ -499,7 +497,7 @@ async def _build_image_generation_contents(
499497

500498
# Combine with default system prompt if configured
501499
system_instruction = self._combine_system_prompts(
502-
user_system_instruction, __user__, __metadata__
500+
user_system_instruction, __user__, __model__
503501
)
504502

505503
last_user_msg = next(
@@ -962,15 +960,15 @@ def _prepare_content(
962960
self,
963961
messages: List[Dict[str, Any]],
964962
__user__: Optional[dict] = None,
965-
__metadata__: Optional[dict] = None,
963+
__model__: Optional[dict] = None,
966964
) -> Tuple[List[Dict[str, Any]], Optional[str]]:
967965
"""
968966
Prepare messages content for the API and extract system message if present.
969967
970968
Args:
971969
messages: List of message objects from the request
972970
__user__: The user dict passed to the pipe method
973-
__metadata__: The metadata dict passed to the pipe method
971+
__model__: The model dict passed to the pipe method
974972
975973
Returns:
976974
Tuple of (prepared content list, system message string or None)
@@ -983,7 +981,7 @@ def _prepare_content(
983981

984982
# Combine with default system prompt if configured
985983
system_message = self._combine_system_prompts(
986-
user_system_message, __user__, __metadata__
984+
user_system_message, __user__, __model__
987985
)
988986

989987
# Prepare contents for the API
@@ -2195,6 +2193,7 @@ async def pipe(
21952193
__tools__: dict[str, Any] | None,
21962194
__request__: Optional[Request] = None,
21972195
__user__: Optional[dict] = None,
2196+
__model__: Optional[dict] = None,
21982197
) -> Union[str, AsyncIterator[str]]:
21992198
"""
22002199
Main method for sending requests to the Google Gemini endpoint.
@@ -2206,6 +2205,7 @@ async def pipe(
22062205
__tools__: Available tools
22072206
__request__: FastAPI request object (for image upload)
22082207
__user__: User information (for image upload)
2208+
__model__: Model information (for model-specific system prompt)
22092209
22102210
Returns:
22112211
Response from Google Gemini API, which could be a string or an iterator for streaming.
@@ -2243,7 +2243,7 @@ async def pipe(
22432243
contents,
22442244
system_instruction,
22452245
) = await self._build_image_generation_contents(
2246-
messages, __event_emitter__, __user__, __metadata__
2246+
messages, __event_emitter__, __user__, __model__
22472247
)
22482248
# For image generation, system_instruction is integrated into the prompt
22492249
# so it will be None here (this is expected and correct)
@@ -2256,7 +2256,7 @@ async def pipe(
22562256
# For non-image generation models, use the full conversation history
22572257
# Prepare content and extract system message normally
22582258
contents, system_instruction = self._prepare_content(
2259-
messages, __user__, __metadata__
2259+
messages, __user__, __model__
22602260
)
22612261
if not contents:
22622262
return "Error: No valid message content found"

0 commit comments

Comments
 (0)