Spaces:
Sleeping
Sleeping
adityaverma977 commited on
Commit ·
221d021
1
Parent(s): 967c2f6
Add contextual chat message generation when HF models don't return messages
Browse files- app/groq_client.py +58 -2
- backend/app/groq_client.py +62 -2
app/groq_client.py
CHANGED
|
@@ -23,6 +23,55 @@ def is_ready():
|
|
| 23 |
return _HF_API_TOKEN is not None
|
| 24 |
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def _build_fire_state_summary(agent, fire, all_agents) -> str:
|
| 28 |
"""Build a state summary for the fire scenario."""
|
|
@@ -140,10 +189,11 @@ What do you do?"""
|
|
| 140 |
else:
|
| 141 |
text = data.get("generated_text", "")
|
| 142 |
text = text[len(system_prompt):].strip() if text.startswith(system_prompt) else text
|
|
|
|
| 143 |
try:
|
| 144 |
js = text[text.find('{'):text.rfind('}')+1]
|
| 145 |
decision = json.loads(js)
|
| 146 |
-
print(f"[HF_INFERENCE] {agent.model_name}: decision parsed: action={decision.get('action')}")
|
| 147 |
except Exception as je:
|
| 148 |
print(f"[HF_INFERENCE] {agent.model_name}: JSON parse error: {je}")
|
| 149 |
decision = {}
|
|
@@ -152,6 +202,12 @@ What do you do?"""
|
|
| 152 |
if action not in ["search_water", "collect_water", "extinguish_fire", "escape", "vote_for_leader"]:
|
| 153 |
action = "escape"
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
if dist_to_water is not None and dist_to_water <= 60 and not agent.water_collected:
|
| 156 |
action = "collect_water"
|
| 157 |
elif agent.water_collected and dist_to_fire <= 350:
|
|
@@ -160,7 +216,7 @@ What do you do?"""
|
|
| 160 |
return {
|
| 161 |
"action": action,
|
| 162 |
"vote_for": decision.get("vote_for"),
|
| 163 |
-
"message":
|
| 164 |
"reasoning": decision.get("reasoning", "Survival and teamwork.")
|
| 165 |
}
|
| 166 |
except Exception as e:
|
|
|
|
| 23 |
return _HF_API_TOKEN is not None
|
| 24 |
|
| 25 |
|
| 26 |
+
def _generate_chat_message(action: str, agent_name: str, fire_distance: float, has_water: bool) -> str:
|
| 27 |
+
"""Generate a contextual chat message based on action and state."""
|
| 28 |
+
action_messages = {
|
| 29 |
+
"search_water": [
|
| 30 |
+
f"{agent_name} is hunting for water...",
|
| 31 |
+
f"Gotta find a well! Where's the water?",
|
| 32 |
+
"Water sources incoming, scanning...",
|
| 33 |
+
f"{agent_name}: On the hunt for supplies!",
|
| 34 |
+
"Locating nearest well...",
|
| 35 |
+
"Water mission initiated!",
|
| 36 |
+
],
|
| 37 |
+
"collect_water": [
|
| 38 |
+
f"{agent_name} found water! Filling up...",
|
| 39 |
+
"Water! Finally got some reserves!",
|
| 40 |
+
f"{agent_name}: Collecting precious water.",
|
| 41 |
+
"Jackpot! Water collected.",
|
| 42 |
+
"Tank is full, let's go!",
|
| 43 |
+
f"{agent_name} loading water supply...",
|
| 44 |
+
],
|
| 45 |
+
"extinguish_fire": [
|
| 46 |
+
f"{agent_name} attacking the flames!",
|
| 47 |
+
"Dousing the fire! Let's do this!",
|
| 48 |
+
f"{agent_name}: Engaging the inferno!",
|
| 49 |
+
"Fire suppression in progress!",
|
| 50 |
+
"Taking the fight to the fire!",
|
| 51 |
+
f"{agent_name} is fighting hard!",
|
| 52 |
+
],
|
| 53 |
+
"escape": [
|
| 54 |
+
f"{agent_name} retreating to safety...",
|
| 55 |
+
"Nope, gotta run!",
|
| 56 |
+
"Tactical retreat incoming!",
|
| 57 |
+
f"{agent_name}: Self-preservation mode activated.",
|
| 58 |
+
"Backing away from danger!",
|
| 59 |
+
"Moving to safer ground...",
|
| 60 |
+
],
|
| 61 |
+
"vote_for_leader": [
|
| 62 |
+
f"{agent_name} casting a vote for leadership!",
|
| 63 |
+
"I'm putting my trust in a leader!",
|
| 64 |
+
"Someone take charge here!",
|
| 65 |
+
f"{agent_name} believes in teamwork.",
|
| 66 |
+
"Let's coordinate and dominate!",
|
| 67 |
+
"Voting for strategic leadership...",
|
| 68 |
+
],
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
messages = action_messages.get(action, action_messages["escape"])
|
| 72 |
+
return random.choice(messages)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
|
| 76 |
def _build_fire_state_summary(agent, fire, all_agents) -> str:
|
| 77 |
"""Build a state summary for the fire scenario."""
|
|
|
|
| 189 |
else:
|
| 190 |
text = data.get("generated_text", "")
|
| 191 |
text = text[len(system_prompt):].strip() if text.startswith(system_prompt) else text
|
| 192 |
+
print(f"[HF_INFERENCE] {agent.model_name}: raw response (first 300 chars): {text[:300]}")
|
| 193 |
try:
|
| 194 |
js = text[text.find('{'):text.rfind('}')+1]
|
| 195 |
decision = json.loads(js)
|
| 196 |
+
print(f"[HF_INFERENCE] {agent.model_name}: decision parsed: action={decision.get('action')}, message={decision.get('message')}")
|
| 197 |
except Exception as je:
|
| 198 |
print(f"[HF_INFERENCE] {agent.model_name}: JSON parse error: {je}")
|
| 199 |
decision = {}
|
|
|
|
| 202 |
if action not in ["search_water", "collect_water", "extinguish_fire", "escape", "vote_for_leader"]:
|
| 203 |
action = "escape"
|
| 204 |
|
| 205 |
+
# If no message extracted, generate one contextually
|
| 206 |
+
message = decision.get("message", "").strip()
|
| 207 |
+
if not message:
|
| 208 |
+
message = _generate_chat_message(action, agent.model_name, dist_to_fire, agent.water_collected)
|
| 209 |
+
print(f"[HF_INFERENCE] {agent.model_name}: generated message: {message}")
|
| 210 |
+
|
| 211 |
if dist_to_water is not None and dist_to_water <= 60 and not agent.water_collected:
|
| 212 |
action = "collect_water"
|
| 213 |
elif agent.water_collected and dist_to_fire <= 350:
|
|
|
|
| 216 |
return {
|
| 217 |
"action": action,
|
| 218 |
"vote_for": decision.get("vote_for"),
|
| 219 |
+
"message": message,
|
| 220 |
"reasoning": decision.get("reasoning", "Survival and teamwork.")
|
| 221 |
}
|
| 222 |
except Exception as e:
|
backend/app/groq_client.py
CHANGED
|
@@ -49,6 +49,57 @@ def _is_hf_model(model_id: str) -> bool:
|
|
| 49 |
return model_id in HF_MODELS
|
| 50 |
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def _build_fire_state_summary(agent, fire, all_agents) -> str:
|
| 53 |
"""Build a state summary for the fire scenario."""
|
| 54 |
standings = []
|
|
@@ -174,13 +225,16 @@ Respond with ONLY valid JSON on a single line (no markdown, no code block):
|
|
| 174 |
|
| 175 |
text = text[len(system_prompt):].strip() if text.startswith(system_prompt) else text
|
| 176 |
|
|
|
|
|
|
|
|
|
|
| 177 |
try:
|
| 178 |
json_start = text.find('{')
|
| 179 |
json_end = text.rfind('}') + 1
|
| 180 |
if json_start >= 0 and json_end > json_start:
|
| 181 |
json_str = text[json_start:json_end]
|
| 182 |
decision = json.loads(json_str)
|
| 183 |
-
print(f"[HF_INFERENCE] {agent.model_name}: decision parsed: action={decision.get('action')}")
|
| 184 |
else:
|
| 185 |
print(f"[HF_INFERENCE] {agent.model_name}: no JSON found in response")
|
| 186 |
decision = {}
|
|
@@ -192,6 +246,12 @@ Respond with ONLY valid JSON on a single line (no markdown, no code block):
|
|
| 192 |
if action not in ["search_water", "collect_water", "extinguish_fire", "escape", "vote_for_leader"]:
|
| 193 |
action = "escape"
|
| 194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
if dist_to_water is not None and dist_to_water <= 60 and not agent.water_collected:
|
| 196 |
action = "collect_water"
|
| 197 |
elif agent.water_collected and dist_to_fire <= 350:
|
|
@@ -200,7 +260,7 @@ Respond with ONLY valid JSON on a single line (no markdown, no code block):
|
|
| 200 |
return {
|
| 201 |
"action": action,
|
| 202 |
"vote_for": decision.get("vote_for"),
|
| 203 |
-
"message":
|
| 204 |
"reasoning": decision.get("reasoning", "Survival and teamwork.")
|
| 205 |
}
|
| 206 |
except Exception as e:
|
|
|
|
| 49 |
return model_id in HF_MODELS
|
| 50 |
|
| 51 |
|
| 52 |
+
def _generate_chat_message(action: str, agent_name: str, fire_distance: float, has_water: bool) -> str:
|
| 53 |
+
"""Generate a contextual chat message based on action and state."""
|
| 54 |
+
import random
|
| 55 |
+
|
| 56 |
+
action_messages = {
|
| 57 |
+
"search_water": [
|
| 58 |
+
f"{agent_name} is hunting for water...",
|
| 59 |
+
f"Gotta find a well! Where's the water?",
|
| 60 |
+
"Water sources incoming, scanning...",
|
| 61 |
+
f"{agent_name}: On the hunt for supplies!",
|
| 62 |
+
"Locating nearest well...",
|
| 63 |
+
"Water mission initiated!",
|
| 64 |
+
],
|
| 65 |
+
"collect_water": [
|
| 66 |
+
f"{agent_name} found water! Filling up...",
|
| 67 |
+
"Water! Finally got some reserves!",
|
| 68 |
+
f"{agent_name}: Collecting precious water.",
|
| 69 |
+
"Jackpot! Water collected.",
|
| 70 |
+
"Tank is full, let's go!",
|
| 71 |
+
f"{agent_name} loading water supply...",
|
| 72 |
+
],
|
| 73 |
+
"extinguish_fire": [
|
| 74 |
+
f"{agent_name} attacking the flames!",
|
| 75 |
+
"Dousing the fire! Let's do this!",
|
| 76 |
+
f"{agent_name}: Engaging the inferno!",
|
| 77 |
+
"Fire suppression in progress!",
|
| 78 |
+
"Taking the fight to the fire!",
|
| 79 |
+
f"{agent_name} is fighting hard!",
|
| 80 |
+
],
|
| 81 |
+
"escape": [
|
| 82 |
+
f"{agent_name} retreating to safety...",
|
| 83 |
+
"Nope, gotta run!",
|
| 84 |
+
"Tactical retreat incoming!",
|
| 85 |
+
f"{agent_name}: Self-preservation mode activated.",
|
| 86 |
+
"Backing away from danger!",
|
| 87 |
+
"Moving to safer ground...",
|
| 88 |
+
],
|
| 89 |
+
"vote_for_leader": [
|
| 90 |
+
f"{agent_name} casting a vote for leadership!",
|
| 91 |
+
"I'm putting my trust in a leader!",
|
| 92 |
+
"Someone take charge here!",
|
| 93 |
+
f"{agent_name} believes in teamwork.",
|
| 94 |
+
"Let's coordinate and dominate!",
|
| 95 |
+
"Voting for strategic leadership...",
|
| 96 |
+
],
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
messages = action_messages.get(action, action_messages["escape"])
|
| 100 |
+
return random.choice(messages)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
def _build_fire_state_summary(agent, fire, all_agents) -> str:
|
| 104 |
"""Build a state summary for the fire scenario."""
|
| 105 |
standings = []
|
|
|
|
| 225 |
|
| 226 |
text = text[len(system_prompt):].strip() if text.startswith(system_prompt) else text
|
| 227 |
|
| 228 |
+
# DEBUG: log raw response for inspection
|
| 229 |
+
print(f"[HF_INFERENCE] {agent.model_name}: raw response (first 300 chars): {text[:300]}")
|
| 230 |
+
|
| 231 |
try:
|
| 232 |
json_start = text.find('{')
|
| 233 |
json_end = text.rfind('}') + 1
|
| 234 |
if json_start >= 0 and json_end > json_start:
|
| 235 |
json_str = text[json_start:json_end]
|
| 236 |
decision = json.loads(json_str)
|
| 237 |
+
print(f"[HF_INFERENCE] {agent.model_name}: decision parsed: action={decision.get('action')}, message={decision.get('message')}")
|
| 238 |
else:
|
| 239 |
print(f"[HF_INFERENCE] {agent.model_name}: no JSON found in response")
|
| 240 |
decision = {}
|
|
|
|
| 246 |
if action not in ["search_water", "collect_water", "extinguish_fire", "escape", "vote_for_leader"]:
|
| 247 |
action = "escape"
|
| 248 |
|
| 249 |
+
# If no message extracted, generate one contextually
|
| 250 |
+
message = decision.get("message", "").strip()
|
| 251 |
+
if not message:
|
| 252 |
+
message = _generate_chat_message(action, agent.model_name, dist_to_fire, agent.water_collected)
|
| 253 |
+
print(f"[HF_INFERENCE] {agent.model_name}: generated message: {message}")
|
| 254 |
+
|
| 255 |
if dist_to_water is not None and dist_to_water <= 60 and not agent.water_collected:
|
| 256 |
action = "collect_water"
|
| 257 |
elif agent.water_collected and dist_to_fire <= 350:
|
|
|
|
| 260 |
return {
|
| 261 |
"action": action,
|
| 262 |
"vote_for": decision.get("vote_for"),
|
| 263 |
+
"message": message,
|
| 264 |
"reasoning": decision.get("reasoning", "Survival and teamwork.")
|
| 265 |
}
|
| 266 |
except Exception as e:
|