@@ -226,9 +226,9 @@ def _build_system_prompt(self, source_lang: str, target_lang: str, custom_prompt
226226 final_prompt += base_prompt
227227 return final_prompt
228228
229- def _build_user_prompt (self , texts : List [str ], ctx : Any ) -> str :
229+ def _build_user_prompt (self , texts : List [str ], ctx : Any , retry_attempt : int = 0 , retry_reason : str = "" ) -> str :
230230 """构建用户提示词(纯文本版)- 使用统一方法,只包含上下文和待翻译文本"""
231- return self ._build_user_prompt_for_texts (texts , ctx , self .prev_context )
231+ return self ._build_user_prompt_for_texts (texts , ctx , self .prev_context , retry_attempt = retry_attempt , retry_reason = retry_reason )
232232
233233 def _get_system_instruction (self , source_lang : str , target_lang : str , custom_prompt_json : Dict [str , Any ] = None , line_break_prompt_json : Dict [str , Any ] = None ) -> str :
234234 """获取完整的系统指令(包含断句提示词、自定义提示词和基础系统提示词)"""
@@ -257,20 +257,15 @@ async def _translate_batch(self, texts: List[str], source_lang: str, target_lang
257257 self .logger .error ("Gemini客户端初始化失败" )
258258 return texts
259259
260- # 构建用户提示词(只包含上下文和待翻译文本,不包含系统指令)
261- user_prompt = self ._build_user_prompt (texts , ctx )
260+ # 初始化重试信息
261+ retry_attempt = 0
262+ retry_reason = ""
262263
263264 # 发送请求
264265 max_retries = self .attempts
265266 attempt = 0
266267 is_infinite = max_retries == - 1
267268 local_attempt = 0 # 本次批次的尝试次数
268-
269- # 动态构建请求参数 - 默认总是发送安全设置
270- request_args = {
271- "contents" : user_prompt ,
272- "safety_settings" : self .safety_settings
273- }
274269
275270 # 标记是否需要回退(不发送安全设置)
276271 should_retry_without_safety = False
@@ -294,6 +289,15 @@ def generate_content_with_logging(**kwargs):
294289 if local_attempt > self ._SPLIT_THRESHOLD and len (texts ) > 1 and split_level < self ._MAX_SPLIT_ATTEMPTS :
295290 self .logger .warning (f"Triggering split after { local_attempt } local attempts" )
296291 raise self .SplitException (local_attempt , texts )
292+
293+ # 构建用户提示词(包含重试信息以避免缓存)
294+ user_prompt = self ._build_user_prompt (texts , ctx , retry_attempt = retry_attempt , retry_reason = retry_reason )
295+
296+ # 动态构建请求参数 - 默认总是发送安全设置
297+ request_args = {
298+ "contents" : user_prompt ,
299+ "safety_settings" : self .safety_settings
300+ }
297301
298302 try :
299303 # RPM限制
@@ -371,9 +375,10 @@ def generate_content_with_logging(**kwargs):
371375
372376 # Strict validation: must match input count
373377 if len (translations ) != len (texts ):
374- attempt += 1
378+ retry_attempt += 1
379+ retry_reason = f"Translation count mismatch: expected { len (texts )} , got { len (translations )} "
375380 log_attempt = f"{ attempt } /{ max_retries } " if not is_infinite else f"Attempt { attempt } "
376- self .logger .warning (f"[{ log_attempt } ] Translation count mismatch: expected { len ( texts ) } , got { len ( translations ) } . Retrying..." )
381+ self .logger .warning (f"[{ log_attempt } ] { retry_reason } . Retrying..." )
377382 self .logger .warning (f"Expected texts: { texts } " )
378383 self .logger .warning (f"Got translations: { translations } " )
379384
@@ -386,9 +391,10 @@ def generate_content_with_logging(**kwargs):
386391 # 质量验证:检查空翻译、合并翻译、可疑符号等
387392 is_valid , error_msg = self ._validate_translation_quality (texts , translations )
388393 if not is_valid :
389- attempt += 1
394+ retry_attempt += 1
395+ retry_reason = f"Quality check failed: { error_msg } "
390396 log_attempt = f"{ attempt } /{ max_retries } " if not is_infinite else f"Attempt { attempt } "
391- self .logger .warning (f"[{ log_attempt } ] Quality check failed: { error_msg } . Retrying..." )
397+ self .logger .warning (f"[{ log_attempt } ] { retry_reason } . Retrying..." )
392398
393399 if not is_infinite and attempt >= max_retries :
394400 raise Exception (f"Quality check failed after { max_retries } attempts: { error_msg } " )
@@ -404,9 +410,10 @@ def generate_content_with_logging(**kwargs):
404410
405411 # BR检查:检查翻译结果是否包含必要的[BR]标记
406412 if not self ._validate_br_markers (translations , queries = texts , ctx = ctx ):
407- attempt += 1
413+ retry_attempt += 1
414+ retry_reason = "BR markers missing in translations"
408415 log_attempt = f"{ attempt } /{ max_retries } " if not is_infinite else f"Attempt { attempt } "
409- self .logger .warning (f"[{ log_attempt } ] BR markers missing , retrying..." )
416+ self .logger .warning (f"[{ log_attempt } ] { retry_reason } , retrying..." )
410417
411418 # 如果达到最大重试次数,抛出友好的异常
412419 if not is_infinite and attempt >= max_retries :
0 commit comments