"""parameters.py, M6: the two knobs you'll use most, max_tokens and temperature.

- max_tokens caps how long the reply can be (and your cost).
- temperature controls randomness: low = focused/repeatable, high = varied/creative.

A real-world detail worth knowing: the NEWEST Opus models (like claude-opus-4-8) manage
their own randomness and REJECT a temperature setting (you'd get a 400 error). So this
demo uses claude-haiku-4-5, which accepts temperature, and is cheap and fast, perfect for
running the same prompt several times to watch the knob work.

Run (venv active, key in .env, from this folder):
    python parameters.py
"""

import os
from dotenv import load_dotenv
import anthropic

load_dotenv()
client = anthropic.Anthropic()
MODEL = "claude-haiku-4-5"        # accepts temperature; cheap + fast for repeated runs


def ask(prompt, max_tokens, temperature):
    response = client.messages.create(
        model=MODEL,
        max_tokens=max_tokens,
        temperature=temperature,
        messages=[{"role": "user", "content": prompt}],
    )
    return response.content[0].text


print("--- max_tokens: the same request, capped short then longer ---")
print("max_tokens=15 :", ask("List fun facts about octopuses.", max_tokens=15, temperature=0.0))
print("max_tokens=80 :", ask("List fun facts about octopuses.", max_tokens=80, temperature=0.0))

print("\n--- temperature 0.0: focused & repeatable (run twice, ~same answer) ---")
print("  run 1:", ask("Invent a name for a coffee shop. Reply with just the name.", 20, 0.0))
print("  run 2:", ask("Invent a name for a coffee shop. Reply with just the name.", 20, 0.0))

print("\n--- temperature 1.0: varied & creative (run twice, different answers) ---")
print("  run 1:", ask("Invent a name for a coffee shop. Reply with just the name.", 20, 1.0))
print("  run 2:", ask("Invent a name for a coffee shop. Reply with just the name.", 20, 1.0))

print("\nLow temperature for predictable tasks (extraction, classification);")
print("higher temperature when you want variety (brainstorming, creative writing).")
