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)