Skip to content

Commit b6378b7

Browse files
committed
refactor: update chat route to use new AI SDK and improve streaming response handling
1 parent 24886a6 commit b6378b7

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

app/api/chat/route.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
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

97
export 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-

0 commit comments

Comments
 (0)