gpt4 book ai didi

javascript - 打开 AI 示例代码不起作用 "Await is only valid in async functions and the top level bodies of modules"

转载 作者:行者123 更新时间:2023-12-02 22:46:19 27 4
gpt4 key购买 nike

抱歉,如果这看起来很简单,我对此比较陌生。

如标题中所述,我收到错误“Await 仅在异步函数和模块的顶层主体中有效”虽然我很困惑,因为 await 位于主体的顶部?

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:"],
});

最佳答案

您可以将代码包装在异步 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:"],
});
})();

关于javascript - 打开 AI 示例代码不起作用 "Await is only valid in async functions and the top level bodies of modules",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72028213/

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