|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Smoke-test NVIDIA NIM chat API for qwen/qwen3-coder-480b-a35b-instruct. |
| 3 | +
|
| 4 | +Requires NVIDIA_API_KEY in the environment (never commit the key). |
| 5 | +Docs: https://docs.api.nvidia.com/nim/reference/qwen-qwen3-coder-480b-a35b-instruct-infer |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import argparse |
| 11 | +import json |
| 12 | +import os |
| 13 | +import sys |
| 14 | +import urllib.error |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 18 | +sys.path.insert(0, str(REPO_ROOT / "scripts")) |
| 19 | + |
| 20 | +from nvidia_nim_common import ( # noqa: E402 |
| 21 | + chat_completion, |
| 22 | + chat_completion_stream, |
| 23 | + load_dotenv, |
| 24 | +) |
| 25 | + |
| 26 | +DEFAULT_MODEL = "qwen/qwen3-coder-480b-a35b-instruct" |
| 27 | + |
| 28 | + |
| 29 | +def main() -> int: |
| 30 | + p = argparse.ArgumentParser(description="NVIDIA NIM Qwen3 Coder smoke test.") |
| 31 | + p.add_argument( |
| 32 | + "prompt", |
| 33 | + nargs="?", |
| 34 | + default="Reply with exactly: NVIDIA NIM smoke OK", |
| 35 | + help="User message content (default is a tiny connectivity check).", |
| 36 | + ) |
| 37 | + p.add_argument("--model", default=DEFAULT_MODEL) |
| 38 | + p.add_argument("--temperature", type=float, default=0.7) |
| 39 | + p.add_argument("--top-p", type=float, default=0.8) |
| 40 | + p.add_argument("--max-tokens", type=int, default=32) |
| 41 | + p.add_argument("--timeout", type=int, default=300, help="HTTP timeout seconds.") |
| 42 | + p.add_argument( |
| 43 | + "--no-stream", |
| 44 | + action="store_true", |
| 45 | + help="Wait for full JSON response (slower for large models).", |
| 46 | + ) |
| 47 | + args = p.parse_args() |
| 48 | + |
| 49 | + load_dotenv(REPO_ROOT / ".env") |
| 50 | + api_key = os.environ.get("NVIDIA_API_KEY", "").strip() |
| 51 | + if not api_key: |
| 52 | + print( |
| 53 | + "Missing NVIDIA_API_KEY. Copy .env.example to .env or set the variable.", |
| 54 | + file=sys.stderr, |
| 55 | + ) |
| 56 | + return 1 |
| 57 | + if api_key.startswith("nvapi-your-key"): |
| 58 | + print("Replace placeholder key in .env with a real nvapi- key.", file=sys.stderr) |
| 59 | + return 1 |
| 60 | + |
| 61 | + common = { |
| 62 | + "api_key": api_key, |
| 63 | + "prompt": args.prompt, |
| 64 | + "model": args.model, |
| 65 | + "temperature": args.temperature, |
| 66 | + "top_p": args.top_p, |
| 67 | + "max_tokens": args.max_tokens, |
| 68 | + "timeout": args.timeout, |
| 69 | + } |
| 70 | + try: |
| 71 | + if args.no_stream: |
| 72 | + data = chat_completion(**common) |
| 73 | + message = data["choices"][0]["message"] |
| 74 | + print(json.dumps(message, indent=2, ensure_ascii=False)) |
| 75 | + else: |
| 76 | + print("Streaming (first token = API OK):", file=sys.stderr) |
| 77 | + text = chat_completion_stream(**common) |
| 78 | + if not text.strip(): |
| 79 | + print("No content in stream.", file=sys.stderr) |
| 80 | + return 1 |
| 81 | + except urllib.error.HTTPError as exc: |
| 82 | + body = exc.read().decode("utf-8", errors="replace") |
| 83 | + print(f"HTTP {exc.code}: {body}", file=sys.stderr) |
| 84 | + if exc.code == 403: |
| 85 | + print("Check NVIDIA_API_KEY at build.nvidia.com (rotate if exposed).", file=sys.stderr) |
| 86 | + return 1 |
| 87 | + except TimeoutError: |
| 88 | + msg = f"Timed out after {args.timeout}s. Retry or use --timeout 600." |
| 89 | + print(msg, file=sys.stderr) |
| 90 | + return 1 |
| 91 | + |
| 92 | + return 0 |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + raise SystemExit(main()) |
0 commit comments