| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import asyncio
- from langgraph.graph import StateGraph, START, END
- from langgraph.checkpoint.memory import MemorySaver
- from langgraph.types import Command # 恢复运行
- from backend.agents.bid_review.state import BidReviewState
- from backend.agents.bid_review.nodes import expert_review_node
- # 公共基础 state(expert_review_node 用到的所有字段)
- BASE_STATE = {
- "submission_id": "sub-test-001",
- "bidder_id": "stu-test-001",
- "pre_review_summary": {
- "by_question": [
- {"question_id": "q-001", "score": 7, "comment": "基本正确"},
- {"question_id": "q-002", "score": 5, "comment": "答案不完整"},
- ],
- "total_score": 12,
- },
- "weak_points": ["递归算法", "时间复杂度分析"],
- "weak_points_summary": "投标人对递归理解薄弱,建议补充练习",
- }
- def _build_review_graph():
- """构造只含 expert_review_node 的最小测试图"""
- builder = StateGraph(BidReviewState)
- builder.add_node("expert_review", expert_review_node)
- builder.add_edge(START, "expert_review")
- builder.add_edge("expert_review", END)
- return builder.compile(checkpointer=MemorySaver())
- # ── 用例①:评审专家直接批准 AI 结果 ─────────────────────────────
- async def atest_review_approve():
- print("\n" + "=" * 55)
- print("【expert_review_node】用例① approve")
- print("=" * 55)
- graph = _build_review_graph()
- config = {"configurable": {"thread_id": "test-approve-001"}}
- # 第一次 invoke:图在 interrupt() 处暂停,返回 display_data
- snapshot = await graph.ainvoke(BASE_STATE, config=config)
- print(f"图已暂停,展示给评审专家的数据: {snapshot}")
- # 评审专家选择"批准",不修改任何评分项
- decision = {
- "action": "approve",
- "modifications": [],
- "expert_id": "expert-001",
- }
- final = await graph.ainvoke(Command(resume=decision), config=config)
- print("*"*80)
- print(f'final: {final}')
- print("*" * 80)
- print(f"expert_decision: {final['expert_decision']}")
- print("✅ approve 用例通过")
- # ── 用例②:专家修改部分评分项分数 ─────────────────────────────
- async def atest_review_modify():
- print("\n" + "=" * 55)
- print("【expert_review_node】用例② modify(修改 q-002 分数)")
- print("=" * 55)
- graph = _build_review_graph()
- config = {"configurable": {"thread_id": "test-modify-001"}}
- await graph.ainvoke(BASE_STATE, config=config)
- # 专家修改 q-002 分数,同时追加评语;q-001 不动
- decision = {
- "action": "modify",
- "modifications": [
- {
- "question_id": "q-002",
- "new_score": 8,
- "comment": "重新审阅,核心逻辑正确,调高分数",
- }
- ],
- "expert_id": "expert-001",
- }
- final = await graph.ainvoke(Command(resume=decision), config=config)
- print(f"final: {final}")
- print("*" * 80)
- td = final["expert_decision"]
- print(f"expert_decision: {td}")
- print("✅ modify 用例通过")
- if __name__ == '__main__':
- import asyncio
- # asyncio.run(atest_review_approve())
- asyncio.run(atest_review_modify())
|