#!/usr/bin/env python3 """ 路线图场景模板:运维 / 日志分析 — 通过 API 从模板创建 Agent。 用法: cd backend && .\\venv\\Scripts\\python.exe scripts/templates/template_ops_log_analysis.py """ from __future__ import annotations import os import sys import time import requests BASE = os.getenv("PLATFORM_BASE_URL", "http://127.0.0.1:8037").rstrip("/") USER = os.getenv("PLATFORM_USERNAME", "admin") PWD = os.getenv("PLATFORM_PASSWORD", "123456") TEMPLATE_ID = "template_ops_log_analysis" DEFAULT_NAME = os.getenv("TARGET_NAME") or f"场景模板_运维_{int(time.time())}" def main() -> int: r = requests.post( f"{BASE}/api/v1/auth/login", data={"username": USER, "password": PWD}, headers={"Content-Type": "application/x-www-form-urlencoded"}, timeout=15, ) if r.status_code != 200: print("登录失败:", r.status_code, r.text[:500], file=sys.stderr) return 1 token = r.json().get("access_token") if not token: print("无 access_token", file=sys.stderr) return 1 h = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"} body = { "template_id": TEMPLATE_ID, "name": DEFAULT_NAME, "description": "由 scripts/templates/template_ops_log_analysis.py 创建", "parameters": {"temperature": 0.3}, } cr = requests.post(f"{BASE}/api/v1/agents/from-scene-template", json=body, headers=h, timeout=60) if cr.status_code not in (200, 201): print("创建失败:", cr.status_code, cr.text[:800], file=sys.stderr) return 1 print(cr.json().get("id"), cr.json().get("name")) return 0 if __name__ == "__main__": raise SystemExit(main())