Spaces:
Sleeping
Sleeping
Commit ·
fc4ce92
1
Parent(s): 72ad6e3
Fix Output Parsing: Added loop to run and log all 3 tasks
Browse files- inference.py +58 -58
inference.py
CHANGED
|
@@ -12,11 +12,12 @@ API_BASE_URL = os.getenv("API_BASE_URL", "https://api.openai.com/v1")
|
|
| 12 |
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o-mini")
|
| 13 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 14 |
API_KEY = HF_TOKEN or os.getenv("OPENAI_API_KEY")
|
| 15 |
-
# FIX 1: Match the exact ID from your openenv.yaml
|
| 16 |
-
TASK_NAME = os.getenv("MY_ENV_TASK", "easy_minting")
|
| 17 |
BENCHMARK = "blockchain_certificate_admin"
|
| 18 |
MAX_STEPS = 5
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
SYSTEM_PROMPT = textwrap.dedent(
|
| 21 |
"""
|
| 22 |
You are a Smart Contract Administrator AI.
|
|
@@ -50,67 +51,66 @@ def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> No
|
|
| 50 |
|
| 51 |
def main():
|
| 52 |
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
| 53 |
-
env = BlockchainEnv(max_steps=MAX_STEPS)
|
| 54 |
-
|
| 55 |
-
history: List[str] = []
|
| 56 |
-
rewards: List[float] = []
|
| 57 |
-
steps_taken = 0
|
| 58 |
-
score = 0.0
|
| 59 |
-
|
| 60 |
-
log_start(task=TASK_NAME, env=BENCHMARK, model=MODEL_NAME)
|
| 61 |
-
|
| 62 |
-
result = env.reset()
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
action_dict = json.loads(raw_response)
|
| 84 |
-
action = BlockchainAction(**action_dict)
|
| 85 |
-
result = env.step(action)
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
# FIX 2: Never output negative numbers or raw text! Bot parses this!
|
| 93 |
-
reward = 0.01
|
| 94 |
-
error = "format_error" # No spaces allowed in error logs
|
| 95 |
-
action_str = "{}" # Must be valid JSON brackets
|
| 96 |
-
result.done = True
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
if result.done:
|
| 106 |
-
break
|
| 107 |
-
|
| 108 |
-
score = sum(rewards) / steps_taken if steps_taken > 0 else 0.01
|
| 109 |
-
score = max(0.01, min(0.99, score))
|
| 110 |
-
success = score >= 0.6
|
| 111 |
-
|
| 112 |
-
finally:
|
| 113 |
-
log_end(success=success, steps=steps_taken, score=score, rewards=rewards)
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
main()
|
|
|
|
| 12 |
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o-mini")
|
| 13 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 14 |
API_KEY = HF_TOKEN or os.getenv("OPENAI_API_KEY")
|
|
|
|
|
|
|
| 15 |
BENCHMARK = "blockchain_certificate_admin"
|
| 16 |
MAX_STEPS = 5
|
| 17 |
|
| 18 |
+
# FIX: Define all 3 tasks to run in a loop so the bot parses 3 logs!
|
| 19 |
+
TASKS = ["easy_minting", "medium_gas_management", "hard_perfectionist"]
|
| 20 |
+
|
| 21 |
SYSTEM_PROMPT = textwrap.dedent(
|
| 22 |
"""
|
| 23 |
You are a Smart Contract Administrator AI.
|
|
|
|
| 51 |
|
| 52 |
def main():
|
| 53 |
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# THE LOOP: This ensures 3 separate runs are printed to standard output
|
| 56 |
+
for current_task in TASKS:
|
| 57 |
+
env = BlockchainEnv(max_steps=MAX_STEPS)
|
| 58 |
+
rewards: List[float] = []
|
| 59 |
+
steps_taken = 0
|
| 60 |
+
score = 0.0
|
| 61 |
+
|
| 62 |
+
log_start(task=current_task, env=BENCHMARK, model=MODEL_NAME)
|
| 63 |
+
|
| 64 |
+
result = env.reset()
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
for step in range(1, MAX_STEPS + 1):
|
| 68 |
+
if result.done:
|
| 69 |
+
break
|
| 70 |
+
|
| 71 |
+
obs = result.observation
|
| 72 |
+
user_prompt = f"Pending Requests: {obs.pending_requests}\nGas Balance: {obs.gas_balance}\nStep: {step}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
try:
|
| 75 |
+
completion = client.chat.completions.create(
|
| 76 |
+
model=MODEL_NAME,
|
| 77 |
+
messages=[
|
| 78 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 79 |
+
{"role": "user", "content": user_prompt},
|
| 80 |
+
],
|
| 81 |
+
temperature=0.0,
|
| 82 |
+
)
|
| 83 |
+
raw_response = (completion.choices[0].message.content or "").strip()
|
| 84 |
+
|
| 85 |
+
action_dict = json.loads(raw_response)
|
| 86 |
+
action = BlockchainAction(**action_dict)
|
| 87 |
+
result = env.step(action)
|
| 88 |
+
|
| 89 |
+
reward = result.reward
|
| 90 |
+
error = None
|
| 91 |
+
action_str = json.dumps(action_dict).replace(" ", "")
|
| 92 |
+
|
| 93 |
+
except Exception as e:
|
| 94 |
+
reward = 0.01
|
| 95 |
+
error = "format_error"
|
| 96 |
+
action_str = "{}"
|
| 97 |
+
result.done = True
|
| 98 |
+
|
| 99 |
+
reward = max(0.01, min(0.99, reward))
|
| 100 |
+
rewards.append(reward)
|
| 101 |
+
steps_taken = step
|
| 102 |
|
| 103 |
+
log_step(step=step, action=action_str, reward=reward, done=result.done, error=error)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
if result.done:
|
| 106 |
+
break
|
| 107 |
+
|
| 108 |
+
score = sum(rewards) / steps_taken if steps_taken > 0 else 0.01
|
| 109 |
+
score = max(0.01, min(0.99, score))
|
| 110 |
+
success = score >= 0.6
|
| 111 |
|
| 112 |
+
finally:
|
| 113 |
+
log_end(success=success, steps=steps_taken, score=score, rewards=rewards)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
main()
|