template derivation: add support for llama.cpp server backend
Signed| @@ -59,6 +59,6 @@ export async function deriveTemplatesFromChatTemplate(chat_template, hash) { | |||
| 59 | if (hash in derivations) { | 59 | if (hash in derivations) { |
| 60 | return derivations[hash]; | 60 | return derivations[hash]; |
| 61 | } | 61 | } |
| 62 | console.log(`Unknown chat template hash: ${hash}`); | 62 | console.log(`Unknown chat template hash: ${hash} for [${chat_template}]`); |
| 63 | return null; | 63 | return null; |
| 64 | } | 64 | } |
| @@ -231,6 +231,9 @@ router.post('/status', jsonParser, async function (request, response) { | |||
| 231 | } catch (error) { | 231 | } catch (error) { |
| 232 | console.error(`Failed to fetch chat template info: ${error}`); | 232 | console.error(`Failed to fetch chat template info: ${error}`); |
| 233 | } | 233 | } |
| 234 | } else if (apiType == TEXTGEN_TYPES.LLAMACPP) { | ||
| 235 | // the /props endpoint includes chat template | ||
| 236 | response.setHeader('x-supports-chat-template', 'true'); | ||
| 234 | } | 237 | } |
| 235 | 238 | ||
| 236 | return response.send({ result, data: data.data }); | 239 | return response.send({ result, data: data.data }); |
| @@ -240,6 +243,11 @@ router.post('/status', jsonParser, async function (request, response) { | |||
| 240 | } | 243 | } |
| 241 | }); | 244 | }); |
| 242 | 245 | ||
| 246 | const chat_template_endpoints = { | ||
| 247 | koboldcpp: '/api/extra/chat_template', | ||
| 248 | llamacpp: '/props', | ||
| 249 | } | ||
| 250 | |||
| 243 | router.post('/chat_template', jsonParser, async function (request, response) { | 251 | router.post('/chat_template', jsonParser, async function (request, response) { |
| 244 | if (!request.body.api_server) return response.sendStatus(400); | 252 | if (!request.body.api_server) return response.sendStatus(400); |
| 245 | 253 | ||
| @@ -251,7 +259,8 @@ router.post('/chat_template', jsonParser, async function (request, response) { | |||
| 251 | 259 | ||
| 252 | setAdditionalHeaders(request, args, baseUrl); | 260 | setAdditionalHeaders(request, args, baseUrl); |
| 253 | 261 | ||
| 254 | const chatTemplateUrl = baseUrl + '/api/extra/chat_template'; | 262 | const apiType = request.body.api_type; |
| 263 | const chatTemplateUrl = baseUrl + chat_template_endpoints[apiType]; | ||
| 255 | const chatTemplateReply = await fetch(chatTemplateUrl, args); | 264 | const chatTemplateReply = await fetch(chatTemplateUrl, args); |
| 256 | 265 | ||
| 257 | if (!chatTemplateReply.ok) { | 266 | if (!chatTemplateReply.ok) { |
| @@ -261,7 +270,12 @@ router.post('/chat_template', jsonParser, async function (request, response) { | |||
| 261 | 270 | ||
| 262 | /** @type {any} */ | 271 | /** @type {any} */ |
| 263 | const chatTemplate = await chatTemplateReply.json(); | 272 | 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 | } | ||
| 264 | chatTemplate['chat_template_hash'] = createHash('sha256').update(chatTemplate['chat_template']).digest('hex'); | 277 | chatTemplate['chat_template_hash'] = createHash('sha256').update(chatTemplate['chat_template']).digest('hex'); |
| 278 | console.log(`We have chat template stuff: ${JSON.stringify(chatTemplate)}`); | ||
| 265 | return response.send(chatTemplate); | 279 | return response.send(chatTemplate); |
| 266 | } catch (error) { | 280 | } catch (error) { |
| 267 | console.error(error); | 281 | console.error(error); |