gpt4 book ai didi

typescript - OpenAI GPT-3 API 错误 : "TypeError: openai.completions is not a function"

转载 作者:行者123 更新时间:2023-12-02 05:47:47 25 4
gpt4 key购买 nike

我正在尝试运行教程中的测试代码 https://harishgarg.com/writing/building-a-chat-app-with-gpt-3-reactjs-and-nextjs-a-step-by-step-guide/我得到了

TypeError: openai.completions is not a function

我将以下代码放入 my.js 并在 Windows 10 的 git bash 窗口中使用“node my.js”运行


const openai = require('openai');
openai.apiKey = "api-key";
openai.completions({
engine: "text-davinci-003",
prompt: "Hello, how are you?",
max_tokens: 32,
n: 1,
stop: ".",
temperature: 0.5,
}).then((response) => {
console.log(response.data.choices[0].text);
});



我尝试了 OpenAI 文档中的各种替代代码片段以及其他问题中建议的一些代码片段,但未能使其正常工作。

最佳答案

如果您运行 test.js,OpenAI API 将返回以下完成信息:

This is indeed a test

test.js

const { Configuration, OpenAIApi } = require('openai');

const configuration = new Configuration({
apiKey: 'sk-xxxxxxxxxxxxxxxxxxxx'
});

const openai = new OpenAIApi(configuration);

openai.createCompletion({
model: 'text-davinci-003',
prompt: 'Say this is a test',
max_tokens: 7,
temperature: 0
})
.then((response) => {
console.log(response.data.choices[0].text);
})
.catch(err => {
console.log(err.message);
})

package.json

{
"name": "openai",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"next": "13.1.1",
"openai": "^3.1.0",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"eslint": "8.30.0",
"eslint-config-next": "13.1.1"
}
}

关于typescript - OpenAI GPT-3 API 错误 : "TypeError: openai.completions is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74924661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com