from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
classState(TypedDict):# Messages have the type "list". The `add_messages` function# in the annotation defines how this state key should be updated# (in this case, it appends messages to the list, rather than overwriting them)
llm = init_chat_model(model="deepseek:deepseek-chat")defchatbot(state: State):return{"messages":[llm.invoke(state["messages"])]}# The first argument is the unique node name# The second argument is the function or object that will be called whenever# the node is used.
display(Image(graph.get_graph().draw_mermaid_png()))except Exception:# This requires some extra dependencies and is optionalpass
复制代码
Graph的可视化结果:
(, 下载次数: 0)
上传
点击文件名下载附件
8. 构建聊天循环
可以通过如下的代码构建聊天循环,当输入quit、exit或q时,结束聊天:
defstream_graph_updates(user_input:str):for event in graph.stream({"messages":[{"role":"user","content": user_input}]}):for value in event.values():print("Assistant:", value["messages"][-1].content)whileTrue:try: