adityaverma977 commited on
Commit
967c2f6
·
1 Parent(s): 11ba25d

Add comprehensive logging for HF API token and inference failures

Browse files
Files changed (2) hide show
  1. app/groq_client.py +13 -3
  2. backend/app/groq_client.py +13 -4
app/groq_client.py CHANGED
@@ -14,11 +14,16 @@ _HF_API_BASE = "https://api-inference.huggingface.co/models"
14
  DEFAULT_DECISION_MODEL = "mistralai/Mistral-7B-Instruct-v0.2"
15
  MAX_AGENT_SPEED = 80
16
 
 
 
 
 
17
 
18
  def is_ready():
19
  return _HF_API_TOKEN is not None
20
 
21
 
 
22
  def _build_fire_state_summary(agent, fire, all_agents) -> str:
23
  """Build a state summary for the fire scenario."""
24
  standings = []
@@ -51,7 +56,8 @@ async def generate_fire_decision(agent, fire, water_sources, other_agents, bound
51
  Fire scenario decision system.
52
  Actions: search_water, collect_water, extinguish_fire, escape, vote_for_leader
53
  """
54
- if not _client:
 
55
  return _fallback_escape(agent, fire)
56
 
57
  dist_to_fire = math.dist((agent.x, agent.y), (fire.x, fire.y))
@@ -119,6 +125,7 @@ What do you do?"""
119
  try:
120
  # Use HF Inference API directly for the requested model (or default)
121
  target_model = agent.model_name if agent.model_name else DEFAULT_DECISION_MODEL
 
122
  async with httpx.AsyncClient(timeout=15.0) as client:
123
  resp = await client.post(
124
  f"{_HF_API_BASE}/{target_model}",
@@ -127,6 +134,7 @@ What do you do?"""
127
  )
128
  resp.raise_for_status()
129
  data = resp.json()
 
130
  if isinstance(data, list) and len(data) > 0:
131
  text = data[0].get("generated_text", "")
132
  else:
@@ -135,7 +143,9 @@ What do you do?"""
135
  try:
136
  js = text[text.find('{'):text.rfind('}')+1]
137
  decision = json.loads(js)
138
- except Exception:
 
 
139
  decision = {}
140
 
141
  action = decision.get("action", "escape")
@@ -154,7 +164,7 @@ What do you do?"""
154
  "reasoning": decision.get("reasoning", "Survival and teamwork.")
155
  }
156
  except Exception as e:
157
- print(f"HF inference failed for {agent.model_name}: {e}")
158
  return _fallback_escape(agent, fire)
159
 
160
 
 
14
  DEFAULT_DECISION_MODEL = "mistralai/Mistral-7B-Instruct-v0.2"
15
  MAX_AGENT_SPEED = 80
16
 
17
+ print(f"[GROQ_CLIENT_INIT] HF_API_TOKEN present: {_HF_API_TOKEN is not None and len(_HF_API_TOKEN) > 0}")
18
+ if not _HF_API_TOKEN:
19
+ print("[GROQ_CLIENT_INIT] WARNING: No HF API token found!")
20
+
21
 
22
  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."""
29
  standings = []
 
56
  Fire scenario decision system.
57
  Actions: search_water, collect_water, extinguish_fire, escape, vote_for_leader
58
  """
59
+ if not is_ready():
60
+ print(f"[INFERENCE_FAIL] {agent.model_name}: HF token not ready, using fallback")
61
  return _fallback_escape(agent, fire)
62
 
63
  dist_to_fire = math.dist((agent.x, agent.y), (fire.x, fire.y))
 
125
  try:
126
  # Use HF Inference API directly for the requested model (or default)
127
  target_model = agent.model_name if agent.model_name else DEFAULT_DECISION_MODEL
128
+ print(f"[HF_INFERENCE] {agent.model_name} -> calling {target_model}")
129
  async with httpx.AsyncClient(timeout=15.0) as client:
130
  resp = await client.post(
131
  f"{_HF_API_BASE}/{target_model}",
 
134
  )
135
  resp.raise_for_status()
136
  data = resp.json()
137
+ print(f"[HF_INFERENCE] {agent.model_name}: response received, status={resp.status_code}")
138
  if isinstance(data, list) and len(data) > 0:
139
  text = data[0].get("generated_text", "")
140
  else:
 
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 = {}
150
 
151
  action = decision.get("action", "escape")
 
164
  "reasoning": decision.get("reasoning", "Survival and teamwork.")
165
  }
166
  except Exception as e:
167
+ print(f"[HF_INFERENCE_ERROR] {agent.model_name}: {type(e).__name__}: {e}")
168
  return _fallback_escape(agent, fire)
169
 
170
 
backend/app/groq_client.py CHANGED
@@ -13,6 +13,11 @@ _HF_API_BASE = "https://api-inference.huggingface.co/models"
13
 
14
  MAX_AGENT_SPEED = 80
15
 
 
 
 
 
 
16
  # Curated HF model ids (small → large)
17
  HF_MODELS = [
18
  "google/flan-t5-small",
@@ -76,8 +81,7 @@ async def generate_fire_decision(agent, fire, water_sources, other_agents, bound
76
  Fire scenario decision system supporting both Groq and HF models.
77
  Actions: search_water, collect_water, extinguish_fire, escape, vote_for_leader
78
  """
79
- if not is_ready():
80
- return _fallback_escape(agent, fire)
81
 
82
  dist_to_fire = math.dist((agent.x, agent.y), (fire.x, fire.y))
83
  nearest_water = min(water_sources, key=lambda w: math.dist((agent.x, agent.y), (w.x, w.y))) if water_sources else None
@@ -144,6 +148,7 @@ Respond with ONLY valid JSON on a single line (no markdown, no code block):
144
  # Always prefer HF models — if agent requested a HF model use it, otherwise
145
  # route to a default HF model from the list.
146
  target_model = agent.model_name if _is_hf_model(agent.model_name) else HF_MODELS[0]
 
147
 
148
  async with httpx.AsyncClient(timeout=15.0) as client:
149
  response = await client.post(
@@ -160,6 +165,7 @@ Respond with ONLY valid JSON on a single line (no markdown, no code block):
160
  )
161
  response.raise_for_status()
162
  data = response.json()
 
163
 
164
  if isinstance(data, list) and len(data) > 0:
165
  text = data[0].get("generated_text", "")
@@ -174,9 +180,12 @@ Respond with ONLY valid JSON on a single line (no markdown, no code block):
174
  if json_start >= 0 and json_end > json_start:
175
  json_str = text[json_start:json_end]
176
  decision = json.loads(json_str)
 
177
  else:
 
178
  decision = {}
179
- except json.JSONDecodeError:
 
180
  decision = {}
181
 
182
  action = decision.get("action", "escape")
@@ -195,7 +204,7 @@ Respond with ONLY valid JSON on a single line (no markdown, no code block):
195
  "reasoning": decision.get("reasoning", "Survival and teamwork.")
196
  }
197
  except Exception as e:
198
- print(f"Error calling inference for {agent.model_name}: {e}")
199
  return _fallback_escape(agent, fire)
200
 
201
 
 
13
 
14
  MAX_AGENT_SPEED = 80
15
 
16
+ # Debug: log token status on startup
17
+ print(f"[GROQ_CLIENT_INIT] HF_API_TOKEN present: {_HF_API_TOKEN is not None and len(_HF_API_TOKEN) > 0}")
18
+ if not _HF_API_TOKEN:
19
+ print("[GROQ_CLIENT_INIT] WARNING: No HF API token found! Set HF_API_TOKEN or HUGGINGFACE_API_TOKEN env var.")
20
+
21
  # Curated HF model ids (small → large)
22
  HF_MODELS = [
23
  "google/flan-t5-small",
 
81
  Fire scenario decision system supporting both Groq and HF models.
82
  Actions: search_water, collect_water, extinguish_fire, escape, vote_for_leader
83
  """
84
+ if not is_ready(): print(f"[INFERENCE_FAIL] {agent.model_name}: HF token not ready, using fallback") return _fallback_escape(agent, fire)
 
85
 
86
  dist_to_fire = math.dist((agent.x, agent.y), (fire.x, fire.y))
87
  nearest_water = min(water_sources, key=lambda w: math.dist((agent.x, agent.y), (w.x, w.y))) if water_sources else None
 
148
  # Always prefer HF models — if agent requested a HF model use it, otherwise
149
  # route to a default HF model from the list.
150
  target_model = agent.model_name if _is_hf_model(agent.model_name) else HF_MODELS[0]
151
+ print(f"[HF_INFERENCE] {agent.model_name} -> calling {target_model}")
152
 
153
  async with httpx.AsyncClient(timeout=15.0) as client:
154
  response = await client.post(
 
165
  )
166
  response.raise_for_status()
167
  data = response.json()
168
+ print(f"[HF_INFERENCE] {agent.model_name}: response received, status={response.status_code}")
169
 
170
  if isinstance(data, list) and len(data) > 0:
171
  text = data[0].get("generated_text", "")
 
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 = {}
187
+ except json.JSONDecodeError as je:
188
+ print(f"[HF_INFERENCE] {agent.model_name}: JSON parse error: {je}")
189
  decision = {}
190
 
191
  action = decision.get("action", "escape")
 
204
  "reasoning": decision.get("reasoning", "Survival and teamwork.")
205
  }
206
  except Exception as e:
207
+ print(f"[HF_INFERENCE_ERROR] {agent.model_name}: {type(e).__name__}: {e}")
208
  return _fallback_escape(agent, fire)
209
 
210