theaniketgiri commited on
Commit
2a3cac2
·
1 Parent(s): 5674cfc

Fix environment variable import caching and remove rogue proxy preflight

Browse files
Files changed (1) hide show
  1. inference.py +18 -30
inference.py CHANGED
@@ -43,15 +43,16 @@ import urllib.error
43
  # Configuration — fully environment-driven
44
  # ---------------------------------------------------------------------------
45
 
46
- API_BASE_URL: str = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
47
  API_KEY: str = (
48
- os.getenv("API_KEY")
49
- or os.getenv("HF_TOKEN")
50
- or os.getenv("OPENAI_API_KEY")
51
  or "missing-api-key"
52
  )
53
- MODEL_NAME: str = os.getenv("MODEL_NAME", "")
54
- ENV_SERVER_URL: str = os.getenv("ENV_SERVER_URL", "http://localhost:8000")
 
55
 
56
  BENCHMARK = "code_review_env"
57
  TASKS = ["task_easy", "task_medium", "task_hard"]
@@ -312,7 +313,7 @@ def get_action(client: Any, obs: dict[str, Any], step: int) -> dict[str, Any]:
312
 
313
 
314
  # ---------------------------------------------------------------------------
315
- # Server readiness and proxy preflight checks
316
  # ---------------------------------------------------------------------------
317
 
318
 
@@ -329,25 +330,6 @@ def wait_for_server(timeout: int = 60) -> None:
329
  raise RuntimeError(f"Server at {ENV_SERVER_URL} not ready after {timeout}s")
330
 
331
 
332
- def ensure_proxy_call(client: Any) -> None:
333
- """Make at least one guaranteed real proxy-routed completion call."""
334
- if client is None:
335
- return
336
- try:
337
- _ = client.chat.completions.create(
338
- model=MODEL_NAME,
339
- messages=[
340
- {"role": "system", "content": "Return OK."},
341
- {"role": "user", "content": "Reply with OK"},
342
- ],
343
- temperature=0,
344
- max_tokens=4,
345
- stream=False,
346
- )
347
- except Exception:
348
- pass
349
-
350
-
351
  # ---------------------------------------------------------------------------
352
  # Agent loop — one task episode
353
  # ---------------------------------------------------------------------------
@@ -414,10 +396,16 @@ def main() -> None:
414
  client = None
415
  try:
416
  from openai import OpenAI
417
- client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY or "missing-api-key")
418
- ensure_proxy_call(client)
419
- except Exception:
420
- pass
 
 
 
 
 
 
421
 
422
  wait_for_server(timeout=60)
423
 
 
43
  # Configuration — fully environment-driven
44
  # ---------------------------------------------------------------------------
45
 
46
+ API_BASE_URL: str = os.environ.get("API_BASE_URL", "https://router.huggingface.co/v1")
47
  API_KEY: str = (
48
+ os.environ.get("API_KEY")
49
+ or os.environ.get("HF_TOKEN")
50
+ or os.environ.get("OPENAI_API_KEY")
51
  or "missing-api-key"
52
  )
53
+ # Provide fallback explicitly instead of an empty string
54
+ MODEL_NAME: str = os.environ.get("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
55
+ ENV_SERVER_URL: str = os.environ.get("ENV_SERVER_URL", "http://localhost:8000")
56
 
57
  BENCHMARK = "code_review_env"
58
  TASKS = ["task_easy", "task_medium", "task_hard"]
 
313
 
314
 
315
  # ---------------------------------------------------------------------------
316
+ # Server readiness
317
  # ---------------------------------------------------------------------------
318
 
319
 
 
330
  raise RuntimeError(f"Server at {ENV_SERVER_URL} not ready after {timeout}s")
331
 
332
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
  # ---------------------------------------------------------------------------
334
  # Agent loop — one task episode
335
  # ---------------------------------------------------------------------------
 
396
  client = None
397
  try:
398
  from openai import OpenAI
399
+
400
+ # Dynamically fetch at runtime to prevent caching via import loops
401
+ val_api_base = os.environ.get("API_BASE_URL", "https://router.huggingface.co/v1")
402
+ val_api_key = os.environ.get("API_KEY") or os.environ.get("HF_TOKEN") or "missing-api-key"
403
+
404
+ client = OpenAI(base_url=val_api_base, api_key=val_api_key)
405
+ except Exception as e:
406
+ import sys
407
+ print(f"[WARN] Failed to initialize OpenAI client: {e}", file=sys.stderr)
408
+ client = None
409
 
410
  wait_for_server(timeout=60)
411