|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import time |
| 4 | +import requests |
| 5 | + |
| 6 | + |
| 7 | +def require_env(var_name: str) -> str: |
| 8 | + value = os.getenv(var_name) |
| 9 | + if not value: |
| 10 | + print(f"Missing required env var: {var_name}", file=sys.stderr) |
| 11 | + sys.exit(1) |
| 12 | + return value |
| 13 | + |
| 14 | + |
| 15 | +def require_logs_endpoints(base_url: str) -> None: |
| 16 | + try: |
| 17 | + r = requests.get(f"{base_url}/openapi.json", timeout=30) |
| 18 | + if not r.ok: |
| 19 | + print("OpenAPI schema unavailable", file=sys.stderr) |
| 20 | + sys.exit(1) |
| 21 | + paths = r.json().get("paths", {}) |
| 22 | + ok = any(p.startswith("/logs") or p.startswith("/v1/logs") for p in paths.keys()) |
| 23 | + if not ok: |
| 24 | + print("/logs endpoints not present on deployment", file=sys.stderr) |
| 25 | + sys.exit(1) |
| 26 | + except Exception as e: |
| 27 | + print(f"Failed to check OpenAPI: {e}", file=sys.stderr) |
| 28 | + sys.exit(1) |
| 29 | + |
| 30 | + |
| 31 | +def post_chat_completion(base_url: str, api_key: str, rollout_id: str) -> None: |
| 32 | + headers = {"Authorization": f"Bearer {api_key}"} |
| 33 | + now = int(time.time()) |
| 34 | + url = ( |
| 35 | + f"{base_url}/rollout_id/{rollout_id}/" |
| 36 | + f"invocation_id/inv{now}/" |
| 37 | + f"experiment_id/remote-validate/" |
| 38 | + f"run_id/run-1/" |
| 39 | + f"row_id/row-1/" |
| 40 | + f"chat/completions" |
| 41 | + ) |
| 42 | + body = { |
| 43 | + "model": "fireworks_ai/accounts/fireworks/models/llama-v3p1-8b-instruct", |
| 44 | + "messages": [{"role": "user", "content": "Say 'ok' if you can read this."}], |
| 45 | + "temperature": 0.1, |
| 46 | + } |
| 47 | + r = requests.post(url, headers=headers, json=body, timeout=60) |
| 48 | + if r.status_code != 200: |
| 49 | + print(f"Chat completion failed: {r.status_code} {r.text[:500]}", file=sys.stderr) |
| 50 | + sys.exit(1) |
| 51 | + print("chat: ok") |
| 52 | + |
| 53 | + |
| 54 | +def wait_for_traces(base_url: str, api_key: str, rollout_id: str, max_attempts: int = 8) -> None: |
| 55 | + headers = {"Authorization": f"Bearer {api_key}"} |
| 56 | + params = { |
| 57 | + "tags": [f"rollout_id:{rollout_id}"], |
| 58 | + "limit": 10, |
| 59 | + "hours_back": 6, |
| 60 | + } |
| 61 | + url = f"{base_url}/traces" |
| 62 | + for attempt in range(1, max_attempts + 1): |
| 63 | + r = requests.get(url, headers=headers, params=params, timeout=30) |
| 64 | + if r.status_code == 200: |
| 65 | + data = r.json() |
| 66 | + total = int(data.get("total_traces") or 0) |
| 67 | + print(f"traces: ok total_traces={total}") |
| 68 | + if total > 0: |
| 69 | + return |
| 70 | + elif r.status_code != 404 and r.status_code != 401: |
| 71 | + print(f"Traces fetch failed: {r.status_code} {r.text[:500]}", file=sys.stderr) |
| 72 | + sys.exit(1) |
| 73 | + sleep_s = min(2 ** (attempt - 1), 10) |
| 74 | + time.sleep(sleep_s) |
| 75 | + print("Traces not available after retries (indexing delay?)", file=sys.stderr) |
| 76 | + sys.exit(1) |
| 77 | + |
| 78 | + |
| 79 | +def validate_logs_endpoints(base_url: str, rollout_id: str) -> None: |
| 80 | + require_logs_endpoints(base_url) |
| 81 | + |
| 82 | + # Ingest a structured log |
| 83 | + payload = { |
| 84 | + "program": "eval_protocol", |
| 85 | + "status": "completed", |
| 86 | + "message": "Remote validation run finished", |
| 87 | + "tags": [f"rollout_id:{rollout_id}", "experiment_id:remote", "run_id:test"], |
| 88 | + "metadata": {"dataset": "AIME"}, |
| 89 | + "extras": {"num_examples": 3}, |
| 90 | + } |
| 91 | + r = requests.post(f"{base_url}/logs", json=payload, timeout=30) |
| 92 | + if r.status_code != 200: |
| 93 | + print(f"logs ingest failed: {r.status_code} {r.text[:500]}", file=sys.stderr) |
| 94 | + sys.exit(1) |
| 95 | + print("logs ingest: ok") |
| 96 | + |
| 97 | + # Retrieve logs (retry for indexing) |
| 98 | + params = { |
| 99 | + "tags": [f"rollout_id:{rollout_id}"], |
| 100 | + "program": "eval_protocol", |
| 101 | + "hours_back": 1, |
| 102 | + "limit": 10, |
| 103 | + } |
| 104 | + total = 0 |
| 105 | + for attempt in range(1, 12): |
| 106 | + rr = requests.get(f"{base_url}/logs", params=params, timeout=30) |
| 107 | + if rr.status_code == 200: |
| 108 | + data = rr.json() |
| 109 | + total = int(data.get("total_entries") or 0) |
| 110 | + if total > 0: |
| 111 | + print(f"logs fetch: ok total_entries={total}") |
| 112 | + break |
| 113 | + sleep_s = min(2 ** (attempt - 1), 10) |
| 114 | + time.sleep(sleep_s) |
| 115 | + if total == 0: |
| 116 | + print("logs fetch: no entries found within retry window", file=sys.stderr) |
| 117 | + sys.exit(1) |
| 118 | + |
| 119 | + |
| 120 | +def main(): |
| 121 | + base_url = require_env("GATEWAY_URL") |
| 122 | + api_key = require_env("FIREWORKS_API_KEY") |
| 123 | + rollout_id = f"r{int(time.time())}" |
| 124 | + |
| 125 | + print(f"Gateway: {base_url}") |
| 126 | + print(f"Rollout: rollout_id:{rollout_id}") |
| 127 | + |
| 128 | + post_chat_completion(base_url, api_key, rollout_id) |
| 129 | + wait_for_traces(base_url, api_key, rollout_id) |
| 130 | + validate_logs_endpoints(base_url, rollout_id) |
| 131 | + |
| 132 | + print("remote validation: SUCCESS") |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + main() |
0 commit comments