/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';
8import { debounce_timeout } from '../../constants.js';8import { debounce_timeout } from '../../constants.js';
9import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js';9import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js';
10import { SlashCommand } from '../../slash-commands/SlashCommand.js';10import { SlashCommand } from '../../slash-commands/SlashCommand.js';
11import { ARGUMENT_TYPE, SlashCommandArgument } from '../../slash-commands/SlashCommandArgument.js';11import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '../../slash-commands/SlashCommandArgument.js';
12import { isFunctionCallingSupported } from '../../openai.js';12import { isFunctionCallingSupported } from '../../openai.js';
13import { SlashCommandEnumValue, enumTypes } from '../../slash-commands/SlashCommandEnumValue.js';13import { SlashCommandEnumValue, enumTypes } from '../../slash-commands/SlashCommandEnumValue.js';
14import { commonEnumProviders } from '../../slash-commands/SlashCommandCommonEnumsProvider.js';14import { commonEnumProviders } from '../../slash-commands/SlashCommandCommonEnumsProvider.js';
@@ -52,6 +52,7 @@ const DEFAULT_EXPRESSIONS = [
52 'surprise',52 'surprise',
53 'neutral',53 'neutral',
54];54];
55/** @enum {number} */
55const EXPRESSION_API = {56const EXPRESSION_API = {
56 local: 0,57 local: 0,
57 extras: 1,58 extras: 1,
@@ -920,18 +921,24 @@ async function setSpriteSetCommand(_, folder) {
920 return '';921 return '';
921}922}
922923
923async function classifyCommand(_, text) {924async function classifyCallback(/** @type {{api: string?}} */ { api = null }, text) {
924 if (!text) {925 if (!text) {
925 console.log('No text provided');926 toastr.warning('No text provided');
927 return '';
928 }
929 if (api && !Object.keys(EXPRESSION_API).includes(api)) {
930 toastr.warning('Invalid API provided');
926 return '';931 return '';
927 }932 }
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) {
930 toastr.warning('Text classification is disabled or not available');937 toastr.warning('Text classification is disabled or not available');
931 return '';938 return '';
932 }939 }
933940
934 const label = getExpressionLabel(text);941 const label = getExpressionLabel(text, expressionApi);
935 console.debug(`Classification result for "${text}": ${label}`);942 console.debug(`Classification result for "${text}": ${label}`);
936 return label;943 return label;
937}944}
@@ -1108,9 +1115,16 @@ function onTextGenSettingsReady(args) {
1108 }1115 }
1109}1116}
11101117
1111async 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 */
1125export async function getExpressionLabel(text, expressionsApi = extension_settings.expressions.api) {
1112 // Return if text is undefined, saving a costly fetch request1126 // Return if text is undefined, saving a costly fetch request
1113 if ((!modules.includes('classify') && extension_settings.expressions.api == EXPRESSION_API.extras) || !text) {1127 if ((!modules.includes('classify') && expressionsApi == EXPRESSION_API.extras) || !text) {
1114 return getFallbackExpression();1128 return getFallbackExpression();
1115 }1129 }
11161130
@@ -1121,7 +1135,7 @@ async function getExpressionLabel(text) {
1121 text = sampleClassifyText(text);1135 text = sampleClassifyText(text);
11221136
1123 try {1137 try {
1124 switch (extension_settings.expressions.api) {1138 switch (expressionsApi) {
1125 // Local BERT pipeline1139 // Local BERT pipeline
1126 case EXPRESSION_API.local: {1140 case EXPRESSION_API.local: {
1127 const localResult = await fetch('/api/extra/classify', {1141 const localResult = await fetch('/api/extra/classify', {
@@ -2105,7 +2119,15 @@ function migrateSettings() {
2105 }));2119 }));
2106 SlashCommandParser.addCommandObject(SlashCommand.fromProps({2120 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2107 name: 'classify',2121 name: 'classify',
2108 callback: classifyCommand,2122 callback: classifyCallback,
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 ],
2109 unnamedArgumentList: [2131 unnamedArgumentList: [
2110 new SlashCommandArgument(2132 new SlashCommandArgument(
2111 'text', [ARGUMENT_TYPE.STRING], true,2133 'text', [ARGUMENT_TYPE.STRING], true,
@@ -2117,6 +2139,9 @@ function migrateSettings() {
2117 Performs an emotion classification of the given text and returns a label.2139 Performs an emotion classification of the given text and returns a label.
2118 </div>2140 </div>
2119 <div>2141 <div>
2142 Allows to specify which Classifier API to perform the classification with.
2143 </div>
2144 <div>
2120 <strong>Example:</strong>2145 <strong>Example:</strong>
2121 <ul>2146 <ul>
2122 <li>2147 <li>