我在许多项目中使用 open ai 一段时间了,今天我为我的新项目创建了一个新的 apiKey,当我初始化 open ai 时,我收到此错误:
未处理的运行时错误 错误:OPENAI_API_KEY环境变量丢失或为空;要么提供它,要么使用 apiKey 选项实例化 OpenAI 客户端,例如 new OpenAI({ apiKey: 'My API Key' })。
代码:
import OpenAI from 'openai';
export const openai = new OpenAI({
apiKey: process.env['OPENAI_API_KEY'], // This is the default and can be omitted
});
apiKey 位于我的 .env 中,也在我的 openAi 门户中
发生什么事了??
谢谢!!
将“使用服务器”添加到该文件的顶部,您的代码就可以工作了。这是 nextjs 的一项安全功能,可确保您的 api 密钥不会在客户端公开