require("dotenv").config()
const OpenAI = require("openai")
const readlineSync = require("readline-sync")
// Open AI configuration
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
})
let messages = [{ role: "system", content: "You are a helpful assistant." }]
// Get user input
function getInput(promptMessage) {
return readlineSync.question(promptMessage, {
hideEchoBack: false // The typed characters won't be displayed if set to true
})
}
async function main() {
console.log("\n\n----------------------------------")
console.log(" CHAT WITH AI 🤖 ")
console.log("----------------------------------\n")
console.log("type 'x' to exit the conversation")
console.log(process.env.OPENAI_API_KEY)
if (!process.env.OPENAI_API_KEY) {
console.error("Error: OPENAI_API_KEY is not set in the environment variables.")
process.exit(1) // Exit with error code 1
}
await runConversation()
}
async function runConversation() {
while (true) {
const input = getInput("You: ")
if (input === "x") {
console.log("Goodbye!")
process.exit()
}
messages.push({ role: "user", content: input })
try {
const completion = await openai.chat.completions.create({
model: "gpt-4o",
messages: messages
})
} catch (error) {
console.error("Error communicating with OpenAI:", error.message)
continue // Skip this iteration
}
}
}
main()
getting this error—
Error communicating with OpenAI: 404 The model gpt-4o
does not exist or you do not have access to it.