我试图在Google脚本中使用OpenAI API,但在运行cURL时遇到了一些问题。这些文件给了我以下内容:
curl https://api.openai.com/v1/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"model": "text-davinci-002", "prompt": "Say this is a test", "temperature": 0, "max_tokens": 6}'我用汽油写成的:
function myFunction() {
var url = "https://api.openai.com/v1/completions";
var headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ******',
'data': {
'model': 'text-davinci-002', 'prompt': 'Say this is a test', 'temperature': 0, 'max_tokens': 6
}
};
var response = UrlFetchApp.fetch(url, headers);
var text = response.getResponseCode();
var data = JSON.parse(response.getContentText());
Logger.log(data);
}其中*是我从帐户中获得的API密钥。当我运行这个脚本时,我会收到一个错误,说明我没有提供API密钥。我已经检查过了,钥匙是正确的,所以我猜我的格式有问题吧?提亚
发布于 2022-09-06 06:24:29
在您的脚本中,下面的修改如何?
发自:
var headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ******',
'data': {
'model': 'text-davinci-002', 'prompt': 'Say this is a test', 'temperature': 0, 'max_tokens': 6
}
};
var response = UrlFetchApp.fetch(url, headers);至:
var options = {
'contentType': 'application/json',
'headers': { 'Authorization': 'Bearer ******' },
'payload': JSON.stringify({ 'model': 'text-davinci-002', 'prompt': 'Say this is a test', 'temperature': 0, 'max_tokens': 6 })
};
var response = UrlFetchApp.fetch(url, options);注意:
参考资料:
https://stackoverflow.com/questions/73617522
复制相似问题