Enhance text generation API to support model parameter for llama.cpp router mode. Update getStatusTextgen to include model in request body and improve context size handling. (#4914)
Signed| @@ -730,12 +730,17 @@ async function getStatusTextgen() { | |||
| 730 | const supportsChatTemplate = [textgen_types.KOBOLDCPP, textgen_types.LLAMACPP].includes(textgenerationwebui_settings.type); | 730 | const supportsChatTemplate = [textgen_types.KOBOLDCPP, textgen_types.LLAMACPP].includes(textgenerationwebui_settings.type); |
| 731 | 731 | ||
| 732 | if (supportsChatTemplate && (wantsInstructDerivation || wantsContextDerivation || wantsContextSize)) { | 732 | if (supportsChatTemplate && (wantsInstructDerivation || wantsContextDerivation || wantsContextSize)) { |
| 733 | const model = textgenerationwebui_settings.type === textgen_types.LLAMACPP | ||
| 734 | ? textgenerationwebui_settings.llamacpp_model | ||
| 735 | : undefined; | ||
| 736 | |||
| 733 | const response = await fetch('/api/backends/text-completions/props', { | 737 | const response = await fetch('/api/backends/text-completions/props', { |
| 734 | method: 'POST', | 738 | method: 'POST', |
| 735 | headers: getRequestHeaders(), | 739 | headers: getRequestHeaders(), |
| 736 | body: JSON.stringify({ | 740 | body: JSON.stringify({ |
| 737 | api_server: endpoint, | 741 | api_server: endpoint, |
| 738 | api_type: textgenerationwebui_settings.type, | 742 | api_type: textgenerationwebui_settings.type, |
| 743 | model: model, | ||
| 739 | }), | 744 | }), |
| 740 | }); | 745 | }); |
| 741 | 746 | ||
| @@ -747,6 +752,7 @@ async function getStatusTextgen() { | |||
| 747 | 752 | ||
| 748 | if (wantsContextSize && 'default_generation_settings' in data) { | 753 | if (wantsContextSize && 'default_generation_settings' in data) { |
| 749 | const backend_max_context = data['default_generation_settings']['n_ctx']; | 754 | const backend_max_context = data['default_generation_settings']['n_ctx']; |
| 755 | if (backend_max_context && typeof backend_max_context === 'number') { | ||
| 750 | const old_value = max_context; | 756 | const old_value = max_context; |
| 751 | if (max_context !== backend_max_context) { | 757 | if (max_context !== backend_max_context) { |
| 752 | setGenerationParamsFromPreset({ max_length: backend_max_context }); | 758 | setGenerationParamsFromPreset({ max_length: backend_max_context }); |
| @@ -756,6 +762,7 @@ async function getStatusTextgen() { | |||
| 756 | toastr.info(`${old_value} ⇒ ${max_context}`, 'Context Size Changed'); | 762 | toastr.info(`${old_value} ⇒ ${max_context}`, 'Context Size Changed'); |
| 757 | } | 763 | } |
| 758 | } | 764 | } |
| 765 | } | ||
| 759 | console.log(`We have chat template ${chat_template.split('\n')[0]}...`); | 766 | console.log(`We have chat template ${chat_template.split('\n')[0]}...`); |
| 760 | const savedTemplate = power_user.model_templates_mappings[chat_template_hash]; | 767 | const savedTemplate = power_user.model_templates_mappings[chat_template_hash]; |
| 761 | const derivedTemplate = await deriveTemplatesFromChatTemplate(chat_template, chat_template_hash); | 768 | const derivedTemplate = await deriveTemplatesFromChatTemplate(chat_template, chat_template_hash); |
| @@ -243,7 +243,11 @@ router.post('/props', async function (request, response) { | |||
| 243 | setAdditionalHeaders(request, args, baseUrl); | 243 | setAdditionalHeaders(request, args, baseUrl); |
| 244 | 244 | ||
| 245 | const apiType = request.body.api_type; | 245 | const apiType = request.body.api_type; |
| 246 | const propsUrl = baseUrl + '/props'; | 246 | let propsUrl = baseUrl + '/props'; |
| 247 | if (apiType === TEXTGEN_TYPES.LLAMACPP && request.body.model) { | ||
| 248 | propsUrl += `?model=${encodeURIComponent(request.body.model)}`; | ||
| 249 | console.debug(`Querying llama-server props with model parameter: ${request.body.model}`); | ||
| 250 | } | ||
| 247 | const propsReply = await fetch(propsUrl, args); | 251 | const propsReply = await fetch(propsUrl, args); |
| 248 | 252 | ||
| 249 | if (!propsReply.ok) { | 253 | if (!propsReply.ok) { |
| @@ -253,7 +257,7 @@ router.post('/props', async function (request, response) { | |||
| 253 | /** @type {any} */ | 257 | /** @type {any} */ |
| 254 | const props = await propsReply.json(); | 258 | const props = await propsReply.json(); |
| 255 | // TEMPORARY: llama.cpp's /props endpoint has a bug which replaces the last newline with a \0 | 259 | // TEMPORARY: llama.cpp's /props endpoint has a bug which replaces the last newline with a \0 |
| 256 | if (apiType === TEXTGEN_TYPES.LLAMACPP && props['chat_template'].endsWith('\u0000')) { | 260 | if (apiType === TEXTGEN_TYPES.LLAMACPP && props['chat_template'] && props['chat_template'].endsWith('\u0000')) { |
| 257 | props['chat_template'] = props['chat_template'].slice(0, -1) + '\n'; | 261 | props['chat_template'] = props['chat_template'].slice(0, -1) + '\n'; |
| 258 | } | 262 | } |
| 259 | props['chat_template_hash'] = createHash('sha256').update(props['chat_template']).digest('hex'); | 263 | props['chat_template_hash'] = createHash('sha256').update(props['chat_template']).digest('hex'); |