2
0

🚸 (openai) Implement retries if openai rate limit reached

This commit is contained in:
Baptiste Arnaud
2023-08-24 15:28:04 +02:00
parent 793218a673
commit d700af17e9
6 changed files with 131 additions and 11 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@typebot.io/js",
"version": "0.1.17",
"version": "0.1.18",
"description": "Javascript library to display typebots on your website",
"type": "module",
"main": "dist/index.js",

View File

@@ -3,9 +3,11 @@ import { guessApiHost } from '@/utils/guessApiHost'
import { isNotEmpty } from '@typebot.io/lib/utils'
let abortController: AbortController | null = null
const secondsToWaitBeforeRetries = 3
const maxRetryAttempts = 3
export const streamChat =
(context: ClientSideActionContext) =>
(context: ClientSideActionContext & { retryAttempt?: number }) =>
async (
messages: {
content?: string | undefined
@@ -36,6 +38,18 @@ export const streamChat =
)
if (!res.ok) {
if (
(context.retryAttempt ?? 0) < maxRetryAttempts &&
(res.status === 403 || res.status === 500 || res.status === 503)
) {
await new Promise((resolve) =>
setTimeout(resolve, secondsToWaitBeforeRetries * 1000)
)
return streamChat({
...context,
retryAttempt: (context.retryAttempt ?? 0) + 1,
})(messages, { onMessageStream })
}
return {
error: (await res.json()) || 'Failed to fetch the chat response.',
}