Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files426 unmodified lines4274284294304314324334344354364334374384354394404414429 unmodified lines452453454451455456453457458459460355 unmodified lines81681781881982082182227 unmodified lines85085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289384985085185285385485585685785885986089489589689786286389889990090190245 unmodified lines94894995095191595295395495595695795895996096197 unmodified lines105910601061101910201021102210231024102510621063106410651030106610671068106910701071107214 unmodified lines1087108810891090109110921093426 unmodified lines import re from html import unescape
cleaned_query = (query or "").strip() if not cleaned_query: return "Brak wyników."
try: resp = requests.get( "https://en.wikipedia.org/w/api.php", params={"action": "opensearch", "search": query, "limit": 3, "format": "json"}, params={"action": "opensearch", "search": cleaned_query, "limit": 3, "format": "json"}, headers={"User-Agent": "BasicAgent/1.0"}, timeout=30, timeout=15, ) resp.raise_for_status() data = resp.json()9 unmodified lines try: ddg_resp = requests.post( "https://html.duckduckgo.com/html/", data={"q": query}, data={"q": cleaned_query}, headers={"User-Agent": "Mozilla/5.0"}, timeout=30, timeout=15, ) ddg_resp.raise_for_status() matches = re.findall(355 unmodified lines
@classmethod def _validate_message(cls, msg): import json import re
if msg is None:27 unmodified lines reasoning_str = str(reasoning).strip() if reasoning else "" tool_calls = getattr(msg, "tool_calls", None)
if not content_str and tool_calls: rendered_calls = [] for tc in tool_calls: fn_obj = getattr(tc, "function", None) if fn_obj is None and isinstance(tc, dict): fn_obj = tc.get("function") fn_name = getattr(fn_obj, "name", None) if fn_obj is not None else None if fn_name is None and isinstance(fn_obj, dict): fn_name = fn_obj.get("name") fn_args = getattr(fn_obj, "arguments", None) if fn_obj is not None else None if fn_args is None and isinstance(fn_obj, dict): fn_args = fn_obj.get("arguments") if not fn_name: continue if isinstance(fn_args, str): try: parsed_args = json.loads(fn_args) if fn_args.strip() else {} except Exception: parsed_args = {"query": fn_args} else: parsed_args = fn_args or {} if isinstance(parsed_args, dict): rendered_calls.append( f"print({fn_name}(**{json.dumps(parsed_args, ensure_ascii=False)}))" ) if rendered_calls: content_str = "<code>\n" + "\n".join(rendered_calls) + "\n</code>"
if not content_str and reasoning_str: code_match = re.search( r"(<code>.*?</code>|```(?:py|python)?\s*\n.*?```)", reasoning_str, flags=re.DOTALL | re.IGNORECASE, ) if code_match: content_str = code_match.group(1).strip() else: first_line = reasoning_str.splitlines()[0].replace('"', "'") content_str = f'<code>\nprint(ask_web("{first_line[:120]}")) # fallback from reasoning\n</code>'
if not content_str: if reasoning_str: code_match = re.search( r"(<code>.*?</code>|```(?:py|python)?\s*\n.*?```)", reasoning_str, flags=re.DOTALL | re.IGNORECASE, ) if code_match: content_str = code_match.group(1).strip() if not content_str and not tool_calls: raise RuntimeError( "Error in generating model output: empty model response (content=None)" ) raise RuntimeError( "Error in generating model output: empty model response (content=None)" )
cleaned = cls._sanitize_code_tags(content_str) if content_str else "" if not cleaned.strip() and not tool_calls: cleaned = cls._sanitize_code_tags(content_str) if not cleaned.strip(): raise RuntimeError( "Error in generating model output: empty model response after sanitization" )45 unmodified lines kwargs["reasoning_effort"] = "low" elif model_name.startswith("groq/"): kwargs["tool_choice"] = "auto" kwargs["parallel_tool_calls"] = False return self._model_cls(**kwargs) model_instance = self._model_cls(**kwargs) if model_name.startswith("groq/"): for attr_name in ("kwargs", "completion_kwargs", "model_kwargs"): target_dict = getattr(model_instance, attr_name, None) if isinstance(target_dict, dict): target_dict["tool_choice"] = "auto" return model_instance
self._build_model = _build_model model = self._build_model(self.model_id)97 unmodified lines if self.model_id and self.model_id not in models_to_try: models_to_try.append(self.model_id)
models_to_try = [ m for m in models_to_try if m not in blocked_models and (m.split("/", 1)[0].lower() if "/" in m else "gemini") not in blocked_providers and bool(self._get_api_key(m)) ]
for model_name in models_to_try: if not hasattr(self, "_build_model") or not hasattr(self, "agent"): break provider = model_name.split("/", 1)[0].lower() if "/" in model_name else "gemini" if provider in blocked_providers: if model_name in blocked_models or provider in blocked_providers: continue if not self._get_api_key(model_name): blocked_providers.add(provider) continue try: self.model_id = model_name14 unmodified lines "authentication", "missing authentication header", "insufficient permissions", "does not have sufficient permissions", ) ): blocked_providers.add(provider)app.py+67-27