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