Compare commits

..

2 Commits

Author SHA1 Message Date
b7141d787b Merge pull request 'fix(offers): DE→DA translation returned nothing — disable thinking, batch big' (#3) from worktree-citti-arkiver-tur into main
All checks were successful
Build and Deploy citti / build-and-deploy (push) Successful in 41s
Reviewed-on: #3
2026-10-30 11:23:41 +01:00
Henrik Jess Nielsen
f374088211 fix(offers): DE→DA translation returned nothing — disable thinking, batch big
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)
2026-10-30 11:19:40 +01:00

32
app.py
View File

@@ -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])