diff --git a/examples/next-openai/app/api/completion/route.ts b/examples/next-openai/app/api/completion/route.ts index 016a89b89794..b44873b64d96 100644 --- a/examples/next-openai/app/api/completion/route.ts +++ b/examples/next-openai/app/api/completion/route.ts @@ -1,27 +1,16 @@ -import OpenAI from 'openai'; -import { - OpenAIStream, - StreamingTextResponse, - experimental_StreamData, -} from 'ai'; +import { StreamingTextResponse, experimental_StreamData } from 'ai'; +import { streamText } from 'ai/core'; +import { openai } from 'ai/openai'; -// Create an OpenAI API client (that's edge friendly!) -const openai = new OpenAI({ - apiKey: process.env.OPENAI_API_KEY || '', -}); - -// IMPORTANT! Set the runtime to edge export const runtime = 'edge'; export async function POST(req: Request) { // Extract the `prompt` from the body of the request const { prompt } = await req.json(); - // Ask OpenAI for a streaming completion given the prompt - const response = await openai.completions.create({ - model: 'gpt-3.5-turbo-instruct', - max_tokens: 2000, - stream: true, + const result = await streamText({ + model: openai.completion('gpt-3.5-turbo-instruct'), + maxTokens: 2000, prompt, }); @@ -31,7 +20,7 @@ export async function POST(req: Request) { data.append({ test: 'value' }); // Convert the response into a friendly text-stream - const stream = OpenAIStream(response, { + const stream = result.toAIStream({ onFinal(completion) { data.close(); },