We could not parse the JSON body of your request

const express = require("express");
const https = require("https");
const cors = require("cors");

const app = express();

app.use(cors());
app.use(express.json());

const OPENAI_API_KEY = KEY;

const system_prompt = {
  "instruction": "You are a multilingual AI assistant called Foodion. Please provide a 1-day diet plan based on the following grocery list. The diet plan should include breakfast, lunch, and dinner. Each meal should have at least two recipes. The diet plan must be returned as a JSON object in the following format: {diet: {breakfast: {recipe1: '', instructions1: '', recipe2: '', instructions2: ''}, lunch: {recipe3: '', instructions3: '', recipe4: '', instructions4: ''}, dinner: {recipe5: '', instructions5: '', recipe6: '', instructions6: ''}}}. The response must be a diet plan, and it must not exceed one day. If the user requests a diet plan for more than one day, respond with  JSON object that says {'error_message': 'Max 1 günlük diet programı isteyebilirsiniz.'}",
  "user_profile": {
    "gender": "Male",
    "age": 18,
    "height": 180,
    "activity_level": "active sport life"
  },
 "grocery_list": [
    "3600 gram makarna",
    "4500 gram pirinc",
    "500 gram salca",
    "500 gram mantar",
    "500 gram zeytin",
    "500 gram turşu",
    "500 gram ekmek",
    "10 adet yumurta",
    "500 gram peynir",
    "1000 gram tost ekmeği",
    "1000 gram un",
    "500 gram tuz",
    "250 gram tereyağı",
    "200 gram salam dilimi",
    "200 gram sucuk dilimi",
    "300 gram pastırma dilimi",
    "500 gram sosis",
    "200 gram domates",
    "300 gram salatalık",
    "200 gram biber",
    "1000 gram yoğurt",
    "1000 gram sogan",
    "100 gram sarimsak"
  ],
}

app.post("/generate-text", async (req, res) => {
  const messageContent = req.body.message;

  const data = JSON.stringify({
    model: "gpt-3.5-turbo",
    messages: [
      { role: "system", content: system_prompt},
      { role: "user", content: messageContent },
    ],
    max_tokens: 2048,
    temperature: 0.5,
    top_p: 0.8,
  });

  const options = {
    hostname: "api.openai.com",
    path: "/v1/chat/completions",
    method: "POST",
    headers: {
      Authorization: `Bearer ${OPENAI_API_KEY}`,
      "Content-Type": "application/json",
      "Content-Length": data.length,
    },
  };

  const request = https.request(options, (response) => {
    const chunks = [];

    response.on("data", (chunk) => {
      chunks.push(chunk);
    });

    response.on("end", () => {
      const responseBody = Buffer.concat(chunks);
      res.send(responseBody); // Send the raw response body as is.
    });
  });

  request.on("error", (error) => {
    console.error("Error with OpenAI request:", error);
    res.status(500).send("Error with OpenAI request: " + error.message);
  });

  request.write(data);
  request.end();
});

const PORT = process.env.PORT || portnumber;

app.listen(PORT, () => console.log(`Server started on port ${PORT}`));

with this code i was expecting to able to send system_prompt to openai gpt to get answer in the specific way i wanted. But i keep getting this error. While in another project i tried to do almost same thing and i didn’t get any error i was using same model just different prompt that is structured in the same way but while that one is working this one is refusing to work

I tried to send prompt as string and js object both attempts failied but if i simply change

 { role: "system", content: system_prompt}

to

 { role: "system", content: "You are helping AI"}

or anything similar to code works fine

As you’ve noticed, the system content needs to be a plain string. You can try stringifying your JSON object and send that, but generally system prompt is written-out natural language text (but may include example JSON output if you’re using few-shot learning).

1 Like