Files
ZNJJ-api-server/examples/langgraph_smoke.py
T
Eric Wang 5020903791 feat(agent): add LangGraph scaffold for assignment 1
- src/agent/state.py: minimal five-field AgentState
- src/agent/graph.py: START -> normalize_input -> reply -> END,
  compiled with InMemorySaver; reply emits <state>1002</state> prefix
- examples/langgraph_smoke.py: offline smoke, turns 1/2/1 verified
- test/agent/test_scaffold.py: turn accumulation, thread isolation,
  fixed-template assertions (3 passed)
- requirements.txt: pin langgraph==1.2.9

Known limit: InMemorySaver state disappears on process exit.
2026-10-19 13:32:43 +08:00

39 lines
1.2 KiB
Python

"""LangGraph 脚手架冒烟脚本(作业 1)。
同一 thread 输入两次、另一个 thread 输入一次,打印轮数和文本。
预期输出轮数依次为 1、2、1。
本脚本不导入 FastGPT、不读取其密钥;离线即可运行:
python examples/langgraph_smoke.py
"""
import asyncio
from src.agent.graph import build_graph
async def main() -> None:
graph = build_graph()
async def ask(thread_id: str, text: str) -> None:
# 只传本轮变化的字段;会话字段(turn_count 等)由 checkpoint 保管。
# 注意:对已有 checkpoint 的 thread 传入完整初始状态,
# 其中的 turn_count=0 会覆盖已累计的值。
result = await graph.ainvoke(
{"session_id": thread_id, "input_text": text},
config={"configurable": {"thread_id": thread_id}},
)
print(
f"thread={thread_id} turn_count={result['turn_count']} "
f"stage_code={result['stage_code']} text={result['response_text']}"
)
await ask("thread-a", "你好")
await ask("thread-a", "发生了交通事故")
await ask("thread-b", "你好")
if __name__ == "__main__":
asyncio.run(main())