Spaces:
Sleeping
Sleeping
Deploy Myco from CI
Browse files- game/engine.py +20 -11
game/engine.py
CHANGED
|
@@ -97,24 +97,32 @@ POISONOUS_MUSHROOM_NAMES = {
|
|
| 97 |
def _format_messages_for_template(system_prompt: str, user_prompt: str, history_turns: list = None) -> list:
|
| 98 |
"""
|
| 99 |
Formats messages securely for models with strict chat templates (like Gemma).
|
| 100 |
-
|
| 101 |
-
- Maps 'assistant' role tags directly to 'model'.
|
| 102 |
-
- Implements alternative processing to merge consecutive same-role text blocks.
|
| 103 |
"""
|
| 104 |
# Gemma doesn't support the 'system' role, so we inject it as the first 'user' prompt
|
| 105 |
raw_turns = [{"role": "user", "content": system_prompt}]
|
| 106 |
|
| 107 |
if history_turns:
|
| 108 |
for entry in history_turns:
|
| 109 |
-
|
| 110 |
-
if
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
# Add the current payload action
|
| 120 |
raw_turns.append({"role": "user", "content": user_prompt})
|
|
@@ -128,6 +136,7 @@ def _format_messages_for_template(system_prompt: str, user_prompt: str, history_
|
|
| 128 |
alternated_turns.append(turn)
|
| 129 |
|
| 130 |
return alternated_turns
|
|
|
|
| 131 |
|
| 132 |
# ---------------------------------------------------------------------------
|
| 133 |
# Game Constants
|
|
|
|
| 97 |
def _format_messages_for_template(system_prompt: str, user_prompt: str, history_turns: list = None) -> list:
|
| 98 |
"""
|
| 99 |
Formats messages securely for models with strict chat templates (like Gemma).
|
| 100 |
+
Handles both Gradio's default tuple history and OpenAI-style dict history.
|
|
|
|
|
|
|
| 101 |
"""
|
| 102 |
# Gemma doesn't support the 'system' role, so we inject it as the first 'user' prompt
|
| 103 |
raw_turns = [{"role": "user", "content": system_prompt}]
|
| 104 |
|
| 105 |
if history_turns:
|
| 106 |
for entry in history_turns:
|
| 107 |
+
# Handle Gradio style history: [[user_msg, bot_msg], ...]
|
| 108 |
+
if isinstance(entry, (list, tuple)) and len(entry) == 2:
|
| 109 |
+
user_msg, bot_msg = entry
|
| 110 |
+
if user_msg:
|
| 111 |
+
raw_turns.append({"role": "user", "content": str(user_msg)})
|
| 112 |
+
if bot_msg:
|
| 113 |
+
raw_turns.append({"role": "model", "content": str(bot_msg)})
|
| 114 |
|
| 115 |
+
# Handle standard dictionary style history (just in case)
|
| 116 |
+
elif isinstance(entry, dict):
|
| 117 |
+
role = entry.get("role", "assistant")
|
| 118 |
+
if role == "assistant":
|
| 119 |
+
role = "model"
|
| 120 |
+
elif role == "system":
|
| 121 |
+
continue # Strip extraneous system tags
|
| 122 |
+
|
| 123 |
+
content = entry.get("content", "")
|
| 124 |
+
if isinstance(content, str) and content.strip():
|
| 125 |
+
raw_turns.append({"role": role, "content": content})
|
| 126 |
|
| 127 |
# Add the current payload action
|
| 128 |
raw_turns.append({"role": "user", "content": user_prompt})
|
|
|
|
| 136 |
alternated_turns.append(turn)
|
| 137 |
|
| 138 |
return alternated_turns
|
| 139 |
+
|
| 140 |
|
| 141 |
# ---------------------------------------------------------------------------
|
| 142 |
# Game Constants
|