Skip to content

Commit

Permalink
🚸 (openai) Implement retries if openai rate limit reached
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Aug 24, 2023
1 parent 793218a commit d700af1
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 11 deletions.
2 changes: 1 addition & 1 deletion apps/viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@trpc/server": "10.34.0",
"@typebot.io/prisma": "workspace:*",
"@typebot.io/nextjs": "workspace:*",
"ai": "2.1.30",
"ai": "2.2.8",
"aws-sdk": "2.1415.0",
"bot-engine": "workspace:*",
"cors": "2.8.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/js/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.',
}
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/nextjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/nextjs",
"version": "0.1.17",
"version": "0.1.18",
"description": "Convenient library to display typebots on your Next.js website",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/react",
"version": "0.1.17",
"version": "0.1.18",
"description": "Convenient library to display typebots on your React app",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
118 changes: 112 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d700af1

Please sign in to comment.