1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
| """
W6-D4:LangGraph HITL
三态审批 + 改参 + 审计留痕 + 结构收口
学习目标:
1. interrupt()
2. Command(resume=...)
3. update_state()
4. tool_calls 改参
5. audit
6. 无工具 summarize
"""
import os
from dotenv import load_dotenv
load_dotenv(override=True)
from typing import Annotated, TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langgraph.checkpoint.memory import MemorySaver
from langgraph.prebuilt import ToolNode
from langgraph.types import interrupt, Command
from langchain_core.tools import tool
from langchain_core.messages import AIMessage, HumanMessage
from langchain_openai import ChatOpenAI
# ============================================================
# 1. 工具
# ============================================================
@tool
def delete_file(path: str) -> str:
"""
演示工具。
不真的删除文件,只打印执行结果。
"""
print(f">>> 工具真身执行:删除 {path}")
return f"文件已删除:{path}"
# ============================================================
# 2. State
# ============================================================
class S(TypedDict):
# 对话历史
#
# add_messages 会负责消息的追加 / 更新。
messages: Annotated[list, add_messages]
# 普通字段:
# 专门记录审批人的操作。
audit_note: str
# ============================================================
# 3. 两个 LLM
# ============================================================
_common = {
"model": os.getenv(
"DEEPSEEK_MODEL",
"deepseek-v4-flash",
),
"api_key": os.getenv(
"DEEPSEEK_API_KEY",
),
"base_url": os.getenv(
"DEEPSEEK_BASE_URL",
"https://api.deepseek.com",
),
"temperature": 0,
}
# 决策模型:
# 有工具。
llm_decide = ChatOpenAI(
**_common
).bind_tools(
[delete_file]
)
# 收口模型:
# 没有工具。
#
# 这样最后只能总结,
# 不能再次产生 tool_calls。
llm_summarize = ChatOpenAI(
**_common
)
# ============================================================
# 4. decide
# ============================================================
def decide(state: S) -> dict:
"""
决策官。
只负责:
让模型决定要不要调用工具。
注意:
这里不能先 interrupt,
因为 AIMessage 需要先 return,
才会真正进入 state。
"""
ai_msg = llm_decide.invoke(
state["messages"]
)
print(
"[decide] tool_calls =",
len(ai_msg.tool_calls),
)
return {
"messages": [ai_msg]
}
# ============================================================
# 5. approve
# ============================================================
def approve(state: S) -> dict:
"""
审批官。
读取上一条 AIMessage,
找到模型准备执行的 tool_call,
然后问人。
"""
last = state["messages"][-1]
if not (
isinstance(last, AIMessage)
and last.tool_calls
):
return {}
tc = last.tool_calls[0]
print(
"[approve] 待审批参数:",
tc["args"],
)
# interrupt 第一次运行会暂停。
#
# resume 后这个节点会重新执行,
# 再次来到这里时,
# interrupt() 会直接返回 resume 值。
answer = interrupt(
{
"question": (
f"模型想删除 "
f"{tc['args']['path']},"
f"批准吗?"
),
"options": [
"approve",
"reject",
"edit",
],
}
)
# reject:
# 返回一条没有 tool_calls 的消息。
#
# 后面路由看到没有 tool_calls,
# 就会 END。
if answer == "reject":
return {
"messages": [
AIMessage(
content=(
"已取消删除,"
"本次未执行任何操作。"
)
)
]
}
# approve / edit:
#
# 两者都直接放行。
#
# edit 真正修改参数的动作,
# 已经在图外 update_state() 完成。
return {}
# ============================================================
# 6. audit
# ============================================================
def audit(state: S) -> dict:
"""
把人工修改重新注入消息历史。
解决:
“为什么实际参数和原来不一样?”
"""
note = state.get(
"audit_note",
"",
)
if not note:
return {}
print(
"[audit]",
note,
)
return {
"messages": [
HumanMessage(
content=(
f"【审批人操作记录】{note}。"
"这是审批人主动、有意做出的决定,"
"已获批准执行。"
"这不是错误,无需重试,"
"请直接总结实际结果。"
)
)
],
# 用完清空
"audit_note": "",
}
# ============================================================
# 7. summarize
# ============================================================
def summarize(state: S) -> dict:
"""
收口。
这里故意使用没有绑定工具的 LLM。
"""
ai_msg = llm_summarize.invoke(
state["messages"]
)
print(
"[summarize] tool_calls = 0"
)
return {
"messages": [ai_msg]
}
# ============================================================
# 8. decide 后的路由
# ============================================================
def route_after_decide(state: S):
last = state["messages"][-1]
if (
isinstance(last, AIMessage)
and last.tool_calls
):
return "approve"
return END
# ============================================================
# 9. approve 后的路由
# ============================================================
def route_after_approve(state: S):
last = state["messages"][-1]
if (
isinstance(last, AIMessage)
and last.tool_calls
):
return "tools"
return END
# ============================================================
# 10. 构图
# ============================================================
builder = StateGraph(S)
builder.add_node(
"decide",
decide,
)
builder.add_node(
"approve",
approve,
)
builder.add_node(
"tools",
ToolNode([delete_file]),
)
builder.add_node(
"audit",
audit,
)
builder.add_node(
"summarize",
summarize,
)
# START → decide
builder.add_edge(
START,
"decide",
)
# decide → approve / END
builder.add_conditional_edges(
"decide",
route_after_decide,
{
"approve": "approve",
END: END,
},
)
# approve → tools / END
builder.add_conditional_edges(
"approve",
route_after_approve,
{
"tools": "tools",
END: END,
},
)
# tools → audit → summarize → END
builder.add_edge(
"tools",
"audit",
)
builder.add_edge(
"audit",
"summarize",
)
builder.add_edge(
"summarize",
END,
)
# ============================================================
# 11. 编译
# ============================================================
graph = builder.compile(
checkpointer=MemorySaver()
)
# ============================================================
# 12. C 场景:edit
# ============================================================
cfg = {
"configurable": {
"thread_id": "w6-d4-edit",
}
}
# ------------------------------------------------------------
# 第一次运行
#
# 图会走到:
#
# START
# ↓
# decide
# ↓
# approve
# ↓
# interrupt
#
# 然后暂停。
# ------------------------------------------------------------
graph.invoke(
{
"messages": [
(
"user",
"帮我删除 /tmp/a.txt",
)
],
"audit_note": "",
},
cfg,
)
# ============================================================
# 13. 查看暂停时的 state
# ============================================================
st = graph.get_state(cfg)
print("\n当前 next =", st.next)
print(
"当前消息数量 =",
len(st.values["messages"]),
)
last = st.values["messages"][-1]
print(
"当前 tool_calls =",
last.tool_calls,
)
# ============================================================
# 14. 修改 pending tool_call
# ============================================================
new_tool_calls = [
{
**tc,
# 只修改 path,
# 其它参数保留。
"args": {
**tc["args"],
"path": "/tmp/old.txt",
},
}
for tc in last.tool_calls
]
# ------------------------------------------------------------
# ★ 关键:
# 必须保留原来的 message id。
#
# 表示:
# “更新刚才那条消息”
#
# 而不是:
# “新增一条消息”
# ------------------------------------------------------------
fixed = AIMessage(
id=last.id,
content=last.content,
tool_calls=new_tool_calls,
)
# ============================================================
# 15. 改档 + 写审计
# ============================================================
graph.update_state(
cfg,
{
"messages": [fixed],
"audit_note": (
"审批人把删除目标从 "
"/tmp/a.txt 改成了 "
"/tmp/old.txt"
),
},
)
# ============================================================
# 16. resume
# ============================================================
#
# edit 已经体现在 state 里。
#
# resume("edit") 只是告诉 approve:
# “人的答案是 edit。”
#
# 然后 approve 放行。
# ============================================================
result = graph.invoke(
Command(
resume="edit"
),
cfg,
)
print(
"\n最终结果:",
result["messages"][-1].content,
)
# ============================================================
# 17. 最终验收
# ============================================================
final_state = graph.get_state(cfg)
print(
"\n最终 next =",
final_state.next,
)
print(
"最后一条 tool_calls =",
getattr(
final_state.values["messages"][-1],
"tool_calls",
None,
),
)
# 理论上的最终状态应该:
#
# next == ()
#
# 表示图结束。
|