shreyas-garg commited on
Commit
7eed0a5
·
1 Parent(s): 04f7ba6

Match exact [START]/[STEP]/[END] hackathon output spec

Browse files
Files changed (1) hide show
  1. inference.py +47 -19
inference.py CHANGED
@@ -9,6 +9,12 @@ MANDATORY
9
 
10
  - The inference script must be named `inference.py` and placed in the root directory of the project
11
  - Participants must use OpenAI Client for all LLM calls using above variables
 
 
 
 
 
 
12
  """
13
 
14
  import os
@@ -23,7 +29,9 @@ from email_env.tasks import TASKS
23
 
24
  API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
25
  MODEL_NAME = os.getenv("MODEL_NAME", "meta-llama/Llama-3.1-8B-Instruct")
26
- HF_TOKEN = os.getenv("HF_TOKEN")
 
 
27
 
28
  if not HF_TOKEN:
29
  raise EnvironmentError("HF_TOKEN environment variable is required.")
@@ -44,14 +52,23 @@ Reply ONLY with valid JSON in this exact format (no markdown, no extra text):
44
  def run_inference():
45
  client = OpenAI(api_key=HF_TOKEN, base_url=API_BASE_URL)
46
  env = EmailTriageEnv()
47
- scores = []
48
 
49
- for task_id, task in TASKS.items():
50
- reward = 0.0
51
  success = False
52
- try:
53
- print(f"[START] task={task_id}", flush=True)
 
 
 
 
 
 
 
 
54
 
 
55
  obs = env.reset(task_id=task_id)
56
 
57
  user_msg = (
@@ -70,47 +87,58 @@ def run_inference():
70
  )
71
 
72
  raw = completion.choices[0].message.content.strip()
73
-
74
- # Strip markdown fences if present
75
  if raw.startswith("```"):
76
- lines = raw.split("\n")
77
- lines = [l for l in lines if not l.startswith("```")]
78
  raw = "\n".join(lines).strip()
79
 
80
  try:
81
  parsed = json.loads(raw)
82
  except json.JSONDecodeError:
83
  parsed = {"category": "general", "priority": "low", "response": ""}
 
84
 
85
  action = Action(
86
  category=parsed.get("category", "general"),
87
  priority=parsed.get("priority", "low"),
88
  response=parsed.get("response", ""),
89
  )
 
 
 
 
90
 
91
  result = env.step(action)
92
  reward = float(result.reward)
93
  done = bool(result.done)
94
- success = reward >= 0.7
 
 
 
95
 
96
  print(
97
- f"[STEP] task={task_id} step=1 reward={reward:.2f} "
98
- f"done={'true' if done else 'false'} "
99
- f"success={'true' if success else 'false'}",
100
  flush=True,
101
  )
102
- scores.append(reward)
103
 
104
  except Exception as exc:
105
- print(f"ERROR in task {task_id}: {exc}", file=sys.stderr, flush=True)
 
 
 
 
 
 
106
  finally:
 
107
  print(
108
- f"[END] task={task_id} score={reward:.2f} steps=1 "
109
- f"success={'true' if success else 'false'}",
110
  flush=True,
111
  )
112
 
113
- avg = round(sum(scores) / len(scores), 2) if scores else 0.0
114
  print(f"\n=== Average Score: {avg:.2f} ===", flush=True)
115
  return avg
116
 
 
9
 
10
  - The inference script must be named `inference.py` and placed in the root directory of the project
11
  - Participants must use OpenAI Client for all LLM calls using above variables
12
+
13
+ STDOUT FORMAT
14
+ - The script emits exactly three line types to stdout:
15
+ [START] task=<task_name> env=<benchmark> model=<model_name>
16
+ [STEP] step=<n> action=<action_str> reward=<0.00> done=<true|false> error=<msg|null>
17
+ [END] success=<true|false> steps=<n> score=<score> rewards=<r1,r2,...,rn>
18
  """
19
 
20
  import os
 
29
 
30
  API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
31
  MODEL_NAME = os.getenv("MODEL_NAME", "meta-llama/Llama-3.1-8B-Instruct")
32
+ HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("API_KEY")
33
+ BENCHMARK = "email-triage"
34
+ SUCCESS_THRESHOLD = 0.5
35
 
36
  if not HF_TOKEN:
37
  raise EnvironmentError("HF_TOKEN environment variable is required.")
 
52
  def run_inference():
53
  client = OpenAI(api_key=HF_TOKEN, base_url=API_BASE_URL)
54
  env = EmailTriageEnv()
55
+ all_scores = []
56
 
57
+ for task_id in TASKS.keys():
58
+ rewards = []
59
  success = False
60
+ score = 0.0
61
+ steps = 0
62
+ error_msg = "null"
63
+ action_str = "noop"
64
+ done = False
65
+
66
+ print(
67
+ f"[START] task={task_id} env={BENCHMARK} model={MODEL_NAME}",
68
+ flush=True,
69
+ )
70
 
71
+ try:
72
  obs = env.reset(task_id=task_id)
73
 
74
  user_msg = (
 
87
  )
88
 
89
  raw = completion.choices[0].message.content.strip()
 
 
90
  if raw.startswith("```"):
91
+ lines = [l for l in raw.split("\n") if not l.startswith("```")]
 
92
  raw = "\n".join(lines).strip()
93
 
94
  try:
95
  parsed = json.loads(raw)
96
  except json.JSONDecodeError:
97
  parsed = {"category": "general", "priority": "low", "response": ""}
98
+ error_msg = "json_parse_error"
99
 
100
  action = Action(
101
  category=parsed.get("category", "general"),
102
  priority=parsed.get("priority", "low"),
103
  response=parsed.get("response", ""),
104
  )
105
+ action_str = (
106
+ f"triage(category='{action.category}',"
107
+ f"priority='{action.priority}')"
108
+ )
109
 
110
  result = env.step(action)
111
  reward = float(result.reward)
112
  done = bool(result.done)
113
+ steps = 1
114
+ rewards.append(reward)
115
+ score = reward
116
+ success = score >= SUCCESS_THRESHOLD
117
 
118
  print(
119
+ f"[STEP] step=1 action={action_str} reward={reward:.2f} "
120
+ f"done={'true' if done else 'false'} error={error_msg}",
 
121
  flush=True,
122
  )
123
+ all_scores.append(score)
124
 
125
  except Exception as exc:
126
+ error_msg = str(exc).replace("\n", " ")
127
+ print(
128
+ f"[STEP] step=1 action={action_str} reward=0.00 done=true "
129
+ f"error={error_msg}",
130
+ file=sys.stderr,
131
+ flush=True,
132
+ )
133
  finally:
134
+ rewards_str = ",".join(f"{r:.2f}" for r in rewards) if rewards else "0.00"
135
  print(
136
+ f"[END] success={'true' if success else 'false'} steps={steps} "
137
+ f"score={score:.2f} rewards={rewards_str}",
138
  flush=True,
139
  )
140
 
141
+ avg = round(sum(all_scores) / len(all_scores), 2) if all_scores else 0.0
142
  print(f"\n=== Average Score: {avg:.2f} ===", flush=True)
143
  return avg
144