Feeding tool result back into the messages #1574
Answered
by
lgrammel
msjoshlopez
asked this question in
Help
-
Beta Was this translation helpful? Give feedback.
Answered by
lgrammel
May 29, 2024
Replies: 2 comments 14 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.
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.
12 replies
Answer selected by
lgrammel
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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