Spaces:
Sleeping
Sleeping
Savita Srivastava commited on
Commit ·
c430f78
1
Parent(s): 74ed166
fix llm proxy
Browse files- inference.py +6 -38
inference.py
CHANGED
|
@@ -17,12 +17,8 @@ import os
|
|
| 17 |
from typing import List
|
| 18 |
from openai import OpenAI
|
| 19 |
|
| 20 |
-
# ---------------------------------------------------------------------------
|
| 21 |
-
# CONFIG (STRICT - use injected vars)
|
| 22 |
-
# ---------------------------------------------------------------------------
|
| 23 |
-
|
| 24 |
API_BASE_URL = os.environ["API_BASE_URL"]
|
| 25 |
-
API_KEY = os.environ["
|
| 26 |
MODEL_NAME = os.environ.get("MODEL_NAME", "gpt-4o-mini")
|
| 27 |
SERVER_URL = os.environ.get("ENV_SERVER_URL", "http://localhost:7860")
|
| 28 |
BENCHMARK = "sql-repair"
|
|
@@ -40,10 +36,6 @@ MAX_STEPS = 8
|
|
| 40 |
TEMPERATURE = 0.2
|
| 41 |
MAX_TOKENS = 150
|
| 42 |
|
| 43 |
-
# ---------------------------------------------------------------------------
|
| 44 |
-
# LOGGING
|
| 45 |
-
# ---------------------------------------------------------------------------
|
| 46 |
-
|
| 47 |
def log_start(task: str):
|
| 48 |
print(f"[START] task={task} env={BENCHMARK} model={MODEL_NAME}", flush=True)
|
| 49 |
|
|
@@ -61,10 +53,6 @@ def log_end(success, steps, score, rewards):
|
|
| 61 |
flush=True,
|
| 62 |
)
|
| 63 |
|
| 64 |
-
# ---------------------------------------------------------------------------
|
| 65 |
-
# SAFE HTTP
|
| 66 |
-
# ---------------------------------------------------------------------------
|
| 67 |
-
|
| 68 |
def _post(path: str, body: dict) -> dict:
|
| 69 |
import urllib.request, json
|
| 70 |
try:
|
|
@@ -89,19 +77,11 @@ def _get(path: str) -> dict:
|
|
| 89 |
print(f"[ERROR] GET {path}: {e}", flush=True)
|
| 90 |
return {}
|
| 91 |
|
| 92 |
-
# ---------------------------------------------------------------------------
|
| 93 |
-
# LLM (FIXED - ALWAYS CALL)
|
| 94 |
-
# ---------------------------------------------------------------------------
|
| 95 |
-
|
| 96 |
def create_client():
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
)
|
| 102 |
-
except Exception as e:
|
| 103 |
-
print(f"[WARN] OpenAI init failed: {e}", flush=True)
|
| 104 |
-
return None
|
| 105 |
|
| 106 |
def ask_llm(client, prompt: str) -> str:
|
| 107 |
try:
|
|
@@ -117,10 +97,6 @@ def ask_llm(client, prompt: str) -> str:
|
|
| 117 |
print(f"[ERROR] LLM failed: {e}", flush=True)
|
| 118 |
return "SELECT 1" # fallback but still counted as API call
|
| 119 |
|
| 120 |
-
# ---------------------------------------------------------------------------
|
| 121 |
-
# CORE LOGIC
|
| 122 |
-
# ---------------------------------------------------------------------------
|
| 123 |
-
|
| 124 |
def run_episode(client, task_id, difficulty):
|
| 125 |
rewards: List[float] = []
|
| 126 |
steps_taken = 0
|
|
@@ -158,7 +134,7 @@ Broken Query:
|
|
| 158 |
Return ONLY corrected SQL.
|
| 159 |
"""
|
| 160 |
|
| 161 |
-
fixed = ask_llm(client, prompt)
|
| 162 |
|
| 163 |
action = {"type": "submit_query", "query": fixed}
|
| 164 |
result = _post("/step", action)
|
|
@@ -187,10 +163,6 @@ Return ONLY corrected SQL.
|
|
| 187 |
log_end(success, steps_taken, score, rewards)
|
| 188 |
return score
|
| 189 |
|
| 190 |
-
# ---------------------------------------------------------------------------
|
| 191 |
-
# MAIN
|
| 192 |
-
# ---------------------------------------------------------------------------
|
| 193 |
-
|
| 194 |
async def main():
|
| 195 |
client = create_client()
|
| 196 |
scores = []
|
|
@@ -205,10 +177,6 @@ async def main():
|
|
| 205 |
avg = sum(scores) / len(scores) if scores else 0.0
|
| 206 |
print(f"[SUMMARY] avg_score={avg:.3f}", flush=True)
|
| 207 |
|
| 208 |
-
# ---------------------------------------------------------------------------
|
| 209 |
-
# ENTRY POINT (CRASH SAFE)
|
| 210 |
-
# ---------------------------------------------------------------------------
|
| 211 |
-
|
| 212 |
if __name__ == "__main__":
|
| 213 |
import traceback
|
| 214 |
try:
|
|
|
|
| 17 |
from typing import List
|
| 18 |
from openai import OpenAI
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
API_BASE_URL = os.environ["API_BASE_URL"]
|
| 21 |
+
API_KEY = os.environ["HF_TOKEN"]
|
| 22 |
MODEL_NAME = os.environ.get("MODEL_NAME", "gpt-4o-mini")
|
| 23 |
SERVER_URL = os.environ.get("ENV_SERVER_URL", "http://localhost:7860")
|
| 24 |
BENCHMARK = "sql-repair"
|
|
|
|
| 36 |
TEMPERATURE = 0.2
|
| 37 |
MAX_TOKENS = 150
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def log_start(task: str):
|
| 40 |
print(f"[START] task={task} env={BENCHMARK} model={MODEL_NAME}", flush=True)
|
| 41 |
|
|
|
|
| 53 |
flush=True,
|
| 54 |
)
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
def _post(path: str, body: dict) -> dict:
|
| 57 |
import urllib.request, json
|
| 58 |
try:
|
|
|
|
| 77 |
print(f"[ERROR] GET {path}: {e}", flush=True)
|
| 78 |
return {}
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
def create_client():
|
| 81 |
+
return OpenAI(
|
| 82 |
+
base_url=API_BASE_URL,
|
| 83 |
+
api_key=API_KEY
|
| 84 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
def ask_llm(client, prompt: str) -> str:
|
| 87 |
try:
|
|
|
|
| 97 |
print(f"[ERROR] LLM failed: {e}", flush=True)
|
| 98 |
return "SELECT 1" # fallback but still counted as API call
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
def run_episode(client, task_id, difficulty):
|
| 101 |
rewards: List[float] = []
|
| 102 |
steps_taken = 0
|
|
|
|
| 134 |
Return ONLY corrected SQL.
|
| 135 |
"""
|
| 136 |
|
| 137 |
+
fixed = ask_llm(client, prompt) or broken
|
| 138 |
|
| 139 |
action = {"type": "submit_query", "query": fixed}
|
| 140 |
result = _post("/step", action)
|
|
|
|
| 163 |
log_end(success, steps_taken, score, rewards)
|
| 164 |
return score
|
| 165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
async def main():
|
| 167 |
client = create_client()
|
| 168 |
scores = []
|
|
|
|
| 177 |
avg = sum(scores) / len(scores) if scores else 0.0
|
| 178 |
print(f"[SUMMARY] avg_score={avg:.3f}", flush=True)
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
if __name__ == "__main__":
|
| 181 |
import traceback
|
| 182 |
try:
|