#Node.js error api

2 messages · Page 1 of 1 (latest)

maiden juniper
#

hi i have this code : ```js
const {OpenAIApi, Configuration} = require("openai")

const configuration = new Configuration({
apiKey: "sk-2G8nKBlbkFJclyPMYqL9zrTM8xk1mht"
});

const openai = new OpenAIApi(configuration);

const functionn = async () => {

    const completion = await openai.createCompletion("text-davinci-002", {
        prompt: "tu as quel age ?",
        temperature: 0.6,
        max_tokens: 600,
        top_p: 1,
        frequency_penalty: 0.5,
        presence_penalty: 0,
        best_of: 1,
    });

    if(completion.status != 200) {
        return "Sorry, I don't understand that.";
    }

    return console.log(completion.data.choices.map((choice) => choice.text).join("</br>"))

}
functionn()and i have this error :js
},
data: {
error: {
message: "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please send an email to support@openai.com and include any relevant code you'd like help with.)",
type: 'invalid_request_error',
param: null,
code: null
}
}
},
isAxiosError: true,
toJSON: [Function: toJSON]
}``` this error is very big please help me

tidal hazel
#

Try my version of the implementation:

const configuration = new Configuration({ apiKey: process.env.OPENAI_API_KEY }); const openai = new OpenAIApi(configuration);

`async function completion() {
try {
// Create completion
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: text,
temperature: 0,
max_tokens: 2048,
top_p: 1,
presence_penalty: 0.0,
frequency_penalty: 0.0,
});

        // If response return status 200, send message
        if (response) { 
            const message = response.data.choices[0].text
            return console.log(message)
        };
    } catch (error) {
        return console.log(error)
    }`

}