#how can i send post request in open ai api using app directory

8 messages · Page 1 of 1 (latest)

dry tulip
#

import {NextRequest,NextResponse } from "next/server";
import openai from "@/config/openai.config";

export async function GET(req) {
// const { lyrics } = await req.body;
try {
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: Generate music from lyrics: Boom chick chick chick boom,
max_tokens: 100,
temperature: 0.7,
});

const music = response.choices[0].text;
return NextResponse.json({ music });

} catch (error) {
console.error("Error generating music:", error);
return NextResponse.json({ error: "Failed to generate music" });
}
}

this contineously give me error

boreal moat
#

what is the error you are seeing?

dry tulip
#

Error generating music: TypeError: Cannot read properties of undefined (reading '0')
at GET (webpack-internal:///(sc_server)/./src/app/generate-music/route.js:20:39)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:242:37)

#

and response is undefined

#

thanks previously for helping me sir

boreal moat
#

by the error message, it looks like response.choices is undefined so trying to access any index will throw this error

#

you need to check the shape of the response from open ai