AI创想

标题: 【LangGraph】使用 LangGraph 的 Pregel 运行时日志 [打印本页]

作者: 创想小编    时间: 昨天 09:22
标题: 【LangGraph】使用 LangGraph 的 Pregel 运行时日志
作者:彬彬侠
使用 LangGraph 时,Pregel 作为其底层计算模型,驱动图的执行,而运行时日志(Runtime Logging)是调试和监控 LangGraph 工作流的重要工具。LangGraph 提供了灵活的日志机制,帮助开发者跟踪节点执行、状态更新、消息传递等信息。以下是详细介绍如何使用 LangGraph 的 Pregel 运行时日志,结合 Pregel 的执行机制,确保解答准确、清晰、全面,并以中文呈现。

1. LangGraph 和 Pregel 的运行时日志背景

LangGraph 基于 Pregel 模型运行图工作流,每个超步(Superstep)执行一组节点,更新状态,并通过边传递控制流。运行时日志可以帮助:
LangGraph 的日志通常依赖 Python 的日志模块(logging)或 LangChain 生态的回调机制(Callbacks)。Pregel 的运行时日志记录了每个超步的节点执行、状态更新和条件跳转等信息。

2. 设置 LangGraph 运行时日志的步骤

以下是使用 LangGraph 的 Pregel 运行时日志的详细步骤,涵盖基本配置、自定义日志和高级用法。
步骤 1:配置 Python 日志模块

LangGraph 使用 Python 的 logging 模块记录运行时信息。你需要确保日志级别和输出格式正确配置。
示例代码
  1. import logging
  2. # 配置日志
  3. logging.basicConfig(
  4.     level=logging.INFO,# 设置日志级别(DEBUG、INFO、WARNING、ERROR)format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
  5.     handlers=[
  6.         logging.StreamHandler()# 输出到控制台# 可添加 logging.FileHandler("logfile.log") 输出到文件])# 获取 LangGraph 的日志器
  7. logger = logging.getLogger("langgraph")
复制代码
说明
步骤 2:构建 LangGraph 工作流

假设你有一个简单的 LangGraph 工作流,包含状态和节点。我们将为其添加日志。
示例代码
  1. from typing import TypedDict, Annotated
  2. from langgraph.graph import StateGraph
  3. from pydantic import Field
  4. # 定义状态classState(TypedDict):
  5.     user_input: Annotated[str, Field(description="User's input")]
  6.     response: Annotated[str, Field(description="Generated response")]
  7.     step_count:int# 节点:生成响应defgenerate_response(state: State)-> State:
  8.     logger.info(f"Processing user_input: {state['user_input']}")
  9.     response =f"Echo: {state['user_input']}"
  10.     logger.debug(f"Generated response: {response}")return{"response": response,"step_count": state["step_count"]+1}# 构建图
  11. graph = StateGraph(State)
  12. graph.add_node("generate", generate_response)
  13. graph.set_entry_point("generate")
  14. graph.set_finish_point("generate")# 编译图
  15. app = graph.compile()
复制代码
说明
步骤 3:运行并查看日志

运行工作流,观察 Pregel 的运行时日志。
示例代码
  1. # 执行工作流
  2. initial_state ={"user_input":"Hello!","response":"","step_count":0}
  3. result = app.invoke(initial_state)# 打印结果print(result)
复制代码
示例输出(日志):
  1. 2025-05-14 21:21:45,123 - langgraph - INFO - Processing user_input: Hello!
  2. 2025-05-14 21:21:45,124 - langgraph - DEBUG - Generated response: Echo: Hello!
  3. {'user_input': 'Hello!', 'response': 'Echo: Hello!', 'step_count': 1}
复制代码
说明
步骤 4:使用 LangChain 回调机制

LangGraph 继承了 LangChain 的回调系统(Callbacks),可以通过自定义回调捕获更详细的运行时信息,如节点开始/结束、状态更新等。
示例代码
  1. from langchain_core.callbacks import BaseCallbackHandler
  2. from langgraph.graph import StateGraph
  3. # 自定义回调classLoggingCallback(BaseCallbackHandler):defon_chain_start(self, serialized, inputs,**kwargs):
  4.         logger.info(f"Starting graph execution with inputs: {inputs}")defon_chain_end(self, outputs,**kwargs):
  5.         logger.info(f"Graph execution completed with outputs: {outputs}")defon_node_start(self, serialized, inputs, node_name,**kwargs):
  6.         logger.debug(f"Node {node_name} started with inputs: {inputs}")defon_node_end(self, outputs, node_name,**kwargs):
  7.         logger.debug(f"Node {node_name} ended with outputs: {outputs}")# 构建图(同上)
  8. graph = StateGraph(State)
  9. graph.add_node("generate", generate_response)
  10. graph.set_entry_point("generate")
  11. graph.set_finish_point("generate")# 编译图并添加回调
  12. app = graph.compile(callbacks=[LoggingCallback()])# 执行
  13. result = app.invoke({"user_input":"Hello!","response":"","step_count":0})
复制代码
示例输出
  1. 2025-05-14 21:21:45,123 - langgraph - INFO - Starting graph execution with inputs: {'user_input': 'Hello!', 'response': '', 'step_count': 0}
  2. 2025-05-14 21:21:45,124 - langgraph - DEBUG - Node generate started with inputs: {'user_input': 'Hello!', 'response': '', 'step_count': 0}
  3. 2025-05-14 21:21:45,125 - langgraph - INFO - Processing user_input: Hello!
  4. 2025-05-14 21:21:45,126 - langgraph - DEBUG - Generated response: Echo: Hello!
  5. 2025-05-14 21:21 Virgen,125 - langgraph - DEBUG - Node generate ended with outputs: {'response': 'Echo: Hello!', 'step_count': 1}
  6. 2025-05-14 21:21:45,127 - langgraph - INFO - Graph execution completed with outputs: {'user_input': 'Hello!', 'response': 'Echo: Hello!', 'step_count': 1}
复制代码
说明
步骤 5:高级日志配置(可选)

对于复杂工作流,可以进一步优化日志:

3. Pregel 日志的关键信息

在 LangGraph 的 Pregel 运行时日志中,你可以捕获以下关键信息:
示例:捕获条件跳转日志
  1. from langgraph.graph import StateGraph, END
  2. # 定义状态classState(TypedDict):
  3.     user_input:str
  4.     response:str
  5.     step_count:int# 节点:决定是否继续defdecide_continue(state: State)->str:
  6.     logger.info(f"Deciding with step_count: {state['step_count']}")if state["step_count"]>=3:return END
  7.     return"generate"# 节点:生成响应defgenerate_response(state: State)-> State:
  8.     logger.info(f"Generating response for: {state['user_input']}")return{"response":f"Echo: {state['user_input']}","step_count": state["step_count"]+1}# 构建图
  9. graph = StateGraph(State)
  10. graph.add_node("generate", generate_response)
  11. graph.add_node("decide", decide_continue)
  12. graph.add_edge("generate","decide")
  13. graph.add_conditional_edges("decide", decide_continue)
  14. graph.set_entry_point("generate")# 编译并添加回调
  15. app = graph.compile(callbacks=[LoggingCallback()])# 执行
  16. result = app.invoke({"user_input":"Hello!","response":"","step_count":0})
复制代码
示例输出
  1. 2025-05-14 21:21:45,123 - langgraph - INFO - Starting graph execution with inputs: {'user_input': 'Hello!', 'response': '', 'step_count': 0}
  2. 2025-05-14 21:21:45,124 - langgraph - DEBUG - Node generate started with inputs: {'user_input': 'Hello!', 'response': '', 'step_count': 0}
  3. 2025-05-14 21:21:45,125 - langgraph - INFO - Generating response for: Hello!
  4. 2025-05-14 21:21:45,126 - langgraph - DEBUG - Node generate ended with outputs: {'response': 'Echo: Hello!', 'step_count': 1}
  5. 2025-05-14 21:21:45,127 - langgraph - DEBUG - Node decide started with inputs: {'user_input': 'Hello!', 'response': 'Echo: Hello!', 'step_count': 1}
  6. 2025-05-14 21:21:45,128 - langgraph - INFO - Deciding with step_count: 1
  7. 2025-05-14 21:21:45,129 - langgraph - DEBUG - Node decide ended with outputs: generate
  8. ...
  9. 2025-05-14 21:21:45,135 - langgraph - INFO - Graph execution completed with outputs: {'user_input': 'Hello!', 'response': 'Echo: Hello!', 'step_count': 3}
复制代码
说明

4. 调试和优化技巧


5. 常见问题解答


6. 总结


原文地址:https://blog.csdn.net/u013172930/article/details/147980148




欢迎光临 AI创想 (https://llms-ai.com/) Powered by Discuz! X3.4