Mihir1107 Claude Sonnet 4.6 commited on
Commit
06d5215
·
1 Parent(s): 8d62fdb

Fix inference.py to match checklist exactly

Browse files

- Add HF_TOKEN = os.getenv("HF_TOKEN") at module level (no default)
- Remove OPENAI_API_KEY fallback — spec requires HF_TOKEN only
- main() uses module-level HF_TOKEN directly
- OpenAI() call matches spec: OpenAI(base_url=..., api_key=HF_TOKEN)
- API_BASE_URL and MODEL_NAME retain defaults; HF_TOKEN has none

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. inference.py +10 -10
inference.py CHANGED
@@ -36,9 +36,10 @@ from openai import OpenAI
36
  # Config — all overridable via environment variables
37
  # ---------------------------------------------------------------------------
38
 
39
- DEFAULT_HOST = os.environ.get("ENV_HOST", "http://localhost:7860")
40
- API_BASE_URL = os.environ.get("API_BASE_URL", "https://router.huggingface.co/v1")
41
- MODEL_NAME = os.environ.get("MODEL_NAME", "meta-llama/Llama-3.1-8B-Instruct")
 
42
  BENCHMARK = "DataSelectEnv"
43
  SEED = 42
44
  TASKS = ["easy", "medium", "hard"]
@@ -145,14 +146,14 @@ def rule_based_action(obs: dict) -> dict:
145
 
146
  def make_openai_client(api_key: str) -> OpenAI:
147
  """
148
- Create the required OpenAI client.
149
  Uses an explicit httpx.Client with trust_env=False to bypass proxy
150
  auto-detection that commonly breaks SDK init in containerised environments.
151
  """
152
  base_url = (API_BASE_URL or "https://router.huggingface.co/v1").strip().rstrip("/")
153
  http_client = httpx.Client(trust_env=False)
154
  try:
155
- return OpenAI(api_key=api_key, base_url=base_url, http_client=http_client)
156
  except Exception:
157
  return OpenAI(api_key=api_key, http_client=http_client)
158
 
@@ -338,14 +339,13 @@ def main() -> None:
338
  help="Environment server base URL (http or https)")
339
  args = parser.parse_args()
340
 
341
- # Build OpenAI client (required by spec); warn and fall back if unavailable
342
- api_key = os.getenv("HF_TOKEN") or os.getenv("OPENAI_API_KEY")
343
  client: Optional[OpenAI] = None
344
- if not api_key:
345
- print("WARNING: No HF_TOKEN / OPENAI_API_KEY found — using rule-based fallback.", flush=True)
346
  else:
347
  try:
348
- client = make_openai_client(api_key)
349
  print(f"OpenAI client ready | base_url={API_BASE_URL} | model={MODEL_NAME}", flush=True)
350
  except Exception as e:
351
  print(f"WARNING: Could not init OpenAI client ({e}); using rule-based fallback.", flush=True)
 
36
  # Config — all overridable via environment variables
37
  # ---------------------------------------------------------------------------
38
 
39
+ DEFAULT_HOST = os.environ.get("ENV_HOST", "http://localhost:7860")
40
+ API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
41
+ MODEL_NAME = os.getenv("MODEL_NAME", "meta-llama/Llama-3.1-8B-Instruct")
42
+ HF_TOKEN = os.getenv("HF_TOKEN")
43
  BENCHMARK = "DataSelectEnv"
44
  SEED = 42
45
  TASKS = ["easy", "medium", "hard"]
 
146
 
147
  def make_openai_client(api_key: str) -> OpenAI:
148
  """
149
+ Create the required OpenAI client (as mandated by the spec).
150
  Uses an explicit httpx.Client with trust_env=False to bypass proxy
151
  auto-detection that commonly breaks SDK init in containerised environments.
152
  """
153
  base_url = (API_BASE_URL or "https://router.huggingface.co/v1").strip().rstrip("/")
154
  http_client = httpx.Client(trust_env=False)
155
  try:
156
+ return OpenAI(base_url=base_url, api_key=api_key, http_client=http_client)
157
  except Exception:
158
  return OpenAI(api_key=api_key, http_client=http_client)
159
 
 
339
  help="Environment server base URL (http or https)")
340
  args = parser.parse_args()
341
 
342
+ # Build OpenAI client using HF_TOKEN (required by spec)
 
343
  client: Optional[OpenAI] = None
344
+ if not HF_TOKEN:
345
+ print("WARNING: HF_TOKEN not set — using rule-based fallback.", flush=True)
346
  else:
347
  try:
348
+ client = make_openai_client(HF_TOKEN)
349
  print(f"OpenAI client ready | base_url={API_BASE_URL} | model={MODEL_NAME}", flush=True)
350
  except Exception as e:
351
  print(f"WARNING: Could not init OpenAI client ({e}); using rule-based fallback.", flush=True)