Update inference.py
Browse files- inference.py +13 -19
inference.py
CHANGED
|
@@ -9,17 +9,10 @@ API_BASE_URL = os.environ.get("API_BASE_URL")
|
|
| 9 |
API_KEY = os.environ.get("API_KEY")
|
| 10 |
MODEL_NAME = os.environ.get("MODEL_NAME", "meta-llama/Llama-3-70b-chat-hf")
|
| 11 |
|
| 12 |
-
# Proxy Client Initialization
|
| 13 |
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
| 14 |
|
| 15 |
def get_llm_action(email_desc, context, keywords):
|
| 16 |
-
prompt = f""
|
| 17 |
-
Classify this email:
|
| 18 |
-
Desc: {email_desc}
|
| 19 |
-
Context: {context}
|
| 20 |
-
Keywords: {keywords}
|
| 21 |
-
Return 3 numbers (0-2) separated by commas. Example: 1, 0, 1
|
| 22 |
-
"""
|
| 23 |
try:
|
| 24 |
response = client.chat.completions.create(
|
| 25 |
model=MODEL_NAME,
|
|
@@ -30,7 +23,7 @@ def get_llm_action(email_desc, context, keywords):
|
|
| 30 |
res = response.choices[0].message.content.strip()
|
| 31 |
actions = [int(x.strip()) for x in res.split(",")[:3]]
|
| 32 |
return np.array(actions, dtype=np.int64)
|
| 33 |
-
except
|
| 34 |
return np.array([0, 0, 0], dtype=np.int64)
|
| 35 |
|
| 36 |
def run_inference():
|
|
@@ -38,12 +31,12 @@ def run_inference():
|
|
| 38 |
|
| 39 |
for task_name in tasks:
|
| 40 |
try:
|
| 41 |
-
# Environment
|
| 42 |
env = EmailTriageEnv(task=task_name, shuffle=False)
|
| 43 |
obs, info = env.reset(seed=42)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
print(f"[START] task={task_name}", flush=True)
|
| 47 |
|
| 48 |
terminated = False
|
| 49 |
step_count = 0
|
|
@@ -51,24 +44,25 @@ def run_inference():
|
|
| 51 |
emails = list(env._queue)
|
| 52 |
|
| 53 |
while not terminated and step_count < len(emails):
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
email_data['keywords'])
|
| 58 |
|
| 59 |
obs, reward, terminated, truncated, info = env.step(action)
|
| 60 |
total_reward += reward
|
| 61 |
|
| 62 |
-
|
| 63 |
print(f"[STEP] step={step_count+1} reward={reward:.2f}", flush=True)
|
| 64 |
step_count += 1
|
| 65 |
|
| 66 |
|
| 67 |
final_score = max(0.01, min(0.99, total_reward))
|
| 68 |
-
print(f"[END] task={task_name} score={final_score:.3f} steps={step_count}", flush=True)
|
| 69 |
|
| 70 |
-
|
|
|
|
| 71 |
|
|
|
|
|
|
|
| 72 |
print(f"[END] task={task_name} score=0.010 steps=0", flush=True)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
|
|
|
| 9 |
API_KEY = os.environ.get("API_KEY")
|
| 10 |
MODEL_NAME = os.environ.get("MODEL_NAME", "meta-llama/Llama-3-70b-chat-hf")
|
| 11 |
|
|
|
|
| 12 |
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
| 13 |
|
| 14 |
def get_llm_action(email_desc, context, keywords):
|
| 15 |
+
prompt = f"Classify email: {email_desc}. Return 3 numbers (0-2) separated by commas."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
response = client.chat.completions.create(
|
| 18 |
model=MODEL_NAME,
|
|
|
|
| 23 |
res = response.choices[0].message.content.strip()
|
| 24 |
actions = [int(x.strip()) for x in res.split(",")[:3]]
|
| 25 |
return np.array(actions, dtype=np.int64)
|
| 26 |
+
except:
|
| 27 |
return np.array([0, 0, 0], dtype=np.int64)
|
| 28 |
|
| 29 |
def run_inference():
|
|
|
|
| 31 |
|
| 32 |
for task_name in tasks:
|
| 33 |
try:
|
| 34 |
+
# Environment Setup (Ensure env.py supports 'task' parameter)
|
| 35 |
env = EmailTriageEnv(task=task_name, shuffle=False)
|
| 36 |
obs, info = env.reset(seed=42)
|
| 37 |
|
| 38 |
+
# [START] tag for Output Parsing
|
| 39 |
+
print(f"[START] task={task_name}", flush=True)
|
| 40 |
|
| 41 |
terminated = False
|
| 42 |
step_count = 0
|
|
|
|
| 44 |
emails = list(env._queue)
|
| 45 |
|
| 46 |
while not terminated and step_count < len(emails):
|
| 47 |
+
action = get_llm_action(emails[step_count]['description'],
|
| 48 |
+
emails[step_count]['context'],
|
| 49 |
+
emails[step_count]['keywords'])
|
|
|
|
| 50 |
|
| 51 |
obs, reward, terminated, truncated, info = env.step(action)
|
| 52 |
total_reward += reward
|
| 53 |
|
| 54 |
+
# [STEP] tag
|
| 55 |
print(f"[STEP] step={step_count+1} reward={reward:.2f}", flush=True)
|
| 56 |
step_count += 1
|
| 57 |
|
| 58 |
|
| 59 |
final_score = max(0.01, min(0.99, total_reward))
|
|
|
|
| 60 |
|
| 61 |
+
|
| 62 |
+
print(f"[END] task={task_name} score={final_score:.3f} steps={step_count}", flush=True)
|
| 63 |
|
| 64 |
+
except Exception as e:
|
| 65 |
+
|
| 66 |
print(f"[END] task={task_name} score=0.010 steps=0", flush=True)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|