Feeding tool result back into the messages #1574
Unanswered
msjoshlopez
asked this question in
Help
Replies: 2 comments 5 replies
-
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.
2 replies
-
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.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Beta Was this translation helpful? Give feedback.
All reactions