From 13c8732c42891e64dcd751b952aaa622a63773c6 Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 27 Jul 2026 18:54:44 +0800 Subject: [PATCH] fix: add thinking diagnostic log + skip retry for code-only chunks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two changes to finish off the slow-translation issue: 1. agent.py: log the resolved thinking config at Agent init. Lets operators confirm from task logs that the thinking-disable fix is actually running (e.g. 'field=extra_body, applied_value={enable_thinking: False}') — critical for distinguishing 'fix not deployed' from 'fix not enough'. 2. segments_agent.py: when the whole chunk returns identical to source, skip the retry if the source has fewer than two 4+ letter English words. Technical docs (this client's WI/PRD docs) have many chunks that are pure codes/numbers/abbreviations (FRM-QAD-SQM-018, HNB-020, V1.0) — LLM correctly returns them unchanged, but the old code retried 3x. 4-letter threshold avoids matching 3-letter abbrevs like FRM/QAD/HNB. --- docutranslate/agents/agent.py | 10 ++++++++++ docutranslate/agents/segments_agent.py | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/docutranslate/agents/agent.py b/docutranslate/agents/agent.py index a009315..a067b39 100644 --- a/docutranslate/agents/agent.py +++ b/docutranslate/agents/agent.py @@ -540,6 +540,16 @@ class Agent: self.mt_domains = getattr(config, "custom_prompt", None) self.mt_glossary_dict = getattr(config, "glossary_dict", None) + # 诊断日志:确认 thinking 配置实际生效(用于排查 qwen3 推理模式未关闭的问题) + tm = get_thinking_mode(self.provider, self.model_id) + if tm is not None: + field, val_en, val_dis = tm + target_val = val_en if self.thinking == "enable" else val_dis + self.logger.info( + f"Agent thinking config: provider={self.provider}, model={self.model_id}, " + f"mode={self.thinking}, field={field}, applied_value={target_val}" + ) + def _estimate_tokens(self, text: str) -> int: """ 改进的纯 Python 估算,适配更多语言。 diff --git a/docutranslate/agents/segments_agent.py b/docutranslate/agents/segments_agent.py index 01ce83a..d21ea2e 100644 --- a/docutranslate/agents/segments_agent.py +++ b/docutranslate/agents/segments_agent.py @@ -137,6 +137,18 @@ class SegmentsTranslateAgent(Agent): raise AgentResultError(f"Agent返回结果不是dict的json形式, result: {result}") if repaired_result == original_chunk: + # 启发式:如果原文几乎全是代码/数字/编号(真正的英文单词很少), + # LLM 原样返回是合理的,不应触发重试。技术文档常有这种 chunk。 + # 用 4+ 字母的英文词作判据,排除 FRM/QAD/HNB 这类 3 字母缩写。 + original_text = "".join(str(v) for v in original_chunk.values()) + translatable_words = re.findall(r"\b[A-Za-z]{4,}\b", original_text) + if len(translatable_words) < 2: + logger.info( + f"翻译结果与原文相同,但原文几乎全是代码/数字/缩写(4字母以上词 {len(translatable_words)} 个),跳过重试。" + ) + for key, value in repaired_result.items(): + repaired_result[key] = str(value) + return repaired_result raise AgentResultError("翻译结果与原文完全相同,疑似翻译失败,将进行重试。") original_keys = set(original_chunk.keys())