"""Distill a large model into a small one: the large model answers, the small one learns.

  python -m pip install -U openai
  export GRIDLINE_TOKEN="gla_..."
  python distill.py

It creates two batches and one fine-tuning job, all charged to your prepaid credits.
"""

import json
import os
import random
import time

from openai import OpenAI

client = OpenAI(
    base_url="https://api.gridlinegpu.com/openai/v1",
    api_key=os.environ["GRIDLINE_TOKEN"],
    timeout=60,
)

TEACHER = "qwen3.8-27b"
STUDENT = "qwen3.5-9b"
SYSTEM = (
    "You triage support tickets. Reply with JSON only: queue (billing, shipping, returns, "
    "account, technical or sales), urgency (low, normal or high) and summary (one short "
    "sentence)."
)
TRIAGE = {
    "type": "json_schema",
    "json_schema": {
        "name": "triage",
        "schema": {
            "type": "object",
            "properties": {
                "queue": {
                    "type": "string",
                    "enum": ["billing", "shipping", "returns", "account", "technical", "sales"],
                },
                "urgency": {"type": "string", "enum": ["low", "normal", "high"]},
                "summary": {"type": "string", "maxLength": 200},
            },
            "required": ["queue", "urgency", "summary"],
            "additionalProperties": False,
        },
    },
}


def wait(batch):
    while batch.status not in ("completed", "failed", "expired", "cancelled"):
        time.sleep(30)
        batch = client.batches.retrieve(batch.id)
    if batch.status != "completed":
        raise SystemExit(f"{batch.id} ended {batch.status}: {batch.errors}")
    return batch


def run_batch(path):
    batch_input = client.files.create(file=open(path, "rb"), purpose="batch")
    return wait(
        client.batches.create(
            input_file_id=batch_input.id,
            endpoint="/v1/chat/completions",
            completion_window="24h",
        )
    )


# --- 1. Write the prompts ---
# Unlabelled tickets: no human wrote the answers. Replace these with your own inputs.

SUBJECTS = [
    "I was charged twice for my {plan} plan",
    "order {order} has not arrived",
    "my {product} came with a cracked screen",
    "I cannot sign in and the reset email never comes",
    "the app closes when I open the {product} settings",
    "we want a quote for 200 units of the {product}",
    "my invoice for the {plan} plan is wrong",
    "the tracking for order {order} has not moved in a week",
    "I want to send back the {product}",
    "someone else signed in to my account",
    "the {product} will not connect since the update",
    "is there a discount on the {plan} plan for schools",
]
CONTEXTS = [
    "",
    " and I need it sorted before a trip tomorrow",
    ", no rush",
    " and this is the third time I have written",
    " and our whole team is blocked",
    ", just checking",
    " and I am about to cancel",
]
PLANS = ["Starter", "Team", "Pro", "Business"]
PRODUCTS = ["headphones", "smart speaker", "fitness band", "tablet", "router", "camera"]

rng = random.Random(11)
tickets = set()
while len(tickets) < 300:
    subject = rng.choice(SUBJECTS).format(
        plan=rng.choice(PLANS),
        product=rng.choice(PRODUCTS),
        order=rng.randint(10000, 99999),
    )
    tickets.add(f"Hello, {subject}{rng.choice(CONTEXTS)}.")

with open("teacher-requests.jsonl", "w") as out:
    for n, ticket in enumerate(sorted(tickets)):
        body = {
            "model": TEACHER,
            "messages": [
                {"role": "system", "content": SYSTEM},
                {"role": "user", "content": ticket},
            ],
            "max_tokens": 200,
            "temperature": 0,
            "response_format": TRIAGE,
        }
        line = {
            "custom_id": f"ticket-{n}",
            "method": "POST",
            "url": "/v1/chat/completions",
            "body": body,
        }
        out.write(json.dumps(line) + "\n")

# --- 2. Run the teacher ---

teacher_batch = run_batch("teacher-requests.jsonl")
print("teacher answered", teacher_batch.request_counts.completed, "tickets")

# --- 3. Turn its answers into training files ---
# One row per answer: the request's messages, then the teacher's answer. A fifth is held out.

distilled = client.post(
    "/files/distill",
    body={
        "results_file": teacher_batch.output_file_id,
        "require_json": True,
        "eval_fraction": 0.2,
    },
    cast_to=object,
)
print(distilled["train_rows"], "training rows,", distilled["eval_rows"], "held out")
print("dropped:", distilled["dropped"])

# --- 4. Fine-tune the student ---

job = client.fine_tuning.jobs.create(
    model=STUDENT,
    training_file=distilled["training_file"]["id"],
    validation_file=distilled["validation_file"]["id"],
    hyperparameters={"n_epochs": 3},
)
print(job.id, "costs at most $", job.gridline["quote"]["per_token"]["max_charge_usd"])
while job.status not in ("succeeded", "failed", "cancelled"):
    time.sleep(30)
    job = client.fine_tuning.jobs.retrieve(job.id)
if job.status != "succeeded":
    raise SystemExit(f"{job.id} ended {job.status}: {job.error}")
print("student:", job.fine_tuned_model)

# --- 5. Check the student against the teacher ---
# The held-out rows were never trained on. Run them on the student and on its untrained base,
# then count how often each one gives the teacher's queue and urgency.

held_out = [
    json.loads(line)["messages"]
    for line in client.files.content(distilled["validation_file"]["id"]).text.splitlines()
]
MODELS = {"base": STUDENT, "student": job.fine_tuned_model}
with open("student-requests.jsonl", "w") as out:
    for arm, model in MODELS.items():
        for n, messages in enumerate(held_out):
            body = {
                "model": model,
                "messages": messages[:-1],
                "max_tokens": 200,
                "temperature": 0,
            }
            line = {
                "custom_id": f"{arm}-{n}",
                "method": "POST",
                "url": "/v1/chat/completions",
                "body": body,
            }
            out.write(json.dumps(line) + "\n")

student_batch = run_batch("student-requests.jsonl")
answers = {}
for line in client.files.content(student_batch.output_file_id).text.splitlines():
    row = json.loads(line)
    answers[row["custom_id"]] = row["response"]["body"]["choices"][0]["message"]["content"]


def triage(text):
    try:
        reply = json.loads(text)
        return reply["queue"], reply["urgency"]
    except (ValueError, KeyError, TypeError):
        return None


for arm in MODELS:
    agree = sum(
        triage(answers.get(f"{arm}-{n}", "")) == triage(messages[-1]["content"])
        for n, messages in enumerate(held_out)
    )
    print(f"{arm}: agrees with the teacher on {agree} of {len(held_out)}")
