videogen/apps/api/app/core/moderation_common.py
caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

106 lines
3.7 KiB
Python

"""Helpers shared by every prompt-moderation hop (OpenAI-compatible and Claude).
Env lookup, the user message wrapper, the HTTP call (429 -> ``ModerationRateLimited``)
and strict validation of the model's JSON answer ``{decision, categories[], rationale}``.
Nothing here logs prompt text or the model's rationale.
"""
from __future__ import annotations
import json
import os
import re
from typing import Any
import httpx
from app.core.moderation_policy import CATEGORIES
from app.core.moderation_types import DECISIONS, ModerationRateLimited, ModerationUnavailable
DEFAULT_MAX_TOKENS = 400
ALWAYS_DENY = frozenset({"minors"})
_JSON_OBJECT = re.compile(r"\{.*\}", re.DOTALL)
def env(*names: str) -> str:
for name in names:
value = (os.getenv(name) or "").strip()
if value:
return value
return ""
def max_tokens() -> int:
raw = env("MENIVOR_MODERATION_MAX_TOKENS")
try:
return max(128, int(raw)) if raw else DEFAULT_MAX_TOKENS
except ValueError:
return DEFAULT_MAX_TOKENS
def reasoning_effort() -> str:
value = env("MENIVOR_MODERATION_REASONING_EFFORT").lower()
return value if value in {"low", "medium", "high"} else "low"
def user_message(prompt: str) -> str:
return f"Content to classify:\n\n{prompt}"
def _retry_after(response: Any) -> float | None:
headers = getattr(response, "headers", None) or {}
try:
raw = headers.get("retry-after") or headers.get("Retry-After")
return float(raw) if raw else None
except (TypeError, ValueError, AttributeError):
return None
def post_json(url: str, headers: dict[str, str], body: dict[str, Any], timeout_s: float) -> Any:
"""POST ``body`` and return the decoded JSON. Raises ``ModerationUnavailable``."""
try:
response = httpx.post(url, headers=headers, json=body, timeout=timeout_s)
except httpx.HTTPError as exc:
raise ModerationUnavailable(f"transport: {type(exc).__name__}") from exc
if response.status_code == 429:
raise ModerationRateLimited("status 429", _retry_after(response))
if response.status_code != 200:
raise ModerationUnavailable(f"status {response.status_code}")
try:
return response.json()
except ValueError as exc:
raise ModerationUnavailable("malformed completion body") from exc
def parse_decision(content: Any) -> tuple[str, tuple[str, ...]]:
"""Validate the model's JSON answer. Raises ``ModerationUnavailable``."""
if not isinstance(content, str) or not content.strip():
raise ModerationUnavailable("empty model answer")
text = content.strip()
try:
data = json.loads(text)
except ValueError:
match = _JSON_OBJECT.search(text)
if not match:
raise ModerationUnavailable("model answer is not JSON") from None
try:
data = json.loads(match.group(0))
except ValueError as exc:
raise ModerationUnavailable("model answer is not JSON") from exc
if not isinstance(data, dict):
raise ModerationUnavailable("model answer is not an object")
decision = str(data.get("decision") or "").strip().lower()
if decision not in DECISIONS:
raise ModerationUnavailable(f"unexpected decision {decision[:16]!r}")
raw_categories = data.get("categories") or []
if isinstance(raw_categories, str):
raw_categories = [raw_categories]
if not isinstance(raw_categories, list):
raise ModerationUnavailable("categories is not a list")
categories = tuple(
sorted({str(c).strip().lower() for c in raw_categories} & CATEGORIES)
)
if ALWAYS_DENY.intersection(categories):
decision = "deny"
return decision, categories