0

I literally copied the code from the openAI example and it gives me a remedial Await JS error but I am unsure what it expects me to do. I just want to spin up an Express.js instance and get a hello world from openapi (eventually chatgpt). The web server works fine.

Here is my doinker:

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)

Error:

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

The original OpenAI example code is the same. Why the hell is this the example code?

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);
William
  • 4,422
  • 17
  • 55
  • 108
  • 1
    It might help in answering your question if you provide a link to where the original sample code came from, I searched for it but it looks like there were a few different locations where it could have come from. One result that came up was [this repo](https://github.com/openai/openai-quickstart-node) , it contains a file called [/pages/api/generate.js](https://github.com/openai/openai-quickstart-node/blob/master/pages/api/generate.js) - in that file they wrap the `await` within an `async` function. – user1063287 Jan 01 '23 at 00:11
  • 1
    Does this answer your question? [How can I use async/await at the top level?](https://stackoverflow.com/questions/46515764/how-can-i-use-async-await-at-the-top-level) – esqew Jan 01 '23 at 00:26

2 Answers2

1

As already mentioned, you need to wrap your async code in a function:

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)

Also, make sure you have a package.json file with the appropriate dependencies:

{
  "name": "sample",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2",
    "openai": "^3.1.0"
  }
}
ddastrodd
  • 710
  • 1
  • 10
  • 22
0

you can't use await without async function

create new async function:

async doStuff() {
  const completion = await openai.createCompletion({
    model: "text-davinci-002",
    prompt: "Hello world",
  });

  return completion;
}

doStuff();

the documentation probably uses deno, a new version of nodejs where you can use await outside of async function

and why didn't you use ChatGPT to fix the error? :)

1baz
  • 148
  • 1
  • 9
  • I've been asking chatgpt to write a 1 page node.js script that lets me work with it for the last 30 minutes. It doesn't work – William Jan 01 '23 at 00:11
  • Deno is not necessary. You need ECMAScript modules. This can be done in Node.js by using `type: "module"` in `package.json`. – Sebastian Simon Jan 01 '23 at 00:11
  • I still can't figure it out. Thanks for the effort – William Jan 01 '23 at 00:15
  • @SebastianSimon Bazhan didn't say Deno was needed. Also, your solution requires changing all the require statements. This answer is perfectly correct. – ddastrodd Jan 01 '23 at 00:17
  • @William Why isn't this working? I just tried it and it works as expected. – ddastrodd Jan 01 '23 at 00:17
  • Why isn't what working, adding the modules, changing the code? I don't see anything working. I would like to see a small modification of my original script changed so I know exactly what to change otherwise communication isn't clear – William Jan 01 '23 at 00:20
  • @William I added an answer that shows how I ran your code without issues. – ddastrodd Jan 01 '23 at 00:23
  • 1
    top-level `await` works fine in Node if you use ESM. – Evert Jan 01 '23 at 00:25