refactor(agent): inject deps via context_schema, LLM-driven photo/confirm nodes
- graph.py: LangGraph-native DI - StateGraph(context_schema=GraphDeps), nodes take (state, runtime: Runtime[GraphDeps]); build_graph() decoupled from LLM; deleted scaffold _event_reply/_fake_llm_propose/stub nodes - photo.py: model proposes stage+body via per-mode prompts (direct/addon); validator enforces clientMode exits; deterministic event forcing removed - confirm.py: single runtime for single/dual confirm, extraction call isolated per person_index - llm.py: workflow_fake_llm() (offline workflow-faithful responder, history-aware) for smoke/tests - backend: LangGraphBackend(graph, llm) passes context per call - tests: invoke helpers inject GraphDeps; illegal-proposal test asserts error + state untouched via second well-behaved fake Verified: 143 passed; smoke turns 1/2/1 with original welcome text
This commit is contained in:
@@ -1,28 +1,31 @@
|
||||
"""LangGraph 脚手架冒烟脚本(作业 1)。
|
||||
"""LangGraph 脚手架冒烟脚本。
|
||||
|
||||
同一 thread 输入两次、另一个 thread 输入一次,打印轮数和文本。
|
||||
预期输出轮数依次为 1、2、1。
|
||||
预期轮数依次为 1、2、1;首轮为 welcome(1001 固定问候,不调模型),
|
||||
第二轮起由注入的 workflow fake LLM 按提示词语义回复。
|
||||
|
||||
本脚本不导入 FastGPT、不读取其密钥;离线即可运行:
|
||||
|
||||
python examples/langgraph_smoke.py
|
||||
python -m examples.langgraph_smoke
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
from src.agent.graph import build_graph
|
||||
from src.agent.graph import GraphDeps, build_graph, workflow_fake_llm
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
graph = build_graph()
|
||||
deps = GraphDeps(llm=workflow_fake_llm())
|
||||
|
||||
async def ask(thread_id: str, text: str) -> None:
|
||||
# 只传本轮变化的字段;会话字段(turn_count 等)由 checkpoint 保管。
|
||||
# 注意:对已有 checkpoint 的 thread 传入完整初始状态,
|
||||
# 其中的 turn_count=0 会覆盖已累计的值。
|
||||
# 只传本轮变化的字段;会话字段(turn_count/stage_code 等)
|
||||
# 由 checkpoint 保管。注意:对已有 checkpoint 的 thread 传
|
||||
# 完整初始状态会覆盖已累计的值。
|
||||
result = await graph.ainvoke(
|
||||
{"session_id": thread_id, "input_text": text},
|
||||
config={"configurable": {"thread_id": thread_id}},
|
||||
context=deps,
|
||||
)
|
||||
print(
|
||||
f"thread={thread_id} turn_count={result['turn_count']} "
|
||||
|
||||
Reference in New Issue
Block a user