Block publish when LLM rewrite quality degrades
This commit is contained in:
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any, Callable
|
||||
from urllib.error import HTTPError
|
||||
|
||||
from .llm import parse_json_object
|
||||
from .models import NewsItem
|
||||
@@ -49,6 +50,14 @@ def _fallback(item: NewsItem) -> None:
|
||||
item.summary = item.summary_raw or "该条目暂无摘要。"
|
||||
|
||||
|
||||
def _is_transient_llm_error(exc: Exception) -> bool:
|
||||
if isinstance(exc, TimeoutError):
|
||||
return True
|
||||
if isinstance(exc, HTTPError):
|
||||
return exc.code in {429, 500, 502, 503, 504}
|
||||
return False
|
||||
|
||||
|
||||
def _apply_rewrite_batch(batch: list[NewsItem], llm_call: RewriteLlmCall) -> int:
|
||||
obj = parse_json_object(llm_call(_build_prompt(batch)))
|
||||
rewrites = obj.get("rewrites", [])
|
||||
@@ -75,6 +84,7 @@ def rewrite_items(
|
||||
*,
|
||||
llm_call: RewriteLlmCall,
|
||||
batch_size: int = 10,
|
||||
max_fallback_ratio: float = 0.2,
|
||||
) -> tuple[list[NewsItem], dict[str, Any]]:
|
||||
rewritten_count = 0
|
||||
fallback_count = 0
|
||||
@@ -85,6 +95,11 @@ def rewrite_items(
|
||||
rewritten_count += _apply_rewrite_batch(batch, llm_call)
|
||||
except Exception as exc:
|
||||
errors.append(f"batch:{type(exc).__name__}: {exc}")
|
||||
if _is_transient_llm_error(exc):
|
||||
for item in batch:
|
||||
_fallback(item)
|
||||
fallback_count += 1
|
||||
continue
|
||||
for item in batch:
|
||||
try:
|
||||
rewritten_count += _apply_rewrite_batch([item], llm_call)
|
||||
@@ -93,11 +108,19 @@ def rewrite_items(
|
||||
_fallback(item)
|
||||
fallback_count += 1
|
||||
|
||||
fallback_ratio = fallback_count / len(items) if items else 0
|
||||
blocking_errors: list[str] = []
|
||||
if fallback_ratio > max_fallback_ratio:
|
||||
blocking_errors.append("rewrite_fallback_ratio_exceeded")
|
||||
|
||||
report = {
|
||||
"input_count": len(items),
|
||||
"rewritten_count": rewritten_count,
|
||||
"fallback_count": fallback_count,
|
||||
"fallback_ratio": round(fallback_ratio, 4),
|
||||
"batch_count": len(_chunks(items, max(1, batch_size))),
|
||||
"errors": errors,
|
||||
"blocking_errors": blocking_errors,
|
||||
"quality_gate_failed": bool(blocking_errors),
|
||||
}
|
||||
return items, report
|
||||
|
||||
Reference in New Issue
Block a user