fix: 改进覆盖反馈重试 - Closes #21 #26
@@ -536,17 +536,24 @@ def _quick_validate(
|
|||||||
|
|
||||||
|
|
||||||
def _build_coverage_feedback(gaps: dict) -> str:
|
def _build_coverage_feedback(gaps: dict) -> str:
|
||||||
"""Generate feedback text for re-prompting when coverage is below threshold."""
|
"""Generate targeted feedback text for re-prompting when coverage is below threshold."""
|
||||||
lines = []
|
parts = []
|
||||||
for item in gaps.get("missing_paths", []):
|
for item in gaps.get("missing_paths", []):
|
||||||
lines.append(f"- {item}")
|
parts.append(f"- {item}")
|
||||||
if lines:
|
if not parts:
|
||||||
return (
|
return ""
|
||||||
"\n## 覆盖反馈(上一次运行未满足覆盖要求,请重新生成并修复以下缺口)\n\n"
|
|
||||||
+ "\n".join(lines)
|
return (
|
||||||
+ "\n\n请重新审视文档,为缺失的章节和表格行创建对应的 function_unit。"
|
"\n## 关键覆盖反馈(上一轮 LLM 输出了以下缺口,请重新处理)\n\n"
|
||||||
)
|
+ "\n".join(parts)
|
||||||
return ""
|
+ "\n\n"
|
||||||
|
"### 修复动作(必须执行)\n\n"
|
||||||
|
"1. **重新扫描上述每个缺失章节**,从文字和表格中提取所有可被测试的功能行为\n"
|
||||||
|
"2. **为每个缺失的表格行创建独立的 function_unit**,不得合并不同行的规则\n"
|
||||||
|
"3. **每个 function_unit 必须引用具体的 section 号和 row 号**作为 source\n"
|
||||||
|
"4. **非功能章节可以跳过**(如背景、术语、变更日志),但行为规则章节必须覆盖\n"
|
||||||
|
"5. 输出中必须包含针对上述缺口的新 function_unit\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _collect_logic_tree_nodes(doc: dict) -> dict[str, dict[str, str]]:
|
def _collect_logic_tree_nodes(doc: dict) -> dict[str, dict[str, str]]:
|
||||||
@@ -767,13 +774,22 @@ def run_ensemble_semantic_index(doc: dict) -> dict:
|
|||||||
# Feedback retry: re-run with coverage feedback (one retry)
|
# Feedback retry: re-run with coverage feedback (one retry)
|
||||||
feedback = _build_coverage_feedback(gaps)
|
feedback = _build_coverage_feedback(gaps)
|
||||||
if feedback:
|
if feedback:
|
||||||
print(f"\n 覆盖反馈重试...")
|
print(f"\n 覆盖反馈重试 (feedback长度={len(feedback)}字符)...", flush=True)
|
||||||
try:
|
try:
|
||||||
retry_prompt = build_prompt(doc, feedback, all_paths)
|
retry_prompt = build_prompt(doc, feedback, all_paths)
|
||||||
retry_result = call_llm(retry_prompt, max_retries=1, temperature=0.0)
|
print(f" 重试 prompt 长度: {len(retry_prompt)} 字符", flush=True)
|
||||||
|
retry_result = call_llm(retry_prompt, max_retries=1, temperature=0.3)
|
||||||
n_retry_units = len(retry_result.get("function_units", []))
|
n_retry_units = len(retry_result.get("function_units", []))
|
||||||
print(f" 重试返回: {n_retry_units} 功能单元")
|
n_retry_concepts = len(retry_result.get("concepts", []))
|
||||||
|
print(f" 重试返回: {n_retry_concepts} 概念, {n_retry_units} 功能单元", flush=True)
|
||||||
if n_retry_units > 0:
|
if n_retry_units > 0:
|
||||||
|
# Check which new sections were covered
|
||||||
|
retry_sections = set()
|
||||||
|
for fu in retry_result.get("function_units", []):
|
||||||
|
for src in fu.get("sources", []):
|
||||||
|
if src.get("section"):
|
||||||
|
retry_sections.add(src["section"])
|
||||||
|
print(f" 重试新增 sections: {sorted(retry_sections)}", flush=True)
|
||||||
# Merge retry into results and re-validate
|
# Merge retry into results and re-validate
|
||||||
semantic_indices.append(retry_result)
|
semantic_indices.append(retry_result)
|
||||||
merged = ensemble_merge(semantic_indices)
|
merged = ensemble_merge(semantic_indices)
|
||||||
@@ -783,9 +799,11 @@ def run_ensemble_semantic_index(doc: dict) -> dict:
|
|||||||
merged["validation_gaps"] = {
|
merged["validation_gaps"] = {
|
||||||
k: v for k, v in gaps.items() if v
|
k: v for k, v in gaps.items() if v
|
||||||
}
|
}
|
||||||
print(f" 重试后验证: {'PASS' if passed else 'GAPS FOUND'}")
|
print(f" 重试后验证: {'PASS' if passed else 'GAPS FOUND'}", flush=True)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f" 覆盖反馈重试失败: {e}")
|
print(f" 覆盖反馈重试失败: {e}", flush=True)
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
return merged
|
return merged
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user