149 lines
6.4 KiB
Python
149 lines
6.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Exercise the public OpenAI-compatible gateway without printing secrets.
|
|
|
|
The script deliberately requires the caller to opt in to billed model calls.
|
|
Use MOYI_API_KEY from an environment variable, never as a command-line argument.
|
|
It checks health, verification configuration, model authorization, sync/stream
|
|
responses, and an optional concurrency sweep. Results describe observed behavior;
|
|
they do not replace an upstream provider quota agreement.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import concurrent.futures
|
|
import json
|
|
import os
|
|
import sys
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
from dataclasses import dataclass
|
|
from typing import Any
|
|
|
|
|
|
@dataclass
|
|
class Result:
|
|
status: int
|
|
elapsed: float
|
|
code: str = ""
|
|
message: str = ""
|
|
streamed: bool = False
|
|
|
|
|
|
def request(url: str, *, method: str = "GET", api_key: str = "", body: dict[str, Any] | None = None, timeout: float = 45) -> tuple[int, bytes]:
|
|
data = json.dumps(body).encode() if body is not None else None
|
|
headers = {"Accept": "application/json", "User-Agent": "moyi-live-gateway-smoke/1"}
|
|
if body is not None:
|
|
headers["Content-Type"] = "application/json"
|
|
if api_key:
|
|
headers["Authorization"] = f"Bearer {api_key}"
|
|
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=timeout) as response:
|
|
return response.status, response.read()
|
|
except urllib.error.HTTPError as error:
|
|
return error.code, error.read(8192)
|
|
|
|
|
|
def decoded_error(raw: bytes) -> tuple[str, str]:
|
|
try:
|
|
payload = json.loads(raw.decode("utf-8", "replace"))
|
|
error = payload.get("error", payload) if isinstance(payload, dict) else {}
|
|
if isinstance(error, dict):
|
|
return str(error.get("code", "")), str(error.get("message", ""))
|
|
except (ValueError, UnicodeDecodeError):
|
|
pass
|
|
return "", raw.decode("utf-8", "replace")[:180]
|
|
|
|
|
|
def call(base: str, key: str, model: str, *, stream: bool = False, timeout: float = 60) -> Result:
|
|
body = {
|
|
"model": model,
|
|
"messages": [{"role": "user", "content": "ping"}],
|
|
"max_tokens": 1,
|
|
"stream": stream,
|
|
}
|
|
started = time.monotonic()
|
|
status, raw = request(f"{base}/chat/completions", method="POST", api_key=key, body=body, timeout=timeout)
|
|
elapsed = time.monotonic() - started
|
|
code, message = decoded_error(raw) if status >= 300 else ("", "")
|
|
if status < 300 and stream and b"data:" not in raw:
|
|
return Result(status, elapsed, "INVALID_STREAM", "stream response did not contain SSE data", True)
|
|
return Result(status, elapsed, code, message, stream)
|
|
|
|
|
|
def print_result(label: str, result: Result) -> None:
|
|
suffix = f" {result.code}: {result.message}" if result.code else ""
|
|
print(f"{label}: HTTP {result.status}, {result.elapsed:.2f}s{suffix}")
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--base-url", default="https://moyi.soulofmodel.com/v1", help="public API base URL")
|
|
parser.add_argument("--model", help="authorized model/group ID; defaults to the first returned model")
|
|
parser.add_argument("--run-calls", action="store_true", help="send billed sync and stream requests")
|
|
parser.add_argument("--max-concurrency", type=int, default=0, help="also run levels 1..N in parallel (billed)")
|
|
parser.add_argument("--timeout", type=float, default=60)
|
|
args = parser.parse_args()
|
|
|
|
base = args.base_url.rstrip("/")
|
|
if not base.endswith("/v1"):
|
|
base += "/v1"
|
|
root = base[:-3].rstrip("/")
|
|
key = os.environ.get("MOYI_API_KEY", "").strip()
|
|
print(f"Base URL: {base}")
|
|
for path in ("/health", "/api/auth/verification"):
|
|
status, raw = request(f"{root}{path}", timeout=args.timeout)
|
|
print(f"{path}: HTTP {status} {raw.decode('utf-8', 'replace')[:240]}")
|
|
|
|
unauth_status, unauth_raw = request(f"{base}/models", timeout=args.timeout)
|
|
unauth_code, unauth_message = decoded_error(unauth_raw)
|
|
print(f"/models without key: HTTP {unauth_status} {unauth_code}: {unauth_message}" if unauth_code or unauth_message else f"/models without key: HTTP {unauth_status}")
|
|
|
|
if not key:
|
|
print("未设置 MOYI_API_KEY;只完成公开健康检查。", file=sys.stderr)
|
|
return 0
|
|
status, raw = request(f"{base}/models", api_key=key, timeout=args.timeout)
|
|
if status >= 300:
|
|
code, message = decoded_error(raw)
|
|
print(f"/models: HTTP {status} {code}: {message}", file=sys.stderr)
|
|
return 2
|
|
try:
|
|
models = json.loads(raw.decode("utf-8"))
|
|
choices = models.get("data", [])
|
|
model = args.model or (choices[0].get("id") if choices else "")
|
|
except (ValueError, AttributeError):
|
|
print("/models 返回不是有效 JSON", file=sys.stderr)
|
|
return 2
|
|
print(f"/models: HTTP {status}, authorized models={len(choices)}, selected={model or 'none'}")
|
|
if not args.run_calls and args.max_concurrency <= 0:
|
|
print("未使用 --run-calls;没有发送会产生上游费用的模型请求。")
|
|
return 0
|
|
if not model:
|
|
print("没有可调用模型,请先为该 Key 授权一个同步或流式模型。", file=sys.stderr)
|
|
return 2
|
|
if not args.run_calls:
|
|
print("并发测试需要 --run-calls;跳过。", file=sys.stderr)
|
|
return 2
|
|
|
|
print_result("sync", call(base, key, model, timeout=args.timeout))
|
|
print_result("stream", call(base, key, model, stream=True, timeout=args.timeout))
|
|
if args.max_concurrency > 0:
|
|
print(f"并发扫测 1..{args.max_concurrency};每个请求 max_tokens=1,可能消耗上游余额。")
|
|
for level in range(1, args.max_concurrency + 1):
|
|
started = time.monotonic()
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=level) as executor:
|
|
results = list(executor.map(lambda _: call(base, key, model, timeout=args.timeout), range(level)))
|
|
ok = sum(result.status < 300 for result in results)
|
|
buckets: dict[str, int] = {}
|
|
for result in results:
|
|
label = "ok" if result.status < 300 else result.code or f"HTTP_{result.status}"
|
|
buckets[label] = buckets.get(label, 0) + 1
|
|
print(f"parallel={level}: {ok}/{level} success, {time.monotonic() - started:.2f}s, {buckets}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|