|
| 1 | +import threading |
| 2 | +from typing import Iterator |
| 3 | + |
| 4 | +import httpx |
| 5 | +import requests |
| 6 | +from clarifai_protocol import get_item_id, register_item_abort_callback |
| 7 | + |
| 8 | +from clarifai.runners.models.openai_class import OpenAIModelClass |
| 9 | +from clarifai.utils.logging import logger |
| 10 | + |
| 11 | + |
| 12 | +class SGLangCancellationHandler: |
| 13 | + # Important: in addition to closing the httpx response (which kills the TCP |
| 14 | + # connection and lets sglang detect the disconnect), we also POST the captured |
| 15 | + # request id (rid) to sglang's /abort_request endpoint. This frees the KV cache |
| 16 | + # immediately instead of waiting for the engine to notice the disconnect. |
| 17 | + def __init__(self, base_url: str): |
| 18 | + self._cancel_events = {} |
| 19 | + self._responses = {} |
| 20 | + self._rids = {} |
| 21 | + self._early_aborts = set() |
| 22 | + self._lock = threading.Lock() |
| 23 | + self._base_url = base_url |
| 24 | + register_item_abort_callback(self._handle_abort) |
| 25 | + |
| 26 | + def _call_abort_request(self, rid: str) -> None: |
| 27 | + try: |
| 28 | + resp = requests.post(f"{self._base_url}/abort_request", json={"rid": rid}, timeout=2) |
| 29 | + logger.info( |
| 30 | + f"[SGLangCancellationHandler] /abort_request rid={rid} " |
| 31 | + f"status={resp.status_code} body={resp.text}" |
| 32 | + ) |
| 33 | + except Exception as e: |
| 34 | + logger.warning(f"[SGLangCancellationHandler] /abort_request failed: {e}") |
| 35 | + |
| 36 | + def _handle_abort(self, item_id: str) -> None: |
| 37 | + rid = None |
| 38 | + with self._lock: |
| 39 | + event = self._cancel_events.get(item_id) |
| 40 | + response = self._responses.get(item_id) |
| 41 | + rid = self._rids.get(item_id) |
| 42 | + if event: |
| 43 | + event.set() |
| 44 | + if response: |
| 45 | + try: |
| 46 | + response.close() |
| 47 | + except Exception: |
| 48 | + pass |
| 49 | + else: |
| 50 | + self._early_aborts.add(item_id) |
| 51 | + # Call outside the lock to avoid holding it during network I/O. |
| 52 | + if rid: |
| 53 | + self._call_abort_request(rid) |
| 54 | + |
| 55 | + def register_request(self, item_id: str, response=None) -> threading.Event: |
| 56 | + cancel_event = threading.Event() |
| 57 | + with self._lock: |
| 58 | + self._cancel_events[item_id] = cancel_event |
| 59 | + if response is not None: |
| 60 | + self._responses[item_id] = response |
| 61 | + if item_id in self._early_aborts: |
| 62 | + cancel_event.set() |
| 63 | + self._early_aborts.discard(item_id) |
| 64 | + if response is not None: |
| 65 | + try: |
| 66 | + response.close() |
| 67 | + except Exception: |
| 68 | + pass |
| 69 | + return cancel_event |
| 70 | + |
| 71 | + def register_rid(self, item_id: str, rid: str) -> None: |
| 72 | + """Register the sglang request id once captured from the first chunk. |
| 73 | + If the request was already cancelled before the rid was known, issue |
| 74 | + /abort_request now so the engine frees the KV cache immediately. |
| 75 | + """ |
| 76 | + should_abort = False |
| 77 | + with self._lock: |
| 78 | + if item_id in self._cancel_events: |
| 79 | + self._rids[item_id] = rid |
| 80 | + if self._cancel_events[item_id].is_set(): |
| 81 | + should_abort = True |
| 82 | + if should_abort: |
| 83 | + self._call_abort_request(rid) |
| 84 | + |
| 85 | + def unregister_request(self, item_id: str) -> None: |
| 86 | + with self._lock: |
| 87 | + self._cancel_events.pop(item_id, None) |
| 88 | + self._responses.pop(item_id, None) |
| 89 | + self._rids.pop(item_id, None) |
| 90 | + self._early_aborts.discard(item_id) |
| 91 | + |
| 92 | + |
| 93 | +class SGLangOpenAIModelClass(OpenAIModelClass): |
| 94 | + """SGLang-backed OpenAI model with /health probes and cancellation support. |
| 95 | +
|
| 96 | + Subclasses must set client, model, server, base_url and cancellation_handler in |
| 97 | + load_model(), for example: |
| 98 | +
|
| 99 | + def load_model(self): |
| 100 | + self.server = sglang_openai_server(checkpoints, **server_args) |
| 101 | + self.base_url = f"http://{self.server.host}:{self.server.port}" |
| 102 | + self.client = OpenAI(base_url=f"{self.base_url}/v1", api_key="x") |
| 103 | + self.model = self.client.models.list().data[0].id |
| 104 | + self.cancellation_handler = SGLangCancellationHandler(self.base_url) |
| 105 | +
|
| 106 | + For cancellation in generate() or custom streaming methods, follow this pattern: |
| 107 | +
|
| 108 | + def generate(self, prompt, ...) -> Iterator[str]: |
| 109 | + item_id = None |
| 110 | + cancel_event = None |
| 111 | + try: |
| 112 | + item_id = get_item_id() |
| 113 | + except Exception: |
| 114 | + pass |
| 115 | + try: |
| 116 | + response = self.client.chat.completions.create(..., stream=True) |
| 117 | + if item_id: |
| 118 | + cancel_event = self.cancellation_handler.register_request( |
| 119 | + item_id, response=response.response |
| 120 | + ) |
| 121 | + rid_registered = False |
| 122 | + for chunk in response: |
| 123 | + if item_id and not rid_registered: |
| 124 | + rid = getattr(chunk, 'id', None) |
| 125 | + if rid: |
| 126 | + self.cancellation_handler.register_rid(item_id, rid) |
| 127 | + rid_registered = True |
| 128 | + if cancel_event and cancel_event.is_set(): |
| 129 | + return |
| 130 | + yield ... |
| 131 | + except httpx.ReadError: |
| 132 | + pass |
| 133 | + finally: |
| 134 | + if item_id: |
| 135 | + self.cancellation_handler.unregister_request(item_id) |
| 136 | + """ |
| 137 | + |
| 138 | + server = None |
| 139 | + base_url = None |
| 140 | + cancellation_handler = None |
| 141 | + |
| 142 | + def _health_url(self) -> str: |
| 143 | + if self.base_url: |
| 144 | + return f"{self.base_url}/health" |
| 145 | + return f"http://{self.server.host}:{self.server.port}/health" |
| 146 | + |
| 147 | + def handle_liveness_probe(self) -> bool: |
| 148 | + if self.server is None: |
| 149 | + return super().handle_liveness_probe() |
| 150 | + try: |
| 151 | + resp = httpx.get(self._health_url(), timeout=5.0) |
| 152 | + return resp.status_code == 200 |
| 153 | + except Exception: |
| 154 | + return False |
| 155 | + |
| 156 | + def handle_readiness_probe(self) -> bool: |
| 157 | + if self.server is None: |
| 158 | + return super().handle_readiness_probe() |
| 159 | + try: |
| 160 | + resp = httpx.get(self._health_url(), timeout=10.0) |
| 161 | + return resp.status_code == 200 |
| 162 | + except Exception: |
| 163 | + return False |
| 164 | + |
| 165 | + @OpenAIModelClass.method |
| 166 | + def openai_stream_transport(self, msg: str) -> Iterator[str]: |
| 167 | + from pydantic_core import from_json |
| 168 | + |
| 169 | + try: |
| 170 | + item_id = get_item_id() |
| 171 | + except Exception: |
| 172 | + item_id = None |
| 173 | + |
| 174 | + cancel_event = None |
| 175 | + try: |
| 176 | + request_data = from_json(msg) |
| 177 | + request_data = self._update_old_fields(request_data) |
| 178 | + endpoint = request_data.pop("openai_endpoint", self.DEFAULT_ENDPOINT) |
| 179 | + if endpoint not in [self.ENDPOINT_CHAT_COMPLETIONS, self.ENDPOINT_RESPONSES]: |
| 180 | + raise ValueError( |
| 181 | + f"Only {self.ENDPOINT_CHAT_COMPLETIONS} and {self.ENDPOINT_RESPONSES} endpoints are supported for streaming." |
| 182 | + ) |
| 183 | + |
| 184 | + if endpoint == self.ENDPOINT_RESPONSES: |
| 185 | + response_args = {**request_data} |
| 186 | + response_args.update({"model": self.model}) |
| 187 | + response = self.client.responses.create(**response_args) |
| 188 | + else: |
| 189 | + completion_args = self._create_completion_args(request_data) |
| 190 | + response = self.client.chat.completions.create(**completion_args) |
| 191 | + |
| 192 | + if item_id and self.cancellation_handler: |
| 193 | + cancel_event = self.cancellation_handler.register_request( |
| 194 | + item_id, response=response.response |
| 195 | + ) |
| 196 | + |
| 197 | + rid_registered = False |
| 198 | + for chunk in response: |
| 199 | + if item_id and self.cancellation_handler and not rid_registered: |
| 200 | + rid = getattr(chunk, 'id', None) or getattr( |
| 201 | + getattr(chunk, 'response', None), 'id', None |
| 202 | + ) |
| 203 | + if rid: |
| 204 | + self.cancellation_handler.register_rid(item_id, rid) |
| 205 | + rid_registered = True |
| 206 | + if cancel_event and cancel_event.is_set(): |
| 207 | + return |
| 208 | + self._set_usage(chunk) |
| 209 | + yield chunk.model_dump_json() |
| 210 | + except httpx.ReadError: |
| 211 | + pass |
| 212 | + finally: |
| 213 | + if item_id and self.cancellation_handler: |
| 214 | + self.cancellation_handler.unregister_request(item_id) |
0 commit comments