我研究了各种可能的解决方案:
这是我的代码:
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:无法读取未定义的属性(读取'选择')”
我尝试了以下方法来解决该问题:
有人能指出我正确的方向吗?除了用户输入之外,一切正常,作为指导,我使用了此视频:“https://www.youtube.com/watch?v=4qNwoAAfnk4”并尽可能更新了项目,为什么用户输入不工作?
此外,显然我的 PC 上的代码已实现了功能正常的 apiKey!感谢您的阅读!!
将 console.log(res.data.choices[0].message.content) 更改为 console.log(res.choices[0]) 或 console.log(res.choices[0].message.content) 解决了该问题并修复了正确的用户输入功能。