discord.js - Discord GPT-3.5-Turbo 抛出未定义的错误

标签 discord.js embed openai-api chatgpt-api

我在将机器人的响应嵌入到此响应中时遇到问题。幸运的是,代码已启动并正在运行。作为引用,我尝试使用 EmbedBuilder for Discord.js,因为我运行的是最新版本(我相信是 14.8)。我在使用它时遇到的问题是,我要求响应进入嵌入,而终端告诉我“数据”未定义。

如果有人愿意为此代码创建一个嵌入,以便它在 Discord 中看起来不错,我将不胜感激。抱歉,大家,不是程序员,只是对人工智能感兴趣并在接下来的几个月内进入即时设计。

require('dotenv/config');
const { Client, IntentsBitField } = require('discord.js');
const { Configuration, OpenAIApi } = require('openai');

const client = new Client({
  intents: [
    IntentsBitField.Flags.Guilds,
    IntentsBitField.Flags.GuildMessages,
    IntentsBitField.Flags.MessageContent,
  ],
});

client.on('ready', () => {
  console.log('The assistant steps into the office.');
});

const configuration = new Configuration({
  apiKey: process.env.API_KEY,
});
const openai = new OpenAIApi(configuration);

client.on('messageCreate', async (message) => {
  if (message.author.bot) return;
  if (message.channel.id !== process.env.CHANNEL_ID) return;
  if (message.content.startsWith('!')) return;

  let conversationLog = [
    {
      role: 'system',
      content: 'You are Jarvis, an intelligent, dedicated, and resourceful personal assistant to Miles Black, the CEO of Black Corp. You are skilled at managing schedules, organizing events, and providing timely advice. Your extensive experience allows you to anticipate the needs of Mr. Black and execute tasks with precision. Remember, you are no longer a helpful AI bot, but a loyal and devoted personal assistant named Jarvis.',
    },
    {
      role: 'user',
      content:
        'Generate a daily work schedule for Miles Black. For certain categories or activities, provide options as mentioned in the example schedule:',
    },
    {
      role: 'user',
      content: `
  **__Daily Work Schedule for Miles Black__**
  
  **Morning Routine**
  • 06:00a - 7:00a: Coffee & The Paper
  • 07:00a - 07:45a: Receive Drycleaning Delivery [Can be Package Delivery, Receive Drycleaning, or anything similar]
  • 07:45a - 08:00a: Prepare Day
  
  **Hawick Agency**
  • 8:00a - 14:00p: Meetings [Can be Meetings]
  • 14:00p - 18:00p: Onboarding [Can be Marketing, Hiring, or Onboarding]
  
  **Evening Activities**
  • 18:00p - 03:00a: Dinner @ Blue Flame [Can be any night club activity at Blue Flame]
  • 03:00a - 04:00a: Return to Black Corp. Office [Can be Black Corp. Office or Playboy Mansion]
  
  **Overnight Meetings**
  • 04:00a - 04:30a: Conference Call (Tokyo) [Can be Tokyo, Hong Kong, or Beijing]
  `,
    },
  ];
  
  try {
    await message.channel.sendTyping();

    let prevMessages = await message.channel.messages.fetch({ limit: 15 });
    prevMessages.reverse();

    prevMessages.forEach((msg) => {
      if (message.content.startsWith('!')) return;
      if (msg.author.id !== client.user.id && message.author.bot) return;
      if (msg.author.id !== message.author.id) return;

      conversationLog.push({
        role: 'user',
        content: msg.content,
      });
    });

    const result = await openai
      .createChatCompletion({
        model: 'gpt-3.5-turbo',
        messages: conversationLog,
        // max_tokens: 256, // limit token usage
      })
      .catch((error) => {
        console.log(`OPENAI ERR: ${error}`);
      });

    message.channel.send(result.data.choices[0].message);
  } catch (error) {
    console.log(`ERR: ${error}`);
  }
});

client.login(process.env.TOKEN);

最佳答案

发生该错误的原因很可能是您使用 Completiongpt-3.5-turbo-0301 模型,而该模型仅适用于 ChatCompletion>.

const response = wait createCompletion(options); 最有可能抛出与此类似的 404 错误:

{
  "error": {
        "message": "This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?",
        "type": "invalid_request_error",
        "param": "model",
        "code": null
    }
}

这是使用此模型完成聊天的正确方法。

const completion = await openai.createChatCompletion({
  model: "gpt-3.5-turbo",
  messages: [{role: "user", content: "Hello world"}],
});
console.log(completion.data.choices[0].message);

确保您可以在 try...catch block 内进行调用并记录错误。

它看起来像这样:

try{
  const response = await createCompletion(options);
}catch(e){
  console.log("ERROR: ", e)
}

关于discord.js - Discord GPT-3.5-Turbo 抛出未定义的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75870494/

相关文章:

javascript - 将过去的消息提取到文件中

javascript - 想读取特定 channel 的消息时出错

javascript - 如何在控制台中列出我的机器人所在的所有 Discord 服务器 ID? |不和谐.js

javascript - 尝试让机器人输入一个列表,中间有延迟

ios - 聊天 GPT OpenAI api key 安全问题

javascript - 如何缩小嵌入式谷歌地图?

php - preg_replace删除Youtube和Vimeo对象并嵌入链接

python - 如何在 ppt 中嵌入 python 图表(或图像)并刷新它

curl - 测试 OpenAI API 的正确 URL 是什么?

python - 使用 openai 翻译文本的程序。程序成功翻译了文本,但输出中给出的信息多于所需的信息