Hi there,
I’m not getting success to connect with the gtp3-turbo api…
My node.js script uses whatsapp to communicate with the user…
I would like to ask for help to analyze the following code and help me correct the errors … I am not succeeding with ChatGPT … I always get wrong tips and he apologizes that the knowledge acquired was until 2021 … before GPT3- turbo be launched…
follow the code and thank you in advance.
Helvio
const { Client, MessageMedia, LocalAuth } = require(‘whatsapp-web.js’)
const qrcode = require(‘qrcode-terminal’)
const axios = require(‘axios’)
require(‘dotenv’).config()
const client = new Client({
authStrategy: new LocalAuth()
})
client.on(‘qr’, qr => {
qrcode.generate(qr, {small: true})
});
client.on(‘authenticated’, (session) => console.log(Autenticado
))
client.on(‘ready’, () => console.log('O zap-gpt está pronto '))
client.on(‘message_create’, message => commands(message))
client.initialize();
const headers = {
‘Authorization’: Bearer ${process.env.OPENAI_KEY}
,
‘Content-Type’: ‘application/json’
}
const axiosInstance = axios.create({
baseURL: ‘https://api.openai.com’,
timeout: 120000,
headers: headers
});
const getGPT35TurboResponse = async (clientText) => {
const body = {
“model”: “gpt-3.5-turbo”,
“max_tokens”: 2048,
“temperature”: 0.1,
“messages” : [{“role”: “user”, “content” : clientText }]
}
try {
const { data } = await axiosInstance.post('v1/completions', body)
const botAnswer = data.choices[0].message;
return `Bestfriend 🤖 ${botAnswer}`
} catch (e) {
console.error(e); // Adicione esta linha
return `❌ OpenAI Response Error`
}
}
const commands = async (message) => {
if (message.from === ‘status@broadcast’) {
return;
}
const iaCommands = {
gpt35turbo: "/caddy"
}
let firstWord = message.body.substring(0, message.body.indexOf(" "))
const sender = message.from.includes(process.env.PHONE_NUMBER) ? message.to : message.from
switch (firstWord) {
case iaCommands.gpt35turbo:
const question = message.body.substring(message.body.indexOf(" "));
getGPT35TurboResponse(question).then(async (response) => {
const contact = await message.getContact()
client.sendMessage(sender, `${response}\n\n_Generated by @${contact.id.user}_`, { mentions: [contact] })
})
break
}
}