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
This commit was merged in pull request #3.
This commit is contained in:
2026-10-30 11:23:41 +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 # ANTHROPIC_API_KEY + Haiku pattern). Cached per heading, so only new headings
# cost a call. Without a key, offers simply stay in German. # cost a call. Without a key, offers simply stay in German.
TRANSLATE_MODEL = os.environ.get("TRANSLATE_MODEL", "claude-haiku-5-5") 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 # 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. # heuristic). Bump to claude-sonnet-5-5 via env for harder cases.
MATCH_MODEL = os.environ.get("MATCH_MODEL", "claude-haiku-5-5") 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} '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): def _translate_batch(client, headings):
"""Translate a list of German headings to short Danish names. Returns list """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).""" 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-" "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' 'array på formen [{"i":0,"da":"..."}], ét element per linje, intet andet.\n\n'
+ numbered) + numbered)
msg = client.messages.create( text = _claude_json(client, TRANSLATE_MODEL, 16000, prompt)
model=TRANSLATE_MODEL,
max_tokens=4000,
messages=[{"role": "user", "content": prompt}],
)
text = "".join(b.text for b in msg.content if b.type == "text")
m = re.search(r"\[.*\]", text, re.DOTALL) m = re.search(r"\[.*\]", text, re.DOTALL)
out = list(headings) out = list(headings)
if not m: if not m:
@@ -407,10 +420,7 @@ def match_offers_ai():
'JSON-array [{"item_key":"...","offer_id":"..."}] med de sikre match, ' 'JSON-array [{"item_key":"...","offer_id":"..."}] med de sikre match, '
"intet andet.") "intet andet.")
client = anthropic.Anthropic() client = anthropic.Anthropic()
msg = client.messages.create( text = _claude_json(client, MATCH_MODEL, 8000, prompt)
model=MATCH_MODEL, max_tokens=4000,
messages=[{"role": "user", "content": prompt}])
text = "".join(b.text for b in msg.content if b.type == "text")
m = re.search(r"\[.*\]", text, re.DOTALL) m = re.search(r"\[.*\]", text, re.DOTALL)
if not m: if not m:
app.logger.warning("citti match: unparseable response: %s", text[:200]) app.logger.warning("citti match: unparseable response: %s", text[:200])