-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(pdf2zh): Add error handling for OpenAI API rate limit #716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
highkay
commented
Mar 3, 2025
- 在 OpenAITranslator 类的 do_translate 方法中添加了对 RateLimitError 的捕获
- 当遇到速率限制错误时,程序将暂停 6 秒后重试一次
- 这种处理方式可以避免因频繁请求导致的接口限制问题,提高程序的稳定性
- 在 OpenAITranslator 类的 do_translate 方法中添加了对 RateLimitError 的捕获 - 当遇到速率限制错误时,程序将暂停 6 秒后重试一次 - 这种处理方式可以避免因频繁请求导致的接口限制问题,提高程序的稳定性
|
跑下 |
|
重试可以参考一下这个,使用 tenacity 库,然后用 wait_exponential 策略~ |
- 引入 tenacity 库以实现重试机制 - 在 do_translate 方法中添加重试装饰器,处理 RateLimitError - 配置重试参数:最多重试 100 次,指数退避等待时间 - 在每次重试前记录警告日志 - 移除原有的 sleep 和 递归重试逻辑
- 移除了 OpenAITranslator 类中的 try-except 块 - 删除了速率限制错误的重试逻辑 - 简化了错误响应的处理流程
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 <[email protected]>
|
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. |