Improve writer prompt testing
This commit is contained in:
@@ -916,10 +916,39 @@ async def category_rows() -> list[dict[str, Any]]:
|
||||
return result
|
||||
|
||||
|
||||
async def prompt_test_posts(selected_post_id: int | None = None) -> tuple[list[dict[str, Any]], dict[str, Any] | None]:
|
||||
PROMPT_TEST_STATUS_OPTIONS = [
|
||||
{"value": "all", "label": "Все"},
|
||||
{"value": "qualified", "label": "Оцененные"},
|
||||
{"value": "accepted", "label": "Принятые"},
|
||||
{"value": "maybe", "label": "Спорные"},
|
||||
{"value": "rejected", "label": "Отклоненные"},
|
||||
{"value": "pending", "label": "Ждут AI"},
|
||||
]
|
||||
|
||||
|
||||
def normalize_prompt_test_status(value: str | None) -> str:
|
||||
value = str(value or "all").strip().lower()
|
||||
allowed = {item["value"] for item in PROMPT_TEST_STATUS_OPTIONS}
|
||||
return value if value in allowed else "all"
|
||||
|
||||
|
||||
def prompt_test_status_where(status_filter: str) -> str:
|
||||
if status_filter == "qualified":
|
||||
return "AND rp.qualification_status IN ('accepted', 'maybe', 'rejected')"
|
||||
if status_filter in {"accepted", "maybe", "rejected", "pending"}:
|
||||
return f"AND rp.qualification_status = '{status_filter}'"
|
||||
return ""
|
||||
|
||||
|
||||
async def prompt_test_posts(
|
||||
selected_post_id: int | None = None,
|
||||
status_filter: str = "all",
|
||||
) -> tuple[list[dict[str, Any]], dict[str, Any] | None]:
|
||||
pool = await get_pool()
|
||||
status_filter = normalize_prompt_test_status(status_filter)
|
||||
where_sql = prompt_test_status_where(status_filter)
|
||||
rows = await pool.fetch(
|
||||
"""
|
||||
f"""
|
||||
SELECT rp.id, rp.raw_text, rp.original_url, rp.posted_at, rp.created_at, rp.qualification_status,
|
||||
rp.qualification_score, s.name AS source_name, s.tag AS source_tag,
|
||||
rp.qualification_reason,
|
||||
@@ -928,6 +957,8 @@ async def prompt_test_posts(selected_post_id: int | None = None) -> tuple[list[d
|
||||
FROM raw_post_media m WHERE m.raw_post_id=rp.id) AS media_types
|
||||
FROM raw_posts rp
|
||||
JOIN sources s ON s.id=rp.source_id
|
||||
WHERE TRUE
|
||||
{where_sql}
|
||||
ORDER BY rp.created_at DESC, rp.id DESC
|
||||
LIMIT 150
|
||||
"""
|
||||
@@ -990,6 +1021,34 @@ def prompt_test_payload(post: dict[str, Any], max_text_chars: int) -> dict[str,
|
||||
}
|
||||
|
||||
|
||||
def prompt_test_first_rewrite(parsed: Any) -> dict[str, Any] | None:
|
||||
if not isinstance(parsed, dict):
|
||||
return None
|
||||
rewrites = parsed.get("rewrites")
|
||||
if not isinstance(rewrites, list) and isinstance(parsed.get("data"), dict):
|
||||
rewrites = parsed["data"].get("rewrites")
|
||||
if not isinstance(rewrites, list) or not rewrites:
|
||||
return None
|
||||
first = rewrites[0]
|
||||
return first if isinstance(first, dict) else None
|
||||
|
||||
|
||||
def prompt_test_preview_text(parsed: Any) -> str:
|
||||
rewrite = prompt_test_first_rewrite(parsed)
|
||||
return str((rewrite or {}).get("text") or "").strip()
|
||||
|
||||
|
||||
def prompt_test_preview_category(parsed: Any) -> str:
|
||||
rewrite = prompt_test_first_rewrite(parsed)
|
||||
return str((rewrite or {}).get("category") or "").strip()
|
||||
|
||||
|
||||
def prompt_test_preview_notes(parsed: Any) -> str:
|
||||
rewrite = prompt_test_first_rewrite(parsed)
|
||||
notes = (rewrite or {}).get("notes")
|
||||
return "" if notes in {None, ""} else str(notes).strip()
|
||||
|
||||
|
||||
def setting_value(settings_rows: list[dict], key: str, default: Any = None) -> Any:
|
||||
for row in settings_rows:
|
||||
if row.get("key") == key:
|
||||
@@ -2272,11 +2331,12 @@ async def raw_post_detail(request: Request, post_id: int):
|
||||
|
||||
|
||||
@app.get("/prompt-test", response_class=HTMLResponse)
|
||||
async def prompt_test(request: Request, post_id: int | None = None):
|
||||
async def prompt_test(request: Request, post_id: int | None = None, q_status: str = "all"):
|
||||
user = await get_current_user(request)
|
||||
if not user:
|
||||
return redirect("/login")
|
||||
posts, selected = await prompt_test_posts(post_id)
|
||||
q_status = normalize_prompt_test_status(q_status)
|
||||
posts, selected = await prompt_test_posts(post_id, q_status)
|
||||
categories = await writer_categories()
|
||||
max_text_chars = max(200, int(await fetch_setting("ai_writer_max_text_chars", 3500) or 3500))
|
||||
prompt = str(await fetch_setting("ai_writer_prompt", "") or "")
|
||||
@@ -2295,6 +2355,8 @@ async def prompt_test(request: Request, post_id: int | None = None):
|
||||
prompt=prompt,
|
||||
provider=provider,
|
||||
model=model,
|
||||
q_status=q_status,
|
||||
status_options=PROMPT_TEST_STATUS_OPTIONS,
|
||||
max_text_chars=max_text_chars,
|
||||
payload_json=json.dumps(payload, ensure_ascii=False, indent=2),
|
||||
result=None,
|
||||
@@ -2309,12 +2371,14 @@ async def prompt_test_run(
|
||||
csrf_token: str = Form(...),
|
||||
post_id: int = Form(...),
|
||||
prompt: str = Form(...),
|
||||
q_status: str = Form("all"),
|
||||
):
|
||||
user = await get_current_user(request)
|
||||
if not user:
|
||||
return redirect("/login")
|
||||
require_csrf(user, csrf_token)
|
||||
posts, selected = await prompt_test_posts(post_id)
|
||||
q_status = normalize_prompt_test_status(q_status)
|
||||
posts, selected = await prompt_test_posts(post_id, q_status)
|
||||
categories = await writer_categories()
|
||||
max_text_chars = max(200, int(await fetch_setting("ai_writer_max_text_chars", 3500) or 3500))
|
||||
provider = str(await fetch_setting("ai_writer_provider", "anthropic") or "anthropic")
|
||||
@@ -2367,6 +2431,9 @@ async def prompt_test_run(
|
||||
result = {
|
||||
"content": content,
|
||||
"parsed_json": json.dumps(parsed, ensure_ascii=False, indent=2) if parsed is not None else "",
|
||||
"preview_text": prompt_test_preview_text(parsed),
|
||||
"preview_category": prompt_test_preview_category(parsed),
|
||||
"preview_notes": prompt_test_preview_notes(parsed),
|
||||
"usage": response_usage(response),
|
||||
"model": kwargs["model"],
|
||||
}
|
||||
@@ -2384,6 +2451,8 @@ async def prompt_test_run(
|
||||
prompt=normalized_prompt,
|
||||
provider=provider,
|
||||
model=model,
|
||||
q_status=q_status,
|
||||
status_options=PROMPT_TEST_STATUS_OPTIONS,
|
||||
max_text_chars=max_text_chars,
|
||||
payload_json=json.dumps(payload, ensure_ascii=False, indent=2),
|
||||
result=result,
|
||||
|
||||
Reference in New Issue
Block a user