Feeding tool result back into the messages #1574
-
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 19 replies
-
|
EDIT: It's supported here: https://sdk.vercel.ai/examples/node/tools/call-tools-with-automatic-roundtrips OLD ANSWER: Here's what I'm experimenting with right now: async function main() {
let messages: CoreMessage[] = [
{ role: "user", content: "Hi!" },
{ role: "assistant", content: "Hello, how can I help?" },
{
role: "user",
content: "What's the weather in New York?",
},
];
let textResultIsPresent = false;
let textResult = "";
while (!textResultIsPresent) {
await sleep(2000);
const { text, toolCalls, toolResults } = await generateText({
model,
tools: {
weather: tool({
description: "Get the weather in a location",
parameters: z.object({
location: z
.string()
.describe("The location to get the weather for"),
}),
execute: async ({ location }) => ({
location,
temperature: 72 + Math.floor(Math.random() * 21) - 10,
}),
}),
},
messages,
});
if (toolResults.length > 0 && toolCalls.length > 0) {
messages.push({
role: "assistant" as const,
content: toolCalls,
});
messages.push({
role: "tool" as const,
content: toolResults,
});
} else {
textResult = text;
textResultIsPresent = true;
}
console.log(JSON.stringify(messages, null, 2));
}
console.log(textResult);
}Output: |
Beta Was this translation helpful? Give feedback.
-
|
With useChat, you can now do automatic roundtrips: https://sdk.vercel.ai/docs/ai-sdk-ui/chatbot-with-tool-calling Server-side roundtrip support in streamText and generateText is planned |
Beta Was this translation helpful? Give feedback.
-
|
I want to save the const { usage, fullStream, text, response } = await streamText({
model,
messages,
abortSignal: this.abortController?.signal,
onError: (error) => {
console.error('Error', JSON.stringify(error, null, 2));
throw error;
},
maxSteps: 20,
tools: tools,
maxTokens: 64000,
headers: {
'anthropic-beta': 'output-128k-2025-02-19',
},
});
for await (const partialStream of fullStream) {
emitMessagePart(partialStream)
// ...
} |
Beta Was this translation helpful? Give feedback.
-
|
This discussion was automatically locked because it has not been updated in over 30 days. If you still have questions about this topic, please ask us at community.vercel.com/ai-sdk |
Beta Was this translation helpful? Give feedback.
With useChat, you can now do automatic roundtrips: https://sdk.vercel.ai/docs/ai-sdk-ui/chatbot-with-tool-calling
Server-side roundtrip support in streamText and generateText is planned