admin管理员组文章数量:1315224
I looked into various possible solutions:
- OpenAI and Javascript error : Getting 'TypeError: Cannot read properties of undefined (reading 'create') at Object.<anonymous>"
Here is my code:
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.chatpletions.create({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: input}],
})
console.log(res.data.choices[0].message.content)
userInterface.prompt()
})
When I run the code, I get the error message from the title of the post: TypeError: Cannot read properties of undefined (reading 'choices')
.
I tried the following to fix the issue:
- Reload the Window with mand:
Developer:Reload.Window
- Using different OpenAI API keys
- Update
package.json
OpenAI to the latest version which is4.11.1
- Generally a different console.log code/output & code structure to solve the issue
Can anyone point me in the right direction? Everything works, besides the user input. As a guideline I used this video: . I updated the project as far as I could. Why does the user input not work?
Also, the code on my PC has a functioning apiKey
implemented. Thanks for reading!
I looked into various possible solutions:
- OpenAI and Javascript error : Getting 'TypeError: Cannot read properties of undefined (reading 'create') at Object.<anonymous>"
- https://github./davila7/code-gpt-docs/issues/57
- https://www.npmjs./package/openai
Here is my code:
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.pletions.create({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: input}],
})
console.log(res.data.choices[0].message.content)
userInterface.prompt()
})
When I run the code, I get the error message from the title of the post: TypeError: Cannot read properties of undefined (reading 'choices')
.
I tried the following to fix the issue:
- Reload the Window with mand:
Developer:Reload.Window
- Using different OpenAI API keys
- Update
package.json
OpenAI to the latest version which is4.11.1
- Generally a different console.log code/output & code structure to solve the issue
Can anyone point me in the right direction? Everything works, besides the user input. As a guideline I used this video: https://www.youtube./watch?v=4qNwoAAfnk4. I updated the project as far as I could. Why does the user input not work?
Also, the code on my PC has a functioning apiKey
implemented. Thanks for reading!
- can u log the res and see what u get – cmgchess Commented Oct 6, 2023 at 15:28
-
2
The error means that
res.data
isundefined
, if that's not clear. Have you logged (or checked in the debugger) whatres
is afterawait
expression? – Pointy Commented Oct 6, 2023 at 15:29 - I tested more up to date versions of res.data, many people seem to have the problem as stated in :"github./davila7/code-gpt-docs/issues/57", I don't know if it ultimately has been resolved. That it's possibly undefined is clear to me, as they update the documentation frequently, I don't know what you guys mean with res as well as logging the res. I am pletely new to this. I can post the screenshot of the full error. I never worked with JavaScript/Node.JS this is my first project. – Dimitri Williams Commented Oct 6, 2023 at 15:34
-
1
res.choices[0].message
? – cmgchess Commented Oct 6, 2023 at 15:44 -
1
Yea there's clearly not a
data
property. – Pointy Commented Oct 6, 2023 at 15:46
2 Answers
Reset to default 5OpenAI NodeJS SDK v4
was released on August 16, 2023, and is a plete rewrite of the SDK. Among other things, there are changes in extracting the message content. See the v3
to v4
migration guide.
• If you have the OpenAI NodeJS SDK v3
:
import { Configuration, OpenAIApi } from 'openai';
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const chatCompletion = await openai.createChatCompletion({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: 'Hello!' }],
});
console.log(chatCompletion.data.choices[0].message);
• If you have the OpenAI NodeJS SDK v4
:
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const chatCompletion = await openai.chat.pletions.create({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: 'Hello!'}],
});
console.log(chatCompletion.choices[0].message); // There's no "data" property
Changing console.log(res.data.choices[0].message.content)
to console.log(res.choices[0])
or console.log(res.choices[0].message.content)
solved the issue and fixed the proper user input feature.
本文标签:
版权声明:本文标题:javascript - OpenAI API error: "TypeError: Cannot read properties of undefined (reading 'choices')& 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741981255a2408409.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论