Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Example: Human in the loop over the network #58

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

albertpurnama
Copy link

This PR adds an example page for human in the loop process over the network

There's a couple of package updates that is required for interrupt to run one of them is @langchain/core.

TODOs:

  • Resuming still doesn't work, will need to investigate why.

Copy link

vercel bot commented Jan 6, 2025

@albertpurnama is attempting to deploy a commit to the LangChain Team on Vercel.

A member of the Team first needs to authorize it.

Comment on lines +59 to +79
// Get the agent state, check whether the agent can be resumed.
const state = await agent.getState({
configurable: { thread_id: threadId },
});

const isBeginning =
state.next.length === 0 && Object.keys(state.values).length === 0;

let runInput: { messages: (AIMessage | HumanMessage)[] } | Command =
new Command({
resume: messages.findLast((m: any) => m.role === "user")?.content,
update: {
isResuming: true, // Important if you have workflows that you want to ignore when resuming
},
});

if (isBeginning) {
runInput = {
messages,
};
}
Copy link
Author

@albertpurnama albertpurnama Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's gotta be a simpler way to do this, I simply want to check whether the current state is resumable or not, or whether it is in its end node?

I'm hoping for something like state.currentNode to get the string name of the node, if it is __end__ then I know it's not resumable. something like this exist?

@jacoblee93

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it just be if state.next were populated?

@@ -0,0 +1,88 @@
import { ChatWindow } from "@/components/ChatWindow";
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are copied over from agents/page.tsx

// isResuming is very useful if you want to ignore some workflow in some node
// that you want to ignore when the graph is resuming the run for the first time
// after interrupt happened
isResuming: Annotation<boolean>({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are just want to override the last value in the state, you can just do:

isResuming: Annotation<boolean>,
...

"calculatorAgent helps with calculations" +
"mainAgent is the main agent that handles the user's message",
),
response: z.string().describe("Human readable response to user's message"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small potential optimization: put response first. The LLM will generate it before generating a goto value and it can help steer it to better decisions.

const { gotoNext } = state;

// we're not using the object here. we ask for string feedback.
const input = await interrupt<{}, string>({});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for the type params?

};

const convertLangChainMessageToVercelMessage = (message: BaseMessage) => {
if (message._getType() === "human") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use .getType() now

"@langchain/langgraph": "^0.2.20",
"@langchain/core": "^0.3.27",
"@langchain/langgraph": "^0.2.39",
"@langchain/langgraph-checkpoint-postgres": "^0.0.2",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit - should we make this a peer dep?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants