diff --git a/public/index.html b/public/index.html index 8635239c8..45e580bed 100644 --- a/public/index.html +++ b/public/index.html @@ -1750,6 +1750,9 @@ + + Max prompt cost: Unknown USD +

OpenRouter API Key

diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 56d19dbe7..8393b4ed2 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -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(); });