CC: Remove auto-retry on code 429
| @@ -1792,7 +1792,7 @@ router.post('/generate', function (request, response) { | |||
| 1792 | signal: controller.signal, | 1792 | signal: controller.signal, |
| 1793 | }; | 1793 | }; |
| 1794 | 1794 | ||
| 1795 | console.debug(requestBody); | 1795 | console.debug('Chat Completion request:', requestBody); |
| 1796 | 1796 | ||
| 1797 | makeRequest(config, response, request); | 1797 | makeRequest(config, response, request); |
| 1798 | 1798 | ||
| @@ -1801,10 +1801,8 @@ router.post('/generate', function (request, response) { | |||
| 1801 | * @param {import('node-fetch').RequestInit} config Fetch config | 1801 | * @param {import('node-fetch').RequestInit} config Fetch config |
| 1802 | * @param {express.Response} response Express response | 1802 | * @param {express.Response} response Express response |
| 1803 | * @param {express.Request} request Express request | 1803 | * @param {express.Request} request Express request |
| 1804 | * @param {Number} retries Number of retries left | ||
| 1805 | * @param {Number} timeout Request timeout in ms | ||
| 1806 | */ | 1804 | */ |
| 1807 | async function makeRequest(config, response, request, retries = 5, timeout = 5000) { | 1805 | async function makeRequest(config, response, request) { |
| 1808 | try { | 1806 | try { |
| 1809 | controller.signal.throwIfAborted(); | 1807 | controller.signal.throwIfAborted(); |
| 1810 | const fetchResponse = await fetch(endpointUrl, config); | 1808 | const fetchResponse = await fetch(endpointUrl, config); |
| @@ -1819,14 +1817,7 @@ router.post('/generate', function (request, response) { | |||
| 1819 | /** @type {any} */ | 1817 | /** @type {any} */ |
| 1820 | let json = await fetchResponse.json(); | 1818 | let json = await fetchResponse.json(); |
| 1821 | response.send(json); | 1819 | response.send(json); |
| 1822 | console.debug(json); | 1820 | console.debug('Chat Completion response:', json); |
| 1823 | console.debug(json?.choices?.[0]?.message); | ||
| 1824 | } else if (fetchResponse.status === 429 && retries > 0) { | ||
| 1825 | console.warn(`Out of quota, retrying in ${Math.round(timeout / 1000)}s`); | ||
| 1826 | setTimeout(() => { | ||
| 1827 | timeout *= 2; | ||
| 1828 | makeRequest(config, response, request, retries - 1, timeout); | ||
| 1829 | }, timeout); | ||
| 1830 | } else { | 1821 | } else { |
| 1831 | await handleErrorResponse(fetchResponse); | 1822 | await handleErrorResponse(fetchResponse); |
| 1832 | } | 1823 | } |
| @@ -1858,7 +1849,7 @@ router.post('/generate', function (request, response) { | |||
| 1858 | if (!response.headersSent) { | 1849 | if (!response.headersSent) { |
| 1859 | response.send({ error: { message }, quota_error: quota_error }); | 1850 | response.send({ error: { message }, quota_error: quota_error }); |
| 1860 | } else if (!response.writableEnded) { | 1851 | } else if (!response.writableEnded) { |
| 1861 | response.write(errorResponse); | 1852 | response.write(responseText); |
| 1862 | } else { | 1853 | } else { |
| 1863 | response.end(); | 1854 | response.end(); |
| 1864 | } | 1855 | } |