next-openai example out of the box fails to work
Steps to reproduce
npx create-next-app --example https://github.com/vercel-labs/ai/tree/main/examples/next-openai next-openai-app- npm i
- create .env file with OPENAI_API_KEY with a valid open api key
- type in input box - error returned from api request
It seems to work for me:
Can you share any more details on the error, like what's printed to your server console?
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.
There should be an error on the console which helps you debug this. Also might be helpful to directly check the response from OpenAI.
the model is set to gpt-3.5-turbo ...
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)
}
429 error sounds like you're being rate limited by openai
hmm ok i'll try another account - ty
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.