DarkyCodez/precision-ag-env
0
1import os2import requests3from openai import OpenAI4 5# Required Environment Variables6API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")7MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")8API_KEY = os.getenv("HF_TOKEN", "dummy-token")9ENV_URL = "http://0.0.0.0:7860"10 11TASKS = ["task_thirsty_crop", "task_nutrient_balance", "task_heatwave_crisis"]12 13client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)14 15SYSTEM_PROMPT = """You are an agricultural AI managing a greenhouse.16Observations include soil moisture, nitrogen levels, and crop health.17Choose one action per turn:180: Do nothing191: Irrigate202: Fertilize213: Harvest22Reply ONLY with a single integer (0, 1, 2, or 3)."""23 24def main():25 for task in TASKS:26 print(f"[START] task={task} env=precision-resilience-env model={MODEL_NAME}", flush=True)27 28 try:29 res = requests.post(f"{ENV_URL}/reset", json={"task_id": task}).json()30 obs = res.get("observation", {})31 32 rewards = []33 step = 134 done = False35 36 while not done and step <= 15:37 user_prompt = f"Observation: {obs}\nAction (0-3):"38 try:39 completion = client.chat.completions.create(40 model=MODEL_NAME,41 messages=[42 {"role": "system", "content": SYSTEM_PROMPT},43 {"role": "user", "content": user_prompt}44 ],45 temperature=0.1,46 max_tokens=547 )48 action_str = completion.choices[0].message.content.strip()49 action_int = int(action_str)50 except Exception:51 action_int = 052 53 step_res = requests.post(f"{ENV_URL}/step", json={"action": action_int}).json()54 obs = step_res.get("observation", {})55 reward = float(step_res.get("reward", 0.0))56 done = step_res.get("done", False)57 58 rewards.append(reward)59 60 print(f"[STEP] step={step} action={action_int} reward={reward:.2f} done={str(done).lower()} error=null", flush=True)61 step += 162 63 total_score = sum(rewards) / len(rewards) if rewards else 0.064 total_score = min(max(total_score, 0.0), 1.0)65 success = total_score > 0.566 rewards_str = ",".join(f"{r:.2f}" for r in rewards)67 68 print(f"[END] success={str(success).lower()} steps={step-1} score={total_score:.3f} rewards={rewards_str}", flush=True)69 70 except Exception as e:71 print(f"[END] success=false steps=0 score=0.00 rewards=0.00", flush=True)72 73if __name__ == "__main__":74 main()