langgraph/how-tos/human_in_the_loop/edit-graph-state/ #938
Replies: 5 comments 5 replies
-
Is it possible to update a value of a state? Per my understanding, using Edit: This is what I'm prototyping right now, is this the intended pattern? import "dotenv/config";
import {
BaseMessage,
HumanMessage,
ToolMessage,
} from "@langchain/core/messages";
import {
MemorySaver,
START,
StateGraph,
StateGraphArgs,
} from "@langchain/langgraph";
import { executeWithElapsedTime, invokeGraph, prettyPrint } from "../utils";
import { IState } from "./common/types";
import { callModelNode, routeMessage, toolNode } from "./common/nodes";
import { StateSnapshot } from "@langchain/langgraph/dist/pregel/types";
import { RunnableConfig } from "@langchain/core/runnables";
const graphState: StateGraphArgs<IState>["channels"] = {
messages: {
reducer: (x: BaseMessage[], y: BaseMessage[]) => x.concat(y),
default: () => [],
},
};
const main = async () => {
const workflow = new StateGraph<IState>({
channels: graphState,
})
.addNode("agent", callModelNode)
.addNode("tools", toolNode)
.addEdge(START, "agent")
.addConditionalEdges("agent", routeMessage)
.addEdge("tools", "agent");
// **Persistence**
// Human-in-the-loop workflows require a checkpointer to ensure
// nothing is lost between interactions
const checkpointer = new MemorySaver();
// **Interrupt**
// To always interrupt before a particular node, pass the name of the node to `interruptBefore` when compiling.
const graph = workflow.compile({
checkpointer,
interruptAfter: ["tools"],
});
let config: RunnableConfig = {
configurable: { thread_id: "example-thread-1" },
};
// 1st interaction
console.log("---First iteraction---");
let inputState: IState = {
messages: [new HumanMessage("What is the current weather like in SF")],
};
await invokeGraph<IState, IState>(graph, inputState, config);
// Simulating user view the output of the tool call and then manually updating the output
// We are replacing the last state with the new content from the user
const stateHistory = graph.getStateHistory(config);
let currentState: StateSnapshot | undefined;
let previousState: StateSnapshot | undefined;
let stateIndex = 0;
for await (const state of stateHistory) {
if (stateIndex === 0) {
currentState = state;
}
if (stateIndex === 1) {
previousState = state;
break;
}
stateIndex++;
}
if (previousState && currentState) {
const currentStateValues = currentState.values as IState;
const lastMessage =
currentStateValues.messages[currentStateValues.messages.length - 1];
if (
lastMessage._getType() === "tool" &&
(lastMessage as ToolMessage).tool_call_id
) {
const newMessage = new ToolMessage(
"It's cold outside, with a high of 15C",
(lastMessage as ToolMessage).tool_call_id
);
await graph.updateState(
previousState.config,
{
messages: [newMessage],
},
"tools"
);
console.log("---Updated state---");
prettyPrint(newMessage);
}
}
// 2nd interaction, resuming the graph
// Running an interrupted graph with null as the input means to "proceed as if the interruption didn't occur."
console.log("---Second iteraction---");
await invokeGraph<IState, null>(graph, null, config);
};
executeWithElapsedTime(main); |
Beta Was this translation helpful? Give feedback.
-
This is correct - calling this method will append a new state checkpoint, not modify an existing checkpoint
In order to do this, you need to make sure the The |
Beta Was this translation helpful? Give feedback.
-
Is it possible to override reducers? If a reducer only support list appending, would list overriting be possible by a HITL? |
Beta Was this translation helpful? Give feedback.
-
Hello! I would like to ask if there is an asynchronous version of the function
A simplified version of my code:
|
Beta Was this translation helpful? Give feedback.
-
How to delete an entire state with a certain thread_id ? Is there a way? |
Beta Was this translation helpful? Give feedback.
-
langgraph/how-tos/human_in_the_loop/edit-graph-state/
Build language agents as graphs
https://langchain-ai.github.io/langgraph/how-tos/human_in_the_loop/edit-graph-state/
Beta Was this translation helpful? Give feedback.
All reactions