-
Checked other resources
Commit to Help
Example Codeimport { ChatBedrockConverse } from '@langchain/aws';
import { DynamicStructuredTool } from '@langchain/core/tools';
import { z } from 'zod';
async function modelWithMultiplier(prompt: string) {
const multiplier = new DynamicStructuredTool({
name: 'multiply',
description: 'Multiply two numbers',
schema: z.object({
a: z.number(),
b: z.number(),
}),
func: async (input: { a: number; b: number }) => {
return input.a * input.b;
},
});
const model = new ChatBedrockConverse({
model: 'anthropic.claude-3-haiku-20240307-v1:0',
region: 'us-east-1',
temperature: 0.7,
maxTokens: 256,
streaming: true,
}).bindTools([multiplier]);
// replacing this with model.invoke() works fine
let response = '';
for await (const msg of await model.stream(prompt)) {
response += msg;
}
return response;
}
describe('Tool usage', () => {
it('should stream response after tool call', async () => {
expect(async () => {
return await modelWithMultiplier('what is the product of 12352 and 025.50?');
}).not.toThrow();
});
}); Description
System Info"@langchain/aws": "^0.1.7”, Node: v23.4.0 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hello @tusharm! I'm Dosu, here to help you solve bugs, answer questions, and support your journey as a contributor while we wait for a human maintainer. Let's tackle this together! 🚀 I found a similar unsolved discussion regarding streaming issues with tool binding: ChatAnthropic streamEvents not working with bindTools [1]. This might be relevant to your situation, as it also involves streaming and tool binding. Additionally, there is a documented workaround for handling tool calls in streaming mode with the ChatBedrockConverse model. The solution involves modifying the stream handling code to check for tool calls in the stream chunks, invoke the tool when a tool call is detected, and process the tool response. Here is the relevant code snippet: for (const response of responses) {
console.log(`User input: ${response}\n`);
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_calls && chunk?.tool_calls?.length > 0) {
// Handle tool calls
for (const toolCall of chunk.tool_calls) {
const toolResult = await analysisTool.invoke(toolCall);
process.stdout.write(String(toolResult));
}
} else if (chunk.content) {
process.stdout.write(String(chunk.content));
}
}
console.log("\n");
} This code ensures that tool calls are properly handled and responded to in the streaming process [2]. To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other |
Beta Was this translation helpful? Give feedback.
Hello @tusharm! I'm Dosu, here to help you solve bugs, answer questions, and support your journey as a contributor while we wait for a human maintainer. Let's tackle this together! 🚀
I found a similar unsolved discussion regarding streaming issues with tool binding: ChatAnthropic streamEvents not working with bindTools [1]. This might be relevant to your situation, as it also involves streaming and tool binding.
Additionally, there is a documented workaround for handling tool calls in streaming mode with the ChatBedrockConverse model. The solution involves modifying the stream handling code to check for tool calls in the stream chunks, invoke the tool when a tool call is detected, and pr…