Spaces:
Runtime error
Runtime error
Update app.py
Browse files1162 unmodified lines116311641165116611671168116911701171117211731174117511761177117811791180118111821166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120011841185118611871201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271189122812291230123112321233123412351236123712381807 unmodified lines304630473048300430493050305130525 unmodified lines30583059306030163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673061306230631162 unmodified lines "Error in generating model output: empty model response (content=None)" )
raw_content = getattr(msg, "content", None) if isinstance(raw_content, list): text_parts = [] for part in raw_content: if isinstance(part, dict): if part.get("type") in ("text", "output_text", None) and part.get("text"): text_parts.append(str(part["text"])) else: p_type = getattr(part, "type", None) p_text = getattr(part, "text", None) if p_text and p_type in ("text", "output_text", None): text_parts.append(str(p_text)) content_str = "\n".join(text_parts).strip() elif raw_content is None: content_str = "" else: content_str = str(raw_content).strip() def _extract_text_from_blocks(val) -> str: if val is None: return "" if isinstance(val, str): return val.strip() if isinstance(val, list): parts = [] for part in val: if isinstance(part, dict): txt = part.get("text") or part.get("thinking") or part.get("content") if txt: parts.append(str(txt)) else: txt = ( getattr(part, "text", None) or getattr(part, "thinking", None) or getattr(part, "content", None) ) if txt: parts.append(str(txt)) return "\n".join(parts).strip() return str(val).strip()
raw_obj = getattr(msg, "raw", None) raw_msg = None if raw_obj is not None: choices = raw_obj.get("choices") if isinstance(raw_obj, dict) else getattr(raw_obj, "choices", None) if choices: first_choice = choices[0] raw_msg = ( first_choice.get("message") if isinstance(first_choice, dict) else getattr(first_choice, "message", None) )
reasoning = ( getattr(msg, "reasoning_content", None) or getattr(msg, "reasoning", None) or "" content_str = _extract_text_from_blocks(getattr(msg, "content", None)) if not content_str and raw_msg is not None: raw_msg_content = raw_msg.get("content") if isinstance(raw_msg, dict) else getattr(raw_msg, "content", None) content_str = _extract_text_from_blocks(raw_msg_content)
reasoning_sources = [ getattr(msg, "reasoning_content", None), getattr(msg, "reasoning", None), ] if raw_msg is not None: if isinstance(raw_msg, dict): reasoning_sources.extend([ raw_msg.get("reasoning_content"), raw_msg.get("reasoning"), raw_msg.get("thinking_blocks"), ]) else: reasoning_sources.extend([ getattr(raw_msg, "reasoning_content", None), getattr(raw_msg, "reasoning", None), getattr(raw_msg, "thinking_blocks", None), ])
reasoning_str = next( (txt for src in reasoning_sources if (txt := _extract_text_from_blocks(src))), "", ) reasoning_str = str(reasoning).strip() if reasoning else ""
tool_calls = getattr(msg, "tool_calls", None) if not tool_calls and raw_msg is not None: tool_calls = ( raw_msg.get("tool_calls") if isinstance(raw_msg, dict) else getattr(raw_msg, "tool_calls", None) )
if not content_str and tool_calls: rendered_calls = []1807 unmodified lines "timeout": 30, } if model_name.startswith("gemini/"): kwargs["reasoning_effort"] = "low" kwargs["reasoning_effort"] = "none" elif model_name.startswith("groq/"): kwargs["tool_choice"] = "auto" kwargs["parallel_tool_calls"] = False5 unmodified lines target_dict["tool_choice"] = "auto" return model_instance
self._build_model = _build_model model = self._build_model(self.model_id)
exact_match_rule = ( "CRITICAL OUTPUT RULES FOR EXACT MATCH EVALUATION:\n" "- Always write valid Python code inside <code> and </code> tags. NEVER put 'Thought:' or ``` fences inside the code block.\n" "- If the prompt includes a Task ID and mentions an image, board, diagram, or attached file, FIRST call path = download_task_file(task_id=...) and inspect it.\n" "- Never call built-in open(); use read_local_file(file_path) for text/PDF/ZIP/spreadsheet files, " "inspect_image(image_path, question) for images, and transcribe_audio(audio_path, question) for audio files (.mp3, .wav).\n" "- If web_search returns no results, use search_wikipedia(query) or fetch_url_text(url).\n" "- Return ONLY the concise exact answer value itself " "(e.g. a number, date, name, or short phrase) via final_answer().\n" "- NEVER include introductory text, explanations, markdown formatting, or the phrase 'FINAL ANSWER'.\n" "- Do not wrap the answer in a dictionary like {'answer': ...}; pass the plain answer string directly." )
agent_kwargs = { "model": model, "tools": [ web_search, search_wikipedia, ask_web, fetch_url_text, download_task_file, read_local_file, inspect_image, inspect_youtube, transcribe_audio, ], "additional_authorized_imports": [ "json", "csv", "pandas", "numpy", "urllib", "html", "decimal", "fractions", "string", "re", "time", ], "instructions": exact_match_rule, "code_block_tags": ("<code>", "</code>"), "max_steps": 10, "verbosity_level": 0, } sig = inspect.signature(CodeAgent.__init__) filtered_agent_kwargs = {k: v for k, v in agent_kwargs.items() if k in sig.parameters} self.agent = CodeAgent(**filtered_agent_kwargs) print("CodeAgent initialized with LiteLLMModel.")
@staticmethod def _clean_exact_match_answer(raw_answer) -> str: import reapp.py+68-75