ai icon indicating copy to clipboard operation
ai copied to clipboard

next-openai example out of the box fails to work

Open tomheather-50 opened this issue 3 years ago • 8 comments

Steps to reproduce

  1. npx create-next-app --example https://github.com/vercel-labs/ai/tree/main/examples/next-openai next-openai-app
  2. npm i
  3. create .env file with OPENAI_API_KEY with a valid open api key
  4. type in input box - error returned from api request
Screenshot 2023-06-16 at 14 23 58 Screenshot 2023-06-16 at 14 23 55

tomheather-50 avatar Jun 16 '23 20:06 tomheather-50

It seems to work for me: CleanShot 2023-06-16 at 13 31 25@2x

Can you share any more details on the error, like what's printed to your server console?

MaxLeiter avatar Jun 16 '23 20:06 MaxLeiter

Update: I figure out that this happens if you don't have access to gpt-4. Tried setting the model to gpt-3.5-turbo and it works.

dkasper avatar Jun 16 '23 20:06 dkasper

There should be an error on the console which helps you debug this. Also might be helpful to directly check the response from OpenAI.

shuding avatar Jun 16 '23 21:06 shuding

the model is set to gpt-3.5-turbo ...

tomheather-50 avatar Jun 16 '23 23:06 tomheather-50

import { Configuration, OpenAIApi } from 'openai-edge'
import { OpenAIStream, StreamingTextResponse } from 'ai'

// Create an OpenAI API client (that's edge friendly!)
const config = new Configuration({
  apiKey: process.env.OPENAI_API_KEY
})
const openai = new OpenAIApi(config)

// 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 { messages } = await req.json()

  // Ask OpenAI for a streaming chat completion given the prompt
  const response = await openai.createChatCompletion({
    model: 'gpt-3.5-turbo',
    stream: true,
    messages: messages.map((message: any) => ({
      content: message.content,
      role: message.role
    }))
  })

  // Convert the response into a friendly text-stream
  const stream = OpenAIStream(response)
  // Respond with the stream
  return new StreamingTextResponse(stream)
}

Screenshot 2023-06-16 at 17 54 48

tomheather-50 avatar Jun 16 '23 23:06 tomheather-50

429 error sounds like you're being rate limited by openai

dkasper avatar Jun 16 '23 23:06 dkasper

hmm ok i'll try another account - ty

tomheather-50 avatar Jun 17 '23 00:06 tomheather-50

Identify issue type

Identified as None
Confidence: High

Rationale

The failure was traced to an external OpenAI account/rate-limit condition rather than a defect in the AI SDK or example, making this primarily a support issue.

Evidence

  • The example returned a 500 response because the underlying OpenAI request returned HTTP 429.
  • A maintainer reproduced the example successfully.
  • The discussion concluded the reporter was likely rate-limited by OpenAI and would try another account.

lgrammel avatar Jul 22 '26 19:07 lgrammel