fix(pdf2zh): Add error handling for OpenAI API rate limit#716
Merged
awwaawwa merged 6 commits intoPDFMathTranslate:mainfrom Mar 6, 2025
Merged
fix(pdf2zh): Add error handling for OpenAI API rate limit#716awwaawwa merged 6 commits intoPDFMathTranslate:mainfrom
awwaawwa merged 6 commits intoPDFMathTranslate:mainfrom
Conversation
Contributor
highkay
commented
Mar 3, 2025
- 在 OpenAITranslator 类的 do_translate 方法中添加了对 RateLimitError 的捕获
- 当遇到速率限制错误时,程序将暂停 6 秒后重试一次
- 这种处理方式可以避免因频繁请求导致的接口限制问题,提高程序的稳定性
- 在 OpenAITranslator 类的 do_translate 方法中添加了对 RateLimitError 的捕获 - 当遇到速率限制错误时,程序将暂停 6 秒后重试一次 - 这种处理方式可以避免因频繁请求导致的接口限制问题,提高程序的稳定性
Collaborator
|
跑下 |
Collaborator
|
重试可以参考一下这个,使用 tenacity 库,然后用 wait_exponential 策略~ |
- 引入 tenacity 库以实现重试机制 - 在 do_translate 方法中添加重试装饰器,处理 RateLimitError - 配置重试参数:最多重试 100 次,指数退避等待时间 - 在每次重试前记录警告日志 - 移除原有的 sleep 和 递归重试逻辑
- 移除了 OpenAITranslator 类中的 try-except 块 - 删除了速率限制错误的重试逻辑 - 简化了错误响应的处理流程
Contributor
There was a problem hiding this comment.
PR Overview
This pull request adds rate limit error handling to the OpenAI translation endpoint to improve stability when too many requests are made. Key changes include the addition of a tenacity retry decorator for handling RateLimitError, an updated logging message reflecting retry attempts, and manual error handling with a sleep-and-retry approach in the translation function.
Reviewed Changes
| File | Description |
|---|---|
| pdf2zh/translator.py | Introduces rate limit error handling via tenacity and manual handling of RateLimitError |
Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Collaborator
|
Thank you for your contribution. According to https://funstory-ai.github.io/BabelDOC/CONTRIBUTOR_REWARD/ , you can apply for a monthly membership redemption code for Immersive Translate. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.