Tim Luka Horstmann
commited on
Commit
·
f18ae9b
1
Parent(s):
2f6b259
Updated for game
Browse files
app.py
CHANGED
|
@@ -250,30 +250,40 @@ async def stream_response(query, history, game_context=None, mode: Optional[str]
|
|
| 250 |
yield chunk
|
| 251 |
|
| 252 |
def _format_game_context_for_prompt(game_context: Optional[Union[str, Dict[str, Any]]]) -> str:
|
| 253 |
-
"""Return a concise text snippet to inject into the system prompt from game context.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
if not game_context:
|
| 255 |
return ""
|
| 256 |
try:
|
| 257 |
if isinstance(game_context, str):
|
| 258 |
return f"\nGAME CONTEXT: The player is currently at a station about {game_context}."
|
| 259 |
if isinstance(game_context, dict):
|
| 260 |
-
current = game_context.get('current_station') or game_context.get('station') or ''
|
| 261 |
-
current_id = game_context.get('current_station_id') or ''
|
| 262 |
visited = game_context.get('visited_stations') or []
|
| 263 |
-
context = game_context.get('context') or game_context.get('current_context') or ''
|
|
|
|
| 264 |
parts = ["\nGAME CONTEXT:"]
|
| 265 |
-
if current:
|
| 266 |
-
parts.append(f"Current station: {current}.")
|
| 267 |
if context:
|
| 268 |
parts.append(f"Station details: {context}.")
|
|
|
|
| 269 |
# Look up admin-provided extras
|
| 270 |
extra_text = ''
|
| 271 |
if current_id:
|
| 272 |
-
extra_text = STATION_EXTRAS_BY_ID.get(
|
| 273 |
if not extra_text and current:
|
| 274 |
-
extra_text = STATION_EXTRAS_BY_NAME.get(
|
| 275 |
if extra_text:
|
| 276 |
-
parts.append(f"Additional station notes: {extra_text}")
|
|
|
|
|
|
|
| 277 |
if visited:
|
| 278 |
try:
|
| 279 |
uniq = []
|
|
@@ -284,27 +294,41 @@ def _format_game_context_for_prompt(game_context: Optional[Union[str, Dict[str,
|
|
| 284 |
parts.append(f"Visited stations so far: {', '.join(uniq)}.")
|
| 285 |
except Exception:
|
| 286 |
pass
|
| 287 |
-
|
|
|
|
| 288 |
mem = game_context.get('__memory') if isinstance(game_context, dict) else None
|
| 289 |
if isinstance(mem, dict):
|
| 290 |
try:
|
| 291 |
transcript = mem.get('transcript') or []
|
| 292 |
-
if transcript:
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
if
|
| 307 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
except Exception:
|
| 309 |
pass
|
| 310 |
return " ".join(parts)
|
|
@@ -324,10 +348,11 @@ async def stream_response_gemini(query, history, game_context=None, mode: Option
|
|
| 324 |
if is_game_mode:
|
| 325 |
system_prompt = (
|
| 326 |
"You are Tim Luka Horstmann as a friendly in-game 'Station Guide'. "
|
| 327 |
-
"Stay in first person.
|
| 328 |
-
"Use the CV for personal facts, roles and dates. "
|
| 329 |
-
"
|
| 330 |
-
"
|
|
|
|
| 331 |
f"Today's date is {current_date}. CV: {full_cv_text}" + game_context_text
|
| 332 |
)
|
| 333 |
else:
|
|
@@ -411,10 +436,11 @@ async def stream_response_local(query, history, game_context=None, mode: Optiona
|
|
| 411 |
if is_game_mode:
|
| 412 |
system_prompt = (
|
| 413 |
"/no_think You are Tim Luka Horstmann as a friendly in-game 'Station Guide'. "
|
| 414 |
-
"Stay in first person.
|
| 415 |
-
"Use the CV for personal facts, roles and dates. "
|
| 416 |
-
"
|
| 417 |
-
"
|
|
|
|
| 418 |
f"Today's date is {current_date}. CV: {full_cv_text}" + game_context_text
|
| 419 |
)
|
| 420 |
else:
|
|
|
|
| 250 |
yield chunk
|
| 251 |
|
| 252 |
def _format_game_context_for_prompt(game_context: Optional[Union[str, Dict[str, Any]]]) -> str:
|
| 253 |
+
"""Return a concise text snippet to inject into the system prompt from game context.
|
| 254 |
+
|
| 255 |
+
Key changes:
|
| 256 |
+
- Prefer current station (id/name) and filter transcript strictly to this station to avoid confusion.
|
| 257 |
+
- Include admin-provided extras when available.
|
| 258 |
+
- Provide a short visited list for flavor only.
|
| 259 |
+
"""
|
| 260 |
if not game_context:
|
| 261 |
return ""
|
| 262 |
try:
|
| 263 |
if isinstance(game_context, str):
|
| 264 |
return f"\nGAME CONTEXT: The player is currently at a station about {game_context}."
|
| 265 |
if isinstance(game_context, dict):
|
| 266 |
+
current = (game_context.get('current_station') or game_context.get('station') or '').strip()
|
| 267 |
+
current_id = (game_context.get('current_station_id') or '').strip()
|
| 268 |
visited = game_context.get('visited_stations') or []
|
| 269 |
+
context = (game_context.get('context') or game_context.get('current_context') or '').strip()
|
| 270 |
+
|
| 271 |
parts = ["\nGAME CONTEXT:"]
|
| 272 |
+
if current or current_id:
|
| 273 |
+
parts.append(f"Current station: {current} (id: {current_id}).")
|
| 274 |
if context:
|
| 275 |
parts.append(f"Station details: {context}.")
|
| 276 |
+
|
| 277 |
# Look up admin-provided extras
|
| 278 |
extra_text = ''
|
| 279 |
if current_id:
|
| 280 |
+
extra_text = STATION_EXTRAS_BY_ID.get(current_id.lower(), '')
|
| 281 |
if not extra_text and current:
|
| 282 |
+
extra_text = STATION_EXTRAS_BY_NAME.get(current.lower(), '')
|
| 283 |
if extra_text:
|
| 284 |
+
parts.append(f"Additional station notes (must consider): {extra_text}")
|
| 285 |
+
|
| 286 |
+
# Visited list (names only, de-duplicated)
|
| 287 |
if visited:
|
| 288 |
try:
|
| 289 |
uniq = []
|
|
|
|
| 294 |
parts.append(f"Visited stations so far: {', '.join(uniq)}.")
|
| 295 |
except Exception:
|
| 296 |
pass
|
| 297 |
+
|
| 298 |
+
# Memory transcript: include only messages tied to this station to reduce confusion
|
| 299 |
mem = game_context.get('__memory') if isinstance(game_context, dict) else None
|
| 300 |
if isinstance(mem, dict):
|
| 301 |
try:
|
| 302 |
transcript = mem.get('transcript') or []
|
| 303 |
+
if transcript and (current or current_id):
|
| 304 |
+
def belongs_here(m: Dict[str, Any]) -> bool:
|
| 305 |
+
sid = (m.get('stationId') or '').strip()
|
| 306 |
+
sname = (m.get('stationName') or '').strip()
|
| 307 |
+
if current_id and sid:
|
| 308 |
+
if sid.strip().lower() == current_id.lower():
|
| 309 |
+
return True
|
| 310 |
+
if current and sname:
|
| 311 |
+
if sname.strip().lower() == current.lower():
|
| 312 |
+
return True
|
| 313 |
+
return False
|
| 314 |
+
|
| 315 |
+
filtered = [m for m in transcript if belongs_here(m)]
|
| 316 |
+
# if nothing matches, don't include cross-station chatter
|
| 317 |
+
if filtered:
|
| 318 |
+
lines = []
|
| 319 |
+
for m in filtered[-20:]:
|
| 320 |
+
role = (m.get('role') or '').strip()
|
| 321 |
+
src = (m.get('source') or '').strip()
|
| 322 |
+
sta = (m.get('stationName') or '').strip()
|
| 323 |
+
txt = (m.get('content') or '').replace('\n', ' ').strip()
|
| 324 |
+
if len(txt) > 2000:
|
| 325 |
+
txt = txt[:2000] + '…'
|
| 326 |
+
label = role if role else 'msg'
|
| 327 |
+
if src or sta:
|
| 328 |
+
label += f"[{src}{'/' + sta if sta else ''}]"
|
| 329 |
+
lines.append(f"- {label}: {txt}")
|
| 330 |
+
if lines:
|
| 331 |
+
parts.append("Recent exchanges at this station:\n" + "\n".join(lines))
|
| 332 |
except Exception:
|
| 333 |
pass
|
| 334 |
return " ".join(parts)
|
|
|
|
| 348 |
if is_game_mode:
|
| 349 |
system_prompt = (
|
| 350 |
"You are Tim Luka Horstmann as a friendly in-game 'Station Guide'. "
|
| 351 |
+
"Stay in first person. The current station is the primary focus—treat it as 'now' even when revisiting. "
|
| 352 |
+
"Use the CV for personal facts, roles and dates; include 'Additional station notes' if present and relevant. "
|
| 353 |
+
"Keep answers concise (2–4 sentences). If off-topic, answer briefly, then gently steer back to the current station. "
|
| 354 |
+
"When revisiting a station, optionally acknowledge the revisit in one short clause. "
|
| 355 |
+
"End with a tiny hint or nudge (optional) about what to explore here or next. "
|
| 356 |
f"Today's date is {current_date}. CV: {full_cv_text}" + game_context_text
|
| 357 |
)
|
| 358 |
else:
|
|
|
|
| 436 |
if is_game_mode:
|
| 437 |
system_prompt = (
|
| 438 |
"/no_think You are Tim Luka Horstmann as a friendly in-game 'Station Guide'. "
|
| 439 |
+
"Stay in first person. The current station is the primary focus—treat it as 'now' even when revisiting. "
|
| 440 |
+
"Use the CV for personal facts, roles and dates; include 'Additional station notes' if present (do not ignore them). "
|
| 441 |
+
"Keep answers concise (2–4 sentences). If off-topic, answer briefly, then gently steer back to the current station. "
|
| 442 |
+
"When revisiting a station, optionally acknowledge the revisit in one short clause. "
|
| 443 |
+
"End with a tiny hint or nudge (optional) about what to explore here or next. "
|
| 444 |
f"Today's date is {current_date}. CV: {full_cv_text}" + game_context_text
|
| 445 |
)
|
| 446 |
else:
|