javascript - Node 中的 OpenAI API 给出了基本的 Await 错误。我该如何解决?

标签 javascript node.js openai-api

我从字面上复制了 openAI 示例中的代码,它给了我一个补救性 Await JS 错误,但我不确定它希望我做什么。我只想启动一个 Express.js 实例并从 openapi(最终是 chatgpt)获得一个 hello world。 Web 服务器工作正常。

这是我的工作:

const express = require('express')
const app = express()


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

const configuration = new Configuration({
  apiKey: "my key is here"
});
const openai = new OpenAIApi(configuration);

const completion = await openai.createCompletion({
  model: "text-davinci-002",
  prompt: "Hello world",
});
console.log(completion.data.choices[0].text);



app.get('/', function (req, res) {
  res.send('Hello World')
})

app.listen(3000)

错误:

SyntaxError: await is only valid in async functions and the top level bodies of modules
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1088:15)
    at Module._compile (node:internal/modules/cjs/loader:1123:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

原始的 OpenAI 示例代码是相同的。为什么这是示例代码?

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

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

const completion = await openai.createCompletion({
  model: "text-davinci-002",
  prompt: "Hello world",
});
console.log(completion.data.choices[0].text);

最佳答案

如前所述,您需要将异步代码包装在一个函数中:

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

const configuration = new Configuration({
  apiKey: "my key is here"
});

const openai = new OpenAIApi(configuration);

const completionFunction = async () => {
  const completion = await openai.createCompletion({
    model: "text-davinci-002",
    prompt: "Hello world",
  });
  
  console.log(completion.data.choices[0].text);
};

completionFunction();

app.get('/', function (req, res) {
  res.send('Hello World')
})

app.listen(3000)

此外,请确保您有一个具有适当依赖项的 package.json 文件:

{
  "name": "sample",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2",
    "openai": "^3.1.0"
  }
}

关于javascript - Node 中的 OpenAI API 给出了基本的 Await 错误。我该如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74972567/

相关文章:

javascript - Yammer Embed 无法完成加载 : JavaScript error in IE8 and IE9

javascript - 如何让 OpenAI 停止在其答案中添加 "A:"或 "Answer:"?

python - Azure Api 和 ChatGPT 的问题 (python)

python - 警告 - 找不到 Python 库,libdir=None、ldlibrary=None、multiarch=None、masd=None

javascript - 在 Rails 中的 .js.erb 中使用 ruby​​ 编码时, Assets 管道无法返回 @variables

javascript - 无法读取 Angular 8 中未定义的属性 'cards'

javascript - 为什么我的 SQL 查询执行速度很慢

html - 通过 gulp 将脚本的 src 从 Bower_components 更改为 CDN

node.js - Sequelize HasMany 不起作用

javascript - JS Slice 仅删除位于构造函数中的项目