Update backend/lens_core.py
Browse files561 unmodified lines56256356456556656756856556656756856957057157257357457557657757857958058158258358458558658758858959025 unmodified lines616617618619620621622561 unmodified lines
def _openai_compat_generate_json(api_key: str, base_url: str, model: str, system_text: str, user_parts: list[str]): url = (base_url.rstrip("/") + "/chat/completions") messages = [{"role": "system", "content": system_text}] for p in user_parts: if (p or "").strip(): messages.append({"role": "user", "content": p})
def _user_only_prompt_for_model(m: str) -> bool: ml = (m or "").strip().lower() if not ml: return False if "gemma-3" in ml or "gemma-2" in ml: return True if "gemma" in ml and ("-it" in ml or "instruct" in ml): return True return False
def _build_messages(m: str): parts = [p.strip() for p in (user_parts or []) if isinstance(p, str) and p.strip()] sys = (system_text or "").strip() if _user_only_prompt_for_model(m): combined = "\n\n".join([x for x in ([sys] + parts) if x]) return [{"role": "user", "content": combined}] msgs = [{"role": "system", "content": system_text}] for p in parts: msgs.append({"role": "user", "content": p}) return msgs
messages = _build_messages(model) payload = { "model": model, "messages": messages,25 unmodified lines fallback = _pick_hf_fallback_model(models) if fallback and fallback != model: payload["model"] = fallback payload["messages"] = _build_messages(fallback) used_model = fallback r2 = client.post(url, json=payload, headers=headers) try:backend/lens_core.py+24-4