NodeJS TypeError:Cannot read properties of undefined(阅读'create')openai:v3.2.1 discord.js:14.9.0

mpbci0fu  于 2023-04-11  发布在  Node.js
关注(0)|答案(1)|浏览(254)

我一直得到错误“TypeError:Cannot read properties of undefined(阅读'create')”在shell中,下面是我的代码:

const { SlashCommandBuilder } = require('discord.js');
const openai = require('openai');

openai.apiKey = 'i put my api key here';

module.exports = {
  data: new SlashCommandBuilder()
    .setName('ama')
    .setDescription('ask me anything!')
    .addStringOption(option =>
      option.setName('prompt')
        .setDescription('type any question...')),

  async execute(interaction) {
    try {
      const prompt = interaction.options.getString('prompt');

      const gptResponse = await openai.Completion.create({
        engine: "text-davinci-003",
        prompt: `answer the following question that is asked.\n\
      ChatGPT: Hello, how are you?\n\
      ${interaction.user.username}: ${prompt}\n\
      ChatGPT:`,
        temperature: 0,
        max_tokens: 100,
        stop: ["human:", "AI:", "Rayyan"],
      });
      interaction.reply(`${gptResponse.choices[0].text}`);
      return;
    } catch (err) {
      console.log(err);
    }
  },
};

当我在discord中执行斜线命令时,我期望discord机器人对我的问题给出正确的答案

mrwjdhj3

mrwjdhj31#

问题

您使用Python函数(即openai.Completion.create)而不是NodeJS函数(即openai.createCompletion)来获得完成。

解决方案

把这个换了。。

const gptResponse = await openai.Completion.create()

...这个

const gptResponse = await openai.createCompletion()

另外,将参数engine更改为model,如下所示:

const gptResponse = await openai.createCompletion({
  model: "text-davinci-003",
  prompt: `answer the following question that is asked.\n\
          ChatGPT: Hello, how are you?\n\
          ${interaction.user.username}: ${prompt}\n\
          ChatGPT:`,
  temperature: 0,
  max_tokens: 100,
  stop: ["human:", "AI:", "Rayyan"],
});

参见documentation
accept我的答案,如果这解决了你的问题。:)

相关问题