Improve Scale JWT error handling

This commit is contained in:
Cohee 2024-04-16 18:59:01 +03:00
parent 3dcea41c4e
commit 8546490bcc
2 changed files with 48 additions and 18 deletions

View File

@ -1594,6 +1594,11 @@ async function sendAltScaleRequest(messages, logit_bias, signal, type) {
signal: signal, signal: signal,
}); });
if (!response.ok) {
tryParseStreamingError(response, await response.text());
throw new Error('Scale response does not indicate success.');
}
const data = await response.json(); const data = await response.json();
return data.output; return data.output;
} }

View File

@ -7,16 +7,18 @@ const { readSecret, SECRET_KEYS } = require('../secrets');
const router = express.Router(); const router = express.Router();
router.post('/generate', jsonParser, function (request, response) { router.post('/generate', jsonParser, async function (request, response) {
if (!request.body) return response.sendStatus(400); if (!request.body) return response.sendStatus(400);
fetch('https://dashboard.scale.com/spellbook/api/trpc/v2.variant.run', { try {
method: 'POST', const cookie = readSecret(request.user.directories, SECRET_KEYS.SCALE_COOKIE);
headers: {
'Content-Type': 'application/json', if (!cookie) {
'cookie': `_jwt=${readSecret(request.user.directories, SECRET_KEYS.SCALE_COOKIE)}`, console.log('No Scale cookie found');
}, return response.sendStatus(400);
body: JSON.stringify({ }
const body = {
json: { json: {
variant: { variant: {
name: 'New Variant', name: 'New Variant',
@ -59,18 +61,41 @@ router.post('/generate', jsonParser, function (request, response) {
'modelParameters.logprobs': ['undefined'], 'modelParameters.logprobs': ['undefined'],
}, },
}, },
}), };
})
.then(res => res.json()) console.log('Scale request:', body);
.then(data => {
console.log(data.result.data.json.outputs[0]); const result = await fetch('https://dashboard.scale.com/spellbook/api/trpc/v2.variant.run', {
return response.send({ output: data.result.data.json.outputs[0] }); method: 'POST',
}) headers: {
.catch((error) => { 'Content-Type': 'application/json',
console.error('Error:', error); 'cookie': `_jwt=${cookie}`,
return response.send({ error: true }); },
timeout: 0,
body: JSON.stringify(body),
}); });
if (!result.ok) {
const text = await result.text();
console.log('Scale request failed', result.statusText, text);
return response.status(500).send({ error: { message: result.statusText } });
}
const data = await result.json();
const output = data?.result?.data?.json?.outputs?.[0] || '';
console.log('Scale response:', data);
if (!output) {
console.warn('Scale response is empty');
return response.sendStatus(500).send({ error: { message: 'Empty response' } });
}
return response.json({ output });
} catch (error) {
console.log(error);
return response.sendStatus(500);
}
}); });
module.exports = { router }; module.exports = { router };