我正在嘗試在 Google Apps 腳本中使用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}'
我在 GAS 中這樣寫:
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 密鑰。我已經仔細檢查過,密鑰是正確的,所以我猜我的格式有問題?TIA
uj5u.com熱心網友回復:
在您的腳本中,如何進行以下修改?
從:
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);
筆記:
- 此修改假設您的示例 curl 命令運行良好。請注意這一點。
參考:
- 獲取(網址,引數)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/505181.html
