fix(frontend): 修复假说流式解析字典结构导致界面卡顿的问题并支持多候选假说切换
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import sys
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
root_dir = Path(__file__).resolve().parent.parent
|
||||
if str(root_dir) not in sys.path:
|
||||
sys.path.insert(0, str(root_dir))
|
||||
|
||||
from src.utils.parser import extract_hypotheses_from_text, parse_json_robust
|
||||
from src.skills.hypothesis_generator import HypothesisGeneratorSkill
|
||||
|
||||
# 测试用例 1:用户真实日志中的 dict 格式
|
||||
user_raw_dict = """
|
||||
{
|
||||
"hypotheses": [
|
||||
{
|
||||
"id": "H1",
|
||||
"hypothesis_statement": "基于高维拓扑同调群的多智能体推理轨迹流形校验机制,能够有效识别并剔除多步科学推理中的群体性逻辑幻觉,从而构建高鲁棒性的自一致性科研闭环。",
|
||||
"rationale": "在开放和动态的科研环境中,多智能体系统容易陷入‘群体幻觉’(即多个智能体对同一错误逻辑产生虚假共识)。",
|
||||
"evidence_chain": [
|
||||
"Agentic Reasoning for Large Language Models (10.48550/arXiv.2601.12538v1)",
|
||||
"Large Language Model Reasoning Failures (10.48550/arXiv.2602.06176v1)"
|
||||
],
|
||||
"falsifiable_conditions": "在标准复杂科学推理基准(如GPQA或自动化定理证明)中,引入拓扑同调校验机制后,多智能体系统的推理准确率未显著高于无拓扑校验的基线模型。",
|
||||
"novelty_score": 9
|
||||
},
|
||||
{
|
||||
"id": "H2",
|
||||
"hypothesis_statement": "利用部分二值化(PB-LLM)作为‘逻辑探针’进行对比解码...",
|
||||
"rationale": "LLM在多步推理中的幻觉往往源于对训练数据中统计捷径的过度拟合...",
|
||||
"evidence_chain": ["PB-LLM (10.48550/arXiv.2310.00034v2)"],
|
||||
"falsifiable_conditions": "在长链条科学推理任务中...",
|
||||
"novelty_score": 8
|
||||
},
|
||||
{
|
||||
"id": "H3",
|
||||
"hypothesis_statement": "基于因果可解释性图谱的逻辑熵动态监测...",
|
||||
"rationale": "科研发现闭环的核心在于持续的自我纠错...",
|
||||
"evidence_chain": ["ReasoningRec (10.48550/arXiv.2410.23180v1)"],
|
||||
"falsifiable_conditions": "在长周期科研模拟任务中...",
|
||||
"novelty_score": 9
|
||||
}
|
||||
],
|
||||
"recommended_search_queries": []
|
||||
}
|
||||
"""
|
||||
|
||||
# 测试用例 2:markdown 包裹的代码块
|
||||
user_raw_md = f"```json\n{user_raw_dict}\n```"
|
||||
|
||||
# 测试用例 3:纯数组格式
|
||||
user_raw_list = json.dumps([
|
||||
{"id": "H1", "hypothesis_statement": "假说1", "novelty_score": 9}
|
||||
])
|
||||
|
||||
def test_parsing():
|
||||
print("=== 测试 1: Dict 结构 ===")
|
||||
hypos1 = extract_hypotheses_from_text(user_raw_dict, "科学难题测试")
|
||||
print(f"提取假说数量: {len(hypos1)}")
|
||||
assert len(hypos1) == 3, f"Expected 3, got {len(hypos1)}"
|
||||
assert hypos1[0]["id"] == "H1"
|
||||
print(f"H1 假说内容: {hypos1[0]['hypothesis_statement'][:30]}...")
|
||||
|
||||
print("\n=== 测试 2: Markdown 代码块 ===")
|
||||
hypos2 = extract_hypotheses_from_text(user_raw_md, "科学难题测试")
|
||||
print(f"提取假说数量: {len(hypos2)}")
|
||||
assert len(hypos2) == 3
|
||||
|
||||
print("\n=== 测试 3: List 结构 ===")
|
||||
hypos3 = extract_hypotheses_from_text(user_raw_list, "科学难题测试")
|
||||
print(f"提取假说数量: {len(hypos3)}")
|
||||
assert len(hypos3) == 1
|
||||
|
||||
print("\n=== 测试 4: 异常容错保底 ===")
|
||||
hypos4 = extract_hypotheses_from_text("Invalid text error", "科学难题测试")
|
||||
print(f"提取假说数量: {len(hypos4)}")
|
||||
assert len(hypos4) >= 1
|
||||
assert "hypothesis_statement" in hypos4[0]
|
||||
|
||||
print("\n所有解析测试全部通过!✅")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_parsing()
|
||||
Reference in New Issue
Block a user