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)

29456b9bd22bd39bd72b016712d4d02a331e70f0

my-alt <250851737+my-alt-acct@users.noreply.github.com>

Signed
2 files changed, +13 -2Showing whitespace changes
public/scripts/textgen-settings.js+7 -0
@@ -730,12 +730,17 @@ async function getStatusTextgen() {
730730 const supportsChatTemplate = [textgen_types.KOBOLDCPP, textgen_types.LLAMACPP].includes(textgenerationwebui_settings.type);
731731
732732 if (supportsChatTemplate && (wantsInstructDerivation || wantsContextDerivation || wantsContextSize)) {
733+ const model = textgenerationwebui_settings.type === textgen_types.LLAMACPP
734+ ? textgenerationwebui_settings.llamacpp_model
735+ : undefined;
736+
733737 const response = await fetch('/api/backends/text-completions/props', {
734738 method: 'POST',
735739 headers: getRequestHeaders(),
736740 body: JSON.stringify({
737741 api_server: endpoint,
738742 api_type: textgenerationwebui_settings.type,
743+ model: model,
739744 }),
740745 });
741746
@@ -747,6 +752,7 @@ async function getStatusTextgen() {
747752
748753 if (wantsContextSize && 'default_generation_settings' in data) {
749754 const backend_max_context = data['default_generation_settings']['n_ctx'];
755+ if (backend_max_context && typeof backend_max_context === 'number') {
750756 const old_value = max_context;
751757 if (max_context !== backend_max_context) {
752758 setGenerationParamsFromPreset({ max_length: backend_max_context });
@@ -756,6 +762,7 @@ async function getStatusTextgen() {
756762 toastr.info(`${old_value} ⇒ ${max_context}`, 'Context Size Changed');
757763 }
758764 }
765+ }
759766 console.log(`We have chat template ${chat_template.split('\n')[0]}...`);
760767 const savedTemplate = power_user.model_templates_mappings[chat_template_hash];
761768 const derivedTemplate = await deriveTemplatesFromChatTemplate(chat_template, chat_template_hash);
src/endpoints/backends/text-completions.js+6 -2
@@ -243,7 +243,11 @@ router.post('/props', async function (request, response) {
243243 setAdditionalHeaders(request, args, baseUrl);
244244
245245 const apiType = request.body.api_type;
246246 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+ }
247251 const propsReply = await fetch(propsUrl, args);
248252
249253 if (!propsReply.ok) {
@@ -253,7 +257,7 @@ router.post('/props', async function (request, response) {
253257 /** @type {any} */
254258 const props = await propsReply.json();
255259 // TEMPORARY: llama.cpp's /props endpoint has a bug which replaces the last newline with a \0
256260 if (apiType === TEXTGEN_TYPES.LLAMACPP && props['chat_template'] && props['chat_template'].endsWith('\u0000')) {
257261 props['chat_template'] = props['chat_template'].slice(0, -1) + '\n';
258262 }
259263 props['chat_template_hash'] = createHash('sha256').update(props['chat_template']).digest('hex');