安全、高效地使用 OpenAI 接口的关键知识
OpenAI API 密钥(API Key)是用于身份验证的唯一字符串,允许你的应用程序访问 OpenAI 提供的人工智能服务,如 GPT 模型、DALL·E 图像生成等。
每个密钥都与你的 OpenAI 账户绑定,并关联计费信息。因此,妥善保管密钥至关重要。
以下是一个使用 JavaScript 调用 OpenAI API 的简单示例:
fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY_HERE'
},
body: JSON.stringify({
model: 'gpt-4',
messages: [{ role: 'user', content: '你好!' }]
})
})
.then(response => response.json())
.then(data => console.log(data));
.env 文件)存储密钥,并加入 .gitignore。请立即执行以下操作: