Redirect to OpenAI site to view usage

This commit is contained in:
Cohee1207 2023-07-21 23:35:54 +03:00
parent 8d5fbbe2bc
commit f28e7cf47c
3 changed files with 7 additions and 24 deletions

View File

@ -1423,7 +1423,9 @@
<form id="openai_form" data-source="openai" action="javascript:void(null);" method="post" enctype="multipart/form-data">
<h4><span data-i18n="OpenAI API key">OpenAI API key</span></h4>
<div>
<a id="openai_api_usage" href="javascript:void(0);"><span data-i18n="View API Usage Metrics">View API Usage Metrics</span></a>
<a id="openai_api_usage" href="https://platform.openai.com/account/usage" target="_blank">
<span data-i18n="View API Usage Metrics">View API Usage Metrics</span>
</a>
</div>
<span>
<ol>

View File

@ -1354,26 +1354,6 @@ async function saveOpenAIPreset(name, settings) {
}
}
async function showApiKeyUsage() {
try {
const response = await fetch('/openai_usage', {
method: 'POST',
headers: getRequestHeaders(),
});
if (response.ok) {
const data = await response.json();
const text = `<h3>Total usage this month: $${Number(data.total_usage / 100).toFixed(2)}</h3>
<a href="https://platform.openai.com/account/usage" target="_blank">Learn more (OpenAI platform website)</a>`;
callPopup(text, 'text');
}
}
catch (err) {
console.error(err);
toastr.error('Invalid API key');
}
}
function onLogitBiasPresetChange() {
const value = $('#openai_logit_bias_preset').find(':selected').val();
const preset = oai_settings.bias_presets[value];
@ -2231,7 +2211,6 @@ $(document).ready(function () {
$("#settings_perset_openai").on("change", onSettingsPresetChange);
$("#new_oai_preset").on("click", onNewPresetClick);
$("#delete_oai_preset").on("click", onDeletePresetClick);
$("#openai_api_usage").on("click", showApiKeyUsage);
$("#openai_logit_bias_preset").on("change", onLogitBiasPresetChange);
$("#openai_logit_bias_new_preset").on("click", createNewLogitBiasPreset);
$("#openai_logit_bias_new_entry").on("click", createNewLogitBiasEntry);

View File

@ -2927,7 +2927,8 @@ app.post("/openai_bias", jsonParser, async function (request, response) {
return response.send(result);
});
// Shamelessly stolen from Agnai
// TODO: Dead code, consider deleting. Users will get redirected to OpenAI site instead.
// 'Your request to GET /v1/dashboard/billing/usage must be made with a session key (that is, it can only be made from the browser). You made it with the following key type: secret.'
app.post("/openai_usage", jsonParser, async function (request, response) {
if (!request.body) return response.sendStatus(400);
const key = readSecret(SECRET_KEYS.OPENAI);
@ -2958,7 +2959,8 @@ app.post("/openai_usage", jsonParser, async function (request, response) {
);
return response.send(res);
}
catch {
catch (error) {
console.log(error);
return response.sendStatus(400);
}
});