Skip to content

Commit 8a77536

Browse files
Copilotowndev
andcommitted
Add DEFAULT_SYSTEM_PROMPT configuration for Google Gemini pipeline
Co-authored-by: owndev <69784886+owndev@users.noreply.github.com>
1 parent 65b4767 commit 8a77536

2 files changed

Lines changed: 96 additions & 5 deletions

File tree

docs/google-gemini-integration.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ GOOGLE_MODEL_CACHE_TTL=600
8080
# Default: 2
8181
GOOGLE_RETRY_COUNT=2
8282

83+
# Default system prompt applied to all chats
84+
# If a user-defined system prompt exists, this is prepended to it
85+
# Leave empty to disable
86+
# Default: "" (empty, disabled)
87+
GOOGLE_DEFAULT_SYSTEM_PROMPT=""
88+
8389
# Image processing optimization settings
8490
# Maximum image size in MB before compression is applied
8591
# Default: 15.0
@@ -245,6 +251,47 @@ To use this filter, ensure it's enabled in your Open WebUI configuration. Then,
245251

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

254+
## Default System Prompt
255+
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.
257+
258+
### How It Works
259+
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.
263+
264+
### Configuration
265+
266+
Set via environment variable:
267+
268+
```bash
269+
# Default system prompt applied to all chats
270+
# If a user-defined system prompt exists, this is prepended to it
271+
GOOGLE_DEFAULT_SYSTEM_PROMPT="You are a helpful AI assistant. Always be concise and accurate."
272+
```
273+
274+
Or configure through the pipeline valves in Open WebUI's Admin panel.
275+
276+
### Example
277+
278+
If your default system prompt is:
279+
```
280+
You are a helpful AI assistant.
281+
```
282+
283+
And your model-specific system prompt is:
284+
```
285+
Always respond in formal English.
286+
```
287+
288+
The combined system prompt sent to Gemini will be:
289+
```
290+
You are a helpful AI assistant.
291+
292+
Always respond in formal English.
293+
```
294+
248295
## Thinking Configuration
249296

250297
The Google Gemini pipeline supports advanced thinking configuration to control how much reasoning and computation is applied by the model.

pipelines/google/google_gemini.py

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ class Valves(BaseModel):
206206
default=int(os.getenv("GOOGLE_RETRY_COUNT", "2")),
207207
description="Number of times to retry API calls on temporary failures",
208208
)
209+
DEFAULT_SYSTEM_PROMPT: str = Field(
210+
default=os.getenv("GOOGLE_DEFAULT_SYSTEM_PROMPT", ""),
211+
description="Default system prompt applied to all chats. If a user-defined system prompt exists, "
212+
"this is prepended to it. Leave empty to disable.",
213+
)
209214

210215
# Image Processing Configuration
211216
IMAGE_MAX_SIZE_MB: float = Field(
@@ -284,6 +289,39 @@ def _deduplicate_images(self, images: List[Dict[str, Any]]) -> List[Dict[str, An
284289
result.append(part)
285290
return result
286291

292+
def _combine_system_prompts(
293+
self, user_system_prompt: Optional[str]
294+
) -> Optional[str]:
295+
"""Combine default system prompt with user-defined system prompt.
296+
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.
301+
302+
Args:
303+
user_system_prompt: The user-defined system prompt from messages (may be None)
304+
305+
Returns:
306+
Combined system prompt or None if neither is set
307+
"""
308+
default_prompt = self.valves.DEFAULT_SYSTEM_PROMPT.strip()
309+
user_prompt = user_system_prompt.strip() if user_system_prompt else ""
310+
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
324+
287325
def _apply_order_and_limit(
288326
self,
289327
history: List[Dict[str, Any]],
@@ -374,12 +412,15 @@ async def _build_image_generation_contents(
374412
375413
Returns tuple (contents, system_instruction) where system_instruction is extracted from system messages.
376414
"""
377-
# Extract system instruction first
378-
system_instruction = next(
415+
# Extract user-defined system instruction first
416+
user_system_instruction = next(
379417
(msg["content"] for msg in messages if msg.get("role") == "system"),
380418
None,
381419
)
382420

421+
# Combine with default system prompt if configured
422+
system_instruction = self._combine_system_prompts(user_system_instruction)
423+
383424
last_user_msg = next(
384425
(m for m in reversed(messages) if m.get("role") == "user"), None
385426
)
@@ -848,12 +889,15 @@ def _prepare_content(
848889
Returns:
849890
Tuple of (prepared content list, system message string or None)
850891
"""
851-
# Extract system message
852-
system_message = next(
892+
# Extract user-defined system message
893+
user_system_message = next(
853894
(msg["content"] for msg in messages if msg.get("role") == "system"),
854895
None,
855896
)
856897

898+
# Combine with default system prompt if configured
899+
system_message = self._combine_system_prompts(user_system_message)
900+
857901
# Prepare contents for the API
858902
contents = []
859903
for message in messages:
@@ -2074,7 +2118,7 @@ async def pipe(
20742118
# For image generation, system_instruction is integrated into the prompt
20752119
# so it will be None here (this is expected and correct)
20762120
self.log.debug(
2077-
f"Image generation mode: system instruction integrated into prompt"
2121+
"Image generation mode: system instruction integrated into prompt"
20782122
)
20792123
except ValueError as ve:
20802124
return f"Error: {ve}"

0 commit comments

Comments
 (0)