Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files658 unmodified lines659660661662662663664665666667668669664665670671672673674675676148 unmodified lines825826827820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888658 unmodified lines def _validate_message(cls, msg): content = getattr(msg, "content", None) if msg is not None else None if content is None or not str(content).strip(): raise RuntimeError("Error in generating model output: empty model response (content=None)") reasoning = getattr(msg, "reasoning_content", None) if msg is not None else None tool_calls = getattr(msg, "tool_calls", None) if msg is not None else None if not reasoning and not tool_calls: raise RuntimeError( "Error in generating model output: empty model response (content=None)" ) content = "" cleaned = cls._sanitize_code_tags(str(content)) if not cleaned.strip(): raise RuntimeError("Error in generating model output: empty model response after sanitization") if not cleaned.strip() and not getattr(msg, "tool_calls", None): raise RuntimeError( "Error in generating model output: empty model response after sanitization" ) msg.content = cleaned return msg
148 unmodified lines )
for attempt in range(max_attempts): try: result = self.agent.run(prompt) raw_text = "" if result is None else str(result).strip() raw_lower = raw_text.lower() if any(marker in raw_lower for marker in error_markers): raise RuntimeError(raw_text) return self._clean_exact_match_answer(raw_text) except Exception as exc: err_lower = str(exc).lower() is_recoverable = any(marker in err_lower for marker in error_markers) print( f"[AGENT ERROR] task_id={task_id} model={self.model_id} " f"(attempt {attempt + 1}/{max_attempts}): {exc}" ) if not is_recoverable or attempt == max_attempts - 1: return f"AGENT ERROR: {exc}"
if len(models_to_try) > 1: current_provider = self.model_id.split("/", 1)[0].lower() if "/" in self.model_id else "gemini" other_provider_models = [ m for m in models_to_try if m != self.model_id and ((m.split("/", 1)[0].lower() if "/" in m else "gemini") != current_provider) ] if other_provider_models and any(m in err_lower for m in ("401", "402", "404", "authenticationerror", "notfounderror")): next_model = other_provider_models[0] else: try: idx = models_to_try.index(self.model_id) next_model = models_to_try[(idx + 1) % len(models_to_try)] except ValueError: next_model = models_to_try[0] self.model_id = next_model self.agent.model = model_cls( model_id=self.model_id, api_key=self._get_api_key(self.model_id), max_tokens=8192, reasoning_effort="low", num_retries=1, timeout=25, ) print(f"Switched model to {self.model_id}.") wait_sec = 3 if any(m in err_lower for m in fast_retry_markers) else 25 print(f"Sleeping {wait_sec}s before next attempt...") time.sleep(wait_sec) step_start = time.monotonic() try: result = self.agent.run(prompt) raw_text = "" if result is None else str(result).strip() raw_lower = raw_text.lower() if any(marker in raw_lower for marker in error_markers): raise RuntimeError(raw_text) return self._clean_exact_match_answer(raw_text) except Exception as exc: elapsed = time.monotonic() - step_start err_lower = str(exc).lower() is_recoverable = any(marker in err_lower for marker in error_markers) print( f"[AGENT ERROR] task_id={task_id} model={self.model_id} " f"elapsed={elapsed:.1f}s (attempt {attempt + 1}/{max_attempts}): {exc}" ) if not is_recoverable or attempt == max_attempts - 1: return f"AGENT ERROR: {exc}"
if len(models_to_try) > 1: current_provider = ( self.model_id.split("/", 1)[0].lower() if "/" in self.model_id else "gemini" ) other_provider_models = [ m for m in models_to_try if m != self.model_id and ( (m.split("/", 1)[0].lower() if "/" in m else "gemini") != current_provider ) ] if other_provider_models and any( m in err_lower for m in ( "401", "402", "404", "authenticationerror", "notfounderror", ) ): next_model = other_provider_models[0] else: try: idx = models_to_try.index(self.model_id) next_model = models_to_try[(idx + 1) % len(models_to_try)] except ValueError: next_model = models_to_try[0] self.model_id = next_model self.agent.model = self._build_model(self.model_id) print(f"Switched model to {self.model_id}.")
wait_sec = 3 if any(m in err_lower for m in fast_retry_markers) else 25 print(f"Sleeping {wait_sec}s before next attempt...") time.sleep(wait_sec)
return "AGENT ERROR: Max retries exceeded."
app.py+69-49