Add llama.cpp slots info endpoint (#2070)

This commit is contained in:
sasha0552 2024-04-11 20:53:32 +00:00 committed by GitHub
parent 356f1f9c39
commit 7348199f73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 24 additions and 12 deletions

View File

@ -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}`, {
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.filename}`,
filename: request.body.action !== "erase" ? `${request.body.filename}` : undefined,
}),
});
}
if (!fetchResponse.ok) {
console.log('LlamaCpp slots error:', fetchResponse.status, fetchResponse.statusText);