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