Sushruth21 commited on
Commit
95e023b
·
1 Parent(s): cca44f4

fix: lazy-load OpenAI imports to allow grader discovery without hanging

Browse files

- Made OpenAI imports lazy to prevent module import hang
- TASK_GRADERS in inference.py now discoverable immediately
- Fixes validator 'Not enough tasks with graders' issue by ensuring graders can be imported quickly

Files changed (1) hide show
  1. inference.py +35 -4
inference.py CHANGED
@@ -23,14 +23,39 @@ import asyncio
23
  import os
24
  import subprocess
25
  import textwrap
26
- from typing import List, Optional, Dict, Any, Callable
27
 
28
- from openai import OpenAI, OpenAIError
 
 
 
 
 
29
 
30
  from he_demo.client import EnergyOptimizationEnv
31
  from he_demo.models import EnergyOptimizationAction, EnergyOptimizationObservation
32
 
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  # ============================================================================
35
  # TASK GRADERS - Integrated from task_graders.py
36
  # ============================================================================
@@ -371,10 +396,11 @@ def parse_action(action_str: str) -> EnergyOptimizationAction:
371
 
372
 
373
  def get_model_action(
374
- client: OpenAI, step: int, observation, last_reward: float, history: List[str]
375
  ) -> EnergyOptimizationAction:
376
  """Get optimization action from the language model."""
377
  user_prompt = build_user_prompt(step, observation, last_reward, history)
 
378
  try:
379
  completion = client.chat.completions.create(
380
  model=MODEL_NAME,
@@ -434,7 +460,12 @@ async def main() -> None:
434
  flush=True,
435
  )
436
 
437
- client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
 
 
 
 
 
438
 
439
  async def local_image_exists(image_name: str) -> bool:
440
  try:
 
23
  import os
24
  import subprocess
25
  import textwrap
26
+ from typing import List, Optional, Dict, Any, Callable, TYPE_CHECKING
27
 
28
+ # TYPE_CHECKING for type hints without runtime imports
29
+ if TYPE_CHECKING:
30
+ from openai import OpenAI
31
+
32
+ # Lazy imports - Only imported when needed to avoid hanging on startup
33
+ # from openai import OpenAI, OpenAIError
34
 
35
  from he_demo.client import EnergyOptimizationEnv
36
  from he_demo.models import EnergyOptimizationAction, EnergyOptimizationObservation
37
 
38
 
39
+ # Lazy OpenAI client initialization
40
+ def _get_openai_client() -> "OpenAI":
41
+ """Lazy-load OpenAI client to avoid hanging on module import."""
42
+ try:
43
+ from openai import OpenAI
44
+ return OpenAI()
45
+ except ImportError:
46
+ raise ImportError("OpenAI library not installed. Install with: uv add openai")
47
+
48
+
49
+ # Lazy OpenAIError import
50
+ def _get_openai_error_class():
51
+ """Get OpenAIError class for exception handling."""
52
+ try:
53
+ from openai import OpenAIError
54
+ return OpenAIError
55
+ except ImportError:
56
+ return Exception # Fallback
57
+
58
+
59
  # ============================================================================
60
  # TASK GRADERS - Integrated from task_graders.py
61
  # ============================================================================
 
396
 
397
 
398
  def get_model_action(
399
+ client: "OpenAI", step: int, observation, last_reward: float, history: List[str]
400
  ) -> EnergyOptimizationAction:
401
  """Get optimization action from the language model."""
402
  user_prompt = build_user_prompt(step, observation, last_reward, history)
403
+ OpenAIError = _get_openai_error_class()
404
  try:
405
  completion = client.chat.completions.create(
406
  model=MODEL_NAME,
 
460
  flush=True,
461
  )
462
 
463
+ # Initialize OpenAI client with lazy loading
464
+ try:
465
+ from openai import OpenAI
466
+ client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
467
+ except ImportError:
468
+ raise ImportError("OpenAI library not installed. Install with: uv add openai")
469
 
470
  async def local_image_exists(image_name: str) -> bool:
471
  try: