"""Pull structured data out of free text in bulk: every answer is JSON that fits your schema.

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

It creates one batch, charged to your prepaid credits.
"""

import json
import os
import time

from openai import OpenAI

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

MODEL = "gemma4-26b-a4b-it"

# --- 1. Describe the answer you want ---
# The schema is enforced while the model writes, so a finished answer always parses.

ORDER = {
    "type": "json_schema",
    "json_schema": {
        "name": "order_email",
        "schema": {
            "type": "object",
            "properties": {
                "customer": {"type": "string"},
                "order_id": {"type": "string"},
                "items": {
                    "type": "array",
                    "maxItems": 10,
                    "items": {
                        "type": "object",
                        "properties": {
                            "name": {"type": "string"},
                            "quantity": {"type": "integer"},
                        },
                        "required": ["name", "quantity"],
                        "additionalProperties": False,
                    },
                },
                "wants": {
                    "type": "string",
                    "enum": ["refund", "replacement", "update", "other"],
                },
            },
            "required": ["customer", "order_id", "items", "wants"],
            "additionalProperties": False,
        },
    },
}
SYSTEM = (
    "Extract the order from the email as JSON: the customer's name, the order id, each item "
    "with its quantity, and what the customer wants (refund, replacement, update or other)."
)

# --- 2. Write one request per document ---
# Replace EMAILS with your own text. custom_id is how you match answers back to documents.

EMAILS = [
    "Hi, this is Dana Whitfield. Order A-10492 had two desk lamps and one extension lead. "
    "One lamp came broken, so please send a new one.",
    "Order B-77310: three packs of printer paper. They were never delivered and I would like "
    "my money back. Regards, Tom Okafor",
    "Hello. I ordered a standing desk and two monitor arms last week (C-20018). When will "
    "they ship? Priya Raman",
    "From: Luis Ortega. My order D-55120 was 12 notebooks and 4 boxes of pens. The pens are "
    "the wrong colour. Can you swap them?",
    "I am Mei Tanaka and my order number is E-31007. One office chair. The gas lift fails "
    "and the seat sinks. I want a refund.",
    "Sam Keller here, order F-88421, five whiteboard markers and one eraser. Thank you, "
    "everything arrived fine.",
]

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

# --- 3. Run the batch ---

batch_input = client.files.create(file=open("requests.jsonl", "rb"), purpose="batch")
batch = client.batches.create(
    input_file_id=batch_input.id,
    endpoint="/v1/chat/completions",
    completion_window="24h",
)
print(batch.id, batch.status)
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}")

# --- 4. Read the answers ---
# Results come back in any order. An answer cut off at max_tokens may be half a JSON object,
# so check finish_reason before you parse.

orders = {}
if batch.output_file_id:
    for line in client.files.content(batch.output_file_id).text.splitlines():
        row = json.loads(line)
        choice = row["response"]["body"]["choices"][0]
        if choice["finish_reason"] == "stop":
            orders[row["custom_id"]] = json.loads(choice["message"]["content"])
        else:
            print(row["custom_id"], "was cut off: raise max_tokens and send it again")

for n in range(len(EMAILS)):
    print(f"email-{n}", orders.get(f"email-{n}"))

# --- 5. Read the errors ---
# A request that failed on its own is in the error file. The rest of the batch still completed.

if batch.error_file_id:
    for line in client.files.content(batch.error_file_id).text.splitlines():
        row = json.loads(line)
        print(row["custom_id"], "failed:", row["error"]["code"], row["error"]["message"])
