Add sorting for openrouter models

Alphabetically (default), price, context size
This commit is contained in:
artisticMink 2023-11-12 18:27:56 +01:00
parent a16e34bcef
commit cb2644cdea
2 changed files with 40 additions and 6 deletions

View File

@ -1832,8 +1832,17 @@
</select> </select>
</div> </div>
<div class="marginTopBot5"> <div class="marginTopBot5">
<label for="openrouter_group_options" class="checkbox_label"> <label for="openrouter_sort_models" class="checkbox_label">
<input id="openrouter_group_options" type="checkbox" onchange="document.getElementById('chat_completion_source').dispatchEvent(new Event('change'))"/> <select id="openrouter_sort_models">
<option data-i18n="Alphabetically" value="alphabetically">Alphabetically</option>
<option data-i18n="Price" value="pricing.prompt">Price (cheapest)</option>
<option data-i18n="Context Size" value="context_length">Context Size</option>
</select>
</label>
</div>
<div class="marginTopBot5">
<label for="openrouter_group_models" class="checkbox_label">
<input id="openrouter_group_models" type="checkbox" />
<span data-i18n="Group by vendor names">Group by vendor names</span> <span data-i18n="Group by vendor names">Group by vendor names</span>
</label> </label>
</div> </div>

View File

@ -209,6 +209,8 @@ const default_settings = {
openrouter_model: openrouter_website_model, openrouter_model: openrouter_website_model,
openrouter_use_fallback: false, openrouter_use_fallback: false,
openrouter_force_instruct: false, openrouter_force_instruct: false,
openrouter_group_models: false,
openrouter_sort_models: 'alphabetically',
jailbreak_system: false, jailbreak_system: false,
reverse_proxy: '', reverse_proxy: '',
legacy_streaming: false, legacy_streaming: false,
@ -257,6 +259,8 @@ const oai_settings = {
openrouter_model: openrouter_website_model, openrouter_model: openrouter_website_model,
openrouter_use_fallback: false, openrouter_use_fallback: false,
openrouter_force_instruct: false, openrouter_force_instruct: false,
openrouter_group_models: false,
openrouter_sort_models: 'alphabetically',
jailbreak_system: false, jailbreak_system: false,
reverse_proxy: '', reverse_proxy: '',
legacy_streaming: false, legacy_streaming: false,
@ -1241,11 +1245,14 @@ function saveModelList(data) {
model_list.sort((a, b) => a?.id && b?.id && a.id.localeCompare(b.id)); model_list.sort((a, b) => a?.id && b?.id && a.id.localeCompare(b.id));
if (oai_settings.chat_completion_source == chat_completion_sources.OPENROUTER) { if (oai_settings.chat_completion_source == chat_completion_sources.OPENROUTER) {
const sortingProperty = document.getElementById('openrouter_sort_models').value;
model_list = openRouterSortBy(model_list, sortingProperty);
$('#model_openrouter_select').empty(); $('#model_openrouter_select').empty();
const groupModels = document.getElementById('openrouter_group_options')?.checked ?? false; const groupModels = document.getElementById('openrouter_group_models')?.checked ?? false;
if (groupModels) { if (groupModels) {
appendOpenRouterOptions(groupByVendor(model_list), groupModels); appendOpenRouterOptions(openRouterGroupByVendor(model_list), groupModels);
} else { } else {
appendOpenRouterOptions(model_list); appendOpenRouterOptions(model_list);
} }
@ -1301,7 +1308,20 @@ function appendOpenRouterOptions(model_list, groupModels = false, sort = false)
} }
} }
function groupByVendor(array) { const openRouterSortBy = (data, property = 'alphabetically') => {
return data.sort((a, b) => {
if (property === 'context_length') {
return b.context_length - a.context_length;
} else if (property === 'pricing.prompt') {
return parseFloat(a.pricing.prompt) - parseFloat(b.pricing.prompt);
} else {
// Alphabetically
return a?.id && b?.id && a.id.localeCompare(b.id);
}
});
};
function openRouterGroupByVendor(array) {
const unsorted = array.reduce((acc, curr) => { const unsorted = array.reduce((acc, curr) => {
const vendor = curr.id.split('/')[0]; const vendor = curr.id.split('/')[0];
@ -1314,7 +1334,7 @@ function groupByVendor(array) {
return acc; return acc;
}, new Map()); }, new Map());
return new Map([...unsorted.entries()].sort()); return unsorted;
} }
async function sendAltScaleRequest(openai_msgs_tosend, logit_bias, signal, type) { async function sendAltScaleRequest(openai_msgs_tosend, logit_bias, signal, type) {
@ -2247,6 +2267,7 @@ function loadOpenAISettings(data, settings) {
oai_settings.windowai_model = settings.windowai_model ?? default_settings.windowai_model; oai_settings.windowai_model = settings.windowai_model ?? default_settings.windowai_model;
oai_settings.openrouter_model = settings.openrouter_model ?? default_settings.openrouter_model; oai_settings.openrouter_model = settings.openrouter_model ?? default_settings.openrouter_model;
oai_settings.openrouter_group_models = settings.openrouter_group_models ?? default_settings.openrouter_group_models; oai_settings.openrouter_group_models = settings.openrouter_group_models ?? default_settings.openrouter_group_models;
oai_settings.openrouter_sort_models = settings.openrouter_sort_models ?? default_settings.openrouter_sort_models;
oai_settings.openrouter_use_fallback = settings.openrouter_use_fallback ?? default_settings.openrouter_use_fallback; oai_settings.openrouter_use_fallback = settings.openrouter_use_fallback ?? default_settings.openrouter_use_fallback;
oai_settings.openrouter_force_instruct = settings.openrouter_force_instruct ?? default_settings.openrouter_force_instruct; oai_settings.openrouter_force_instruct = settings.openrouter_force_instruct ?? default_settings.openrouter_force_instruct;
oai_settings.ai21_model = settings.ai21_model ?? default_settings.ai21_model; oai_settings.ai21_model = settings.ai21_model ?? default_settings.ai21_model;
@ -2292,6 +2313,7 @@ function loadOpenAISettings(data, settings) {
$('#openai_max_context_counter').val(`${oai_settings.openai_max_context}`); $('#openai_max_context_counter').val(`${oai_settings.openai_max_context}`);
$('#model_openrouter_select').val(oai_settings.openrouter_model); $('#model_openrouter_select').val(oai_settings.openrouter_model);
$('#openrouter_group_models').val(oai_settings.openrouter_group_models); $('#openrouter_group_models').val(oai_settings.openrouter_group_models);
$('#openrouter_sort_models').val(oai_settings.openrouter_sort_models);
$('#openai_max_tokens').val(oai_settings.openai_max_tokens); $('#openai_max_tokens').val(oai_settings.openai_max_tokens);
@ -2833,6 +2855,7 @@ function onSettingsPresetChange() {
openrouter_use_fallback: ['#openrouter_use_fallback', 'openrouter_use_fallback', true], openrouter_use_fallback: ['#openrouter_use_fallback', 'openrouter_use_fallback', true],
openrouter_force_instruct: ['#openrouter_force_instruct', 'openrouter_force_instruct', true], openrouter_force_instruct: ['#openrouter_force_instruct', 'openrouter_force_instruct', true],
openrouter_group_models: ['#openrouter_group_models', 'openrouter_group_models', false], openrouter_group_models: ['#openrouter_group_models', 'openrouter_group_models', false],
openrouter_sort_models: ['#openrouter_sort_models', 'openrouter_sort_models', false],
ai21_model: ['#model_ai21_select', 'ai21_model', false], ai21_model: ['#model_ai21_select', 'ai21_model', false],
openai_max_context: ['#openai_max_context', 'openai_max_context', false], openai_max_context: ['#openai_max_context', 'openai_max_context', false],
openai_max_tokens: ['#openai_max_tokens', 'openai_max_tokens', false], openai_max_tokens: ['#openai_max_tokens', 'openai_max_tokens', false],
@ -3635,6 +3658,8 @@ $(document).ready(async function () {
$("#model_scale_select").on("change", onModelChange); $("#model_scale_select").on("change", onModelChange);
$("#model_palm_select").on("change", onModelChange); $("#model_palm_select").on("change", onModelChange);
$("#model_openrouter_select").on("change", onModelChange); $("#model_openrouter_select").on("change", onModelChange);
$("#openrouter_group_models").on("change", getStatusOpen);
$("#openrouter_sort_models").on("change", getStatusOpen);
$("#model_ai21_select").on("change", onModelChange); $("#model_ai21_select").on("change", onModelChange);
$("#settings_preset_openai").on("change", onSettingsPresetChange); $("#settings_preset_openai").on("change", onSettingsPresetChange);
$("#new_oai_preset").on("click", onNewPresetClick); $("#new_oai_preset").on("click", onNewPresetClick);