feat: AIPlanner JSON 파싱 실패 시 재시도 로직 개선

- JSON 파싱 실패 감지 시, 다음 재시도에 `JSON-only repair` 프롬프트를 추가하여 모델이 스키마를 준수하도록 유도하는 로직을 구현
- README.md에 JSON 파싱 실패 처리 및 재시도 로직에 대한 설명 추가
This commit is contained in:
kswdev0
2026-03-26 11:48:44 +09:00
parent ace5d63480
commit 3e90090b5e
2 changed files with 25 additions and 3 deletions

View File

@@ -189,7 +189,7 @@ class AIPlanner:
self.step += 1
feedback_text = self._format_feedback()
user_message = (
user_message_base = (
f"## 스텝 {self.step}\n\n"
f"### 현재 게임 상태\n{state_summary}\n\n"
f"{feedback_text}"
@@ -202,9 +202,12 @@ class AIPlanner:
print(f"\n[GLM] 생각 중...")
# JSON 스키마를 위반한 경우(예: 분석 텍스트만 반환)에는 재시도 프롬프트를 강화한다.
repair_suffix = ""
for attempt in range(3):
try:
raw = self._call_glm(user_message, attempt=attempt)
raw = self._call_glm(user_message_base + repair_suffix, attempt=attempt)
plan = self._parse_json(raw)
break
except (
@@ -217,6 +220,25 @@ class AIPlanner:
) as e:
detail = describe_glm_exception(e)
if attempt < 2:
if isinstance(e, ValueError) and (
str(e).startswith("JSON 파싱 실패")
or "JSON 파싱 실패" in str(e)
or "actions 스키마" in str(e)
):
repair_suffix = (
"\n\n[중요] 이전 응답은 JSON 요구사항을 위반했습니다.\n"
"지금은 아래 스키마의 JSON 객체만 '그대로' 반환하세요.\n"
"마크다운/설명 금지.\n"
"키 이름과 구문은 동일해야 합니다.\n"
"{"
"\"thinking\":\"\","
"\"current_goal\":\"\","
"\"actions\":[{\"action\":\"explore\",\"params\":{},\"reason\":\"\"}],"
"\"after_this\":\"\""
"}"
)
if _glm_debug_enabled():
print("[GLM][디버그] JSON-only repair 프롬프트 적용")
print(
f"[경고] GLM 처리 실패 (시도 {attempt+1}/3): "
f"{type(e).__name__} 재시도..."