Has anyone managed to get a tool_calls working when stream and append the tools chunk to message? #7560
Replies: 1 comment
-
Hey @josuasimatupang! I'm here to help you out with any bugs, questions, or contributions you have. Let me know how I can assist you today! To get
Here's a simplified example of how you might implement this in your code: for (const response of responses) {
const responseStream = await chainWithHistory.stream(
{
input: response,
name: "Stacy",
company: "Tech Unicorn",
questions: questions.join("\n"),
companyListings: companyListings,
},
{
configurable: {
sessionId: "123",
},
}
);
process.stdout.write("AI Response: ");
for await (const chunk of responseStream) {
if (chunk?.tool_call_chunks && chunk?.tool_call_chunks?.length > 0) {
// Handle tool call chunks
for (const toolCallChunk of chunk.tool_call_chunks) {
const toolResult = await analysisTool.invoke(toolCallChunk);
process.stdout.write(String(toolResult));
// Ensure each tool call is followed by a ToolMessage
const toolMessage = new ToolMessage({
name: toolCallChunk.name,
tool_call_id: toolCallChunk.id,
content: String(toolResult)
});
// Append the tool message to the message history or handle it as needed
}
} else if (chunk.content) {
process.stdout.write(String(chunk.content));
}
}
console.log("\n");
} This code snippet demonstrates how to handle tool call chunks in a streaming context, ensuring that each tool call is followed by a To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
Hello! I'm new here, so I apologize in advance for any mistakes. I've been experimenting with LangChain and Vercel AI and was curious if anyone has a solution for appending tools to the stream. I'll continue researching on my end, but any guidance would be greatly appreciated. Thanks so much!
System Info
Node v22.13.0
"@langchain/core": "^0.2.10",
"@langchain/openai": "^0.2.0",
"ai": "^3.4.33",
"@ai-sdk/openai": "^1.0.11",
Beta Was this translation helpful? Give feedback.
All reactions