From b9ba4d160267b4d7e1a95b0a3c5e716d13a83621 Mon Sep 17 00:00:00 2001 From: robinnibor Date: Mon, 27 Mar 2023 20:41:02 +0800 Subject: [PATCH 1/2] add debug for textcompletion timeout --- services/openai.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/services/openai.js b/services/openai.js index 126a394b..603f9628 100644 --- a/services/openai.js +++ b/services/openai.js @@ -29,6 +29,18 @@ instance.interceptors.request.use((c) => { return c; }); +// Add an interceptor for error handling +instance.interceptors.response.use( + (response) => response, + (error) => { + // Log the error if it's a timeout error during chatCompletion + if (error.config && error.config.url.endsWith('/v1/completions') && error.code === 'ECONNABORTED') { + console.log('TextCompletion request timed out'); + } + return Promise.reject(error); + } +); + const createChatCompletion = ({ messages, }) => instance.post('/v1/chat/completions', { From a6f7f6fa39bf8c01821c0ef58c5321ef8a5daebd Mon Sep 17 00:00:00 2001 From: robinnibor Date: Mon, 27 Mar 2023 20:41:02 +0800 Subject: [PATCH 2/2] retry when getting 504 --- api/index.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/api/index.js b/api/index.js index 7bebc126..5d7bddba 100644 --- a/api/index.js +++ b/api/index.js @@ -27,10 +27,26 @@ app.get('/info', async (req, res) => { res.status(200).send({ currentVersion, latestVersion }); }); +const retryHandleEvents = async (events, retries = 5) => { + for (let i = 0; i < retries; i++) { + try { + await handleEvents(events); + return; + } catch (err) { + if (err.response?.status === 504) { + console.error(`Attempt ${i + 1} failed with a 504 error. Retrying...`); + } else { + throw err; + } + } + } + throw new Error('All retry attempts failed with a 504 error.'); +}; + app.post(config.APP_WEBHOOK_PATH, validateLineSignature, async (req, res) => { try { await storage.initialize(); - await handleEvents(req.body.events); + await retryHandleEvents(req.body.events); res.sendStatus(200); } catch (err) { console.error(err.message);