Spaces:
Sleeping
Sleeping
Update inference.py
Browse files- inference.py +71 -62
inference.py
CHANGED
|
@@ -1,63 +1,72 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
from
|
| 4 |
-
from
|
| 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 |
run_baseline("hard")
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from dotenv import load_dotenv # <-- ADDED
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
from adaptive_cache.env import AdaptiveCacheEnv, Action
|
| 6 |
+
|
| 7 |
+
# Load variables from .env file into the environment
|
| 8 |
+
load_dotenv() # <-- ADDED
|
| 9 |
+
|
| 10 |
+
def run_baseline(task_level: str):
|
| 11 |
+
print(f"\n--- Running Baseline for Task: {task_level.upper()} ---")
|
| 12 |
+
|
| 13 |
+
# 1. Initialize the official OpenAI client dynamically
|
| 14 |
+
# <-- CHANGED: Now pulls from generic LLM_ variables
|
| 15 |
+
api_key = os.environ.get("LLM_API_KEY")
|
| 16 |
+
base_url = os.environ.get("LLM_BASE_URL")
|
| 17 |
+
model_name = os.environ.get("LLM_MODEL_NAME", "gpt-4o-mini")
|
| 18 |
+
|
| 19 |
+
if not api_key:
|
| 20 |
+
print("ERROR: LLM_API_KEY environment variable not set. Check your .env file.")
|
| 21 |
+
return
|
| 22 |
+
|
| 23 |
+
client = OpenAI(
|
| 24 |
+
base_url=base_url,
|
| 25 |
+
api_key=api_key
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
env = AdaptiveCacheEnv(task_level=task_level)
|
| 29 |
+
obs = env.reset()
|
| 30 |
+
done = False
|
| 31 |
+
total_reward = 0.0
|
| 32 |
+
|
| 33 |
+
system_prompt = """
|
| 34 |
+
You are an intelligent Cache Manager.
|
| 35 |
+
You must decide which cache slot index (0 to 9) to evict.
|
| 36 |
+
Respond ONLY with a JSON object matching this schema: {"evict_index": integer}
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
while not done:
|
| 40 |
+
try:
|
| 41 |
+
# 2. Call the dynamically configured model
|
| 42 |
+
# <-- CHANGED: Now uses the model_name variable
|
| 43 |
+
response = client.chat.completions.create(
|
| 44 |
+
model=model_name,
|
| 45 |
+
response_format={ "type": "json_object" },
|
| 46 |
+
messages=[
|
| 47 |
+
{"role": "system", "content": system_prompt},
|
| 48 |
+
{"role": "user", "content": f"Current State: {obs.model_dump_json()}"}
|
| 49 |
+
],
|
| 50 |
+
temperature=0.0
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
content = response.choices[0].message.content
|
| 54 |
+
action_dict = json.loads(content)
|
| 55 |
+
action = Action(**action_dict)
|
| 56 |
+
|
| 57 |
+
except Exception as e:
|
| 58 |
+
# Failsafe so the script doesn't crash on a bad JSON format
|
| 59 |
+
print(f"LLM Parsing failed ({e}). Defaulting to slot 0.")
|
| 60 |
+
action = Action(evict_index=0)
|
| 61 |
+
|
| 62 |
+
obs, reward, done, info = env.step(action)
|
| 63 |
+
total_reward += reward
|
| 64 |
+
|
| 65 |
+
print(f"Episode Finished.")
|
| 66 |
+
print(f"Total Reward: {total_reward}")
|
| 67 |
+
print(f"Final Grader Score (Hit Rate): {info.get('score', 0.0):.2f} / 1.00")
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
run_baseline("easy")
|
| 71 |
+
run_baseline("medium")
|
| 72 |
run_baseline("hard")
|