Is it normal to get [Object] in the message property of the response and how do I fix it?

Hi I get an error 500 when i run my prompt and I suspect because of the message: property gets a blank [Object] how do I fix this? This is the response object

Find best matches route has started
{
  id: 'chatcmpl-7tbCNKO9tUmbk1yZ9wgrU5qtuldI1',
  object: 'chat.completion',
  created: 1693485387,
  model: 'gpt-3.5-turbo-0613',
  choices: [ { index: 0, message: [Object], finish_reason: 'stop' } ],
  usage: { prompt_tokens: 59, completion_tokens: 67, total_tokens: 126 }
}

This is the code

const aiRouter = express.Router();
const { OpenAI } = require("openai"); 
require("dotenv").config({path: "../server/config.env"});


// Data configuration for chatgpt
const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY // This is also the default, can be omitted
  });


aiRouter.post("/findBestMatches", async (req, res) => {
    try {
        console.log("Find best matches route has started");
        const contentArray = req.query.contentArray;
        const gptQuery = req.query.gptQuery;
    
        //build completion
    
        const completion = await openai.chat.completions.create(
            {
                model: "gpt-3.5-turbo",

                messages: [
                    {role:"system", content:`You will receive an array of names of different files folders and your goal is to find the three that match the ${gptQuery} as closely as possible`},
                    {role: "system", content: `Here is the array ${contentArray}`},
                    {role:"system", content:"format the response as a javascript array comprising of the three alternatives"}
                ],
                temperature: 0,
                max_tokens: 4000
            }
        );
        console.log(completion);
        const responseText = completion.choices[0].message;
        res.status(200).json({gptResponse: responseText, message: "Response from gpt api successful"});
    } catch (error) {
        console.log(error)
        res.status(500).json({message: "Internal server Error"});
    }

})

module.exports = aiRouter;
type or paste code here

Hi,

Seems there is a little confusion over the structures, changing

const responseText = completion.choices[0].message;

to

const responseText = completion.choices[0].message.content;

should sort that out.

1 Like