-
Notifications
You must be signed in to change notification settings - Fork 1
Upgrade OpenAI SDK and migrate API routes to new Responses/Images API #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,9 @@ | ||
| import type { NextApiRequest, NextApiResponse } from "next"; | ||
| import { Configuration, OpenAIApi } from "openai"; | ||
| import { OpenAI } from "openai"; | ||
|
|
||
| const openai = new OpenAIApi( | ||
| new Configuration({ | ||
| apiKey: process.env.OPENAI_API_KEY, | ||
| }) | ||
| ); | ||
| const openai = new OpenAI({ | ||
| apiKey: process.env.OPENAI_API_KEY, | ||
| }); | ||
|
|
||
| type Data = { | ||
| error?: string; | ||
|
|
@@ -23,11 +21,11 @@ export default async function handler( | |
|
|
||
| const { word } = req.query; | ||
|
|
||
| const response = await openai.createImage({ | ||
| const response = await openai.images.generate({ | ||
| model: "gpt-image-1", | ||
| prompt: `clip art of ${word}`, | ||
| n: 1, | ||
| size: "256x256", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
With Suggested fix: either (a) switch to a DALL·E model if you want URL output, or (b) keep |
||
| }); | ||
|
|
||
| res.status(200).json({ url: response.data.data[0].url }); | ||
| res.status(200).json({ url: response.data?.[0]?.url }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For Useful? React with 👍 / 👎. |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gpt-image-1generationThis route now calls
openai.images.generatewithmodel: "gpt-image-1"but still sendssize: "256x256"; GPT image models only accept larger sizes (for example1024x1024/1024x1536/1536x1024), so requests can fail validation at runtime instead of returning an image. In practice this can make/api/imagefail for every POST request after this migration.Useful? React with 👍 / 👎.