Update inference.py
Browse files- inference.py +36 -14
inference.py
CHANGED
|
@@ -1,28 +1,50 @@
|
|
| 1 |
from env import EmailEnv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
env = EmailEnv()
|
| 4 |
|
| 5 |
-
# START block
|
| 6 |
print("[START] task=email_triage", flush=True)
|
| 7 |
|
| 8 |
-
# Reset environment
|
| 9 |
obs = env.reset()
|
| 10 |
-
|
| 11 |
-
# Simple agent logic
|
| 12 |
text = obs["text"]
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
# Step
|
| 22 |
reward = env.step(action)
|
| 23 |
|
| 24 |
-
# STEP block
|
| 25 |
print(f"[STEP] reward={reward}", flush=True)
|
| 26 |
-
|
| 27 |
-
# END block
|
| 28 |
print(f"[END] score={reward}", flush=True)
|
|
|
|
| 1 |
from env import EmailEnv
|
| 2 |
+
import os
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
# ✅ Use validator proxy (MANDATORY)
|
| 6 |
+
client = OpenAI(
|
| 7 |
+
api_key=os.environ["API_KEY"],
|
| 8 |
+
base_url=os.environ["API_BASE_URL"],
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
def call_llm(text):
|
| 12 |
+
res = client.chat.completions.create(
|
| 13 |
+
model="openai/gpt-4.1-mini",
|
| 14 |
+
messages=[
|
| 15 |
+
{
|
| 16 |
+
"role": "system",
|
| 17 |
+
"content": "Classify the email into spam, important, or normal and give a short reply in JSON format: {label, reply}"
|
| 18 |
+
},
|
| 19 |
+
{"role": "user", "content": text},
|
| 20 |
+
],
|
| 21 |
+
temperature=0,
|
| 22 |
+
)
|
| 23 |
+
return res.choices[0].message.content
|
| 24 |
+
|
| 25 |
|
| 26 |
env = EmailEnv()
|
| 27 |
|
|
|
|
| 28 |
print("[START] task=email_triage", flush=True)
|
| 29 |
|
|
|
|
| 30 |
obs = env.reset()
|
|
|
|
|
|
|
| 31 |
text = obs["text"]
|
| 32 |
|
| 33 |
+
# ✅ CALL LLM (THIS FIXES YOUR ERROR)
|
| 34 |
+
response = call_llm(text)
|
| 35 |
+
|
| 36 |
+
# simple parsing (safe fallback)
|
| 37 |
+
import json
|
| 38 |
+
try:
|
| 39 |
+
parsed = json.loads(response)
|
| 40 |
+
action = {
|
| 41 |
+
"label": parsed.get("label", "normal"),
|
| 42 |
+
"reply": parsed.get("reply", "OK"),
|
| 43 |
+
}
|
| 44 |
+
except:
|
| 45 |
+
action = {"label": "normal", "reply": "OK"}
|
| 46 |
|
|
|
|
| 47 |
reward = env.step(action)
|
| 48 |
|
|
|
|
| 49 |
print(f"[STEP] reward={reward}", flush=True)
|
|
|
|
|
|
|
| 50 |
print(f"[END] score={reward}", flush=True)
|