from __future__ import annotations from typing import Any from src.config import settings from src.errors import ApiError def _content_to_text(content: Any) -> str: if content is None: return "" if isinstance(content, str): return content if isinstance(content, list): parts: list[str] = [] unsupported: list[str] = [] for item in content: if not isinstance(item, dict): continue item_type = item.get("type") if item_type == "text": parts.append(str(item.get("text", ""))) elif item_type in {"image_url", "input_image", "video_url"}: unsupported.append(str(item_type)) if unsupported: raise ApiError( code="unsupported_content", message=( "This first GGUF Space is text-only. Unsupported content parts: " + ", ".join(sorted(set(unsupported))) ), status_code=400, ) return "\n".join(p for p in parts if p) return str(content) def normalize_messages(messages: Any) -> list[dict[str, str]]: if not isinstance(messages, list) or not messages: raise ApiError("invalid_request", "messages must be a non-empty list", 400) normalized: list[dict[str, str]] = [] for msg in messages: if not isinstance(msg, dict): raise ApiError("invalid_request", "each message must be an object", 400) role = str(msg.get("role", "user")).strip().lower() if role == "developer": role = "system" if role not in {"system", "user", "assistant"}: raise ApiError("invalid_request", f"unsupported role: {role}", 400) text = _content_to_text(msg.get("content", "")).strip() if text: normalized.append({"role": role, "content": text}) if not any(m["role"] == "user" for m in normalized): raise ApiError("invalid_request", "at least one user message is required", 400) return normalized def build_prompt(messages: list[dict[str, str]], *, thinking: bool | None = None) -> str: """Build a conservative prompt for llama-diffusion-cli conversation mode. `llama-diffusion-cli -cnv` already owns the assistant turn. Passing a manual `Assistant:` suffix makes DiffusionGemma treat the prompt as transcript text and continue with hidden reasoning about that transcript. For a single-turn API request, pass the user's text directly. """ thinking_enabled = settings.thinking_enabled_default if thinking is None else bool(thinking) if len(messages) == 1 and messages[0]["role"] == "user": content = messages[0]["content"] if thinking_enabled and "<|think|>" not in content: content = "<|think|>\n" + content return content has_system = any(m["role"] == "system" for m in messages) lines: list[str] = [] if not has_system: system_prompt = settings.default_system_prompt if thinking_enabled and "<|think|>" not in system_prompt: system_prompt = "<|think|>\n" + system_prompt lines.append(f"System: {system_prompt}") for m in messages: role = m["role"] content = m["content"] if role == "system": if thinking_enabled and "<|think|>" not in content: content = "<|think|>\n" + content lines.append(f"System: {content}") elif role == "user": lines.append(f"User: {content}") elif role == "assistant": # Do not include internal thought traces in history; caller should pass final answers only. lines.append(f"Assistant: {content}") return "\n\n".join(lines)