对不起,如果这是一个简单的问题,但我是新手。
İ下面有我的代码,但是API返回说İ没有正确的API键。
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
async function test() {
const response = await openai.createCompletion("text-davinci-002", {
prompt: "Summarize this for a college student:\n\nJupiter is the fifth planet from the Sun and the largest in the Solar System. It is a gas giant with a mass one-thousandth that of the Sun, but two-and-a-half times that of all the other planets in the Solar System combined. Jupiter is one of the brightest objects visible to the naked eye in the night sky, and has been known to ancient civilizations since before recorded history. It is named after the Roman god Jupiter.[19] When viewed from Earth, Jupiter can be bright enough for its reflected light to cast visible shadows,[20] and is on average the third-brightest natural object in the night sky after the Moon and Venus.",
temperature: 0.7,
max_tokens: 64,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
});
console.log(response)
}
test()API键似乎是在process.env文件中找到的,而İ没有,当İ创建一个名为process.env的文件并创建一个名为OPENAI_API_KEY的变量时,它似乎不起作用。当apiKey设置为实际键时,它返回一些内容,但这似乎是一个迂回的解决方案。谢谢
发布于 2022-05-01 21:21:41
process.env不是文件夹,它用于从主机操作系统获取环境变量。您可以简单地将process.env.OPENAI_API_KEY替换为"your-api-key-here"
process.env在发布到github之类的东西时最常用,在那里您需要将api键保密,这样就没有其他人可以使用它们了,但是在部署时仍然可以使用它们。
https://stackoverflow.com/questions/72080207
复制相似问题