From becd17d7d2e62754ad2e017eb14eea29b5e8956a Mon Sep 17 00:00:00 2001 From: sasha0552 Date: Wed, 10 Apr 2024 08:11:59 +0000 Subject: [PATCH] Add some llama.cpp-specific endpoints --- src/endpoints/backends/text-completions.js | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/src/endpoints/backends/text-completions.js b/src/endpoints/backends/text-completions.js index e02bfbc49..a9fa14cc5 100644 --- a/src/endpoints/backends/text-completions.js +++ b/src/endpoints/backends/text-completions.js @@ -473,6 +473,76 @@ llamacpp.post('/caption-image', jsonParser, async function (request, response) { } }); +llamacpp.post('/props', jsonParser, async function (request, response) { + try { + if (!request.body.server_url) { + return response.sendStatus(400); + } + + console.log('LlamaCpp props request:', request.body); + const baseUrl = trimV1(request.body.server_url); + + const fetchResponse = await fetch(`${baseUrl}/props`, { + method: 'GET', + timeout: 0, + }); + + if (!fetchResponse.ok) { + console.log('LlamaCpp props error:', fetchResponse.status, fetchResponse.statusText); + return response.status(500).send({ error: true }); + } + + const data = await fetchResponse.json(); + console.log('LlamaCpp props response:', data); + + return response.send(data); + + } catch (error) { + console.error(error); + return response.status(500); + } +}); + +llamacpp.post('/slots', jsonParser, async function (request, response) { + try { + if (!request.body.server_url) { + return response.sendStatus(400); + } + if (!/^\d+$/.test(request.body.id_slot)) { + return response.sendStatus(400); + } + if (!/^(erase|restore|save)$/.test(request.body.action)) { + return response.sendStatus(400); + } + + console.log('LlamaCpp slots request:', request.body); + const baseUrl = trimV1(request.body.server_url); + + const fetchResponse = await fetch(`${baseUrl}/slots/${request.body.id_slot}?action=${request.body.action}`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + timeout: 0, + body: JSON.stringify({ + filename: `${request.body.filename}`, + }), + }); + + if (!fetchResponse.ok) { + console.log('LlamaCpp slots error:', fetchResponse.status, fetchResponse.statusText); + return response.status(500).send({ error: true }); + } + + const data = await fetchResponse.json(); + console.log('LlamaCpp slots response:', data); + + return response.send(data); + + } catch (error) { + console.error(error); + return response.status(500); + } +}); + router.use('/ollama', ollama); router.use('/llamacpp', llamacpp);