-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update next-pages example to AI Core. (#1361)
- Loading branch information
Showing
4 changed files
with
19 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,21 @@ | ||
import { OpenAIStream, StreamingTextResponse } from 'ai'; | ||
import OpenAI from 'openai'; | ||
import { OpenAI } from '@ai-sdk/openai'; | ||
import { StreamingTextResponse, experimental_streamText } from 'ai'; | ||
|
||
// Create an OpenAI API client (that's edge friendly!) | ||
const openai = new OpenAI({ | ||
apiKey: process.env.OPENAI_API_KEY || '', | ||
apiKey: process.env.OPENAI_API_KEY ?? '', | ||
}); | ||
|
||
// IMPORTANT! Set the runtime to edge | ||
export const runtime = 'edge'; | ||
|
||
// IMPORTANT: The Next.js Pages Router's API Routes do not support streaming responses. | ||
// see https://sdk.vercel.ai/docs/guides/frameworks/nextjs-pages | ||
export default async function handler(req: Request, res: Response) { | ||
const { messages } = await req.json(); | ||
|
||
// Ask OpenAI for a streaming chat completion given the prompt | ||
const response = await openai.chat.completions.create({ | ||
model: 'gpt-3.5-turbo', | ||
stream: true, | ||
const result = await experimental_streamText({ | ||
model: openai.chat('gpt-4-turbo-preview'), | ||
messages, | ||
}); | ||
|
||
// Convert the response into a friendly text-stream | ||
const stream = OpenAIStream(response); | ||
// Respond with the stream | ||
return new StreamingTextResponse(stream); | ||
return new StreamingTextResponse(result.toAIStream()); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.