/classify can specify classier API as argument

a37b805a5db526158478dd84a20aee96c35f2efc

Wolfsblvt <wolfsblvt@gmail.com>

1 files changed, +34 -9Ignore whitespace
public/scripts/extensions/expressions/index.js+34 -9
@@ -8,7 +8,7 @@ import { isJsonSchemaSupported } from '../../textgen-settings.js';
88import { debounce_timeout } from '../../constants.js';
99import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js';
1010import { SlashCommand } from '../../slash-commands/SlashCommand.js';
1111import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '../../slash-commands/SlashCommandArgument.js';
1212import { isFunctionCallingSupported } from '../../openai.js';
1313import { SlashCommandEnumValue, enumTypes } from '../../slash-commands/SlashCommandEnumValue.js';
1414import { commonEnumProviders } from '../../slash-commands/SlashCommandCommonEnumsProvider.js';
@@ -52,6 +52,7 @@ const DEFAULT_EXPRESSIONS = [
5252 'surprise',
5353 'neutral',
5454];
55+/** @enum {number} */
5556const EXPRESSION_API = {
5657 local: 0,
5758 extras: 1,
@@ -920,18 +921,24 @@ async function setSpriteSetCommand(_, folder) {
920921 return '';
921922}
922923
923-async function classifyCommand(_, text) {
924+async function classifyCallback(/** @type {{api: string?}} */ { api = null }, text) {
924925 if (!text) {
925926 consoletoastr.logwarning('No text provided');
927+ return '';
928+ }
929+ if (api && !Object.keys(EXPRESSION_API).includes(api)) {
930+ toastr.warning('Invalid API provided');
926931 return '';
927932 }
928933
929- if (!modules.includes('classify') && extension_settings.expressions.api == EXPRESSION_API.extras) {
934+ const expressionApi = EXPRESSION_API[api] || extension_settings.expressions.api;
935+
936+ if (!modules.includes('classify') && expressionApi == EXPRESSION_API.extras) {
930937 toastr.warning('Text classification is disabled or not available');
931938 return '';
932939 }
933940
934941 const label = getExpressionLabel(text, expressionApi);
935942 console.debug(`Classification result for "${text}": ${label}`);
936943 return label;
937944}
@@ -1108,9 +1115,16 @@ function onTextGenSettingsReady(args) {
11081115 }
11091116}
11101117
1111-async function getExpressionLabel(text) {
1118+/**
1119+ * Retrieves the label of an expression via classification based on the provided text.
1120+ * Optionally allows to override the expressions API being used.
1121+ * @param {string} text - The text to classify and retrieve the expression label for.
1122+ * @param {EXPRESSION_API} [expressionsApi=extension_settings.expressions.api] - The expressions API to use for classification.
1123+ * @returns {Promise<string>} - The label of the expression.
1124+ */
1125+export async function getExpressionLabel(text, expressionsApi = extension_settings.expressions.api) {
11121126 // Return if text is undefined, saving a costly fetch request
11131127 if ((!modules.includes('classify') && extension_settings.expressions.apiexpressionsApi == EXPRESSION_API.extras) || !text) {
11141128 return getFallbackExpression();
11151129 }
11161130
@@ -1121,7 +1135,7 @@ async function getExpressionLabel(text) {
11211135 text = sampleClassifyText(text);
11221136
11231137 try {
11241138 switch (extension_settings.expressions.apiexpressionsApi) {
11251139 // Local BERT pipeline
11261140 case EXPRESSION_API.local: {
11271141 const localResult = await fetch('/api/extra/classify', {
@@ -2105,7 +2119,15 @@ function migrateSettings() {
21052119 }));
21062120 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
21072121 name: 'classify',
21082122 callback: classifyCommandclassifyCallback,
2123+ namedArgumentList: [
2124+ SlashCommandNamedArgument.fromProps({
2125+ name: 'api',
2126+ description: 'The Classifier API to classify with. If not specified, the configured one will be used.',
2127+ typeList: [ARGUMENT_TYPE.STRING],
2128+ enumList: Object.keys(EXPRESSION_API).map(api => new SlashCommandEnumValue(api, null, enumTypes.enum)),
2129+ }),
2130+ ],
21092131 unnamedArgumentList: [
21102132 new SlashCommandArgument(
21112133 'text', [ARGUMENT_TYPE.STRING], true,
@@ -2117,6 +2139,9 @@ function migrateSettings() {
21172139 Performs an emotion classification of the given text and returns a label.
21182140 </div>
21192141 <div>
2142+ Allows to specify which Classifier API to perform the classification with.
2143+ </div>
2144+ <div>
21202145 <strong>Example:</strong>
21212146 <ul>
21222147 <li>