jeevank0 commited on
Commit
cddc1ec
·
1 Parent(s): dedc89b

fix: OPENAI_API_KEY

Browse files
env/__pycache__/__init__.cpython-313.pyc CHANGED
Binary files a/env/__pycache__/__init__.cpython-313.pyc and b/env/__pycache__/__init__.cpython-313.pyc differ
 
env/__pycache__/farm_env.cpython-313.pyc CHANGED
Binary files a/env/__pycache__/farm_env.cpython-313.pyc and b/env/__pycache__/farm_env.cpython-313.pyc differ
 
inference.py CHANGED
@@ -31,7 +31,7 @@ API_BASE_URL = os.getenv("API_BASE_URL", "https://api.openai.com/v1").strip()
31
  MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o-mini").strip()
32
  TASK_NAME = os.getenv("TASK_NAME", "farm-yield-optimization").strip()
33
  BENCHMARK = os.getenv("BENCHMARK", "farmrl").strip()
34
- OPENAI_API_KEY = require_env("OPENAI_API_KEY")
35
 
36
  PLACEHOLDER_TOKENS = {
37
  "your_openai_api_key_here",
@@ -88,20 +88,13 @@ def build_prompt(state: FarmState, step: int, recent_actions: list[dict[str, flo
88
  def build_client() -> Optional[OpenAI]:
89
  base_lower = API_BASE_URL.lower()
90
  if "huggingface.co" in base_lower:
91
- raise RuntimeError(
92
- "Hugging Face router is disabled for LLM calls. "
93
- "Set API_BASE_URL to https://api.openai.com/v1"
94
- )
95
 
96
  api_key = OPENAI_API_KEY
97
- missing_msg = "Missing API key: set OPENAI_API_KEY."
98
-
99
- if not api_key:
100
- raise RuntimeError(missing_msg)
101
-
102
- if api_key.lower() in PLACEHOLDER_TOKENS:
103
- raise RuntimeError(
104
- "OPENAI_API_KEY is a placeholder; set a real key before running inference.")
105
  return OpenAI(base_url=API_BASE_URL, api_key=api_key)
106
 
107
 
@@ -147,6 +140,38 @@ def coerce_action(payload: Dict[str, Any]) -> FarmAction:
147
  return FarmAction(**normalized)
148
 
149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  def choose_action(
151
  client: OpenAI,
152
  state: FarmState,
@@ -217,7 +242,19 @@ def run_inference() -> None:
217
  dataset_path = Path(__file__).resolve().parent / \
218
  "farmer_advisor_dataset.csv"
219
  env = FarmEnv(dataset_path=dataset_path, seed=42, max_days=30)
220
- client = build_client()
 
 
 
 
 
 
 
 
 
 
 
 
221
 
222
  total_reward = 0.0
223
  total_yield = 0.0
@@ -226,6 +263,7 @@ def run_inference() -> None:
226
  total_steps = 0
227
  rewards: list[float] = []
228
  recent_actions: list[dict[str, float]] = []
 
229
 
230
  log_start()
231
 
@@ -233,13 +271,42 @@ def run_inference() -> None:
233
  state = env.reset(seed=42 + episode)
234
 
235
  for _ in range(STEPS_PER_EPISODE):
236
- action = choose_action(
237
- client=client,
238
- state=state,
239
- step=total_steps + 1,
240
- recent_actions=recent_actions,
241
- )
242
- step_result = env.step(action)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
 
244
  total_steps += 1
245
  total_reward += step_result.reward
@@ -254,13 +321,16 @@ def run_inference() -> None:
254
  action=action,
255
  reward=step_result.reward,
256
  done=step_result.done,
257
- error=None,
258
  )
259
  state = step_result.observation
260
 
261
  if step_result.done:
262
  break
263
 
 
 
 
264
  task_scores = grade_all(
265
  total_reward=total_reward,
266
  total_yield=total_yield,
@@ -276,5 +346,19 @@ def run_inference() -> None:
276
  score=overall_score, rewards=rewards)
277
 
278
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279
  if __name__ == "__main__":
280
- run_inference()
 
31
  MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o-mini").strip()
32
  TASK_NAME = os.getenv("TASK_NAME", "farm-yield-optimization").strip()
33
  BENCHMARK = os.getenv("BENCHMARK", "farmrl").strip()
34
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "").strip()
35
 
36
  PLACEHOLDER_TOKENS = {
37
  "your_openai_api_key_here",
 
88
  def build_client() -> Optional[OpenAI]:
89
  base_lower = API_BASE_URL.lower()
90
  if "huggingface.co" in base_lower:
91
+ print("[WARN] HuggingFace router detected, skipping LLM", flush=True)
92
+ return None
 
 
93
 
94
  api_key = OPENAI_API_KEY
95
+ if not api_key or api_key.lower() in PLACEHOLDER_TOKENS:
96
+ print("[WARN] OPENAI_API_KEY not found, running in fallback mode", flush=True)
97
+ return None
 
 
 
 
 
98
  return OpenAI(base_url=API_BASE_URL, api_key=api_key)
99
 
100
 
 
140
  return FarmAction(**normalized)
141
 
142
 
143
+ def choose_fallback_action(state: FarmState, recent_actions: list[dict[str, float]]) -> FarmAction:
144
+ # Rule-based action used when LLM is unavailable or returns invalid output.
145
+ target_moisture = 62.0 if state.crop_stage < 3 else 68.0
146
+ moisture_gap = target_moisture - state.soil_moisture
147
+ rain_adjustment = max(0.0, 50.0 - state.rainfall) * 0.1
148
+
149
+ water = clamp(12.0 + 0.8 * moisture_gap + rain_adjustment, 0.0, 50.0)
150
+ fertilizer = clamp(
151
+ (6.0 if state.crop_stage < 4 else 4.0)
152
+ - 0.05 * max(0, state.day - 10)
153
+ - 0.1 * max(state.temperature - 32.0, 0.0),
154
+ 0.0,
155
+ 20.0,
156
+ )
157
+
158
+ pesticide = 1.0
159
+ if state.crop_stage >= 2 and state.rainfall > 70.0:
160
+ pesticide = 3.0
161
+ pesticide = clamp(pesticide, 0.0, 10.0)
162
+
163
+ action = {
164
+ "water": water,
165
+ "fertilizer": fertilizer,
166
+ "pesticide": pesticide,
167
+ }
168
+
169
+ if recent_actions and action == recent_actions[-1]:
170
+ action["water"] = clamp(action["water"] + 2.0, 0.0, 50.0)
171
+
172
+ return FarmAction(**action)
173
+
174
+
175
  def choose_action(
176
  client: OpenAI,
177
  state: FarmState,
 
242
  dataset_path = Path(__file__).resolve().parent / \
243
  "farmer_advisor_dataset.csv"
244
  env = FarmEnv(dataset_path=dataset_path, seed=42, max_days=30)
245
+ client: Optional[OpenAI] = None
246
+ startup_error: Optional[str] = None
247
+
248
+ try:
249
+ client = build_client()
250
+ except Exception as exc:
251
+ startup_error = re.sub(
252
+ r"\s+",
253
+ " ",
254
+ f"{exc.__class__.__name__}:{exc}",
255
+ ).strip()
256
+ print(
257
+ f"[WARN] llm_client_unavailable error={startup_error}", flush=True)
258
 
259
  total_reward = 0.0
260
  total_yield = 0.0
 
263
  total_steps = 0
264
  rewards: list[float] = []
265
  recent_actions: list[dict[str, float]] = []
266
+ aborted = False
267
 
268
  log_start()
269
 
 
271
  state = env.reset(seed=42 + episode)
272
 
273
  for _ in range(STEPS_PER_EPISODE):
274
+ step_error: Optional[str] = None
275
+
276
+ if client is not None:
277
+ try:
278
+ action = choose_action(
279
+ client=client,
280
+ state=state,
281
+ step=total_steps + 1,
282
+ recent_actions=recent_actions,
283
+ )
284
+ except Exception as exc:
285
+ step_error = f"llm_error:{exc.__class__.__name__}"
286
+ action = choose_fallback_action(
287
+ state=state,
288
+ recent_actions=recent_actions,
289
+ )
290
+ else:
291
+ action = choose_fallback_action(
292
+ state=state,
293
+ recent_actions=recent_actions,
294
+ )
295
+ if startup_error and total_steps == 0 and episode == 0:
296
+ step_error = f"fallback:{startup_error}"
297
+
298
+ try:
299
+ step_result = env.step(action)
300
+ except Exception as exc:
301
+ aborted = True
302
+ log_step(
303
+ step=total_steps + 1,
304
+ action=action,
305
+ reward=0.0,
306
+ done=True,
307
+ error=f"env_error:{exc.__class__.__name__}",
308
+ )
309
+ break
310
 
311
  total_steps += 1
312
  total_reward += step_result.reward
 
321
  action=action,
322
  reward=step_result.reward,
323
  done=step_result.done,
324
+ error=step_error,
325
  )
326
  state = step_result.observation
327
 
328
  if step_result.done:
329
  break
330
 
331
+ if aborted:
332
+ break
333
+
334
  task_scores = grade_all(
335
  total_reward=total_reward,
336
  total_yield=total_yield,
 
346
  score=overall_score, rewards=rewards)
347
 
348
 
349
+ def main() -> int:
350
+ try:
351
+ run_inference()
352
+ except Exception as exc:
353
+ fatal_error = re.sub(
354
+ r"\s+",
355
+ " ",
356
+ f"{exc.__class__.__name__}:{exc}",
357
+ ).strip()
358
+ print(f"[FATAL] error={fatal_error}", flush=True)
359
+ log_end(success=False, steps=0, score=0.0, rewards=[])
360
+ return 0
361
+
362
+
363
  if __name__ == "__main__":
364
+ raise SystemExit(main())
tasks/__pycache__/__init__.cpython-313.pyc CHANGED
Binary files a/tasks/__pycache__/__init__.cpython-313.pyc and b/tasks/__pycache__/__init__.cpython-313.pyc differ
 
tasks/__pycache__/graders.cpython-313.pyc CHANGED
Binary files a/tasks/__pycache__/graders.cpython-313.pyc and b/tasks/__pycache__/graders.cpython-313.pyc differ