ai-sdk-next-openai/app/api/use-completion-server-side-multi-step/route.ts
2025-09-26 15:46:29 +00:00

33 lines
895 B
TypeScript

import { openai } from '@ai-sdk/openai';
import { stepCountIs, streamText, tool } from 'ai';
import { z } from 'zod';
// Allow streaming responses up to 60 seconds
export const maxDuration = 60;
export async function POST(req: Request) {
// Extract the `prompt` from the body of the request
const { prompt } = await req.json();
const result = streamText({
model: openai('gpt-4-turbo'),
tools: {
weather: tool({
description: 'Get the weather in a location',
inputSchema: 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,
}),
}),
},
stopWhen: stepCountIs(4),
prompt,
});
// Respond with the stream
return result.toUIMessageStreamResponse();
}