clarifai-protocol 0.0.33__cp310-cp310-win_amd64.whl → 0.0.43__cp310-cp310-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: clarifai-protocol
3
- Version: 0.0.33
3
+ Version: 0.0.43
4
4
  Summary: Clarifai Python Runner Protocol
5
5
  Author-email: Clarifai <support@clarifai.com>
6
6
  License: Apache License
@@ -230,5 +230,69 @@ Dynamic: license-file
230
230
 
231
231
  This is a proprietary protocol used by our runners to communicate with our API. This should be installed as part of our python SDK.
232
232
 
233
+ ## Request Cancellation Support
234
+
235
+ The protocol now supports request cancellation, allowing models to abort in-flight requests to external inference servers when a user cancels their request.
236
+
237
+ ### Features
238
+
239
+ - **Request ID Access**: Models can access the current request ID via `get_request_id()`
240
+ - **Abort Callbacks**: Models can register a callback that will be invoked when requests are cancelled or when connections are aborted
241
+ - **Thread Safety**: All operations are thread-safe and work correctly with concurrent requests
242
+ - **Background Execution**: Abort callbacks run in background threads to avoid blocking the protocol
243
+
244
+ ### Usage
245
+
246
+ ```python
247
+ from clarifai_protocol import get_request_id, register_abort_callback
248
+ import requests
249
+
250
+ class MyModel:
251
+ def __init__(self):
252
+ super().__init__()
253
+ self.sglang_url = "http://localhost:30000"
254
+ # Register abort callback during initialization
255
+ register_abort_callback(self._abort_sglang)
256
+
257
+ def _abort_sglang(self, req_id: str) -> None:
258
+ """Abort handler called when requests are cancelled."""
259
+ try:
260
+ requests.post(
261
+ f"{self.sglang_url}/abort_request",
262
+ json={"rid": req_id},
263
+ timeout=2.0
264
+ )
265
+ except Exception:
266
+ pass # Handle exceptions gracefully
267
+
268
+ def generate(self, prompt: str):
269
+ """Generate text using external inference server."""
270
+ # Get the request ID to pass to the external server
271
+ req_id = get_request_id()
272
+
273
+ # Use req_id when calling external services
274
+ for token in self._call_sglang(prompt, req_id):
275
+ yield token
276
+ ```
277
+
278
+ ### API Reference
279
+
280
+ #### `get_request_id() -> Optional[str]`
281
+
282
+ Returns the current request ID, or `None` if called outside of a request context.
283
+
284
+ Use this as an identifier when calling external inference servers so that cancellation can properly abort the request.
285
+
286
+ #### `register_abort_callback(callback: Callable[[str], None]) -> None`
287
+
288
+ Register a function to be called when a request is cancelled or the connection is aborted.
289
+
290
+ - Should be called once during model initialization
291
+ - The callback receives the cancelled request's ID as a parameter
292
+ - The callback runs in a background thread
293
+ - **The callback should be idempotent** (safe to call multiple times with the same req_id)
294
+ - Exceptions in the callback are logged but don't crash the protocol
295
+ - Triggered when: explicit cancellation (`RUNNER_ITEM_CANCELLED`) or connection abort (stream done/cancelled)
296
+
233
297
  # Release instructions
234
298
  Bump the version in the file VERSION, merge it, pull master, git tag to the same version, push the tag to github to release.
@@ -0,0 +1,8 @@
1
+ clarifai_protocol.cp310-win_amd64.pyd,sha256=K0tZeg-jm-mi7l7XZdAyeZSstCnA8t6jz4hCCl2soOg,856064
2
+ clarifai_protocol.pyi,sha256=5m8N23qxNtegaMfw7AeAktlh16fOHzxlj9cQ0g6L6zI,990
3
+ clarifai_protocol-0.0.43.dist-info/licenses/LICENSE,sha256=HrhfyXIkWY2tGFK11kg7vPCqhgh5DcxleloqdhrpyMY,11558
4
+ clarifai_protocol.build/.gitignore,sha256=aEiIwOuxfzdCmLZe4oB1JsBmCUxwG8x-u-HBCV9JT8E,1
5
+ clarifai_protocol-0.0.43.dist-info/METADATA,sha256=wY2dfQ7ONJYTHyhoWPkNiD4o0u9Eado6YJupFzqWJzU,17191
6
+ clarifai_protocol-0.0.43.dist-info/WHEEL,sha256=EHJKs6QQxvilV2yuNDSsIgSJQh_mTAi3OaY72-LUeOA,96
7
+ clarifai_protocol-0.0.43.dist-info/top_level.txt,sha256=qwWV-wytBq70748luF3R1ouCIWiIm2R551LIVXUtpB8,18
8
+ clarifai_protocol-0.0.43.dist-info/RECORD,,
Binary file
clarifai_protocol.pyi CHANGED
@@ -1,7 +1,7 @@
1
1
  # This file was generated by Nuitka
2
2
 
3
3
  # Stubs included by default
4
- from base_runner import BaseRunner
4
+ from base_runner import BaseRunner, get_item_id, register_item_abort_callback, shutdown_abort_executor
5
5
 
6
6
 
7
7
  __name__ = ...
@@ -10,32 +10,40 @@ __name__ = ...
10
10
 
11
11
  # Modules used internally, to allow implicit dependencies to be seen:
12
12
  import os
13
- import atexit
14
- import collections
15
- import concurrent
16
- import concurrent.futures
17
- import itertools
18
- import signal
19
- import sys
13
+ import importlib
14
+ import importlib.util
15
+ import subprocess
20
16
  import threading
21
17
  import time
22
- import traceback
23
- import uuid
24
- import abc
18
+ import concurrent
19
+ import concurrent.futures
25
20
  import concurrent.futures.ThreadPoolExecutor
26
- import queue
21
+ import datetime
27
22
  import typing
28
- import grpc
29
23
  import clarifai
30
24
  import clarifai.client
31
- import clarifai.client.base
25
+ import clarifai.client.input
32
26
  import clarifai.utils
33
27
  import clarifai.utils.logging
34
28
  import clarifai_grpc
35
29
  import clarifai_grpc.grpc
36
30
  import clarifai_grpc.grpc.api
37
31
  import clarifai_grpc.grpc.api.status
32
+ import av
33
+ import atexit
34
+ import collections
35
+ import contextvars
36
+ import itertools
37
+ import signal
38
+ import sys
39
+ import traceback
40
+ import uuid
41
+ import abc
42
+ import queue
43
+ import grpc
44
+ import clarifai.client.base
38
45
  import grpc._cython
39
46
  import grpc._server
40
47
  import http
41
- import http.server
48
+ import http.server
49
+ import requests
@@ -1,8 +0,0 @@
1
- clarifai_protocol.cp310-win_amd64.pyd,sha256=29PVZM_NOwHUkrxYq92LmODgrG0ZvWCoE4bFYnm3S9Q,660992
2
- clarifai_protocol.pyi,sha256=0U76Jmy9bci3OioLcWj0jNp_SlkWS39rf45zz2BGj_A,775
3
- clarifai_protocol-0.0.33.dist-info/licenses/LICENSE,sha256=HrhfyXIkWY2tGFK11kg7vPCqhgh5DcxleloqdhrpyMY,11558
4
- clarifai_protocol.build/.gitignore,sha256=aEiIwOuxfzdCmLZe4oB1JsBmCUxwG8x-u-HBCV9JT8E,1
5
- clarifai_protocol-0.0.33.dist-info/METADATA,sha256=IHshAB-Ipn_0FczMF8VDdn1-HrQuDfnlxp5bYiqB1-A,14597
6
- clarifai_protocol-0.0.33.dist-info/WHEEL,sha256=EHJKs6QQxvilV2yuNDSsIgSJQh_mTAi3OaY72-LUeOA,96
7
- clarifai_protocol-0.0.33.dist-info/top_level.txt,sha256=qwWV-wytBq70748luF3R1ouCIWiIm2R551LIVXUtpB8,18
8
- clarifai_protocol-0.0.33.dist-info/RECORD,,