next.js OpenAI GPT-3 API错误:“类型错误:openai.completions不是函数”

6tqwzwtp  于 2023-03-18  发布在  其他
关注(0)|答案(1)|浏览(239)

我正在尝试运行教程https://harishgarg.com/writing/building-a-chat-app-with-gpt-3-reactjs-and-nextjs-a-step-by-step-guide/中的测试代码,结果得到
TypeError:openai.completions不是函数
我将以下代码放入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文档中的各种替代代码片段,以及其他问题中建议的一些代码片段,但一直无法让它工作。

nnvyjq4y

nnvyjq4y1#

如果您运行test.js,OpenAI API将返回以下完成:
这的确是一个考验

测试.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);
})

包.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"
  }
}

相关问题