File tree Expand file tree Collapse file tree 1 file changed +8
-19
lines changed
Expand file tree Collapse file tree 1 file changed +8
-19
lines changed Original file line number Diff line number Diff line change 1- import { OpenAIStream , StreamingTextResponse } from "ai "
2- import OpenAI from "openai "
1+ import { openai } from "@ai-sdk/openai "
2+ import { streamText } from "ai "
33
4- // Create an OpenAI API client
5- const openai = new OpenAI ( {
6- apiKey : process . env . OPENAI_API_KEY ,
7- } )
4+ // Allow streaming responses up to 30 seconds
5+ export const maxDuration = 30
86
97export async function POST ( req : Request ) {
10- // Extract the `messages` from the body of the request
118 const { messages } = await req . json ( )
129
13- // Request the OpenAI API for the response based on the prompt
14- const response = await openai . chat . completions . create ( {
15- model : "gpt-4o-mini" ,
16- stream : true ,
17- messages : messages ,
10+ const result = streamText ( {
11+ model : openai ( "gpt-4o-mini" ) ,
12+ messages,
1813 } )
1914
20- // Convert the response into a friendly text-stream
21- const stream = OpenAIStream ( response )
22-
23- // Respond with the stream
24- return new StreamingTextResponse ( stream )
15+ return result . toDataStreamResponse ( )
2516}
2617
27-
28-
You can’t perform that action at this time.
0 commit comments