/api-url: Add VertexAI region management (#4808) * /api-url: Add VertexAI region management * Remove unneeded array copy
Signed| @@ -2552,6 +2552,7 @@ export function initDefaultSlashCommands() { | |||
| 2552 | enumList: [ | 2552 | enumList: [ |
| 2553 | new SlashCommandEnumValue('custom', 'custom OpenAI-compatible', enumTypes.getBasedOnIndex(UNIQUE_APIS.findIndex(x => x === 'openai')), 'O'), | 2553 | new SlashCommandEnumValue('custom', 'custom OpenAI-compatible', enumTypes.getBasedOnIndex(UNIQUE_APIS.findIndex(x => x === 'openai')), 'O'), |
| 2554 | new SlashCommandEnumValue('zai', 'Z.AI', enumTypes.getBasedOnIndex(UNIQUE_APIS.findIndex(x => x === 'zai')), 'Z'), | 2554 | 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'), | ||
| 2555 | new SlashCommandEnumValue('kobold', 'KoboldAI Classic', enumTypes.getBasedOnIndex(UNIQUE_APIS.findIndex(x => x === 'kobold')), 'K'), | 2556 | new SlashCommandEnumValue('kobold', 'KoboldAI Classic', enumTypes.getBasedOnIndex(UNIQUE_APIS.findIndex(x => x === 'kobold')), 'K'), |
| 2556 | ...Object.values(textgen_types).map(api => new SlashCommandEnumValue(api, null, enumTypes.getBasedOnIndex(UNIQUE_APIS.findIndex(x => x === 'textgenerationwebui')), 'T')), | 2557 | ...Object.values(textgen_types).map(api => new SlashCommandEnumValue(api, null, enumTypes.getBasedOnIndex(UNIQUE_APIS.findIndex(x => x === 'textgenerationwebui')), 'T')), |
| 2557 | ], | 2558 | ], |
| @@ -2579,13 +2580,13 @@ export function initDefaultSlashCommands() { | |||
| 2579 | ], | 2580 | ], |
| 2580 | helpString: ` | 2581 | helpString: ` |
| 2581 | <div> | 2582 | <div> |
| 2582 | ${t`Set the API url / server url for the currently selected API, including the port. If no argument is provided, it will return the current API url.`} | 2583 | ${t`Set the API URL / server URL / endpoint for the currently selected API, including the port. If no argument is provided, it will return the current API url.`} |
| 2583 | </div> | 2584 | </div> |
| 2584 | <div> | 2585 | <div> |
| 2585 | ${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.`} | 2586 | ${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.`} |
| 2586 | </div> | 2587 | </div> |
| 2587 | <div> | 2588 | <div> |
| 2588 | ${t`This slash command works for most of the Text Completion sources, KoboldAI Classic, and also Custom OpenAI compatible and Z.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.`} | 2589 | ${t`This slash command works for most of the Text Completion sources, KoboldAI Classic, and also Custom OpenAI compatible, 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.`} |
| 2589 | </div> | 2590 | </div> |
| 2590 | `, | 2591 | `, |
| 2591 | })); | 2592 | })); |
| @@ -5186,6 +5187,36 @@ async function setApiUrlCallback({ api = null, connect = 'true', quiet = 'false' | |||
| 5186 | return oai_settings.zai_endpoint || ZAI_ENDPOINT.COMMON; | 5187 | return oai_settings.zai_endpoint || ZAI_ENDPOINT.COMMON; |
| 5187 | } | 5188 | } |
| 5188 | 5189 | ||
| 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 | |||
| 5189 | // Special handling for Kobold Classic API | 5220 | // Special handling for Kobold Classic API |
| 5190 | const isCurrentlyKoboldClassic = main_api === 'kobold'; | 5221 | const isCurrentlyKoboldClassic = main_api === 'kobold'; |
| 5191 | if (api === 'kobold' || (!api && isCurrentlyKoboldClassic)) { | 5222 | if (api === 'kobold' || (!api && isCurrentlyKoboldClassic)) { |