diff --git a/extensions/openai/completions.py b/extensions/openai/completions.py index 10cdbf42..37e9568a 100644 --- a/extensions/openai/completions.py +++ b/extensions/openai/completions.py @@ -239,12 +239,14 @@ def convert_history(history): user_input = "" user_input_last = True system_message = "" + seen_non_system = False for entry in history: content = entry["content"] role = entry["role"] if role == "user": + seen_non_system = True # Extract text content (images handled by model-specific code) content = process_multimodal_content(content) user_input = content @@ -256,6 +258,7 @@ def convert_history(history): current_message = content elif role == "assistant": + seen_non_system = True meta = {} tool_calls = entry.get("tool_calls") if tool_calls and isinstance(tool_calls, list) and len(tool_calls) > 0: @@ -272,13 +275,22 @@ def convert_history(history): else: chat_dialogue.append(['', current_reply, '', meta]) elif role == "tool": + seen_non_system = True user_input_last = False meta = {} if "tool_call_id" in entry: meta["tool_call_id"] = entry["tool_call_id"] chat_dialogue.append(['', '', content, meta]) elif role in ("system", "developer"): - system_message += f"\n{content}" if system_message else content + if not seen_non_system: + # Leading system messages go to custom_system_message (placed at top) + system_message += f"\n{content}" if system_message else content + else: + # Mid-conversation system messages: preserve position in history + if current_message: + chat_dialogue.append([current_message, '', '', {}]) + current_message = "" + chat_dialogue.append([content, '', '', {"role": "system"}]) if not user_input_last: user_input = "" diff --git a/modules/chat.py b/modules/chat.py index 5cf550d6..8af92273 100644 --- a/modules/chat.py +++ b/modules/chat.py @@ -315,7 +315,10 @@ def generate_chat_prompt(user_input, state, **kwargs): "tool_call_id": item.get('tool_call_id', '') }) - if user_msg not in ['', '<|BEGIN-VISIBLE-CHAT|>']: + if entry_meta.get('role') == 'system': + if user_msg: + messages.insert(insert_pos, {"role": "system", "content": user_msg}) + elif user_msg not in ['', '<|BEGIN-VISIBLE-CHAT|>']: # Check for user message attachments in metadata user_key = f"user_{row_idx}" enhanced_user_msg = user_msg