Getting a json parse error on the quick tutorial

Hi all,

I’ve been going through the quick tutorial and I have followed all of the steps. But when actually generating the pet names , I’m met with an error. See below:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

Any suggestions?

import { Configuration, OpenAIApi } from "openai";

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

// eslint-disable-next-line import/no-anonymous-default-export
export default async function (res) {
  const completion = await openai.createCompletion({
    model: "text-davinci-002",
    prompt: generatePrompt(req.body.idea),
    temperature: 0.6,
  });
  res.status(200).json({ result: completion.data.choices[0].text });
}

console.log(completion);

function generatePrompt(idea) {
  const capitalizedIdea = idea[0].toUpperCase() + idea.slice(1).toLowerCase();
  return `Suggest three names for a buisness.
  
  Idea: VR
  Names: Hyper, Vyper, Seeing is believing
  Idea: Finance
  Names: Money Matters, Money Time, Money Mine
  Animal: ${capitalizedIdea}
  Names:`;
}

here is my generate.js file

On your async function, you have the argument ‘res’ but not ‘req’ but declare ‘req’ in your prompt

Hi - brand new to this and have been following the tutorial
I am getting this error as well. i have tried running under XAMP in case I needed a server but that made no difference. I am running the example code out the box:

import { Configuration, OpenAIApi } from “openai”;

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

export default async function (req, res) {
const completion = await openai.createCompletion({
model: “text-davinci-002”,
prompt: generatePrompt(req.body.animal),
temperature: 0.6,
});
res.status(200).json({ result: completion.data.choices[0].text });
}

function generatePrompt(animal) {
const capitalizedAnimal =
animal[0].toUpperCase() + animal.slice(1).toLowerCase();
return `Suggest three names for an animal that is a superhero.

Animal: Cat
Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
Animal: Dog
Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
Animal: ${capitalizedAnimal}
Names:`;
}

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

Can you help?
thanks

Hey!

This was a similar error that I had: '<', "<!DOCTYPE "... is not valid JSON despite parsing a string - #3 by PaulBellow

Tldr; maybe replace your API key and ensure you’ve used it correctly.

If you’re using nextjs, maybe try using NEXT_PUBLIC_ but be careful for prod applications as this will expose your API key to the browser

1 Like