File size: 3,942 Bytes
2828ab3 9fa8d9a 6a5a305 8d79275 2828ab3 0329e58 2828ab3 0329e58 2828ab3 9fa8d9a 2828ab3 9fa8d9a 2828ab3 9fa8d9a 2828ab3 9fa8d9a 2828ab3 9fa8d9a 8d79275 9fa8d9a 0329e58 9fa8d9a 8d79275 9fa8d9a 2828ab3 9fa8d9a 2828ab3 0329e58 9fa8d9a 2828ab3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | import os
import json
from openai import OpenAI
from env import EdgeEnv
from models import EdgeAction
LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME", "edge-deployment-env")
API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
MODEL_NAME = os.getenv("MODEL_NAME", "llama3.1")
HF_TOKEN = os.getenv("HF_TOKEN")
client = OpenAI(
base_url=API_BASE_URL,
api_key=HF_TOKEN
)
def run_inference():
env = EdgeEnv()
tasks = ["task_stable_edge", "task_fluctuating_memory", "task_low_battery_spike"]
BENCHMARK_NAME = "EdgeDeployment"
for task in tasks:
# [START]
print(f"[START] task={task} env={BENCHMARK_NAME} model={MODEL_NAME}", flush=True)
step_count = 0
rewards_list = []
try:
env.set_task(task)
obs = env.reset()
done = False
while not done:
#print(f"[DEBUG] Observation: {obs.model_dump_json()}")
prompt = f"""
System Status: RAM: {obs.ram_usage:.1f}%, Battery: {obs.battery_level:.1f}%
Rules:
1. If RAM is > 80.0, "kill_process" must be true.
2. If Battery is < 40.0, "throttle_cpu" must be true.
3. Otherwise, set everything to false.
Respond ONLY with a raw JSON object containing EXACTLY these three boolean keys. No markdown, no explanation.
Example: {{"kill_process": false, "throttle_cpu": false, "route_to_cloud": false}}
"""
error_msg = "null"
try:
response = client.chat.completions.create(
model=MODEL_NAME,
messages=[{"role": "user", "content": prompt}],
temperature=0.0 # 0.0 forces the model to be completely robotic and follow the rules
)
raw_output = response.choices[0].message.content.strip()
raw_output = raw_output.replace("```json", "").replace("```", "").strip()
action_data = json.loads(raw_output)
action = EdgeAction(
kill_process=action_data.get("kill_process", False),
throttle_cpu=action_data.get("throttle_cpu", False),
route_to_cloud=action_data.get("route_to_cloud", False)
)
except Exception as e:
error_msg = f'"{str(e)}"'
#print(f"[DEBUG] ⚠️ ERROR: {e}")
action = EdgeAction(kill_process=False, throttle_cpu=False, route_to_cloud=False)
obs, reward, done, info = env.step(action)
# Track values
step_count += 1
reward_val = reward.score
rewards_list.append(reward_val)
done_str = str(done).lower()
action_str = action.model_dump_json().replace(" ", "")
print(f"[STEP] step={step_count} action={action_str} reward={reward_val:.2f} done={done_str} error={error_msg}", flush=True)
finally:
# MANDATORY: Close environment and print the [END] line always
try:
env.close()
except:
pass
final_average_score = sum(rewards_list) / step_count if step_count > 0 else 0.0
success_str = "true" if final_average_score > 0.0 else "false"
rewards_str = ",".join(f"{r:.2f}" for r in rewards_list)
print(f"[END] success={success_str} steps={step_count} score={final_average_score:.2f} rewards={rewards_str}", flush=True)
if __name__ == "__main__":
run_inference()
|