Add ability to get model name with /model

This commit is contained in:
Cohee 2024-04-03 00:52:30 +03:00
parent c0bb90b649
commit 27698fd024
3 changed files with 19 additions and 15 deletions

View File

@ -8616,10 +8616,10 @@ jQuery(async function () {
registerSlashCommand('closechat', doCloseChat, [], ' closes the current chat', true, true);
registerSlashCommand('panels', doTogglePanels, ['togglepanels'], ' toggle UI panels on/off', true, true);
registerSlashCommand('forcesave', doForceSave, [], ' forces a save of the current chat and settings', true, true);
registerSlashCommand('instruct', selectInstructCallback, [], '<span class="monospace">(name)</span> selects instruct mode preset by name', true, true);
registerSlashCommand('instruct', selectInstructCallback, [], '<span class="monospace">(name)</span> selects instruct mode preset by name. Gets the current instruct if no name is provided', true, true);
registerSlashCommand('instruct-on', enableInstructCallback, [], ' enables instruct mode', true, true);
registerSlashCommand('instruct-off', disableInstructCallback, [], ' disables instruct mode', true, true);
registerSlashCommand('context', selectContextCallback, [], '<span class="monospace">(name)</span> selects context template by name', true, true);
registerSlashCommand('context', selectContextCallback, [], '<span class="monospace">(name)</span> selects context template by name. Gets the current template if no name is provided', true, true);
registerSlashCommand('chat-manager', () => $('#option_select_chat').trigger('click'), ['chat-history', 'manage-chats'], ' opens the chat manager for the current character/group', true, true);
setTimeout(function () {

View File

@ -470,7 +470,7 @@ async function waitForConnection() {
export async function initPresetManager() {
eventSource.on(event_types.CHAT_CHANGED, autoSelectPreset);
registerPresetManagers();
registerSlashCommand('preset', presetCommandCallback, [], '<span class="monospace">(name)</span> sets a preset by name for the current API', true, true);
registerSlashCommand('preset', presetCommandCallback, [], '<span class="monospace">(name)</span> sets a preset by name for the current API. Gets the current preset if no name is provided', true, true);
$(document).on('click', '[data-preset-manager-update]', async function () {
const apiId = $(this).data('preset-manager-update');

View File

@ -249,7 +249,7 @@ parser.addCommand('inject', injectCallback, [], '<span class="monospace">id=inje
parser.addCommand('listinjects', listInjectsCallback, [], ' lists all script injections for the current chat.', true, true);
parser.addCommand('flushinjects', flushInjectsCallback, [], ' removes all script injections for the current chat.', true, true);
parser.addCommand('tokens', (_, text) => getTokenCount(text), [], '<span class="monospace">(text)</span> counts the number of tokens in the text.', true, true);
parser.addCommand('model', modelCallback, [], '<span class="monospace">(model name)</span> sets the model for the current API.', true, true);
parser.addCommand('model', modelCallback, [], '<span class="monospace">(model name)</span> sets the model for the current API. Gets the current model name if no argument is provided.', true, true);
registerVariableCommands();
const NARRATOR_NAME_KEY = 'narrator_name';
@ -1634,16 +1634,10 @@ function setBackgroundCallback(_, bg) {
/**
* Sets a model for the current API.
* @param {object} _ Unused
* @param {string} model Model name
* @returns {void}
* @param {string} model New model name
* @returns {string} New or existing model name
*/
function modelCallback(_, model) {
if (!model) {
return;
}
console.log('Set model to ' + model);
const modelSelectMap = [
{ id: 'model_togetherai_select', api: 'textgenerationwebui', type: textgen_types.TOGETHERAI },
{ id: 'openrouter_model', api: 'textgenerationwebui', type: textgen_types.OPENROUTER },
@ -1681,23 +1675,31 @@ function modelCallback(_, model) {
if (!modelSelectItem) {
toastr.info('Setting a model for your API is not supported or not implemented yet.');
return;
return '';
}
const modelSelectControl = document.getElementById(modelSelectItem);
if (!(modelSelectControl instanceof HTMLSelectElement)) {
toastr.error(`Model select control not found: ${main_api}[${apiSubType}]`);
return;
return '';
}
const options = Array.from(modelSelectControl.options);
if (!options.length) {
toastr.warning('No model options found. Check your API settings.');
return;
return '';
}
model = String(model || '').trim();
if (!model) {
return modelSelectControl.value;
}
console.log('Set model to ' + model);
let newSelectedOption = null;
const fuse = new Fuse(options, { keys: ['text', 'value'] });
@ -1718,8 +1720,10 @@ function modelCallback(_, model) {
modelSelectControl.value = newSelectedOption.value;
$(modelSelectControl).trigger('change');
toastr.success(`Model set to "${newSelectedOption.text}"`);
return newSelectedOption.value;
} else {
toastr.warning(`No model found with name "${model}"`);
return '';
}
}