我目前正在尝试在 Excel 中制作一个仪表板,使我能够利用 Chat GPT 为同事回答基本的 Excel 问题。它不断返回的问题表明我无法使用从 Chat GPT API 获得的 API key ,但我确实有一个付费帐户。我根据下面列出的视频链接编写了这个 Office 脚本。由于 Excel 中不断出现错误,我确实更改了视频中可用的代码。我也删除了我的 API key 。你们对我应该做什么有什么想法吗?
视频链接:https://youtu.be/kQPUWryXwag
Code:async function main(workbook: ExcelScript.Workbook) {
const apiKey: string = "API Key Insert";
const endpoint: string = "https://api.openai.com/v1/completions";
const sheet: ExcelScript.Worksheet = workbook.getWorksheet("Prompt");
const mytext: string = sheet.getRange("B2").getValue();
const result: ExcelScript.Worksheet = workbook.getWorksheet("Result");
result.getRange("A1:D1000").clear();
sheet.getRange("B3").setValue(" ");
const model: string = "text-davinci-002";
const prompt: string = mytext.toString();
const headers: Headers = new Headers();
headers.append("Content-Type", "application/json");
headers.append("Authorization", `Bearer ${apiKey}`);
const body: string = JSON.stringify({
model: model,
prompt: prompt,
max_tokens: 1024,
n: 1,
temperature: 0.5,
});
console.log("Request body:", body);
const response: Response = await fetch(endpoint, {
method: "POST",
headers: headers,
body: body,
});
const jsonResponse: { choices: { text: string | boolean | number }[] } = await response.json();
console.log("Response:", jsonResponse);
const json: { choices: { text: string | boolean | number }[] } = jsonResponse;
let text: string | boolean | number = "";
if (json.choices && json.choices.length > 0) {
text = json.choices[0].text;
}
console.log("Generated text:", text);
const output: ExcelScript.Range = sheet.getRange("B4");
output.setValue(text);
const cell: ExcelScript.Range = sheet.getRange("B4");
const arr: string[] = cell.getValue().toString().split("\n");
const newcell: ExcelScript.Range = result.getRange("A1");
var offset: number = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i].length > 0) {
newcell.getOffsetRange(offset, 0).setValue(arr[i]);
offset++;
}
}
if (offset > 1) {
sheet.getRange("B3").setValue("Check 'Result' sheet to get answers separated by multiple rows");
}
}
错误消息:
请求正文:{"model":"text-davinci-002","prompt":"美国最大的建筑是什么?","max_tokens":1024,"n":1,"温度”:0.5}响应:{错误:对象}错误:对象消息:“您超出了当前配额,请检查您的计划和账单详细信息。”类型:“insufficient_quota” 参数:null 代码:null 生成的文本:
故障排除:我已尝试使用新的 API key 。我使用 Chat GPT API 购买了付费帐户。老实说,我认为如果我获得了付费帐户,那么当我创建新的 API key 时,错误消息就会消失。
最佳答案
该错误表明您已用完配额。因此,如果您使用的是免费计划,则需要升级到付费计划。如果您已经使用付费套餐,则需要升级才能增加限额。
关于javascript - 聊天 GPT API key 故障排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76533895/