lucadipalma commited on
Commit ·
1881667
1
Parent(s): 0a26b83
hide boss reasoning to avoid injection
Browse files- support/build_graph.py +47 -13
support/build_graph.py
CHANGED
|
@@ -231,7 +231,7 @@ class MyGraph:
|
|
| 231 |
# entry.get("content", "")
|
| 232 |
# for entry in chat_history
|
| 233 |
# ])
|
| 234 |
-
formatted_history, current_round_messages = self._split_chat_history(chat_history)
|
| 235 |
self.current_team = state["current_team"]
|
| 236 |
|
| 237 |
new_message = [
|
|
@@ -488,7 +488,7 @@ class MyGraph:
|
|
| 488 |
# entry.get("content", "")
|
| 489 |
# for entry in chat_history
|
| 490 |
# ])
|
| 491 |
-
formatted_history, current_round_messages = self._split_chat_history(chat_history)
|
| 492 |
|
| 493 |
boss = next((p for p in state["players"] if p.team == "blue" and p.role == "boss"), None)
|
| 494 |
if not boss:
|
|
@@ -1212,7 +1212,30 @@ class MyGraph:
|
|
| 1212 |
elif hasattr(message, 'text') and message.text:
|
| 1213 |
content = message.text
|
| 1214 |
else:
|
| 1215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1216 |
|
| 1217 |
# Return structured entry
|
| 1218 |
if player is None:
|
|
@@ -1236,14 +1259,15 @@ class MyGraph:
|
|
| 1236 |
"info": info,
|
| 1237 |
}, clue, clue_number, guesses
|
| 1238 |
|
| 1239 |
-
def _split_chat_history(self, chat_history):
|
| 1240 |
"""
|
| 1241 |
Splits chat_history into two parts:
|
| 1242 |
- formatted_history: all entries up to and including the last one with info == "chat_history"
|
| 1243 |
- current_round: all entries after that
|
| 1244 |
-
|
| 1245 |
-
|
| 1246 |
-
|
|
|
|
| 1247 |
"""
|
| 1248 |
|
| 1249 |
last_index = None
|
|
@@ -1254,24 +1278,34 @@ class MyGraph:
|
|
| 1254 |
last_index = i
|
| 1255 |
|
| 1256 |
# Helper to format a single entry
|
| 1257 |
-
def format_entry(entry):
|
| 1258 |
name = entry.get("name", "Unknown")
|
| 1259 |
team = entry.get("team", "N/A")
|
| 1260 |
role = entry.get("role", "N/A")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1261 |
content = entry.get("content", "")
|
| 1262 |
return f"[{name} ({team} {role})]: {content}"
|
| 1263 |
|
| 1264 |
# Build formatted strings
|
| 1265 |
if last_index is None:
|
| 1266 |
formatted_history = ""
|
| 1267 |
-
|
| 1268 |
else:
|
|
|
|
| 1269 |
formatted_history = "\n".join(
|
| 1270 |
-
format_entry(e) for e in chat_history[:last_index + 1]
|
| 1271 |
-
)
|
| 1272 |
-
current_round = "\n".join(
|
| 1273 |
-
format_entry(e) for e in chat_history[last_index + 1:]
|
| 1274 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1275 |
|
| 1276 |
return formatted_history, current_round
|
| 1277 |
|
|
|
|
| 231 |
# entry.get("content", "")
|
| 232 |
# for entry in chat_history
|
| 233 |
# ])
|
| 234 |
+
formatted_history, current_round_messages = self._split_chat_history(chat_history, viewer_role="boss")
|
| 235 |
self.current_team = state["current_team"]
|
| 236 |
|
| 237 |
new_message = [
|
|
|
|
| 488 |
# entry.get("content", "")
|
| 489 |
# for entry in chat_history
|
| 490 |
# ])
|
| 491 |
+
formatted_history, current_round_messages = self._split_chat_history(chat_history, viewer_role="boss")
|
| 492 |
|
| 493 |
boss = next((p for p in state["players"] if p.team == "blue" and p.role == "boss"), None)
|
| 494 |
if not boss:
|
|
|
|
| 1212 |
elif hasattr(message, 'text') and message.text:
|
| 1213 |
content = message.text
|
| 1214 |
else:
|
| 1215 |
+
# if player.role == "boss":
|
| 1216 |
+
# content = str(message.content) # this is to avoid the injection of any reasoning of the boss inside the chat history, that can add information about the words colors on the board
|
| 1217 |
+
# else:
|
| 1218 |
+
model_provider = message.response_metadata.get('model_provider')
|
| 1219 |
+
if (model_provider in ["openai", "google_genai", "anthropic"] and isinstance(message.content, list)):
|
| 1220 |
+
for item in message.content:
|
| 1221 |
+
if not isinstance(item, dict):
|
| 1222 |
+
continue
|
| 1223 |
+
|
| 1224 |
+
item_type = item.get('type')
|
| 1225 |
+
# Handle openai reasoning content
|
| 1226 |
+
if item_type == 'reasoning':
|
| 1227 |
+
content = item.get('summary', [])
|
| 1228 |
+
|
| 1229 |
+
# Handle Google and Anthropic thinking content
|
| 1230 |
+
elif item_type == 'thinking':
|
| 1231 |
+
# reasoning_text = ""
|
| 1232 |
+
content = item.get('thinking', [])
|
| 1233 |
+
|
| 1234 |
+
# Handle text content (regular response after reasoning)
|
| 1235 |
+
elif item_type == 'text':
|
| 1236 |
+
content = item.get('text', '')
|
| 1237 |
+
elif message.additional_kwargs.get('reasoning_content'):
|
| 1238 |
+
content = message.additional_kwargs.get('reasoning_content')
|
| 1239 |
|
| 1240 |
# Return structured entry
|
| 1241 |
if player is None:
|
|
|
|
| 1259 |
"info": info,
|
| 1260 |
}, clue, clue_number, guesses
|
| 1261 |
|
| 1262 |
+
def _split_chat_history(self, chat_history, viewer_role=None):
|
| 1263 |
"""
|
| 1264 |
Splits chat_history into two parts:
|
| 1265 |
- formatted_history: all entries up to and including the last one with info == "chat_history"
|
| 1266 |
- current_round: all entries after that
|
| 1267 |
+
|
| 1268 |
+
If viewer_role is specified, filters content appropriately:
|
| 1269 |
+
- 'captain': hides boss reasoning from current round
|
| 1270 |
+
- 'boss' or None: shows everything
|
| 1271 |
"""
|
| 1272 |
|
| 1273 |
last_index = None
|
|
|
|
| 1278 |
last_index = i
|
| 1279 |
|
| 1280 |
# Helper to format a single entry
|
| 1281 |
+
def format_entry(entry, hide_boss_reasoning=False):
|
| 1282 |
name = entry.get("name", "Unknown")
|
| 1283 |
team = entry.get("team", "N/A")
|
| 1284 |
role = entry.get("role", "N/A")
|
| 1285 |
+
|
| 1286 |
+
# Hide boss reasoning only for captains viewing current round
|
| 1287 |
+
if hide_boss_reasoning and entry.get("role") == "boss" and entry.get("tool_name") is None:
|
| 1288 |
+
return f"[{name} ({team} {role})]: [boss reasoning is hidden]" # avoid injection of boss reasoning that may contains the association between words and colors
|
| 1289 |
+
|
| 1290 |
content = entry.get("content", "")
|
| 1291 |
return f"[{name} ({team} {role})]: {content}"
|
| 1292 |
|
| 1293 |
# Build formatted strings
|
| 1294 |
if last_index is None:
|
| 1295 |
formatted_history = ""
|
| 1296 |
+
current_round_entries = chat_history
|
| 1297 |
else:
|
| 1298 |
+
# History is always shown as-is (no filtering)
|
| 1299 |
formatted_history = "\n".join(
|
| 1300 |
+
format_entry(e, hide_boss_reasoning=False) for e in chat_history[:last_index + 1]
|
|
|
|
|
|
|
|
|
|
| 1301 |
)
|
| 1302 |
+
current_round_entries = chat_history[last_index + 1:]
|
| 1303 |
+
|
| 1304 |
+
# Current round: hide boss reasoning only if viewer is captain
|
| 1305 |
+
hide_reasoning = (viewer_role != "boss")
|
| 1306 |
+
current_round = "\n".join(
|
| 1307 |
+
format_entry(e, hide_boss_reasoning=hide_reasoning) for e in current_round_entries
|
| 1308 |
+
)
|
| 1309 |
|
| 1310 |
return formatted_history, current_round
|
| 1311 |
|