template derivation: add support for llama.cpp server backend

feb1b91619d000fb9bb5a25f8a051aab485b5f8d

Karl-Johan Alm <karljohan-alm@garage.co.jp>

Signed
2 files changed, +16 -2Ignore whitespace
public/scripts/chat-cemplates.js+1 -1
@@ -59,6 +59,6 @@ export async function deriveTemplatesFromChatTemplate(chat_template, hash) {
5959 if (hash in derivations) {
6060 return derivations[hash];
6161 }
6262 console.log(`Unknown chat template hash: ${hash} for [${chat_template}]`);
6363 return null;
6464}
src/endpoints/backends/text-completions.js+15 -1
@@ -231,6 +231,9 @@ router.post('/status', jsonParser, async function (request, response) {
231231 } catch (error) {
232232 console.error(`Failed to fetch chat template info: ${error}`);
233233 }
234+ } else if (apiType == TEXTGEN_TYPES.LLAMACPP) {
235+ // the /props endpoint includes chat template
236+ response.setHeader('x-supports-chat-template', 'true');
234237 }
235238
236239 return response.send({ result, data: data.data });
@@ -240,6 +243,11 @@ router.post('/status', jsonParser, async function (request, response) {
240243 }
241244});
242245
246+const chat_template_endpoints = {
247+ koboldcpp: '/api/extra/chat_template',
248+ llamacpp: '/props',
249+}
250+
243251router.post('/chat_template', jsonParser, async function (request, response) {
244252 if (!request.body.api_server) return response.sendStatus(400);
245253
@@ -251,7 +259,8 @@ router.post('/chat_template', jsonParser, async function (request, response) {
251259
252260 setAdditionalHeaders(request, args, baseUrl);
253261
254- const chatTemplateUrl = baseUrl + '/api/extra/chat_template';
262+ const apiType = request.body.api_type;
263+ const chatTemplateUrl = baseUrl + chat_template_endpoints[apiType];
255264 const chatTemplateReply = await fetch(chatTemplateUrl, args);
256265
257266 if (!chatTemplateReply.ok) {
@@ -261,7 +270,12 @@ router.post('/chat_template', jsonParser, async function (request, response) {
261270
262271 /** @type {any} */
263272 const chatTemplate = await chatTemplateReply.json();
273+ // TEMPORARY: llama.cpp's /props endpoint includes a \u0000 at the end of the chat template, resulting in mismatching hashes
274+ if (apiType === TEXTGEN_TYPES.LLAMACPP && chatTemplate['chat_template'].endsWith('\u0000')) {
275+ chatTemplate['chat_template'] = chatTemplate['chat_template'].slice(0, -1);
276+ }
264277 chatTemplate['chat_template_hash'] = createHash('sha256').update(chatTemplate['chat_template']).digest('hex');
278+ console.log(`We have chat template stuff: ${JSON.stringify(chatTemplate)}`);
265279 return response.send(chatTemplate);
266280 } catch (error) {
267281 console.error(error);