[solved] 400 error when summarizing a text

400 error occured when I try to summarize it.

const { Configuration, OpenAIApi } = require(‘openai’);
const { text } = require(‘./crawlTranslate’);
const Axios = require(‘axios’);

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

const openai = new OpenAIApi(configuration);

const axios = require(‘axios’);

async function summarizeText(text) {
try {
const response = await axios.post(
https://api.openai.com/v1/completions’,
{
model: ‘text-davinci-003’,
prompt: Summarize this to standard English:\n\n${text},
maxTokens: 50,
temperature: 0,
},
{
headers: {
‘Content-Type’: ‘application/json’,
‘Authorization’: Bearer ${process.env.OPENAI_API_KEY},
},
}
);

const summary = response.data.choices[0].text.trim();
return summary;

} catch (error) {
console.error(error.response.data);
throw new Error(‘Failed to summarize the text using OpenAI’);
}
}

module.exports = summarizeText;

If you are using ChatGPT to generate your code you will run into issues - especially if the API is changing rapidly.

I would highly recommend knowing what you are creating rather than use GPT, and then come here when it doesn’t work out. You are delegating all of your brain power! It’s nice, but it’s damaging.

OpenAI has made it very simple to use their models using their library.

Here’s an example (very similar to what you have)

const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createCompletion({
  model: "text-davinci-003",
  prompt: "Say this is a test",
  max_tokens: 7,
  temperature: 0,
});
2 Likes

Fair.

What happens when you tried to use the code? What kind of error do you get?
Using try catch isn’t the best during development, I’ve been tempted many times myself.

I’m very basic learner, so I made it with chat gpt first and then I’m trying to correct it using other code. I already use that code to correct it but everytime it failed after crawling the blog text. So I changed it.

I don’t know why, but this code runs properly now… Thank you.

const { Configuration, OpenAIApi } = require(‘openai’);
const { text } = require(‘./crawlTranslate’);
const Axios = require(‘axios’);

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

const openai = new OpenAIApi(configuration);

const axios = require(‘axios’);

async function summarizeText(text) {
try {
const response = await axios.post(
https://api.openai.com/v1/completions’,
{
model: ‘text-davinci-003’,
prompt: Summarize this to standard English:\n\n${text},
max_tokens: 50,
temperature: 0,
},
{
headers: {
‘Content-Type’: ‘application/json’,
‘Authorization’: Bearer ${process.env.OPENAI_API_KEY},
},
}
);

const summary = response.data.choices[0].text.trim();
return summary;

} catch (error) {
console.error(error.response.data);
throw new Error(‘Failed to summarize the text using OpenAI’);
}
}

module.exports = summarizeText;

Looks like max_tokens was the trick. Well. I guess I’m glad it worked out.