Spaces:
Sleeping
Sleeping
eeshwar143 commited on
Commit ·
edf1c38
1
Parent(s): 9be17b9
Use OpenEnv websocket client sessions for inference
Browse files- inference.py +18 -3
- support_queue_env/client.py +82 -149
inference.py
CHANGED
|
@@ -20,6 +20,7 @@ API_BASE_URL = os.getenv("API_BASE_URL", "https://api.openai.com/v1")
|
|
| 20 |
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o-mini")
|
| 21 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 22 |
LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")
|
|
|
|
| 23 |
|
| 24 |
BENCHMARK = "support_queue_env"
|
| 25 |
SUCCESS_SCORE_THRESHOLD = 0.80
|
|
@@ -46,11 +47,14 @@ def log_end(success: bool, steps: int, score: float, rewards: list[float]) -> No
|
|
| 46 |
|
| 47 |
|
| 48 |
def create_openai_client() -> Any:
|
|
|
|
|
|
|
|
|
|
| 49 |
if OpenAI is not None:
|
| 50 |
-
return OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN
|
| 51 |
|
| 52 |
openai_module.api_base = API_BASE_URL
|
| 53 |
-
openai_module.api_key = HF_TOKEN
|
| 54 |
return openai_module
|
| 55 |
|
| 56 |
|
|
@@ -61,6 +65,9 @@ def get_model_message(
|
|
| 61 |
last_reward: float,
|
| 62 |
history: List[str],
|
| 63 |
) -> str:
|
|
|
|
|
|
|
|
|
|
| 64 |
prompt = (
|
| 65 |
"Return a short support-triage recommendation as JSON with fields priority, queue, disposition, summary, response. "
|
| 66 |
f"Step: {step}. Last reward: {last_reward:.4f}. History: {history[-4:]}. Observation: {observation.model_dump_json()}"
|
|
@@ -215,6 +222,14 @@ def heuristic_action(observation: SupportQueueObservation) -> SupportQueueAction
|
|
| 215 |
)
|
| 216 |
|
| 217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
async def run_task(client: Any, env: SupportQueueEnv, task: TaskCard) -> dict[str, Any]:
|
| 219 |
history: List[str] = []
|
| 220 |
rewards: List[float] = []
|
|
@@ -285,7 +300,7 @@ async def main() -> None:
|
|
| 285 |
env: SupportQueueEnv | None = None
|
| 286 |
|
| 287 |
try:
|
| 288 |
-
env = await
|
| 289 |
for task in tasks:
|
| 290 |
results.append(await run_task(client, env, task))
|
| 291 |
except Exception as exc:
|
|
|
|
| 20 |
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o-mini")
|
| 21 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 22 |
LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")
|
| 23 |
+
ENV_BASE_URL = os.getenv("ENV_BASE_URL")
|
| 24 |
|
| 25 |
BENCHMARK = "support_queue_env"
|
| 26 |
SUCCESS_SCORE_THRESHOLD = 0.80
|
|
|
|
| 47 |
|
| 48 |
|
| 49 |
def create_openai_client() -> Any:
|
| 50 |
+
if not HF_TOKEN:
|
| 51 |
+
return None
|
| 52 |
+
|
| 53 |
if OpenAI is not None:
|
| 54 |
+
return OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
|
| 55 |
|
| 56 |
openai_module.api_base = API_BASE_URL
|
| 57 |
+
openai_module.api_key = HF_TOKEN
|
| 58 |
return openai_module
|
| 59 |
|
| 60 |
|
|
|
|
| 65 |
last_reward: float,
|
| 66 |
history: List[str],
|
| 67 |
) -> str:
|
| 68 |
+
if client is None:
|
| 69 |
+
return "hello"
|
| 70 |
+
|
| 71 |
prompt = (
|
| 72 |
"Return a short support-triage recommendation as JSON with fields priority, queue, disposition, summary, response. "
|
| 73 |
f"Step: {step}. Last reward: {last_reward:.4f}. History: {history[-4:]}. Observation: {observation.model_dump_json()}"
|
|
|
|
| 222 |
)
|
| 223 |
|
| 224 |
|
| 225 |
+
async def build_env() -> SupportQueueEnv:
|
| 226 |
+
if ENV_BASE_URL:
|
| 227 |
+
env = SupportQueueEnv(base_url=ENV_BASE_URL)
|
| 228 |
+
await env.connect()
|
| 229 |
+
return env
|
| 230 |
+
return await SupportQueueEnv.from_docker_image(LOCAL_IMAGE_NAME or "support-queue-openenv")
|
| 231 |
+
|
| 232 |
+
|
| 233 |
async def run_task(client: Any, env: SupportQueueEnv, task: TaskCard) -> dict[str, Any]:
|
| 234 |
history: List[str] = []
|
| 235 |
rewards: List[float] = []
|
|
|
|
| 300 |
env: SupportQueueEnv | None = None
|
| 301 |
|
| 302 |
try:
|
| 303 |
+
env = await build_env()
|
| 304 |
for task in tasks:
|
| 305 |
results.append(await run_task(client, env, task))
|
| 306 |
except Exception as exc:
|
support_queue_env/client.py
CHANGED
|
@@ -1,157 +1,90 @@
|
|
| 1 |
-
"""
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import
|
| 6 |
-
import os
|
| 7 |
-
import socket
|
| 8 |
-
import subprocess
|
| 9 |
-
import time
|
| 10 |
-
from typing import Any
|
| 11 |
|
| 12 |
import requests
|
| 13 |
|
| 14 |
from support_queue_env.models import TaskCard, SupportQueueAction, SupportQueueObservation, SupportQueueState
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
self
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
self
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
while time.time() < deadline:
|
| 97 |
-
try:
|
| 98 |
-
response = requests.get(f"{base_url}/health", timeout=3)
|
| 99 |
-
if response.ok:
|
| 100 |
-
return
|
| 101 |
-
except Exception as exc:
|
| 102 |
-
last_error = str(exc)
|
| 103 |
-
time.sleep(1)
|
| 104 |
-
|
| 105 |
-
raise RuntimeError(f"Environment did not become ready at {base_url}: {last_error}")
|
| 106 |
-
|
| 107 |
-
@staticmethod
|
| 108 |
-
def _run(command: list[str]) -> str:
|
| 109 |
-
result = subprocess.run(command, check=False, capture_output=True, text=True)
|
| 110 |
-
if result.returncode != 0:
|
| 111 |
-
raise RuntimeError((result.stderr or result.stdout).strip() or f"Command failed: {' '.join(command)}")
|
| 112 |
-
return result.stdout
|
| 113 |
-
|
| 114 |
-
@staticmethod
|
| 115 |
-
def _safe_remove_container(container_id: str) -> None:
|
| 116 |
-
subprocess.run(["docker", "rm", "-f", container_id], check=False, capture_output=True, text=True)
|
| 117 |
-
|
| 118 |
-
def list_tasks(self) -> list[TaskCard]:
|
| 119 |
-
response = requests.get(f"{self.base_url}/tasks", timeout=30)
|
| 120 |
-
response.raise_for_status()
|
| 121 |
-
payload = response.json()
|
| 122 |
-
return [TaskCard.model_validate(item) for item in payload["tasks"]]
|
| 123 |
-
|
| 124 |
-
async def alist_tasks(self) -> list[TaskCard]:
|
| 125 |
-
return await asyncio.to_thread(self.list_tasks)
|
| 126 |
-
|
| 127 |
-
def reset_sync(self, **kwargs: Any) -> _Result:
|
| 128 |
-
response = requests.post(f"{self.base_url}/reset", json=kwargs or {}, timeout=30)
|
| 129 |
-
response.raise_for_status()
|
| 130 |
-
return _Result(response.json())
|
| 131 |
-
|
| 132 |
-
async def reset(self, **kwargs: Any) -> _Result:
|
| 133 |
-
return await asyncio.to_thread(self.reset_sync, **kwargs)
|
| 134 |
-
|
| 135 |
-
def step_sync(self, action: SupportQueueAction) -> _Result:
|
| 136 |
-
response = requests.post(
|
| 137 |
-
f"{self.base_url}/step",
|
| 138 |
-
json={"action": action.model_dump()},
|
| 139 |
-
timeout=30,
|
| 140 |
-
)
|
| 141 |
-
response.raise_for_status()
|
| 142 |
-
return _Result(response.json())
|
| 143 |
-
|
| 144 |
-
async def step(self, action: SupportQueueAction) -> _Result:
|
| 145 |
-
return await asyncio.to_thread(self.step_sync, action)
|
| 146 |
-
|
| 147 |
-
def state_sync(self) -> SupportQueueState:
|
| 148 |
-
response = requests.get(f"{self.base_url}/state", timeout=30)
|
| 149 |
-
response.raise_for_status()
|
| 150 |
-
return SupportQueueState.model_validate(response.json())
|
| 151 |
-
|
| 152 |
-
async def state(self) -> SupportQueueState:
|
| 153 |
-
return await asyncio.to_thread(self.state_sync)
|
| 154 |
-
|
| 155 |
-
async def close(self) -> None:
|
| 156 |
-
if self.container_id:
|
| 157 |
-
await asyncio.to_thread(self._safe_remove_container, self.container_id)
|
|
|
|
| 1 |
+
"""OpenEnv client for interacting with the support queue environment."""
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
+
from typing import Any, Dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
import requests
|
| 8 |
|
| 9 |
from support_queue_env.models import TaskCard, SupportQueueAction, SupportQueueObservation, SupportQueueState
|
| 10 |
|
| 11 |
+
try:
|
| 12 |
+
from openenv.core.client_types import StepResult
|
| 13 |
+
from openenv.core.env_client import EnvClient as OpenEnvClient
|
| 14 |
+
except Exception: # pragma: no cover - fallback for environments without openenv-core
|
| 15 |
+
OpenEnvClient = None
|
| 16 |
+
StepResult = None
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
if OpenEnvClient is not None:
|
| 20 |
+
|
| 21 |
+
class SupportQueueEnv(OpenEnvClient[SupportQueueAction, SupportQueueObservation, SupportQueueState]):
|
| 22 |
+
def __init__(self, base_url: str, **kwargs: Any) -> None:
|
| 23 |
+
super().__init__(base_url=base_url, **kwargs)
|
| 24 |
+
self.base_url = base_url.rstrip("/")
|
| 25 |
+
|
| 26 |
+
def _step_payload(self, action: SupportQueueAction) -> Dict[str, Any]:
|
| 27 |
+
return action.model_dump()
|
| 28 |
+
|
| 29 |
+
def _parse_result(self, payload: Dict[str, Any]) -> StepResult[SupportQueueObservation]:
|
| 30 |
+
observation = SupportQueueObservation.model_validate(payload.get("observation", {}))
|
| 31 |
+
return StepResult(
|
| 32 |
+
observation=observation,
|
| 33 |
+
reward=payload.get("reward"),
|
| 34 |
+
done=payload.get("done", False),
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
def _parse_state(self, payload: Dict[str, Any]) -> SupportQueueState:
|
| 38 |
+
return SupportQueueState.model_validate(payload)
|
| 39 |
+
|
| 40 |
+
def list_tasks(self) -> list[TaskCard]:
|
| 41 |
+
response = requests.get(f"{self.base_url.rstrip('/')}/tasks", timeout=30)
|
| 42 |
+
response.raise_for_status()
|
| 43 |
+
payload = response.json()
|
| 44 |
+
return [TaskCard.model_validate(item) for item in payload["tasks"]]
|
| 45 |
+
|
| 46 |
+
else:
|
| 47 |
+
|
| 48 |
+
class _Result:
|
| 49 |
+
def __init__(self, payload: dict[str, Any]) -> None:
|
| 50 |
+
self.observation = SupportQueueObservation.model_validate(payload["observation"])
|
| 51 |
+
self.reward = float(payload.get("reward") or 0.0)
|
| 52 |
+
self.done = bool(payload.get("done"))
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class SupportQueueEnv:
|
| 56 |
+
def __init__(self, base_url: str, **_: Any) -> None:
|
| 57 |
+
self.base_url = base_url.rstrip("/")
|
| 58 |
+
|
| 59 |
+
@classmethod
|
| 60 |
+
async def from_docker_image(cls, image_name: str | None = None) -> "SupportQueueEnv":
|
| 61 |
+
_ = image_name
|
| 62 |
+
return cls(base_url="http://127.0.0.1:8000")
|
| 63 |
+
|
| 64 |
+
def list_tasks(self) -> list[TaskCard]:
|
| 65 |
+
response = requests.get(f"{self.base_url}/tasks", timeout=30)
|
| 66 |
+
response.raise_for_status()
|
| 67 |
+
payload = response.json()
|
| 68 |
+
return [TaskCard.model_validate(item) for item in payload["tasks"]]
|
| 69 |
+
|
| 70 |
+
async def reset(self, **kwargs: Any) -> _Result:
|
| 71 |
+
response = requests.post(f"{self.base_url}/reset", json=kwargs or {}, timeout=30)
|
| 72 |
+
response.raise_for_status()
|
| 73 |
+
return _Result(response.json())
|
| 74 |
+
|
| 75 |
+
async def step(self, action: SupportQueueAction) -> _Result:
|
| 76 |
+
response = requests.post(
|
| 77 |
+
f"{self.base_url}/step",
|
| 78 |
+
json={"action": action.model_dump()},
|
| 79 |
+
timeout=30,
|
| 80 |
+
)
|
| 81 |
+
response.raise_for_status()
|
| 82 |
+
return _Result(response.json())
|
| 83 |
+
|
| 84 |
+
async def state(self) -> SupportQueueState:
|
| 85 |
+
response = requests.get(f"{self.base_url}/state", timeout=30)
|
| 86 |
+
response.raise_for_status()
|
| 87 |
+
return SupportQueueState.model_validate(response.json())
|
| 88 |
+
|
| 89 |
+
async def close(self) -> None:
|
| 90 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|