From 7348199f732d5dc1092e4439940154d4d1cd04a2 Mon Sep 17 00:00:00 2001 From: sasha0552 Date: Thu, 11 Apr 2024 20:53:32 +0000 Subject: [PATCH] Add llama.cpp slots info endpoint (#2070) --- src/endpoints/backends/text-completions.js | 36 ++++++++++++++-------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/endpoints/backends/text-completions.js b/src/endpoints/backends/text-completions.js index a9fa14cc5..8e0ebba8d 100644 --- a/src/endpoints/backends/text-completions.js +++ b/src/endpoints/backends/text-completions.js @@ -508,24 +508,36 @@ llamacpp.post('/slots', jsonParser, async function (request, response) { 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)) { + if (!/^(erase|info|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}`, - }), - }); + let fetchResponse; + if (request.body.action === "info") { + fetchResponse = await fetch(`${baseUrl}/slots`, { + method: 'GET', + timeout: 0, + }); + } else { + if (!/^\d+$/.test(request.body.id_slot)) { + return response.sendStatus(400); + } + if (request.body.action !== "erase" && !request.body.filename) { + return response.sendStatus(400); + } + + 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.action !== "erase" ? `${request.body.filename}` : undefined, + }), + }); + } if (!fetchResponse.ok) { console.log('LlamaCpp slots error:', fetchResponse.status, fetchResponse.statusText);