➡️Nodejs ➡️Text-Davinci-003 to GPT-3.5-turbo

Hello!

I’m a beginner and I need a lot of help from colleagues to adjust the code below.
I am getting the response ====> OpenAI Response Error: undefined.

Thank you very much!

const getDavinciResponse = async (clientText) => {
const options = {
model: “gpt-3.5-turbo”, // Modelo GPT a ser usado
messages: [
{role: “user”, content: clientText},
]
}

try {
    const response = await openai.createChatCompletion()
    let botResponse = ""
    response.choices[0].message.content; {
    botResponse += text         
    }
    return `🤖\n\n ${botResponse.trim()}`
} catch (e) {
    return `❌ OpenAI Response Error: ${e.response}`
}

}

Several things I noticed:

  1. You didn’t pass your messages and model options to createChatCompletion(). It should instead look something like this:
    const response = await openai.createChatCompletion({ model: "gpt-3.5-turbo", messages: messages, });

  2. It should be response.data.choices[0].message instead of response.choices[0].message.content;

  3. Not sure where does this text come from. Perhaps it should be botResponse += response.data.choices[0].message instead of botResponse += text?

  4. The curly brackets outside of botResponse += text seems unnecessary to me.

@waiyip Thank you so much for your kindness and help!

I followed your instruction and made the changes.
As I am inexperienced I am not able to solve the basic problems.

My code is still not working. I believe I have a problem with the commands function. If you could take a look I would be very grateful.

const openai = new OpenAIApi(configuration);

const getDavinciResponse = async (clientText) => {
const options = {
model: “gpt-3.5-turbo”, // Modelo GPT a ser usado
messages: [
{role: “user”, content: clientText},
]

}

try {
    const response = await openai.createChatCompletion({ model: "gpt-3.5-turbo", messages: messages, });
    let botResponse = ""
    response.data.choices[0].message;  
        botResponse += response.data.choices[0].message; 
       
        
    return `🤖\n\n ${botResponse}`
} catch (e) {
    return `❌ OpenAI Response Error: ${e.response}`
}

}

const commands = (client, message) => {
const iaCommands = {
davinci3: “/oi”
}

let firstWord = message.text.substring(0, message.text.indexOf(" "));

switch (firstWord) {
    case iaCommands.davinci3:
        const question = message.text.substring(message.text.indexOf(" "));
        getDavinciResponse(question).then((response) => {
            
            client.sendText(message.from === process.env.BOT_NUMBER ? message.to : message.from, response)
        })
        break;

}

async function start(client) {
client.onAnyMessage((message) => commands(client, message));
}

What is not working exactly? The bot is not responding or does it reply with some error messages. If there are some error messages, can you paste them here too?

1 Like

@waiyip

Thank you for your understanding and help to resolve this issue.

Here is the error message I am receiving: :x: OpenAI Response Error: undefined

you need to add .content in order to get the repsone content from the api:
response.data.choices[0].message.content;
botResponse += response.data.choices[0].message.content;
however it is not clear for me what is causing this issue, so my advice is to try and use chatgpt to help you solve this problem, like copy paste the full code and ask chatgpt to check if there is any issues with your code, add the error message you get and see what it will say, here is an example on how to do it:

i am trying to create a bot, this bot is supposed to do xxxxxxxxxx , i am using gpt3.5-turbo api and node.js, below is the code, however whenever i run the code i get the following error: xxxxxxxxxxxx
please help.
the code:
xxxxxxxxx