当使用基于模型决策的非确定性系统(例如,由 LLM 驱动的代理)时,详细检查其决策过程会很有用:🔍探索替代方案:测试不同的路径以发现更好的解决方案。我们将这些调试技术称为time-trave(时间旅行),由两个关键操作组成:
重放功能允许我们重新访问和重现代理的过去操作。这可以从图的当前状态(或检查点)或特定检查点完成。要从当前状态重播,只需将 None 作为输入与thread id一起传递:thread = {"configurable": {"thread_id": "1"}}
for event in graph.stream(None, thread, stream_mode="values"):
print(event)
要从特定检查点重播操作,请首先检索线程的所有检查点:all_checkpoints = []
for state in graph.get_state_history(thread):
all_checkpoints.append(state)
每个检查点都有一个唯一的 ID。识别所需的检查点(例如 xyz)后,将其 ID 包含在配置中:config = {'configurable': {'thread_id': '1', 'checkpoint_id': 'xyz'}}
for event in graph.stream(None, config, stream_mode="values"):
print(event)
graph 有效地重播先前执行的节点而不是重新执行它们,利用其对先前检查点执行的感知。
forKing允许重新访问agent的过去操作并探索图中的替代路径。要编辑特定检查点(例如 xyz),请在更新图形状态时提供其 checkpoint_id:config = {"configurable": {"thread_id": "1", "checkpoint_id": "xyz"}}
graph.update_state(config, {"state": "updated state"})
这将创建一个新的分叉检查点 xyz-fork,我们可以从该检查点继续运行graph:config = {'configurable': {'thread_id': '1', 'checkpoint_id': 'xyz-fork'}}
for event in graph.stream(None, config, stream_mode="values"):
print(event)