#!/usr/bin/env python3 # -*- coding: utf-8 -*- """生成作业14练习用导出样本(默认「昨天」时间戳)。""" from __future__ import annotations import json from datetime import datetime, timedelta from pathlib import Path OUT = Path(__file__).resolve().parents[2] / "data" / "homework14" OUT.mkdir(parents=True, exist_ok=True) def main() -> None: y = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=1) rows = [] apps = [ ("客服助手", "qwen3.6-27b", 12000, 3000, 40), ("知识库问答", "qwen3.6-27b", 9000, 2500, 35), ("编程助手", "qwen3.6-27b", 8000, 4000, 20), ("运维巡检Agent", "qwen3.6-27b", 3000, 1500, 25), ("周报生成", "qwen3.6-27b", 2000, 1800, 8), ("意图路由", "qwen3.6-27b", 1500, 800, 30), ("健康检查", "qwen3.6-27b", 800, 400, 15), ("演示App-A", "qwen3.6-27b", 600, 200, 10), ("演示App-B", "qwen3.6-27b", 500, 100, 6), ("演示App-C", "qwen3.6-27b", 400, 80, 5), ("演示App-D", "qwen3.6-27b", 200, 50, 3), ("低用量工具", "qwen3.6-27b", 50, 20, 2), ] n = 0 for app, model, ptok, ctok, traces in apps: per_p = max(1, ptok // traces) per_c = max(1, ctok // traces) for i in range(traces): n += 1 ts = y + timedelta(hours=9, minutes=(n % 50), seconds=n % 60) err = "rate_limit" if app == "编程助手" and i % 7 == 0 else "" rows.append( { "id": f"tr-{n:04d}", "traceId": f"tr-{n:04d}", "sessionId": f"sess-{app}-{i%5}", "name": "message", "createdAt": ts.strftime("%Y-%m-%dT%H:%M:%S"), "model": model, "promptTokens": per_p, "completionTokens": per_c, "totalTokens": per_p + per_c, "latencyMs": 800 + (n % 9) * 120, "toolName": "getServerStatus" if "运维" in app and i % 2 == 0 else "", "errorType": err, "metadata": {"app_name": app}, "tags": [f"app:{app}"], } ) path = OUT / "sample_export.json" path.write_text(json.dumps(rows, ensure_ascii=False, indent=2), encoding="utf-8") # CSV 同步一份 import csv csv_path = OUT / "sample_export.csv" fields = [ "trace_id", "session_id", "model", "prompt_tokens", "completion_tokens", "tool_name", "latency_ms", "error_type", "app_name", "timestamp", ] with csv_path.open("w", encoding="utf-8", newline="") as f: w = csv.DictWriter(f, fieldnames=fields) w.writeheader() for r in rows: w.writerow( { "trace_id": r["traceId"], "session_id": r["sessionId"], "model": r["model"], "prompt_tokens": r["promptTokens"], "completion_tokens": r["completionTokens"], "tool_name": r["toolName"], "latency_ms": r["latencyMs"], "error_type": r["errorType"], "app_name": r["metadata"]["app_name"], "timestamp": r["createdAt"].replace("T", " "), } ) print("wrote", path, "rows", len(rows)) print("wrote", csv_path) if __name__ == "__main__": main()