/api-url: Add VertexAI region management (#4808) * /api-url: Add VertexAI region management * Remove unneeded array copy

c932e9398878d0f7de0446f4fa19c4d326a8e7ba

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
1 files changed, +33 -2Ignore whitespace
public/scripts/slash-commands.js+33 -2
@@ -2552,6 +2552,7 @@ export function initDefaultSlashCommands() {
25522552 enumList: [
25532553 new SlashCommandEnumValue('custom', 'custom OpenAI-compatible', enumTypes.getBasedOnIndex(UNIQUE_APIS.findIndex(x => x === 'openai')), 'O'),
25542554 new SlashCommandEnumValue('zai', 'Z.AI', enumTypes.getBasedOnIndex(UNIQUE_APIS.findIndex(x => x === 'zai')), 'Z'),
2555+ new SlashCommandEnumValue('vertexai', 'Google Vertex AI', enumTypes.getBasedOnIndex(UNIQUE_APIS.findIndex(x => x === 'vertexai')), 'V'),
25552556 new SlashCommandEnumValue('kobold', 'KoboldAI Classic', enumTypes.getBasedOnIndex(UNIQUE_APIS.findIndex(x => x === 'kobold')), 'K'),
25562557 ...Object.values(textgen_types).map(api => new SlashCommandEnumValue(api, null, enumTypes.getBasedOnIndex(UNIQUE_APIS.findIndex(x => x === 'textgenerationwebui')), 'T')),
25572558 ],
@@ -2579,13 +2580,13 @@ export function initDefaultSlashCommands() {
25792580 ],
25802581 helpString: `
25812582 <div>
25822583 ${t`Set the API urlURL / server urlURL / endpoint for the currently selected API, including the port. If no argument is provided, it will return the current API url.`}
25832584 </div>
25842585 <div>
25852586 ${t`If a manual API is provided to <b>set</b> the URL, make sure to set <code>connect=false</code>, as auto-connect only works for the currently selected API, or consider switching to it with <code>/api</code> first.`}
25862587 </div>
25872588 <div>
25882589 ${t`This slash command works for most of the Text Completion sources, KoboldAI Classic, and also Custom OpenAI compatible and, Z.AI, and Google Vertex AI for the Chat Completion sources. If unsure which APIs are supported, check the auto-completion of the optional <code>api</code> argument of this command.`}
25892590 </div>
25902591 `,
25912592 }));
@@ -5186,6 +5187,36 @@ async function setApiUrlCallback({ api = null, connect = 'true', quiet = 'false'
51865187 return oai_settings.zai_endpoint || ZAI_ENDPOINT.COMMON;
51875188 }
51885189
5190+ const isCurrentlyVertexAI = main_api === 'openai' && oai_settings.chat_completion_source === chat_completion_sources.VERTEXAI;
5191+ if (api === chat_completion_sources.VERTEXAI || (!api && isCurrentlyVertexAI)) {
5192+ const defaultRegion = 'us-central1';
5193+ const permittedValues = Array
5194+ .from(document.querySelectorAll('#vertexai_region_suggestions option'))
5195+ .map(e => e instanceof HTMLOptionElement ? e.value : '')
5196+ .filter(x => x);
5197+
5198+ if (!url) {
5199+ return oai_settings.vertexai_region || defaultRegion;
5200+ }
5201+
5202+ if (!permittedValues.includes(url)) {
5203+ !isQuiet && toastr.info(t`Generation requests may fail.`, t`Unknown VertexAI region '${url}'`);
5204+ }
5205+
5206+ if (!isCurrentlyVertexAI && autoConnect) {
5207+ toastr.warning(t`VertexAI is not the currently selected API, so we cannot do an auto-connect. Consider switching to it via /api beforehand.`);
5208+ return '';
5209+ }
5210+
5211+ $('#vertexai_region').val(url).trigger('input');
5212+
5213+ if (autoConnect) {
5214+ $('#api_button_openai').trigger('click');
5215+ }
5216+
5217+ return oai_settings.vertexai_region || defaultRegion;
5218+ }
5219+
51895220 // Special handling for Kobold Classic API
51905221 const isCurrentlyKoboldClassic = main_api === 'kobold';
51915222 if (api === 'kobold' || (!api && isCurrentlyKoboldClassic)) {