Merge pull request #3980 from SillyTavern/fix-lcpp-caption Fix llama.cpp captioning
Signed| @@ -22,7 +22,7 @@ export async function getMultimodalCaption(base64Img, prompt) { | ||
| 22 | 22 | |
| 23 | 23 | throwIfInvalidModel(useReverseProxy); |
| 24 | 24 | |
| 25 | 25 | const noPrefix = ['ollama', 'llamacpp'].includes(extension_settings.caption.multimodal_api); |
| 26 | 26 | |
| 27 | 27 | if (noPrefix && base64Img.startsWith('data:image/')) { |
| 28 | 28 | base64Img = base64Img.split(',')[1]; |
| @@ -97,8 +97,6 @@ export async function getMultimodalCaption(base64Img, prompt) { | ||
| 97 | 97 | return '/api/google/caption-image'; |
| 98 | 98 | case 'anthropic': |
| 99 | 99 | return '/api/anthropic/caption-image'; |
| 100 | - case 'llamacpp': | |
| 101 | - return '/api/backends/text-completions/llamacpp/caption-image'; | |
| 102 | 100 | case 'ollama': |
| 103 | 101 | return '/api/backends/text-completions/ollama/caption-image'; |
| 104 | 102 | default: |
| @@ -502,51 +502,6 @@ ollama.post('/caption-image', async function (request, response) { | ||
| 502 | 502 | |
| 503 | 503 | const llamacpp = express.Router(); |
| 504 | 504 | |
| 505 | -llamacpp.post('/caption-image', async function (request, response) { | |
| 506 | - try { | |
| 507 | - if (!request.body.server_url) { | |
| 508 | - return response.sendStatus(400); | |
| 509 | - } | |
| 510 | - | |
| 511 | - console.debug('LlamaCpp caption request:', request.body); | |
| 512 | - const baseUrl = trimV1(request.body.server_url); | |
| 513 | - | |
| 514 | - const fetchResponse = await fetch(`${baseUrl}/completion`, { | |
| 515 | - method: 'POST', | |
| 516 | - headers: { 'Content-Type': 'application/json' }, | |
| 517 | - body: JSON.stringify({ | |
| 518 | - prompt: `USER:[img-1]${String(request.body.prompt).trim()}\nASSISTANT:`, | |
| 519 | - image_data: [{ data: request.body.image, id: 1 }], | |
| 520 | - temperature: 0.1, | |
| 521 | - stream: false, | |
| 522 | - stop: ['USER:', '</s>'], | |
| 523 | - }), | |
| 524 | - }); | |
| 525 | - | |
| 526 | - if (!fetchResponse.ok) { | |
| 527 | - console.error('LlamaCpp caption error:', fetchResponse.status, fetchResponse.statusText); | |
| 528 | - return response.status(500).send({ error: true }); | |
| 529 | - } | |
| 530 | - | |
| 531 | - /** @type {any} */ | |
| 532 | - const data = await fetchResponse.json(); | |
| 533 | - console.debug('LlamaCpp caption response:', data); | |
| 534 | - | |
| 535 | - const caption = data?.content || ''; | |
| 536 | - | |
| 537 | - if (!caption) { | |
| 538 | - console.error('LlamaCpp caption is empty.'); | |
| 539 | - return response.status(500).send({ error: true }); | |
| 540 | - } | |
| 541 | - | |
| 542 | - return response.send({ caption }); | |
| 543 | - | |
| 544 | - } catch (error) { | |
| 545 | - console.error(error); | |
| 546 | - return response.sendStatus(500); | |
| 547 | - } | |
| 548 | -}); | |
| 549 | - | |
| 550 | 505 | llamacpp.post('/props', async function (request, response) { |
| 551 | 506 | try { |
| 552 | 507 | if (!request.body.server_url) { |
| @@ -45,6 +45,10 @@ router.post('/caption-image', async (request, response) => { | ||
| 45 | 45 | key = readSecret(request.user.directories, SECRET_KEYS.KOBOLDCPP); |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | + if (request.body.api === 'llamacpp') { | |
| 49 | + key = readSecret(request.user.directories, SECRET_KEYS.LLAMACPP); | |
| 50 | + } | |
| 51 | + | |
| 48 | 52 | if (request.body.api === 'vllm') { |
| 49 | 53 | key = readSecret(request.user.directories, SECRET_KEYS.VLLM); |
| 50 | 54 | } |
| @@ -69,7 +73,8 @@ router.post('/caption-image', async (request, response) => { | ||
| 69 | 73 | key = readSecret(request.user.directories, SECRET_KEYS.XAI); |
| 70 | 74 | } |
| 71 | 75 | |
| 72 | 76 | if (!key &&const !request.body.reverse_proxynoKeyTypes &&= ['custom', 'ooba', 'koboldcpp', 'vllm'].includes(request.body.api) === false), {'llamacpp']; |
| 77 | + if (!key && !request.body.reverse_proxy && !noKeyTypes.includes(request.body.api)) { | |
| 73 | 78 | console.warn('No key found for API', request.body.api); |
| 74 | 79 | return response.sendStatus(400); |
| 75 | 80 | } |
| @@ -156,7 +161,7 @@ router.post('/caption-image', async (request, response) => { | ||
| 156 | 161 | }); |
| 157 | 162 | } |
| 158 | 163 | |
| 159 | 164 | if (request.body.api === ['koboldcpp', ||'vllm', 'llamacpp'].includes(request.body.api === 'vllm')) { |
| 160 | 165 | apiUrl = `${trimV1(request.body.server_url)}/v1/chat/completions`; |
| 161 | 166 | } |
| 162 | 167 | |