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

How to Disable streaming with .streamEvent method for a particular node #882

Open
Valentine340 opened this issue Feb 20, 2025 · 0 comments

Comments

@Valentine340
Copy link

I have seen "nostream" used in an example code using .stream method. How do you apply that in streamEvent method?
here is my grapgh:
const graphBuilder = new StateGraph(GraphStateWithToken)
// Main nodes
.addNode("analyzeQuery", handleInitialQuery)
.addNode("fetchTicketHistory", getTicketHistory)
.addNode("generateSupportResponse", createResponse)
.addNode("escalationHandler", handleEscalation)
.addNode("knowledgeSearch", searchKnowledge)
.addNode("priorityCheck", assessPriority)

// Start flow
.addEdge("__start__", "analyzeQuery")

// Route based on query analysis
.addConditionalEdges("analyzeQuery", queryTypeCondition, {
    __end__: "__end__",
    needsHistory: "fetchTicketHistory",
})

// Check ticket history
.addEdge("fetchTicketHistory", "priorityCheck")

here is the example code found on https://langchain-ai.github.io/langgraphjs/how-tos/stream-tokens/#the-stream-method

`import { RunnableLambda } from "@langchain/core/runnables";

const unstreamed = async (_: typeof StateAnnotation.State) => {
const model = new ChatOpenAI({
model: "gpt-4o-mini",
temperature: 0,
});
const res = await model.invoke("How are you?");
console.log("LOGGED UNSTREAMED MESSAGE", res.content);
// Don't update the state, this is just to show a call that won't be streamed
return {};
}

const agentWithNoStream = new StateGraph(StateAnnotation)
.addNode("unstreamed",
// Add a "nostream" tag to the entire node
RunnableLambda.from(unstreamed).withConfig({
tags: ["nostream"]
})
)
.addNode("agent", callModel)
.addNode("tools", toolNode)
// Run the unstreamed node before the agent
.addEdge("start", "unstreamed")
.addEdge("unstreamed", "agent")
.addConditionalEdges("agent", routeMessage)
.addEdge("tools", "agent")
.compile();

const stream = await agentWithNoStream.stream(
{ messages: [{ role: "user", content: "What's the current weather in Nepal?" }] },
{ streamMode: "messages" },
);

for await (const [message, _metadata] of stream) {
if (isAIMessageChunk(message) && message.tool_call_chunks?.length) {
console.log(${message.getType()} MESSAGE TOOL CALL CHUNK: ${message.tool_call_chunks[0].args});
} else {
console.log(${message.getType()} MESSAGE CONTENT: ${message.content});
}
}`

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

No branches or pull requests

1 participant