mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
#909 Add OpenRouter prompt cost calculation
This commit is contained in:
@ -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();
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user