Add global research verification policy

This commit is contained in:
Mikei386
2026-08-31 22:35:09 +02:00
parent 82a1809423
commit b2ea53c383
5 changed files with 151 additions and 0 deletions
+62
View File
@@ -121,6 +121,8 @@ POLL_INTERVAL = float(os.environ.get("POLL_INTERVAL", "2")) # s, Polling
MAX_GENERATION_TOKENS = int(os.environ.get("MAX_GENERATION_TOKENS", "8192"))
DEFAULT_REASONING_EFFORT = os.environ.get(
"DEFAULT_REASONING_EFFORT", "off").strip().lower()
GLOBAL_SYSTEM_POLICY_FILE = os.environ.get(
"GLOBAL_SYSTEM_POLICY_FILE", "").strip()
# --- Bildgenerierung und Referenzbild-Bearbeitung (FLUX.2 Klein 4B) ---
LLAMA_SERVICE = os.environ.get("LLAMA_SERVICE", "mike-ai-llama-ui.service")
@@ -1270,6 +1272,57 @@ _REASONING_EFFORT_MAP = {
_REASONING_OFF = {"", "none", "off", "disabled", "false"}
def _load_global_system_policy() -> str:
"""Load the static cross-client platform policy.
The file is read for every request so operators can revise the policy
without rebuilding or restarting the router. Its contents remain stable
between edits and therefore remain friendly to upstream prompt caches.
"""
if not GLOBAL_SYSTEM_POLICY_FILE:
return ""
try:
with open(GLOBAL_SYSTEM_POLICY_FILE, encoding="utf-8") as handle:
return handle.read().strip()
except OSError as exc:
raise ValueError(
f"globale Systemrichtlinie nicht lesbar: {exc}") from exc
def _inject_global_system_policy(data: dict, path: str) -> dict:
"""Prepend the shared policy to OpenAI chat and Responses requests."""
policy = _load_global_system_policy()
if not policy:
return data
if path == "/v1/chat/completions":
messages = data.get("messages")
if not isinstance(messages, list):
return data
if messages and isinstance(messages[0], dict) and (
messages[0].get("role") == "system"
and isinstance(messages[0].get("content"), str)):
existing = messages[0]["content"]
if policy not in existing:
messages[0]["content"] = f"{policy}\n\n{existing}"
elif not any(
isinstance(message, dict)
and message.get("role") == "system"
and message.get("content") == policy
for message in messages):
messages.insert(0, {"role": "system", "content": policy})
return data
if path == "/v1/responses":
instructions = data.get("instructions")
if isinstance(instructions, str) and instructions:
if policy not in instructions:
data["instructions"] = f"{policy}\n\n{instructions}"
elif instructions is None or instructions == "":
data["instructions"] = policy
return data
def _normalize_llamacpp_reasoning(data: dict) -> dict:
"""Mappt OpenAI/Hermes-Reasoning auf llama.cpp-Template-Parameter.
@@ -2203,6 +2256,15 @@ class Handler(BaseHTTPRequestHandler):
"invalid_request_error", "unknown_model")
return
if path in {"/v1/chat/completions", "/v1/responses"}:
try:
data = _inject_global_system_policy(data, path)
except ValueError as exc:
self._send_error(500, str(exc), "server_error",
"system_policy_unavailable")
return
body = json.dumps(data).encode()
# Alle modellbezogenen Requests erhalten eine atomare Lease. Damit
# kann kein zweiter Client zwischen Profilwahl und Upstream-Request das
# Modell austauschen. Vision-Vorbereitung gehört zur selben Transaktion.