fix(analyze): don't hard-fail when LLM generates <15 personas (retry + accept)
Live test found: deepseek returned 14/15 personas -> whole analyze 500'd, breaking the 'กดสร้าง -> auto-analyze' flow. Now generate() retries up to 3x with a nudge, and accepts a short result (>=8 personas) instead of crashing — admin can top up the rest with 'สร้างบุคคลต้นแบบเพิ่มเติม'. Also default tolerance/recontact on generated personas. All backend suites pass.
This commit is contained in:
@@ -31,10 +31,23 @@ class PersonaGenerator:
|
|||||||
f"Sales Kit:\n{kit_json}\n\n"
|
f"Sales Kit:\n{kit_json}\n\n"
|
||||||
f"Generate exactly 15 personas (5 per tier A/B/C) as JSON."
|
f"Generate exactly 15 personas (5 per tier A/B/C) as JSON."
|
||||||
)
|
)
|
||||||
result = self.llm.complete_json(
|
# Real LLMs sometimes return fewer than 15 (truncation / merge). Retry up to 2 extra
|
||||||
PERSONA_SYSTEM, user_prompt, temperature=0.8, max_tokens=14000
|
# times with a nudge; the body below already ACCEPTS short results (>= 8) instead of
|
||||||
)
|
# hard-failing, so these retries are just best-effort to reach a fuller set.
|
||||||
personas = result.get("personas") or []
|
attempt = 0
|
||||||
|
while True:
|
||||||
|
attempt += 1
|
||||||
|
prompt = user_prompt + (
|
||||||
|
""
|
||||||
|
if attempt == 1
|
||||||
|
else "\n\n(Note: you left some personas out — please output all 15, one JSON object per persona, no extra prose.)"
|
||||||
|
)
|
||||||
|
result = self.llm.complete_json(
|
||||||
|
PERSONA_SYSTEM, prompt, temperature=0.8, max_tokens=14000
|
||||||
|
)
|
||||||
|
personas = result.get("personas") or []
|
||||||
|
if (isinstance(personas, list) and len(personas) >= 15) or attempt >= 3:
|
||||||
|
break
|
||||||
if not isinstance(personas, list) or not personas:
|
if not isinstance(personas, list) or not personas:
|
||||||
raise ValueError("persona generator returned no personas")
|
raise ValueError("persona generator returned no personas")
|
||||||
|
|
||||||
@@ -57,6 +70,8 @@ class PersonaGenerator:
|
|||||||
p.setdefault("pains", [])
|
p.setdefault("pains", [])
|
||||||
p.setdefault("negotiation_levers", [])
|
p.setdefault("negotiation_levers", [])
|
||||||
p.setdefault("objections", [])
|
p.setdefault("objections", [])
|
||||||
|
p.setdefault("tolerance", 3)
|
||||||
|
p.setdefault("recontact", False)
|
||||||
normalized.append(p)
|
normalized.append(p)
|
||||||
|
|
||||||
# Wrap tier-C: ensure at least one wrong_text persona
|
# Wrap tier-C: ensure at least one wrong_text persona
|
||||||
@@ -69,6 +84,9 @@ class PersonaGenerator:
|
|||||||
p["special"] = "wrong_text"
|
p["special"] = "wrong_text"
|
||||||
break
|
break
|
||||||
|
|
||||||
if len(normalized) < 15:
|
if len(normalized) < 8:
|
||||||
raise ValueError(f"expected 15 personas, generated {len(normalized)}")
|
raise ValueError(f"expected ~15 personas, generated only {len(normalized)}")
|
||||||
|
# NOTE: if we're short of 15 (real LLMs occasionally return 14/13), we ACCEPT what we
|
||||||
|
# got rather than crashing the whole analyze — the caller/UI can top up with
|
||||||
|
# "create more personas". A retry loop lives in generate().
|
||||||
return normalized
|
return normalized
|
||||||
|
|||||||
Reference in New Issue
Block a user