@@ -296,16 +296,20 @@ async def _translate_batch_high_quality(self, texts: List[str], batch_data: List
296296 delay = 60.0 / self ._MAX_REQUESTS_PER_MINUTE
297297 elapsed = now - OpenAIHighQualityTranslator ._GLOBAL_LAST_REQUEST_TS [self ._last_request_ts_key ]
298298 if elapsed < delay :
299- await asyncio . sleep ( delay - elapsed )
300- # 在请求前更新时间戳,确保下次计算的是从这次请求开始的间隔
301- OpenAIHighQualityTranslator . _GLOBAL_LAST_REQUEST_TS [ self . _last_request_ts_key ] = time . time ( )
299+ sleep_time = delay - elapsed
300+ self . logger . info ( f'Ratelimit sleep: { sleep_time :.2f } s' )
301+ await asyncio . sleep ( sleep_time )
302302
303303 response = await self .client .chat .completions .create (
304304 model = self .model ,
305305 messages = messages ,
306306 max_tokens = self .max_tokens ,
307307 temperature = self .temperature
308308 )
309+
310+ # 在API调用成功后立即更新时间戳,确保所有请求(包括重试)都被计入速率限制
311+ if self ._MAX_REQUESTS_PER_MINUTE > 0 :
312+ OpenAIHighQualityTranslator ._GLOBAL_LAST_REQUEST_TS [self ._last_request_ts_key ] = time .time ()
309313
310314 # 检查成功条件
311315 if response .choices and response .choices [0 ].message .content and response .choices [0 ].finish_reason != 'content_filter' :
@@ -485,9 +489,9 @@ async def _translate(self, from_lang: str, to_lang: str, queries: List[str], ctx
485489 delay = 60.0 / self ._MAX_REQUESTS_PER_MINUTE
486490 elapsed = now - OpenAIHighQualityTranslator ._GLOBAL_LAST_REQUEST_TS [self ._last_request_ts_key ]
487491 if elapsed < delay :
488- await asyncio . sleep ( delay - elapsed )
489- # 在请求前更新时间戳,确保下次计算的是从这次请求开始的间隔
490- OpenAIHighQualityTranslator . _GLOBAL_LAST_REQUEST_TS [ self . _last_request_ts_key ] = time . time ( )
492+ sleep_time = delay - elapsed
493+ self . logger . info ( f'Ratelimit sleep: { sleep_time :.2f } s' )
494+ await asyncio . sleep ( sleep_time )
491495
492496 response = await self .client .chat .completions .create (
493497 model = self .model ,
@@ -496,6 +500,10 @@ async def _translate(self, from_lang: str, to_lang: str, queries: List[str], ctx
496500 temperature = self .temperature
497501 )
498502
503+ # 在API调用成功后立即更新时间戳,确保所有请求(包括重试)都被计入速率限制
504+ if self ._MAX_REQUESTS_PER_MINUTE > 0 :
505+ OpenAIHighQualityTranslator ._GLOBAL_LAST_REQUEST_TS [self ._last_request_ts_key ] = time .time ()
506+
499507 if response .choices and response .choices [0 ].message .content :
500508 result = response .choices [0 ].message .content .strip ()
501509 translations = result .split ('\n ' )
0 commit comments