33 lines
895 B
TypeScript
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();
|
|
}
|