2026-10-21 15:11:16 +08:00
|
|
|
"""LangGraph 脚手架冒烟脚本。
|
2026-10-19 13:32:43 +08:00
|
|
|
|
|
|
|
|
同一 thread 输入两次、另一个 thread 输入一次,打印轮数和文本。
|
2026-10-21 15:11:16 +08:00
|
|
|
预期轮数依次为 1、2、1;首轮为 welcome(1001 固定问候,不调模型),
|
|
|
|
|
第二轮起由注入的 workflow fake LLM 按提示词语义回复。
|
2026-10-19 13:32:43 +08:00
|
|
|
|
|
|
|
|
本脚本不导入 FastGPT、不读取其密钥;离线即可运行:
|
|
|
|
|
|
2026-10-21 15:11:16 +08:00
|
|
|
python -m examples.langgraph_smoke
|
2026-10-19 13:32:43 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
|
2026-10-21 15:11:16 +08:00
|
|
|
from src.agent.graph import GraphDeps, build_graph, workflow_fake_llm
|
2026-10-19 13:32:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def main() -> None:
|
|
|
|
|
graph = build_graph()
|
2026-10-21 15:11:16 +08:00
|
|
|
deps = GraphDeps(llm=workflow_fake_llm())
|
2026-10-19 13:32:43 +08:00
|
|
|
|
|
|
|
|
async def ask(thread_id: str, text: str) -> None:
|
2026-10-21 15:11:16 +08:00
|
|
|
# 只传本轮变化的字段;会话字段(turn_count/stage_code 等)
|
|
|
|
|
# 由 checkpoint 保管。注意:对已有 checkpoint 的 thread 传
|
|
|
|
|
# 完整初始状态会覆盖已累计的值。
|
2026-10-19 13:32:43 +08:00
|
|
|
result = await graph.ainvoke(
|
|
|
|
|
{"session_id": thread_id, "input_text": text},
|
|
|
|
|
config={"configurable": {"thread_id": thread_id}},
|
2026-10-21 15:11:16 +08:00
|
|
|
context=deps,
|
2026-10-19 13:32:43 +08:00
|
|
|
)
|
|
|
|
|
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())
|