From f374088211c821e946968723add5ddc56f40fd8e Mon Sep 17 00:00:00 2001 From: Henrik Jess Nielsen Date: Fri, 30 Oct 2026 11:19:40 +0100 Subject: [PATCH] =?UTF-8?q?fix(offers):=20DE=E2=86=92DA=20translation=20re?= =?UTF-8?q?turned=20nothing=20=E2=80=94=20disable=20thinking,=20batch=20bi?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prod showed 0 of 467 headings translated while AI matching worked. Cause: Haiku 5.5 defaults thinking on, which consumed the 4000-token budget on the 40-item translation batches and truncated the JSON, so every batch fell back to German. Matching (one small response) slipped through. - Disable thinking on both Claude calls (mechanical JSON tasks), via a shared _claude_json helper that retries without the flag if a model rejects it - Raise max_tokens (16000 translate / 8000 match) so output fits even if thinking stays on - Batch ~all headings in 1-2 calls (250) instead of 40, so the preamble isn't repeated per batch and we don't hammer the rate limit (cheaper, as suggested) --- app.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/app.py b/app.py index 9abd138..bba73e7 100644 --- a/app.py +++ b/app.py @@ -33,7 +33,10 @@ OFFERS_REFRESH_HOURS = float(os.environ.get("OFFERS_REFRESH_HOURS", "20")) # ANTHROPIC_API_KEY + Haiku pattern). Cached per heading, so only new headings # cost a call. Without a key, offers simply stay in German. TRANSLATE_MODEL = os.environ.get("TRANSLATE_MODEL", "claude-haiku-5-5") -TRANSLATE_BATCH = int(os.environ.get("TRANSLATE_BATCH", "40")) +# Big batches: ~all headings in 1-2 calls, so the instruction preamble isn't +# repeated per 40 and we don't hammer the rate limit. ~22 output tokens/item, +# so 250 stays well under max_tokens=16000. +TRANSLATE_BATCH = int(os.environ.get("TRANSLATE_BATCH", "250")) # AI matching of the fixed list against offers (more precise than the token # heuristic). Bump to claude-sonnet-5-5 via env for harder cases. MATCH_MODEL = os.environ.get("MATCH_MODEL", "claude-haiku-5-5") @@ -297,6 +300,21 @@ def fetch_offers(): 'offers_total': total, 'translated': translated, 'matched': matched} +def _claude_json(client, model, max_tokens, prompt): + """One Claude call returning text. Thinking is disabled (these are mechanical + JSON tasks and thinking would eat the token budget); if a model rejects the + disabled flag we retry without it, and the large max_tokens still fits the + output alongside thinking.""" + kwargs = dict(model=model, max_tokens=max_tokens, + messages=[{"role": "user", "content": prompt}]) + try: + msg = client.messages.create(thinking={"type": "disabled"}, **kwargs) + except Exception as e: + app.logger.info("citti: thinking=disabled rejected (%s); retrying plain", e) + msg = client.messages.create(**kwargs) + return "".join(b.text for b in msg.content if b.type == "text") + + def _translate_batch(client, headings): """Translate a list of German headings to short Danish names. Returns list aligned to input (falls back to the German text on any parse gap).""" @@ -306,12 +324,7 @@ def _translate_batch(client, headings): "danske produktnavne. Behold mærke-/egennavne uændret. Svar KUN med et JSON-" 'array på formen [{"i":0,"da":"..."}], ét element per linje, intet andet.\n\n' + numbered) - msg = client.messages.create( - model=TRANSLATE_MODEL, - max_tokens=4000, - messages=[{"role": "user", "content": prompt}], - ) - text = "".join(b.text for b in msg.content if b.type == "text") + text = _claude_json(client, TRANSLATE_MODEL, 16000, prompt) m = re.search(r"\[.*\]", text, re.DOTALL) out = list(headings) if not m: @@ -407,10 +420,7 @@ def match_offers_ai(): 'JSON-array [{"item_key":"...","offer_id":"..."}] med de sikre match, ' "intet andet.") client = anthropic.Anthropic() - msg = client.messages.create( - model=MATCH_MODEL, max_tokens=4000, - messages=[{"role": "user", "content": prompt}]) - text = "".join(b.text for b in msg.content if b.type == "text") + text = _claude_json(client, MATCH_MODEL, 8000, prompt) m = re.search(r"\[.*\]", text, re.DOTALL) if not m: app.logger.warning("citti match: unparseable response: %s", text[:200]) -- 2.53.0