Completions - Request failed with status code 400

Hey guys. I am having a weird issue with a chatbot I have created. I am able to successfully send some questions and get answers back, but other questions fail with status code 400. I am connecting to the completions endpoint like so:

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: prompt,
            max_tokens,
            temperature
          });

prompt = “Devin: Hello, how can I help you?\nyou: What a summary about Tim Grover\nDevin: Tim Grover is a renowned American author, trainer, and motivational speaker. He is best known for his work with some of the world’s greatest athletes, including Michael Jordan, Kobe Bryant, and Dwayne Wade. He has written several books, including Relentless: From Good to Great to Unstoppable and Jump Attack: The Formula for Explosive Athletic Performance. Grover’s philosophy is to help athletes reach their highest potential by pushing them beyond their limits and instilling a sense of mental toughness. He also emphasizes the importance of goal setting, visualization, and self-discipline.\nyou: What’s his most famous book\n”
max_tokens = 4000
temperature = 0.5

Upon sending this request, an error is thrown with the following information:

{"message":"Request failed with status code 400","name":"Error","stack":"Error: Request failed with status code 400\n    at createError (/.../createError.js:16:15)\n    at settle (/.../settle.js:17:12)\n    at IncomingMessage.handleStreamEnd (/.../http.js:322:11)\n    at IncomingMessage.emit (node:events:539:35)\n    at endReadableNT (node:internal/streams/readable:1345:12)\n    at processTicksAndRejections (node:internal/process/task_queues:83:21)","config":{"transitional":{"silentJSONParsing":true,"forcedJSONParsing":true,"clarifyTimeoutError":false},"transformRequest":[null],"transformResponse":[null],"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"maxBodyLength":-1,"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json","User-Agent":"OpenAI/NodeJS/3.1.0","Authorization":"Bearer sk-****","Content-Length":790},"method":"post","data":"{\"model\":\"text-davinci-003\",\"prompt\":\"Devin: Hello, how can I help you?\\nyou: What a summary about Tim Grover\\nDevin: Tim Grover is a renowned American author, trainer, and motivational speaker. He is best known for his work with some of the world’s greatest athletes, including Michael Jordan, Kobe Bryant, and Dwayne Wade. He has written several books, including Relentless: From Good to Great to Unstoppable and Jump Attack: The Formula for Explosive Athletic Performance. Grover's philosophy is to help athletes reach their highest potential by pushing them beyond their limits and instilling a sense of mental toughness. He also emphasizes the importance of goal setting, visualization, and self-discipline.\\nyou: What’s his most famous book\\n\",\"max_tokens\":4000,\"temperature\":0.5}","url":"https://api.openai.com/v1/completions"},"status":400}

Do you have any idea what could be causing this bad response? It seems like it could be something that happens when my prompts get longer, because shorter prompts seems to work alright. Could it also be something with the way I am formatting the prompt? Could newline characters cause a bad request?

Please help and thanks in advance!

1 Like