"""
Claude as the evaluator of a session
====================================

The examples of the previous sections used the offline mock judge. This one
calls Claude through the Anthropic Messages API (an API key is needed, so the
gallery shows the code without running it). Set the key once:

* locally: ``export ANTHROPIC_API_KEY=...`` in the shell;
* in Google Colab: add a secret named ``ANTHROPIC_API_KEY`` (key icon on the
  left) and allow the notebook to read it; :func:`easyiec.evaluators.llm.clients.get_api_key`
  finds it.

Never paste a key into a notebook cell or a file that others may see.
"""

# %%
# A cached client and a debiased judge
# ------------------------------------
# :class:`~easyiec.evaluators.CachedClient` stores every answer on disk, so a
# rerun of the notebook costs nothing and gives the same results.
import easyiec as ei
from easyiec.evaluators import AnthropicClient, CachedClient, LLMJudge
from easyiec.problems import ImageSegmentation
from easyiec.strategies import InteractiveGA

problem = ImageSegmentation()
client = CachedClient(AnthropicClient("claude-sonnet-5-5", effort="low"), ".easyiec_cache")
judge = LLMJudge(client, problem, debias="reverse", n_views=2, image_size=224)
user = ei.SimulatedUser(problem, target=None)            # only as the oracle: Dice-based ground truth

res = ei.run(problem, InteractiveGA(pop_size=9), judge, generations=8, seed=1, oracle=user.true_utility)
print(res.summary())                                   # queries, items, usd, best true utility
print(res.log.records[-1].feedback.rationale)          # Claude's reason for its last choice

# %%
# Report and cost
# ---------------
ei.viz.session_report(res, "claude_segmentation.html", evaluator=judge, title="Segmentation tuned by Claude")
print(client.inner.usage)                              # calls, tokens and estimated USD of new (uncached) calls
