'Open AI example code not working "Await is only valid in async functions and the top level bodies of modules"
Apologies if this seems simple, I'm relatively new to this.
As said in the title I get the error "Await is only valid in async functions and the top level bodies of modules" Though I'm confused because the await is at the top of the body?
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: 'Api Key Go Brrrrrr',
});
const openai = new OpenAIApi(configuration);
const response = await openai.createCompletion("text-davinci-002", {
prompt: "You: What have you been up to?\nFriend: Watching old movies.\nYou: Did you watch anything interesting?\nFriend:",
temperature: 0.5,
max_tokens: 60,
top_p: 1.0,
frequency_penalty: 0.5,
presence_penalty: 0.0,
stop: ["You:"],
});
Solution 1:[1]
You can wrap the code in an async IIFE:
// add ; at the start to be safe, this is one of the very few cases where semicolons matter in JS
;(async ()=>{
const response = await openai.createCompletion("text-davinci-002", {
prompt: "You: What have you been up to?\nFriend: Watching old movies.\nYou: Did you watch anything interesting?\nFriend:",
temperature: 0.5,
max_tokens: 60,
top_p: 1.0,
frequency_penalty: 0.5,
presence_penalty: 0.0,
stop: ["You:"],
});
})();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Taxel |
