Langgraph HITL 中断运行,用户输入后输出打字效果

ini 复制代码
from langchain_core.messages import AIMessageChunk, HumanMessage
from langgraph.types import Command,interrupt
from langgraph.graph import StateGraph, START, END, MessagesState
from langgraph.checkpoint.memory import InMemorySaver  # ✅ 补上导入
from dotenv import load_dotenv
load_dotenv(override=True)
from langchain.chat_models import init_chat_model

model = init_chat_model(
    model="deepseek-chat",
    extra_body={
        "thinking": {
            "type": "disabled"
        }
    }
)

class OverAllState(MessagesState):
    text: str

def input_node(state: OverAllState) -> OverAllState:
    val = interrupt("请输入")
    res = model.invoke([HumanMessage(val)])
    return {
        "messages": [res]
    }

builder = StateGraph(state_schema=OverAllState)
builder.add_node("input_node", input_node)
builder.add_edge(START, "input_node")
builder.add_edge("input_node", END)
checkpointer = InMemorySaver()
graph = builder.compile(checkpointer=checkpointer)

config = {
    "configurable": {"thread_id": "9667"}
}
graph.invoke({},config=config)
val=input("请输入")
# 流式输出,把内容里的换行替换成空格,实现"打字机不自动换行"
for chunk, metadata in graph.stream(
    Command(resume=val),
    config=config,
    stream_mode="messages",
):
    if isinstance(chunk, AIMessageChunk) and chunk.content:
        print(chunk.content, end="", flush=True)
相关推荐
ZhengEnCi1 小时前
agent 为什么需要自进化:从静态模型到经验驱动的持续成长
人工智能
QXWZ_IA1 小时前
RTK平面很准、高程总飘,问题出在哪
人工智能
Ferries1 小时前
用 WorkBuddy+DeepSeek 跑了30天自动矩阵,事情没那么简单
人工智能·aigc·vibecoding
时早玖沐1 小时前
给 AstrBot 写一个天气推送插件:12 次提交、6 个坑
python·开源·ai编程
触底反弹2 小时前
🚀 FastAPI 五大重难点深度剖析:从 500 报错到彻底搞懂 Depends、Pydantic 与 SQLAlchemy
python·fastapi
用户837133200762 小时前
AI总是会忘记细节咋办?我用GitHub实现外部记忆管理
人工智能·github
谁在黄金彼岸2 小时前
OCR 方案对比:本地多模态可行吗?
人工智能
cpolar技术支持2 小时前
网关加 gzip,同时开启 chunked,下载回来的包解不开:把两层编码的解压顺序与长度核对做成只读页
python·http·网络编程·socket·内网穿透
小嘉丶学长2 小时前
Day14 · 字典与集合
java·开发语言·网络·python·学习·1024程序员节