#909 Add OpenRouter prompt cost calculation
This commit is contained in:
parent
e39e9428b6
commit
1f8f200caf
|
@ -1750,6 +1750,9 @@
|
|||
<select id="model_openrouter_select">
|
||||
<option data-i18n="Connect to the API">-- Connect to the API --</option>
|
||||
</select>
|
||||
<small>
|
||||
Max prompt cost: <span id="openrouter_max_prompt_cost">Unknown</span> <span id="openrouter_max_prompt_cost_currency">USD</span>
|
||||
</small>
|
||||
</div>
|
||||
<h4 data-i18n="OpenRouter API Key">OpenRouter API Key</h4>
|
||||
<div>
|
||||
|
|
|
@ -692,8 +692,30 @@ function getChatCompletionModel() {
|
|||
}
|
||||
}
|
||||
|
||||
function calculateOpenRouterCost() {
|
||||
if (oai_settings.chat_completion_source !== chat_completion_sources.OPENROUTER) {
|
||||
return;
|
||||
}
|
||||
|
||||
let cost = 'Unknown';
|
||||
const model = model_list.find(x => x.id === oai_settings.openrouter_model);
|
||||
|
||||
if (model?.pricing) {
|
||||
const completionCost = Number(model.pricing.completion);
|
||||
const promptCost = Number(model.pricing.prompt);
|
||||
const completionTokens = oai_settings.openai_max_tokens;
|
||||
const promptTokens = (oai_settings.openai_max_context - completionTokens);
|
||||
const totalCost = (completionCost * completionTokens) + (promptCost * promptTokens);
|
||||
if (!isNaN(totalCost)) {
|
||||
cost = totalCost.toFixed(2);
|
||||
}
|
||||
}
|
||||
|
||||
$('#openrouter_max_prompt_cost').text(cost);
|
||||
}
|
||||
|
||||
function saveModelList(data) {
|
||||
model_list = data.map((model) => ({ id: model.id, context_length: model.context_length }));
|
||||
model_list = data.map((model) => ({ id: model.id, context_length: model.context_length, pricing: model.pricing }));
|
||||
model_list.sort((a, b) => a?.id && b?.id && a.id.localeCompare(b.id));
|
||||
|
||||
if (oai_settings.chat_completion_source == chat_completion_sources.OPENROUTER) {
|
||||
|
@ -1801,6 +1823,8 @@ async function onModelChange() {
|
|||
oai_settings.temp_openai = Math.min(oai_max_temp, oai_settings.temp_openai);
|
||||
$('#temp_openai').attr('max', oai_max_temp).val(oai_settings.temp_openai).trigger('input');
|
||||
}
|
||||
|
||||
calculateOpenRouterCost();
|
||||
}
|
||||
|
||||
if (oai_settings.chat_completion_source == chat_completion_sources.CLAUDE) {
|
||||
|
@ -2038,11 +2062,13 @@ $(document).ready(function () {
|
|||
$(document).on('input', '#openai_max_context', function () {
|
||||
oai_settings.openai_max_context = parseInt($(this).val());
|
||||
$('#openai_max_context_counter').text(`${$(this).val()}`);
|
||||
calculateOpenRouterCost();
|
||||
saveSettingsDebounced();
|
||||
});
|
||||
|
||||
$(document).on('input', '#openai_max_tokens', function () {
|
||||
oai_settings.openai_max_tokens = parseInt($(this).val());
|
||||
calculateOpenRouterCost();
|
||||
saveSettingsDebounced();
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue