【LangGraph】03-LangGraph之state

state通常用于保存当前会话中的消息,也可以自定义State用来存储额外的信息字段,state在节点之间传递,可以在节点中访问和修改state,此外LangGraph中的短期记忆通常使用state实现。

一、访问State中的消息

python 复制代码
from langgraph.graph import StateGraph, MessagesState, START, END

# 定义节点
def node1(state: MessagesState):
    print(state)
    print(state['messages'])

#定义State类型为MessagesState
graph = StateGraph(MessagesState)
graph.add_node("node1", node1)

graph.add_edge(START, "node1")
graph.add_edge("node1", END)

graph = graph.compile()
graph.invoke({"messages": [{"role": "user", "content": "hi!"}]})

输出结果:

{'messages': HumanMessage(content='hi!', additional_kwargs={}, response_metadata={}, id='6bcb92b6-d508-4ba4-996c-f05af82c1c2a')}

HumanMessage(content='hi!', additional_kwargs={}, response_metadata={}, id='6bcb92b6-d508-4ba4-996c-f05af82c1c2a')

  • MessagesState是内置的state类型,默认只有"messages"一个字段,可用于保存图执行过程中产生的消息。

  • 在节点中可以访问state中的消息

二、自定义state

python 复制代码
from typing import TypedDict
from langgraph.graph import StateGraph, MessagesState, START, END

# 自定义State
class State(TypedDict):
    # 注意:这里也可以定义message字段来接收上下文消息,大家可以自行尝试
    name: str
    age: int

def node1(state: State):
    print(state['name'])
    print(state['age'])
    # print(state['name'])
    # return state

graph = StateGraph(State)
graph.add_node("node1", node1)

graph.add_edge(START, "node1")
graph.add_edge("node1", END)

graph = graph.compile()
# 调用图时可传入State的内容
result = graph.invoke({"messages": [{"role": "user", "content": "hi!"}], "name": '张三', 'age': 18})
print(result)

输出结果:

张三

18

{'name': '张三', 'age': 18}

  • 自定义state可通过继承TypedDict来实现
  • 在调用图时,可传入state的内容字段

三、修改State

python 复制代码
from typing import TypedDict
from langgraph.graph import StateGraph, MessagesState, START, END

class State(TypedDict):
    name: str
    age: int

def node1(state: State):
    print(state['name'])
    print(state['age'])
    state['name'] = '李四'
    state['age'] += 1
    return state

graph = StateGraph(State)
graph.add_node("node1", node1)

graph.add_edge(START, "node1")
graph.add_edge("node1", END)

graph = graph.compile()
result = graph.invoke({"messages": [{"role": "user", "content": "hi!"}], "name": '张三', 'age': 18})
print(result)

输出结果:

张三

18

{'name': '李四', 'age': 19}

  • 可以在节点中修改state中的字段

  • 节点必须返回修改后的state,否则修改无效

四、总结

  1. state是用于保存当前会话的上下文信息的,不同会话间不能共享,可用于实现短期记忆
  2. MessageState保存图执行过程中产生的消息;
  3. 自定义state可以额外增加字段属性,当然也可以接受上下文的消息,上述案例为了简单起见只是定义了两个字段;
  4. 节点中可修改state,但需要返回修改后的State否则修改无效。
相关推荐
云水初8 小时前
【项目篇】基于LanGraph的自动处理客服邮件的 Agent汇总
开发语言·笔记·python·学习·fastapi·langgraph
坊钰3 天前
【LangGraph框架入门级】12. 人机交互:interrupt 中断与 Command 恢复
开发语言·数据库·python·人机交互·ai编程·langgraph
范中勤4 天前
LangGraph Checkpointer 封装(下):with 的双层上下文与 MemorySaver 为什么不用 with
python·大模型·llm·agent·langgraph
for_ever_love__5 天前
LangGraph State 设计:多节点之间怎么安全地共享数据
redis·mysql·状态模式·checkpoint·state·状态图·langgraph
for_ever_love__5 天前
LangGraph Checkpointer:持久化、断点续跑与多轮记忆
服务器·数据库·学习·持久化·langgraph·checkpointer·断点续跑
for_ever_love__5 天前
LangChain 多工具协作:一个 Agent 不够用时怎么办
人工智能·langchain·langgraph·协作·多agent·多工具·工具编排
寻道码路5 天前
AI Agent 工程化开发实战(一):Agent 开发的四个层次——积木、运行时、薄壳、套件
人工智能·langchain·大模型·ai agent·langgraph·ai工程化·deep agents
旖旎夜光5 天前
【LangGraph实战】LangGraph 学习笔记(五):信息裁剪、人机交互与时间旅行
笔记·python·学习·ai·人机交互·langgraph
范中勤5 天前
LangChain / LangGraph Memory 记忆体系技术文档(作用域判定 · 官方现版本实现 · 工程坑点)
langchain·大模型·agent·langgraph·记忆体系
坊钰6 天前
【LangGraph框架入门级】4. 支持联网搜索的智能代理系统
python·ai编程·langgraph