Update inference.py
Browse files- inference.py +36 -23
inference.py
CHANGED
|
@@ -4,44 +4,42 @@ import numpy as np
|
|
| 4 |
from openai import OpenAI
|
| 5 |
from env import EmailTriageEnv
|
| 6 |
|
| 7 |
-
# 1. Environment Variables (
|
| 8 |
-
# Grader yahan se check karta hai
|
| 9 |
API_BASE_URL = os.environ.get("API_BASE_URL")
|
| 10 |
-
API_KEY = os.environ.get("API_KEY")
|
| 11 |
MODEL_NAME = os.environ.get("MODEL_NAME", "meta-llama/Llama-3-70b-chat-hf")
|
| 12 |
|
| 13 |
-
# client
|
| 14 |
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
| 15 |
|
| 16 |
-
def
|
| 17 |
-
"""
|
| 18 |
prompt = f"""
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
3. Resolution (0: Wait, 1: Reply, 2: Escalate)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
Return only
|
| 29 |
"""
|
| 30 |
-
|
| 31 |
try:
|
| 32 |
-
# --- YE HAI WO API CALL JO GRADER
|
| 33 |
response = client.chat.completions.create(
|
| 34 |
model=MODEL_NAME,
|
| 35 |
messages=[{"role": "user", "content": prompt}],
|
| 36 |
max_tokens=10,
|
| 37 |
temperature=0
|
| 38 |
)
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
actions = [int(x.strip()) for x in
|
| 42 |
return np.array(actions, dtype=np.int64)
|
| 43 |
-
except:
|
| 44 |
-
# Fallback agar API fail ho
|
| 45 |
return np.array([0, 0, 0], dtype=np.int64)
|
| 46 |
|
| 47 |
def run_inference():
|
|
@@ -49,29 +47,44 @@ def run_inference():
|
|
| 49 |
|
| 50 |
for task_name in tasks:
|
| 51 |
try:
|
|
|
|
| 52 |
env = EmailTriageEnv(task=task_name, shuffle=False)
|
| 53 |
obs, info = env.reset(seed=42)
|
| 54 |
|
|
|
|
| 55 |
print(f"[START] task={task_name}", flush=True)
|
| 56 |
|
| 57 |
terminated = False
|
| 58 |
step_count = 0
|
| 59 |
total_reward = 0.0
|
|
|
|
|
|
|
| 60 |
emails = list(env._queue)
|
| 61 |
|
| 62 |
while not terminated and step_count < len(emails):
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
|
|
|
| 66 |
obs, reward, terminated, truncated, info = env.step(action)
|
| 67 |
total_reward += reward
|
| 68 |
|
|
|
|
| 69 |
print(f"[STEP] step={step_count+1} reward={reward:.2f}", flush=True)
|
| 70 |
step_count += 1
|
| 71 |
|
|
|
|
| 72 |
print(f"[END] task={task_name} score={total_reward:.3f} steps={step_count}", flush=True)
|
| 73 |
|
| 74 |
except Exception as e:
|
|
|
|
|
|
|
| 75 |
continue
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
|
|
|
| 4 |
from openai import OpenAI
|
| 5 |
from env import EmailTriageEnv
|
| 6 |
|
| 7 |
+
# 1. Environment Variables (Grader yahan se API keys uthayega)
|
|
|
|
| 8 |
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 |
+
# OpenAI client initialize karna zaroori hai Proxy ke liye
|
| 13 |
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
| 14 |
|
| 15 |
+
def get_llm_action(email_desc, context, keywords):
|
| 16 |
+
"""Llama-3 ko call karke action lena taaki API call record ho."""
|
| 17 |
prompt = f"""
|
| 18 |
+
Email: {email_desc}
|
| 19 |
+
Context: {context}
|
| 20 |
+
Keywords: {keywords}
|
|
|
|
| 21 |
|
| 22 |
+
Classify this email into 3 labels:
|
| 23 |
+
1. Urgency (0: General, 1: Billing, 2: Security Breach)
|
| 24 |
+
2. Routing (0: AI Auto-Reply, 1: Tech Support, 2: Legal)
|
| 25 |
+
3. Resolution (0: Archive, 1: Draft Reply, 2: Escalate to Human)
|
| 26 |
|
| 27 |
+
Return only the numbers separated by commas. Example: 1, 0, 1
|
| 28 |
"""
|
|
|
|
| 29 |
try:
|
| 30 |
+
# --- YE HAI WO API CALL JO GRADER KO CHAHIYE ---
|
| 31 |
response = client.chat.completions.create(
|
| 32 |
model=MODEL_NAME,
|
| 33 |
messages=[{"role": "user", "content": prompt}],
|
| 34 |
max_tokens=10,
|
| 35 |
temperature=0
|
| 36 |
)
|
| 37 |
+
res = response.choices[0].message.content.strip()
|
| 38 |
+
# Clean the response to get numbers
|
| 39 |
+
actions = [int(x.strip()) for x in res.split(",")[:3]]
|
| 40 |
return np.array(actions, dtype=np.int64)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
# Fallback agar API fail ho (Security breach emails ke liye default high rakhna safe hai)
|
| 43 |
return np.array([0, 0, 0], dtype=np.int64)
|
| 44 |
|
| 45 |
def run_inference():
|
|
|
|
| 47 |
|
| 48 |
for task_name in tasks:
|
| 49 |
try:
|
| 50 |
+
# Task-specific batch load karna
|
| 51 |
env = EmailTriageEnv(task=task_name, shuffle=False)
|
| 52 |
obs, info = env.reset(seed=42)
|
| 53 |
|
| 54 |
+
# [START] tag grader ke liye
|
| 55 |
print(f"[START] task={task_name}", flush=True)
|
| 56 |
|
| 57 |
terminated = False
|
| 58 |
step_count = 0
|
| 59 |
total_reward = 0.0
|
| 60 |
+
|
| 61 |
+
# Email queue se data nikalna
|
| 62 |
emails = list(env._queue)
|
| 63 |
|
| 64 |
while not terminated and step_count < len(emails):
|
| 65 |
+
email_data = emails[step_count]
|
| 66 |
+
|
| 67 |
+
# LLM se action lena (Real API Call)
|
| 68 |
+
action = get_llm_action(
|
| 69 |
+
email_data['description'],
|
| 70 |
+
email_data['context'],
|
| 71 |
+
email_data['keywords']
|
| 72 |
+
)
|
| 73 |
|
| 74 |
+
# Step execute karna
|
| 75 |
obs, reward, terminated, truncated, info = env.step(action)
|
| 76 |
total_reward += reward
|
| 77 |
|
| 78 |
+
# [STEP] tag grader ke liye
|
| 79 |
print(f"[STEP] step={step_count+1} reward={reward:.2f}", flush=True)
|
| 80 |
step_count += 1
|
| 81 |
|
| 82 |
+
# [END] tag grader ke liye
|
| 83 |
print(f"[END] task={task_name} score={total_reward:.3f} steps={step_count}", flush=True)
|
| 84 |
|
| 85 |
except Exception as e:
|
| 86 |
+
# Print error for debugging but don't stop the loop
|
| 87 |
+
print(f"Error in task {task_name}: {e}", file=sys.stderr)
|
| 88 |
continue
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|