import json
import os
import time
from pathlib import Path
from openai import OpenAI


TASKS = [
    {"id": "boundary-tests", "prompt": "为 Python average(xs)=sum(xs)/len(xs) 编写 unittest，明确空数组应抛 ValueError，覆盖单值和负数。仅给代码。"},
    {"id": "source-grounding", "prompt": "仅根据原文回答并引用短句。原文：标准交付为 7 个工作日；加急为 3 个工作日；未说明价格。问题：加急多久，多少钱？"},
    {"id": "chinese-summary", "prompt": "把下面说明改成给普通用户看的中文，保留数字：请求已入队，预计 30 秒返回；超时不自动重发，因为可能产生重复计费。"},
]


def main():
    client = OpenAI(api_key=os.environ["QUICKROUTER_API_KEY"],
                    base_url="https://api.quickrouter.ai/v1", timeout=120, max_retries=0)
    rows = []
    for model in ["deepseek-v4-pro", "qwen3.8-max"]:
        for task in TASKS:
            start = time.perf_counter()
            try:
                result = client.chat.completions.create(
                    model=model, messages=[{"role": "user", "content": task["prompt"]}], max_tokens=4096)
                row = {"model": model, "task_id": task["id"],
                       "returned_model": result.model,
                       "answer": result.choices[0].message.content,
                       "finish_reason": result.choices[0].finish_reason,
                       "usage": result.usage.model_dump() if result.usage else None,
                       "status": "awaiting_human_acceptance"}
            except Exception as error:
                row = {"model": model, "task_id": task["id"], "status": "failed_or_unknown",
                       "error_type": type(error).__name__}
            row["elapsed_seconds"] = round(time.perf_counter() - start, 3)
            rows.append(row)
            Path("model-comparison-results.json").write_text(
                json.dumps(rows, ensure_ascii=False, indent=2), encoding="utf-8")
    print("Saved all six attempts. Add human acceptance and console charges before choosing.")


if __name__ == "__main__":
    main()
