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