ChatGPT/OpenAIApi 的用户输入不起作用类型错误:无法读取未定义的属性(读取“选择”)

问题描述 投票:0回答:1

我研究了各种可能的解决方案:

这是我的代码:

import { config } from "dotenv"
config()

// New
import OpenAI from 'openai';
import readline from "readline"

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

const userInterface = new readline.createInterface({
    input: process.stdin,
    output: process.stdout

})

userInterface.prompt()
userInterface.on("line", async input => {
    const res = await openai.chat.completions.create({
        model: 'gpt-3.5-turbo',
        messages: [{ role: 'user', content: input}],
    })
    console.log(res.data.choices[0].message.content)
    userInterface.prompt()
})

当我运行代码时,我从帖子标题中收到错误消息:“TypeError:无法读取未定义的属性(读取'选择')”

我尝试了以下方法来解决该问题:

  • 使用命令重新加载窗口:“Developer:Reload.Window”
  • 使用 OpenAI 的不同 API 密钥
  • 将package.json OpenAi更新到最新版本4.11.1
  • 通常使用不同的 console.log 代码/输出和代码结构来解决问题

有人能指出我正确的方向吗?除了用户输入之外,一切正常,作为指导,我使用了此视频:“https://www.youtube.com/watch?v=4qNwoAAfnk4”并尽可能更新了项目,为什么用户输入不工作?

此外,显然我的 PC 上的代码已实现了功能正常的 apiKey!感谢您的阅读!!

javascript node.js user-input openai-api chatgpt-api
1个回答
0
投票

将 console.log(res.data.choices[0].message.content) 更改为 console.log(res.choices[0]) 或 console.log(res.choices[0].message.content) 解决了该问题并修复了正确的用户输入功能。

© www.soinside.com 2019 - 2024. All rights reserved.