TanujInsane commited on
Commit
e263f9c
·
1 Parent(s): 676fd8d

fix: add LLM classification via API_BASE_URL and API_KEY

Browse files
Files changed (1) hide show
  1. inference.py +70 -11
inference.py CHANGED
@@ -3,17 +3,27 @@ import traceback
3
  import sys
4
  import os
5
  import time
 
 
 
6
 
7
  BASE_URL = os.environ.get("ENV_URL", "http://127.0.0.1:7860")
8
 
 
 
 
 
 
 
 
 
 
9
  def wait_for_server():
10
  """Retries GET /reset to ensure server is ready before proceeding."""
11
  print(f"Waiting for server at {BASE_URL}...")
12
  for i in range(20):
13
  try:
14
- # Perform GET /reset as requested
15
  resp = requests.get(f"{BASE_URL}/reset", timeout=5)
16
- # If the server responds (even with 405 Method Not Allowed), it's online
17
  if resp.status_code < 500:
18
  print("Server online.")
19
  return True
@@ -26,6 +36,43 @@ def wait_for_server():
26
  print("Warning: Server did not respond after 20 retries. Proceeding anyway...")
27
  return False
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  def main():
30
  print("[START] task=DocumentClassification-v1", flush=True)
31
 
@@ -33,23 +80,37 @@ def main():
33
  wait_for_server()
34
 
35
  try:
36
- # Reset the environment (POST /reset is the valid action endpoint)
37
  reset_resp = requests.post(f"{BASE_URL}/reset", json={}, timeout=60)
38
  reset_resp.raise_for_status()
39
- obs = reset_resp.json()
 
 
 
 
 
 
 
40
 
41
  total_reward = 0.0
42
  num_steps = 5
43
 
44
  for step in range(1, num_steps + 1):
45
- # Take a step with a dummy action (0)
46
- step_resp = requests.post(f"{BASE_URL}/step", json={"action": 0}, timeout=60)
 
 
 
 
47
  step_resp.raise_for_status()
48
- result = step_resp.json()
49
 
50
- reward = float(result.get("reward", 0.0))
51
  total_reward += reward
52
- done = result.get("done", False)
 
 
 
53
 
54
  print(f"[STEP] step={step} reward={reward}", flush=True)
55
 
@@ -60,10 +121,8 @@ def main():
60
  print(f"[END] task=DocumentClassification-v1 score={score:.4f} steps={num_steps}", flush=True)
61
 
62
  except Exception as e:
63
- # On failure, ensure we print the END tag with zero score to satisfy the grader
64
  print(f"[END] task=DocumentClassification-v1 score=0.0 steps=0", flush=True)
65
  traceback.print_exc()
66
- # sys.exit(1) removed - returning gracefully
67
 
68
  if __name__ == "__main__":
69
  main()
 
3
  import sys
4
  import os
5
  import time
6
+ import re
7
+ from openai import OpenAI
8
+ from constants import CATEGORIES
9
 
10
  BASE_URL = os.environ.get("ENV_URL", "http://127.0.0.1:7860")
11
 
12
+ # LLM API configuration from environment variables (Grader injected)
13
+ API_BASE_URL = os.environ.get("API_BASE_URL")
14
+ API_KEY = os.environ.get("API_KEY")
15
+
16
+ # Initialize LLM client
17
+ client = None
18
+ if API_BASE_URL and API_KEY:
19
+ client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
20
+
21
  def wait_for_server():
22
  """Retries GET /reset to ensure server is ready before proceeding."""
23
  print(f"Waiting for server at {BASE_URL}...")
24
  for i in range(20):
25
  try:
 
26
  resp = requests.get(f"{BASE_URL}/reset", timeout=5)
 
27
  if resp.status_code < 500:
28
  print("Server online.")
29
  return True
 
36
  print("Warning: Server did not respond after 20 retries. Proceeding anyway...")
37
  return False
38
 
39
+ def get_llm_prediction(content, difficulty):
40
+ """Uses LLM to classify document content based on available categories for the given difficulty."""
41
+ if not client:
42
+ return 0
43
+
44
+ # Get categories for the current task difficulty from constants
45
+ difficulty_cats = CATEGORIES.get(difficulty, CATEGORIES["easy"])
46
+ category_list = "\n".join([f"- {idx}: {name}" for idx, name in difficulty_cats.items()])
47
+
48
+ prompt = f"""You are a document classification expert.
49
+ Classify the following customer support document into exactly one of these categories:
50
+ {category_list}
51
+
52
+ Document Content:
53
+ {content}
54
+
55
+ Respond ONLY with the category index (integer). Do not include any other text."""
56
+
57
+ try:
58
+ # LiteLLM proxy typically handles the specific model routing
59
+ response = client.chat.completions.create(
60
+ model="gpt-4o",
61
+ messages=[{"role": "user", "content": prompt}],
62
+ temperature=0,
63
+ max_tokens=10
64
+ )
65
+ prediction = response.choices[0].message.content.strip()
66
+
67
+ # Robust parsing to extract the integer index
68
+ match = re.search(r'\d+', prediction)
69
+ if match:
70
+ return int(match.group())
71
+ except Exception as e:
72
+ print(f"LLM Reasoning Error: {e}")
73
+
74
+ return 0
75
+
76
  def main():
77
  print("[START] task=DocumentClassification-v1", flush=True)
78
 
 
80
  wait_for_server()
81
 
82
  try:
83
+ # Reset the environment and get initial observation
84
  reset_resp = requests.post(f"{BASE_URL}/reset", json={}, timeout=60)
85
  reset_resp.raise_for_status()
86
+ init_data = reset_resp.json()
87
+ current_obs = init_data.get("observation", {})
88
+
89
+ # Check environment state to determine difficulty/categories
90
+ state_resp = requests.get(f"{BASE_URL}/state", timeout=10)
91
+ state_resp.raise_for_status()
92
+ state_info = state_resp.json()
93
+ difficulty = state_info.get("difficulty", "easy")
94
 
95
  total_reward = 0.0
96
  num_steps = 5
97
 
98
  for step in range(1, num_steps + 1):
99
+ # Use LLM to predict the action based on document content
100
+ content = current_obs.get("content", "")
101
+ action = get_llm_prediction(content, difficulty)
102
+
103
+ # Execute step with predicted action
104
+ step_resp = requests.post(f"{BASE_URL}/step", json={"action": action}, timeout=60)
105
  step_resp.raise_for_status()
106
+ step_result = step_resp.json()
107
 
108
+ reward = float(step_result.get("reward", 0.0))
109
  total_reward += reward
110
+ done = step_result.get("done", False)
111
+
112
+ # Update the observation for the next iteration
113
+ current_obs = step_result.get("observation", {})
114
 
115
  print(f"[STEP] step={step} reward={reward}", flush=True)
116
 
 
121
  print(f"[END] task=DocumentClassification-v1 score={score:.4f} steps={num_steps}", flush=True)
122
 
123
  except Exception as e:
 
124
  print(f"[END] task=DocumentClassification-v1 score=0.0 steps=0", flush=True)
125
  traceback.print_exc()
 
126
 
127
  if __name__ == "__main__":
128
  main()