#Discord.js Bot: Error with 'openai.ChatCompletion.create' Function

1 messages · Page 1 of 1 (latest)

copper merlin
#

Issue Description:
I'm encountering an issue with my Discord bot that uses the OpenAI library. The code is designed to facilitate a text-based adventure, and it includes the OpenAI API for generating responses. The issue I'm facing is with the 'openai.ChatCompletion.create' function, which is giving me the following error:

Code and Dependencies:
I'm using Discord.js v13 with the 'openai' library for text-based adventures. Here's a snippet of the code:

Code and Dependencies:
Here's the relevant part of my code:

const openai = require('openai');

openai.apiKey = 'my key & it works, im sure';

// ...

const response = await openai.ChatCompletion.create({
    model: "gpt-3.5-turbo",
    messages: messages,
    temperature: 0.7,
    max_tokens: 150,
});

I have openai and discord.js packages installed.

#

Discord.js Bot: Error with 'openai.ChatCompletion.create' Function

#

Please @ me if you respond, thanks

#

Error:

/home/container/commands/story.js:33
            const response = await openai.ChatCompletion.create({
                                                         ^
TypeError: Cannot read properties of undefined (reading 'create')
    at MessageCollector.<anonymous> (/home/container/commands/story.js:33:58)
    at MessageCollector.emit (node:events:514:28)
    at MessageCollector.emit (node:domain:488:12)
    at MessageCollector.handleCollect (/home/container/node_modules/discord.js/src/structures/interfaces/Collector.js:110:12)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
sand ermine
copper merlin
#
import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: 'My API Key', // defaults to process.env["OPENAI_API_KEY"]
});

async function main() {
  const chatCompletion = await openai.chat.completions.create({
    messages: [{ role: 'user', content: 'Say this is a test' }],
    model: 'gpt-3.5-turbo',
  });
}

main();
#

this is how you use the API in node.js/js