kumoai 2.12.0.dev202511071730__cp310-cp310-win_amd64.whl → 2.13.0.dev202511261731__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.
kumoai/__init__.py CHANGED
@@ -184,15 +184,12 @@ def init(
184
184
  snowflake_credentials
185
185
  ) if not api_key and snowflake_credentials else None
186
186
  client = KumoClient(url=url, api_key=api_key, spcs_token=spcs_token)
187
- if client.authenticate():
188
- global_state._url = client._url
189
- global_state._api_key = client._api_key
190
- global_state._snowflake_credentials = snowflake_credentials
191
- global_state._spcs_token = client._spcs_token
192
- global_state._snowpark_session = snowpark_session
193
- else:
194
- raise ValueError("Client authentication failed. Please check if you "
195
- "have a valid API key.")
187
+ client.authenticate()
188
+ global_state._url = client._url
189
+ global_state._api_key = client._api_key
190
+ global_state._snowflake_credentials = snowflake_credentials
191
+ global_state._spcs_token = client._spcs_token
192
+ global_state._snowpark_session = snowpark_session
196
193
 
197
194
  if not api_key and snowflake_credentials:
198
195
  # Refresh token every 10 minutes (expires in 1 hour):
kumoai/_version.py CHANGED
@@ -1 +1 @@
1
- __version__ = '2.12.0.dev202511071730'
1
+ __version__ = '2.13.0.dev202511261731'
kumoai/client/client.py CHANGED
@@ -20,7 +20,6 @@ if TYPE_CHECKING:
20
20
  )
21
21
  from kumoai.client.online import OnlineServingEndpointAPI
22
22
  from kumoai.client.pquery import PQueryAPI
23
- from kumoai.client.rfm import RFMAPI
24
23
  from kumoai.client.source_table import SourceTableAPI
25
24
  from kumoai.client.table import TableAPI
26
25
 
@@ -73,12 +72,15 @@ class KumoClient:
73
72
  self._session.headers.update(
74
73
  {'Authorization': f'Snowflake Token={self._spcs_token}'})
75
74
 
76
- def authenticate(self) -> bool:
77
- r"""Raises an exception if authentication fails. Succeeds if the
78
- client is properly formed.
79
- """
80
- return self._session.get(f"{self._url}/v1/connectors",
81
- verify=self._verify_ssl).ok
75
+ def authenticate(self) -> None:
76
+ """Raises an exception if authentication fails."""
77
+ try:
78
+ self._session.get(self._url + '/v1/connectors',
79
+ verify=self._verify_ssl).raise_for_status()
80
+ except Exception:
81
+ raise ValueError(
82
+ "Client authentication failed. Please check if you "
83
+ "have a valid API key/credentials.")
82
84
 
83
85
  def set_spcs_token(self, spcs_token: str) -> None:
84
86
  r"""Sets the SPCS token for the client and updates the session
@@ -163,12 +165,6 @@ class KumoClient:
163
165
  from kumoai.client.online import OnlineServingEndpointAPI
164
166
  return OnlineServingEndpointAPI(self)
165
167
 
166
- @property
167
- def rfm_api(self) -> 'RFMAPI':
168
- r"""Returns the typed RFM API."""
169
- from kumoai.client.rfm import RFMAPI
170
- return RFMAPI(self)
171
-
172
168
  def _request(self, endpoint: Endpoint, **kwargs: Any) -> requests.Response:
173
169
  r"""Send a HTTP request to the specified endpoint."""
174
170
  endpoint_str = endpoint.get_path()
kumoai/client/rfm.py CHANGED
@@ -1,3 +1,5 @@
1
+ from typing import Any
2
+
1
3
  from kumoapi.json_serde import to_json_dict
2
4
  from kumoapi.rfm import (
3
5
  RFMEvaluateResponse,
@@ -28,25 +30,32 @@ class RFMAPI:
28
30
  Returns:
29
31
  RFMPredictResponse containing the predictions
30
32
  """
31
- # Send binary data to the predict endpoint
32
33
  response = self._client._request(
33
- RFMEndpoints.predict, data=request,
34
- headers={'Content-Type': 'application/x-protobuf'})
34
+ RFMEndpoints.predict,
35
+ data=request,
36
+ headers={'Content-Type': 'application/x-protobuf'},
37
+ )
35
38
  raise_on_error(response)
36
39
  return parse_response(RFMPredictResponse, response)
37
40
 
38
- def explain(self, request: bytes) -> RFMExplanationResponse:
41
+ def explain(
42
+ self,
43
+ request: bytes,
44
+ skip_summary: bool = False,
45
+ ) -> RFMExplanationResponse:
39
46
  """Explain the RFM model on the given context.
40
47
 
41
48
  Args:
42
49
  request: The predict request as serialized protobuf.
50
+ skip_summary: Whether to skip generating a human-readable summary
51
+ of the explanation.
43
52
 
44
53
  Returns:
45
54
  RFMPredictResponse containing the explanations
46
55
  """
47
- # Send binary data to the explain endpoint
56
+ params: dict[str, Any] = {'generate_summary': not skip_summary}
48
57
  response = self._client._request(
49
- RFMEndpoints.explain, data=request,
58
+ RFMEndpoints.explain, data=request, params=params,
50
59
  headers={'Content-Type': 'application/x-protobuf'})
51
60
  raise_on_error(response)
52
61
  return parse_response(RFMExplanationResponse, response)
@@ -60,7 +69,6 @@ class RFMAPI:
60
69
  Returns:
61
70
  RFMEvaluateResponse containing the computed metrics
62
71
  """
63
- # Send binary data to the evaluate endpoint
64
72
  response = self._client._request(
65
73
  RFMEndpoints.evaluate, data=request,
66
74
  headers={'Content-Type': 'application/x-protobuf'})
@@ -31,14 +31,122 @@ Please create a feature request at 'https://github.com/kumo-ai/kumo-rfm'."""
31
31
 
32
32
  raise RuntimeError(_msg) from e
33
33
 
34
- from typing import Optional, Dict
34
+ from dataclasses import dataclass
35
+ from enum import Enum
36
+ import ipaddress
37
+ import logging
38
+ import re
39
+ import socket
40
+ import threading
41
+ from typing import Optional, Dict, Tuple
35
42
  import os
43
+ from urllib.parse import urlparse
36
44
  import kumoai
45
+ from kumoai.client.client import KumoClient
46
+ from .sagemaker import (KumoClient_SageMakerAdapter,
47
+ KumoClient_SageMakerProxy_Local)
37
48
  from .local_table import LocalTable
38
49
  from .local_graph import LocalGraph
39
- from .rfm import KumoRFM
50
+ from .rfm import ExplainConfig, Explanation, KumoRFM
40
51
  from .authenticate import authenticate
41
52
 
53
+ logger = logging.getLogger('kumoai_rfm')
54
+
55
+
56
+ def _is_local_address(host: str | None) -> bool:
57
+ """Return True if the hostname/IP refers to the local machine."""
58
+ if not host:
59
+ return False
60
+ try:
61
+ infos = socket.getaddrinfo(host, None)
62
+ for _, _, _, _, sockaddr in infos:
63
+ ip = sockaddr[0]
64
+ ip_obj = ipaddress.ip_address(ip)
65
+ if ip_obj.is_loopback or ip_obj.is_unspecified:
66
+ return True
67
+ return False
68
+ except Exception:
69
+ return False
70
+
71
+
72
+ class InferenceBackend(str, Enum):
73
+ REST = "REST"
74
+ LOCAL_SAGEMAKER = "LOCAL_SAGEMAKER"
75
+ AWS_SAGEMAKER = "AWS_SAGEMAKER"
76
+ UNKNOWN = "UNKNOWN"
77
+
78
+
79
+ def _detect_backend(
80
+ url: str) -> Tuple[InferenceBackend, Optional[str], Optional[str]]:
81
+ parsed = urlparse(url)
82
+
83
+ # Remote SageMaker
84
+ if ("runtime.sagemaker" in parsed.netloc
85
+ and parsed.path.endswith("/invocations")):
86
+ # Example: https://runtime.sagemaker.us-west-2.amazonaws.com/
87
+ # endpoints/Name/invocations
88
+ match = re.search(r"runtime\.sagemaker\.([a-z0-9-]+)\.amazonaws\.com",
89
+ parsed.netloc)
90
+ region = match.group(1) if match else None
91
+ m = re.search(r"/endpoints/([^/]+)/invocations", parsed.path)
92
+ endpoint_name = m.group(1) if m else None
93
+ return InferenceBackend.AWS_SAGEMAKER, region, endpoint_name
94
+
95
+ # Local SageMaker
96
+ if parsed.port == 8080 and parsed.path.endswith(
97
+ "/invocations") and _is_local_address(parsed.hostname):
98
+ return InferenceBackend.LOCAL_SAGEMAKER, None, None
99
+
100
+ # Default: regular REST
101
+ return InferenceBackend.REST, None, None
102
+
103
+
104
+ @dataclass
105
+ class RfmGlobalState:
106
+ _url: str = '__url_not_provided__'
107
+ _backend: InferenceBackend = InferenceBackend.UNKNOWN
108
+ _region: Optional[str] = None
109
+ _endpoint_name: Optional[str] = None
110
+ _thread_local = threading.local()
111
+
112
+ # Thread-safe init-once.
113
+ _initialized: bool = False
114
+ _lock: threading.Lock = threading.Lock()
115
+
116
+ @property
117
+ def client(self) -> KumoClient:
118
+ if self._backend == InferenceBackend.REST:
119
+ return kumoai.global_state.client
120
+
121
+ if hasattr(self._thread_local, '_sagemaker'):
122
+ # Set the spcs token in the client to ensure it has the latest.
123
+ return self._thread_local._sagemaker
124
+
125
+ sagemaker_client: KumoClient
126
+ if self._backend == InferenceBackend.LOCAL_SAGEMAKER:
127
+ sagemaker_client = KumoClient_SageMakerProxy_Local(self._url)
128
+ else:
129
+ assert self._backend == InferenceBackend.AWS_SAGEMAKER
130
+ assert self._region
131
+ assert self._endpoint_name
132
+ sagemaker_client = KumoClient_SageMakerAdapter(
133
+ self._region, self._endpoint_name)
134
+
135
+ self._thread_local._sagemaker = sagemaker_client
136
+ return sagemaker_client
137
+
138
+ def reset(self) -> None: # For testing only.
139
+ with self._lock:
140
+ self._initialized = False
141
+ self._url = '__url_not_provided__'
142
+ self._backend = InferenceBackend.UNKNOWN
143
+ self._region = None
144
+ self._endpoint_name = None
145
+ self._thread_local = threading.local()
146
+
147
+
148
+ global_state = RfmGlobalState()
149
+
42
150
 
43
151
  def init(
44
152
  url: Optional[str] = None,
@@ -47,19 +155,54 @@ def init(
47
155
  snowflake_application: Optional[str] = None,
48
156
  log_level: str = "INFO",
49
157
  ) -> None:
50
- if url is None:
51
- url = os.getenv("KUMO_API_URL", "https://kumorfm.ai/api")
158
+ with global_state._lock:
159
+ if global_state._initialized:
160
+ if url != global_state._url:
161
+ raise ValueError(
162
+ "Kumo RFM has already been initialized with a different "
163
+ "URL. Re-initialization with a different URL is not "
164
+ "supported.")
165
+ return
166
+
167
+ if url is None:
168
+ url = os.getenv("RFM_API_URL", "https://kumorfm.ai/api")
169
+
170
+ backend, region, endpoint_name = _detect_backend(url)
171
+ if backend == InferenceBackend.REST:
172
+ # Initialize kumoai.global_state
173
+ if (kumoai.global_state.initialized
174
+ and kumoai.global_state._url != url):
175
+ raise ValueError(
176
+ "Kumo AI SDK has already been initialized with different "
177
+ "API URL. Please restart Python interpreter and "
178
+ "initialize via kumoai.rfm.init()")
179
+ kumoai.init(url=url, api_key=api_key,
180
+ snowflake_credentials=snowflake_credentials,
181
+ snowflake_application=snowflake_application,
182
+ log_level=log_level)
183
+ elif backend == InferenceBackend.AWS_SAGEMAKER:
184
+ assert region
185
+ assert endpoint_name
186
+ KumoClient_SageMakerAdapter(region, endpoint_name).authenticate()
187
+ else:
188
+ assert backend == InferenceBackend.LOCAL_SAGEMAKER
189
+ KumoClient_SageMakerProxy_Local(url).authenticate()
52
190
 
53
- kumoai.init(url=url, api_key=api_key,
54
- snowflake_credentials=snowflake_credentials,
55
- snowflake_application=snowflake_application,
56
- log_level=log_level)
191
+ global_state._url = url
192
+ global_state._backend = backend
193
+ global_state._region = region
194
+ global_state._endpoint_name = endpoint_name
195
+ global_state._initialized = True
196
+ logger.info("Kumo RFM initialized with backend: %s, url: %s", backend,
197
+ url)
57
198
 
58
199
 
59
200
  __all__ = [
60
201
  'LocalTable',
61
202
  'LocalGraph',
62
203
  'KumoRFM',
204
+ 'ExplainConfig',
205
+ 'Explanation',
63
206
  'authenticate',
64
207
  'init',
65
208
  ]
@@ -2,7 +2,6 @@ from typing import Dict, List, Optional, Tuple
2
2
 
3
3
  import numpy as np
4
4
  import pandas as pd
5
- from kumoapi.model_plan import RunMode
6
5
  from kumoapi.rfm.context import EdgeLayout, Link, Subgraph, Table
7
6
  from kumoapi.typing import Stype
8
7
 
@@ -33,7 +32,6 @@ class LocalGraphSampler:
33
32
  entity_table_names: Tuple[str, ...],
34
33
  node: np.ndarray,
35
34
  time: np.ndarray,
36
- run_mode: RunMode,
37
35
  num_neighbors: List[int],
38
36
  exclude_cols_dict: Dict[str, List[str]],
39
37
  ) -> Subgraph:
@@ -264,8 +264,8 @@ class LocalPQueryDriver:
264
264
  reached_end = True
265
265
  break
266
266
  candidate_offset = 0
267
- end_offset = self._query.target_timeframe.end_date_offset
268
- anchor_time = anchor_time - (end_offset *
267
+ time_frame = self._query.target_timeframe.timeframe
268
+ anchor_time = anchor_time - (time_frame *
269
269
  self._query.num_forecasts)
270
270
  if anchor_time < self._graph_store.min_time:
271
271
  reached_end = True
@@ -5,7 +5,17 @@ from collections import defaultdict
5
5
  from collections.abc import Generator
6
6
  from contextlib import contextmanager
7
7
  from dataclasses import dataclass, replace
8
- from typing import Iterator, List, Literal, Optional, Tuple, Union, overload
8
+ from typing import (
9
+ Any,
10
+ Dict,
11
+ Iterator,
12
+ List,
13
+ Literal,
14
+ Optional,
15
+ Tuple,
16
+ Union,
17
+ overload,
18
+ )
9
19
 
10
20
  import numpy as np
11
21
  import pandas as pd
@@ -20,7 +30,7 @@ from kumoapi.rfm import (
20
30
  )
21
31
  from kumoapi.task import TaskType
22
32
 
23
- from kumoai import global_state
33
+ from kumoai.client.rfm import RFMAPI
24
34
  from kumoai.exceptions import HTTPException
25
35
  from kumoai.experimental.rfm import LocalGraph
26
36
  from kumoai.experimental.rfm.local_graph_sampler import LocalGraphSampler
@@ -29,6 +39,7 @@ from kumoai.experimental.rfm.local_pquery_driver import (
29
39
  LocalPQueryDriver,
30
40
  date_offset_to_seconds,
31
41
  )
42
+ from kumoai.mixin import CastMixin
32
43
  from kumoai.utils import InteractiveProgressLogger, ProgressLogger
33
44
 
34
45
  _RANDOM_SEED = 42
@@ -59,6 +70,17 @@ _SIZE_LIMIT_MSG = ("Context size exceeds the 30MB limit. {stats}\nPlease "
59
70
  "beyond this for your use-case.")
60
71
 
61
72
 
73
+ @dataclass(repr=False)
74
+ class ExplainConfig(CastMixin):
75
+ """Configuration for explainability.
76
+
77
+ Args:
78
+ skip_summary: Whether to skip generating a human-readable summary of
79
+ the explanation.
80
+ """
81
+ skip_summary: bool = False
82
+
83
+
62
84
  @dataclass(repr=False)
63
85
  class Explanation:
64
86
  prediction: pd.DataFrame
@@ -86,6 +108,12 @@ class Explanation:
86
108
  def __repr__(self) -> str:
87
109
  return str((self.prediction, self.summary))
88
110
 
111
+ def _ipython_display_(self) -> None:
112
+ from IPython.display import Markdown, display
113
+
114
+ display(self.prediction)
115
+ display(Markdown(self.summary))
116
+
89
117
 
90
118
  class KumoRFM:
91
119
  r"""The Kumo Relational Foundation model (RFM) from the `KumoRFM: A
@@ -113,9 +141,9 @@ class KumoRFM:
113
141
 
114
142
  rfm = KumoRFM(graph)
115
143
 
116
- query = ("PREDICT COUNT(transactions.*, 0, 30, days)>0 "
117
- "FOR users.user_id=0")
118
- result = rfm.query(query)
144
+ query = ("PREDICT COUNT(orders.*, 0, 30, days)>0 "
145
+ "FOR users.user_id=1")
146
+ result = rfm.predict(query)
119
147
 
120
148
  print(result) # user_id COUNT(transactions.*, 0, 30, days) > 0
121
149
  # 1 0.85
@@ -144,9 +172,20 @@ class KumoRFM:
144
172
  self._graph_store = LocalGraphStore(graph, preprocess, verbose)
145
173
  self._graph_sampler = LocalGraphSampler(self._graph_store)
146
174
 
175
+ self._client: Optional[RFMAPI] = None
176
+
147
177
  self._batch_size: Optional[int | Literal['max']] = None
148
178
  self.num_retries: int = 0
149
179
 
180
+ @property
181
+ def _api_client(self) -> RFMAPI:
182
+ if self._client is not None:
183
+ return self._client
184
+
185
+ from kumoai.experimental.rfm import global_state
186
+ self._client = RFMAPI(global_state.client)
187
+ return self._client
188
+
150
189
  def __repr__(self) -> str:
151
190
  return f'{self.__class__.__name__}()'
152
191
 
@@ -208,7 +247,7 @@ class KumoRFM:
208
247
  query: str,
209
248
  indices: Union[List[str], List[float], List[int], None] = None,
210
249
  *,
211
- explain: Literal[True],
250
+ explain: Union[Literal[True], ExplainConfig, Dict[str, Any]],
212
251
  anchor_time: Union[pd.Timestamp, Literal['entity'], None] = None,
213
252
  context_anchor_time: Union[pd.Timestamp, None] = None,
214
253
  run_mode: Union[RunMode, str] = RunMode.FAST,
@@ -226,7 +265,7 @@ class KumoRFM:
226
265
  query: str,
227
266
  indices: Union[List[str], List[float], List[int], None] = None,
228
267
  *,
229
- explain: bool = False,
268
+ explain: Union[bool, ExplainConfig, Dict[str, Any]] = False,
230
269
  anchor_time: Union[pd.Timestamp, Literal['entity'], None] = None,
231
270
  context_anchor_time: Union[pd.Timestamp, None] = None,
232
271
  run_mode: Union[RunMode, str] = RunMode.FAST,
@@ -246,9 +285,12 @@ class KumoRFM:
246
285
  be generated for all indices, independent of whether they
247
286
  fulfill entity filter constraints. To pre-filter entities, use
248
287
  :meth:`~KumoRFM.is_valid_entity`.
249
- explain: If set to ``True``, will additionally explain the
250
- prediction. Explainability is currently only supported for
251
- single entity predictions with ``run_mode="FAST"``.
288
+ explain: Configuration for explainability.
289
+ If set to ``True``, will additionally explain the prediction.
290
+ Passing in an :class:`ExplainConfig` instance provides control
291
+ over which parts of explanation are generated.
292
+ Explainability is currently only supported for single entity
293
+ predictions with ``run_mode="FAST"``.
252
294
  anchor_time: The anchor timestamp for the prediction. If set to
253
295
  ``None``, will use the maximum timestamp in the data.
254
296
  If set to ``"entity"``, will use the timestamp of the entity.
@@ -272,16 +314,25 @@ class KumoRFM:
272
314
 
273
315
  Returns:
274
316
  The predictions as a :class:`pandas.DataFrame`.
275
- If ``explain=True``, additionally returns a textual summary that
276
- explains the prediction.
317
+ If ``explain`` is provided, returns an :class:`Explanation` object
318
+ containing the prediction, summary, and details.
277
319
  """
320
+ explain_config: Optional[ExplainConfig] = None
321
+ if explain is True:
322
+ explain_config = ExplainConfig()
323
+ elif explain is not False:
324
+ explain_config = ExplainConfig._cast(explain)
325
+
278
326
  query_def = self._parse_query(query)
327
+ query_str = query_def.to_string()
279
328
 
280
329
  if num_hops != 2 and num_neighbors is not None:
281
330
  warnings.warn(f"Received custom 'num_neighbors' option; ignoring "
282
331
  f"custom 'num_hops={num_hops}' option")
283
332
 
284
- if explain and run_mode in {RunMode.NORMAL, RunMode.BEST}:
333
+ if explain_config is not None and run_mode in {
334
+ RunMode.NORMAL, RunMode.BEST
335
+ }:
285
336
  warnings.warn(f"Explainability is currently only supported for "
286
337
  f"run mode 'FAST' (got '{run_mode}'). Provided run "
287
338
  f"mode has been reset. Please lower the run mode to "
@@ -298,13 +349,13 @@ class KumoRFM:
298
349
  if len(indices) == 0:
299
350
  raise ValueError("At least one entity is required")
300
351
 
301
- if explain and len(indices) > 1:
352
+ if explain_config is not None and len(indices) > 1:
302
353
  raise ValueError(
303
354
  f"Cannot explain predictions for more than a single entity "
304
355
  f"(got {len(indices)})")
305
356
 
306
357
  query_repr = query_def.to_string(rich=True, exclude_predict=True)
307
- if explain:
358
+ if explain_config is not None:
308
359
  msg = f'[bold]EXPLAIN[/bold] {query_repr}'
309
360
  else:
310
361
  msg = f'[bold]PREDICT[/bold] {query_repr}'
@@ -355,6 +406,7 @@ class KumoRFM:
355
406
  request = RFMPredictRequest(
356
407
  context=context,
357
408
  run_mode=RunMode(run_mode),
409
+ query=query_str,
358
410
  use_prediction_time=use_prediction_time,
359
411
  )
360
412
  with warnings.catch_warnings():
@@ -378,12 +430,15 @@ class KumoRFM:
378
430
 
379
431
  for attempt in range(self.num_retries + 1):
380
432
  try:
381
- if explain:
382
- resp = global_state.client.rfm_api.explain(_bytes)
433
+ if explain_config is not None:
434
+ resp = self._api_client.explain(
435
+ request=_bytes,
436
+ skip_summary=explain_config.skip_summary,
437
+ )
383
438
  summary = resp.summary
384
439
  details = resp.details
385
440
  else:
386
- resp = global_state.client.rfm_api.predict(_bytes)
441
+ resp = self._api_client.predict(_bytes)
387
442
  df = pd.DataFrame(**resp.prediction)
388
443
 
389
444
  # Cast 'ENTITY' to correct data type:
@@ -430,7 +485,7 @@ class KumoRFM:
430
485
  else:
431
486
  prediction = pd.concat(predictions, ignore_index=True)
432
487
 
433
- if explain:
488
+ if explain_config is not None:
434
489
  assert len(predictions) == 1
435
490
  assert summary is not None
436
491
  assert details is not None
@@ -586,10 +641,10 @@ class KumoRFM:
586
641
 
587
642
  if len(request_bytes) > _MAX_SIZE:
588
643
  stats_msg = Context.get_memory_stats(request_msg.context)
589
- raise ValueError(_SIZE_LIMIT_MSG.format(stats_msg=stats_msg))
644
+ raise ValueError(_SIZE_LIMIT_MSG.format(stats=stats_msg))
590
645
 
591
646
  try:
592
- resp = global_state.client.rfm_api.evaluate(request_bytes)
647
+ resp = self._api_client.evaluate(request_bytes)
593
648
  except HTTPException as e:
594
649
  try:
595
650
  msg = json.loads(e.detail)['detail']
@@ -687,7 +742,8 @@ class KumoRFM:
687
742
  graph_definition=self._graph_def,
688
743
  )
689
744
 
690
- resp = global_state.client.rfm_api.parse_query(request)
745
+ resp = self._api_client.parse_query(request)
746
+
691
747
  # TODO Expose validation warnings.
692
748
 
693
749
  if len(resp.validation_response.warnings) > 0:
@@ -991,7 +1047,6 @@ class KumoRFM:
991
1047
  train_time.astype('datetime64[ns]').astype(int).to_numpy(),
992
1048
  test_time.astype('datetime64[ns]').astype(int).to_numpy(),
993
1049
  ]),
994
- run_mode=run_mode,
995
1050
  num_neighbors=num_neighbors,
996
1051
  exclude_cols_dict=exclude_cols_dict,
997
1052
  )
@@ -0,0 +1,130 @@
1
+ import base64
2
+ import json
3
+ from typing import Any, Dict, List, Tuple
4
+
5
+ import boto3
6
+ import requests
7
+ from mypy_boto3_sagemaker_runtime.client import SageMakerRuntimeClient
8
+ from mypy_boto3_sagemaker_runtime.type_defs import InvokeEndpointOutputTypeDef
9
+
10
+ from kumoai.client import KumoClient
11
+ from kumoai.client.endpoints import Endpoint, HTTPMethod
12
+ from kumoai.exceptions import HTTPException
13
+
14
+
15
+ class SageMakerResponseAdapter(requests.Response):
16
+ def __init__(self, sm_response: InvokeEndpointOutputTypeDef):
17
+ super().__init__()
18
+ # Read the body bytes
19
+ self._content = sm_response['Body'].read()
20
+ self.status_code = 200
21
+ self.headers['Content-Type'] = sm_response.get('ContentType',
22
+ 'application/json')
23
+ # Optionally, you can store original sm_response for debugging
24
+ self.sm_response = sm_response
25
+
26
+ @property
27
+ def text(self) -> str:
28
+ assert isinstance(self._content, bytes)
29
+ return self._content.decode('utf-8')
30
+
31
+ def json(self, **kwargs) -> dict[str, Any]: # type: ignore
32
+ return json.loads(self.text, **kwargs)
33
+
34
+
35
+ class KumoClient_SageMakerAdapter(KumoClient):
36
+ def __init__(self, region: str, endpoint_name: str):
37
+ self._client: SageMakerRuntimeClient = boto3.client(
38
+ service_name="sagemaker-runtime", region_name=region)
39
+ self._endpoint_name = endpoint_name
40
+
41
+ # Recording buffers.
42
+ self._recording_active = False
43
+ self._recorded_reqs: List[Dict[str, Any]] = []
44
+ self._recorded_resps: List[Dict[str, Any]] = []
45
+
46
+ def authenticate(self) -> None:
47
+ # TODO(siyang): call /ping to verify?
48
+ pass
49
+
50
+ def _request(self, endpoint: Endpoint, **kwargs: Any) -> requests.Response:
51
+ assert endpoint.method == HTTPMethod.POST
52
+ if 'json' in kwargs:
53
+ payload = json.dumps(kwargs.pop('json'))
54
+ elif 'data' in kwargs:
55
+ raw_payload = kwargs.pop('data')
56
+ assert isinstance(raw_payload, bytes)
57
+ payload = base64.b64encode(raw_payload).decode()
58
+ else:
59
+ raise HTTPException(400, 'Unable to send data to KumoRFM.')
60
+
61
+ request = {
62
+ 'method': endpoint.get_path().rsplit('/')[-1],
63
+ 'payload': payload,
64
+ }
65
+ response: InvokeEndpointOutputTypeDef = self._client.invoke_endpoint(
66
+ EndpointName=self._endpoint_name,
67
+ ContentType="application/json",
68
+ Body=json.dumps(request),
69
+ )
70
+
71
+ adapted_response = SageMakerResponseAdapter(response)
72
+
73
+ # If validation is active, store input/output
74
+ if self._recording_active:
75
+ self._recorded_reqs.append(request)
76
+ self._recorded_resps.append(adapted_response.json())
77
+
78
+ return adapted_response
79
+
80
+ def start_recording(self) -> None:
81
+ """Start recording requests/responses to/from sagemaker endpoint."""
82
+ assert not self._recording_active
83
+ self._recording_active = True
84
+ self._recorded_reqs.clear()
85
+ self._recorded_resps.clear()
86
+
87
+ def end_recording(self) -> List[Tuple[Dict[str, Any], Dict[str, Any]]]:
88
+ """Stop recording and return recorded requests/responses."""
89
+ assert self._recording_active
90
+ self._recording_active = False
91
+ recorded = list(zip(self._recorded_reqs, self._recorded_resps))
92
+ self._recorded_reqs.clear()
93
+ self._recorded_resps.clear()
94
+ return recorded
95
+
96
+
97
+ class KumoClient_SageMakerProxy_Local(KumoClient):
98
+ def __init__(self, url: str):
99
+ self._client = KumoClient(url, api_key=None)
100
+ self._client._api_url = self._client._url
101
+ self._endpoint = Endpoint('/invocations', HTTPMethod.POST)
102
+
103
+ def authenticate(self) -> None:
104
+ try:
105
+ self._client._session.get(
106
+ self._url + '/ping',
107
+ verify=self._verify_ssl).raise_for_status()
108
+ except Exception:
109
+ raise ValueError(
110
+ "Client authentication failed. Please check if you "
111
+ "have a valid API key/credentials.")
112
+
113
+ def _request(self, endpoint: Endpoint, **kwargs: Any) -> requests.Response:
114
+ assert endpoint.method == HTTPMethod.POST
115
+ if 'json' in kwargs:
116
+ payload = json.dumps(kwargs.pop('json'))
117
+ elif 'data' in kwargs:
118
+ raw_payload = kwargs.pop('data')
119
+ assert isinstance(raw_payload, bytes)
120
+ payload = base64.b64encode(raw_payload).decode()
121
+ else:
122
+ raise HTTPException(400, 'Unable to send data to KumoRFM.')
123
+ return self._client._request(
124
+ self._endpoint,
125
+ json={
126
+ 'method': endpoint.get_path().rsplit('/')[-1],
127
+ 'payload': payload,
128
+ },
129
+ **kwargs,
130
+ )
Binary file
kumoai/spcs.py CHANGED
@@ -54,9 +54,7 @@ def _refresh_spcs_token() -> None:
54
54
  api_key=global_state._api_key,
55
55
  spcs_token=spcs_token,
56
56
  )
57
- if not client.authenticate():
58
- raise ValueError("Client authentication failed. Please check if you "
59
- "have a valid API key.")
57
+ client.authenticate()
60
58
 
61
59
  # Update state:
62
60
  global_state.set_spcs_token(spcs_token)
@@ -103,10 +103,13 @@ class InteractiveProgressLogger(ProgressLogger):
103
103
  self._progress.update(self._task, advance=1) # type: ignore
104
104
 
105
105
  def __enter__(self) -> Self:
106
+ from kumoai import in_notebook
107
+
106
108
  super().__enter__()
107
109
 
108
- sys.stdout.write("\x1b]9;4;3\x07")
109
- sys.stdout.flush()
110
+ if not in_notebook(): # Render progress bar in TUI.
111
+ sys.stdout.write("\x1b]9;4;3\x07")
112
+ sys.stdout.flush()
110
113
 
111
114
  if self.verbose:
112
115
  self._live = Live(
@@ -119,6 +122,8 @@ class InteractiveProgressLogger(ProgressLogger):
119
122
  return self
120
123
 
121
124
  def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
125
+ from kumoai import in_notebook
126
+
122
127
  super().__exit__(exc_type, exc_val, exc_tb)
123
128
 
124
129
  if exc_type is not None:
@@ -134,8 +139,9 @@ class InteractiveProgressLogger(ProgressLogger):
134
139
  self._live.stop()
135
140
  self._live = None
136
141
 
137
- sys.stdout.write("\x1b]9;4;0\x07")
138
- sys.stdout.flush()
142
+ if not in_notebook():
143
+ sys.stdout.write("\x1b]9;4;0\x07")
144
+ sys.stdout.flush()
139
145
 
140
146
  def __rich_console__(
141
147
  self,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: kumoai
3
- Version: 2.12.0.dev202511071730
3
+ Version: 2.13.0.dev202511261731
4
4
  Summary: AI on the Modern Data Stack
5
5
  Author-email: "Kumo.AI" <hello@kumo.ai>
6
6
  License-Expression: MIT
@@ -23,11 +23,13 @@ Requires-Dist: requests>=2.28.2
23
23
  Requires-Dist: urllib3
24
24
  Requires-Dist: plotly
25
25
  Requires-Dist: typing_extensions>=4.5.0
26
- Requires-Dist: kumo-api==0.45.0
26
+ Requires-Dist: kumo-api==0.46.0
27
27
  Requires-Dist: tqdm>=4.66.0
28
28
  Requires-Dist: aiohttp>=3.10.0
29
29
  Requires-Dist: pydantic>=1.10.21
30
30
  Requires-Dist: rich>=9.0.0
31
+ Requires-Dist: mypy-boto3-sagemaker-runtime
32
+ Requires-Dist: boto3
31
33
  Provides-Extra: doc
32
34
  Requires-Dist: sphinx; extra == "doc"
33
35
  Requires-Dist: sphinx-book-theme; extra == "doc"
@@ -38,6 +40,13 @@ Provides-Extra: test
38
40
  Requires-Dist: pytest; extra == "test"
39
41
  Requires-Dist: pytest-mock; extra == "test"
40
42
  Requires-Dist: requests-mock; extra == "test"
43
+ Provides-Extra: test-sagemaker
44
+ Requires-Dist: sagemaker; extra == "test-sagemaker"
45
+ Requires-Dist: pandas==2.1.4; extra == "test-sagemaker"
46
+ Requires-Dist: pyarrow==12.0.1; extra == "test-sagemaker"
47
+ Provides-Extra: sagemaker
48
+ Requires-Dist: boto3<2.0,>=1.30.0; extra == "sagemaker"
49
+ Requires-Dist: mypy-boto3-sagemaker-runtime<2.0,>=1.34.0; extra == "sagemaker"
41
50
  Dynamic: license-file
42
51
  Dynamic: requires-dist
43
52
 
@@ -1,27 +1,27 @@
1
- kumoai/__init__.py,sha256=4efagNAotP3c8mj8yyDGfVFcbgQ9l4wRC4FP-Yt0J3E,11002
1
+ kumoai/__init__.py,sha256=qu-qohU2cQlManX1aZIlzA3ivKl52m-cSQBPSW8urUU,10837
2
2
  kumoai/_logging.py,sha256=qL4JbMQwKXri2f-SEJoFB8TY5ALG12S-nobGTNWxW-A,915
3
3
  kumoai/_singleton.py,sha256=i2BHWKpccNh5SJGDyU0IXsnYzJAYr8Xb0wz4c6LRbpo,861
4
- kumoai/_version.py,sha256=eTnPFx1EhbvlnxZni04U0p2axk-lywAnES4dQ8MuDTs,39
4
+ kumoai/_version.py,sha256=P7PbPaqmt6kLq-80AyouMRr_ZBx8A7_nPBBPEXW44ag,39
5
5
  kumoai/databricks.py,sha256=ahwJz6DWLXMkndT0XwEDBxF-hoqhidFR8wBUQ4TLZ68,490
6
6
  kumoai/exceptions.py,sha256=7TMs0SC8xrU009_Pgd4QXtSF9lxJq8MtRbeX9pcQUy4,859
7
7
  kumoai/formatting.py,sha256=o3uCnLwXPhe1KI5WV9sBgRrcU7ed4rgu_pf89GL9Nc0,983
8
8
  kumoai/futures.py,sha256=J8rtZMEYFzdn5xF_x-LAiKJz3KGL6PT02f6rq_2bOJk,3836
9
9
  kumoai/jobs.py,sha256=dCi7BAdfm2tCnonYlGU4WJokJWbh3RzFfaOX2EYCIHU,2576
10
- kumoai/kumolib.cp310-win_amd64.pyd,sha256=GINcSpEYpFW8tP_vuwH5kWZSsWPYKnM83XHadL7hHGI,194048
10
+ kumoai/kumolib.cp310-win_amd64.pyd,sha256=lfpQDN2Fu1tGqTxas5A9Jv3fLm-WdA_oSkhQfo6-pvg,194048
11
11
  kumoai/mixin.py,sha256=IaiB8SAI0VqOoMVzzIaUlqMt53-QPUK6OB0HikG-V9E,840
12
- kumoai/spcs.py,sha256=SWvfkeJvb_7sGkjSqyMBIuPbMTWCP6v0BC9HBXM1uSI,4398
12
+ kumoai/spcs.py,sha256=KWfENrwSLruprlD-QPh63uU0N6npiNrwkeKfBk3EUyQ,4260
13
13
  kumoai/artifact_export/__init__.py,sha256=UXAQI5q92ChBzWAk8o3J6pElzYHudAzFZssQXd4o7i8,247
14
14
  kumoai/artifact_export/config.py,sha256=PRoUByzu5l-nyBKFR4vnRlq19b53ExGVy8YDCD7zMuI,8233
15
15
  kumoai/artifact_export/job.py,sha256=lOFIdPCrvhwdfvvDhQ2yzW8J4qIdYQoHZO1Rz3kJky4,3383
16
16
  kumoai/client/__init__.py,sha256=v0ISO1QD8JJhIJS6IzWz5-SL3EhtNCPeX3j1b2HBY0s,69
17
- kumoai/client/client.py,sha256=IoZ6WH-VIAdwpwmd5DhP4HqjQL_YpB5vaWjtaWrNECk,8801
17
+ kumoai/client/client.py,sha256=T6Kw7-XWuAy5Dh7XU5graBl1-cTARiobycwtgxzaSE8,8731
18
18
  kumoai/client/connector.py,sha256=CO2LG5aDpCLxWNYYFRXGZs1AhYH3dRcbqBEUGwHQGzQ,4030
19
19
  kumoai/client/endpoints.py,sha256=DpEKEQ1yvL15iHZadXZKO94t-qXrYLaeV1sknX4IuPg,5532
20
20
  kumoai/client/graph.py,sha256=6MFyPYxDPfGTWeAI_84RUgWx9rVvqbLnR0Ourtgj5rg,3951
21
21
  kumoai/client/jobs.py,sha256=Y8wKiTk1I5ywc-2cxR72LaBjfhPTCVOezSCTeDpTs8Q,17521
22
22
  kumoai/client/online.py,sha256=4s_8Sv8m_k_tty4CO7RuAt0e6BDMkGvsZZ3VX8zyDb8,2798
23
23
  kumoai/client/pquery.py,sha256=0pXgQLxjoaFWDif0XRAuC_P-X3OSnXNWsiVrXej9uMk,7094
24
- kumoai/client/rfm.py,sha256=XCLJsSBe82fErLchpuS4Zb7fA3LBY8QxxIhrbw4_NPQ,3678
24
+ kumoai/client/rfm.py,sha256=Gmt_dqoXekBCLiF0eQPgpoJ1cbnhnU8VbINF3U13qbQ,3838
25
25
  kumoai/client/source_table.py,sha256=mMHJtQ_yUHRI9LdHLVHxNGt83bbzmC1_d-NmXjbiTuI,2154
26
26
  kumoai/client/table.py,sha256=VhjLEMLQS1Z7zjcb2Yt3gZfiVqiD7b1gj-WNux_504A,3336
27
27
  kumoai/client/utils.py,sha256=RSD5Ia0lQQDR1drRFBJFdo2KVHfQqhJuk6m6du7Kl4E,3979
@@ -53,14 +53,15 @@ kumoai/connector/source_table.py,sha256=fnqwIKY6qYo4G0EsRzchb6FgZ-dQyU6aRaD9UAxs
53
53
  kumoai/connector/utils.py,sha256=SlkjPJS_wqfwFzIaQOHZtENQnbOz5sgLbvvvPDXE1ww,65786
54
54
  kumoai/encoder/__init__.py,sha256=8FeP6mUyCeXxr1b8kUIi5dxe5vEXQRft9tPoaV1CBqg,186
55
55
  kumoai/experimental/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
56
- kumoai/experimental/rfm/__init__.py,sha256=21qX8kSSQ6SJ62zGZSAhESFHVrm7UgKIMqjhZiniLxk,1706
56
+ kumoai/experimental/rfm/__init__.py,sha256=gpjpeN8PT3ZESi6kUaeyZqYnoJnysRVXDaY9hrycJA4,7020
57
57
  kumoai/experimental/rfm/authenticate.py,sha256=G89_4TMeUpr5fG_0VTzMF5sdNhaciitA1oc2loTlTmo,19321
58
58
  kumoai/experimental/rfm/local_graph.py,sha256=nZ9hDfyWg1dHFLoTEKoLt0ZJPvf9MUA1MNyfTRzJThg,30886
59
- kumoai/experimental/rfm/local_graph_sampler.py,sha256=ZCnILozG95EzpgMqhGTG2AF85JphLvAhj-3YPaTqoaQ,6922
59
+ kumoai/experimental/rfm/local_graph_sampler.py,sha256=3JNpktW__nwxVKZxP4cQBgsIin7J_LNXYS7YlV36xbU,6854
60
60
  kumoai/experimental/rfm/local_graph_store.py,sha256=eUuIMFcdIRqN1kRxnqOdJpKEt-S_oyupAyHr7YuQoSU,14206
61
- kumoai/experimental/rfm/local_pquery_driver.py,sha256=XHxRTMRVUzKNlTItkOmW_ClEQ1xgvvwIC6MBLt7qihA,26857
61
+ kumoai/experimental/rfm/local_pquery_driver.py,sha256=Yd_yHIrvuDj16IC1pvsqiQvZS41vvOOCRMiuDGtN6Fk,26851
62
62
  kumoai/experimental/rfm/local_table.py,sha256=5H08657TIyH7n_QnpFKr2g4BtVqdXTymmrfhSGaDmkU,20150
63
- kumoai/experimental/rfm/rfm.py,sha256=K9Fm6O3GWkoOCv9Bq8jSdnWvuMyPYk4lmU1WJIpLSPY,47815
63
+ kumoai/experimental/rfm/rfm.py,sha256=MarISSPKuv6nIaGG69zFAwIagF6EA37xcSRClZrQMFc,49470
64
+ kumoai/experimental/rfm/sagemaker.py,sha256=eebpZtASqiIGF2FpY53bbWLj6p-u5hkK4RLgBNAvEzg,4953
64
65
  kumoai/experimental/rfm/utils.py,sha256=dLx2wdyTWg7vZI_7R-I0z_lA-2aV5M8h9n3bnnLyylI,11467
65
66
  kumoai/experimental/rfm/infer/__init__.py,sha256=fPsdDr4D3hgC8snW0j3pAVpCyR-xrauuogMnTOMrfok,304
66
67
  kumoai/experimental/rfm/infer/categorical.py,sha256=bqmfrE5ZCBTcb35lA4SyAkCu3MgttAn29VBJYMBNhVg,893
@@ -90,9 +91,9 @@ kumoai/trainer/util.py,sha256=LCXkY5MNl6NbEVd2OZ0aVqF6fvr3KiCFh6pH0igAi_g,4165
90
91
  kumoai/utils/__init__.py,sha256=wAKgmwtMIGuiauW9D_GGKH95K-24Kgwmld27mm4nsro,278
91
92
  kumoai/utils/datasets.py,sha256=UyAII-oAn7x3ombuvpbSQ41aVF9SYKBjQthTD-vcT2A,3011
92
93
  kumoai/utils/forecasting.py,sha256=ZgKeUCbWLOot0giAkoigwU5du8LkrwAicFOi5hVn6wg,7624
93
- kumoai/utils/progress_logger.py,sha256=tzwFrUO5VuiArxx9_tSETno8JF5rnFOedX26I2yDW10,5046
94
- kumoai-2.12.0.dev202511071730.dist-info/licenses/LICENSE,sha256=ZUilBDp--4vbhsEr6f_Upw9rnIx09zQ3K9fXQ0rfd6w,1111
95
- kumoai-2.12.0.dev202511071730.dist-info/METADATA,sha256=iXxnyQJGKAUlzPdN8-dA9N4aIFXi6Y2vdzpsGTzl8w8,2112
96
- kumoai-2.12.0.dev202511071730.dist-info/WHEEL,sha256=KUuBC6lxAbHCKilKua8R9W_TM71_-9Sg5uEP3uDWcoU,101
97
- kumoai-2.12.0.dev202511071730.dist-info/top_level.txt,sha256=YjU6UcmomoDx30vEXLsOU784ED7VztQOsFApk1SFwvs,7
98
- kumoai-2.12.0.dev202511071730.dist-info/RECORD,,
94
+ kumoai/utils/progress_logger.py,sha256=MZsWgHd4UZQKCXiJZgQeW-Emi_BmzlCKPLPXOL_HqBo,5239
95
+ kumoai-2.13.0.dev202511261731.dist-info/licenses/LICENSE,sha256=ZUilBDp--4vbhsEr6f_Upw9rnIx09zQ3K9fXQ0rfd6w,1111
96
+ kumoai-2.13.0.dev202511261731.dist-info/METADATA,sha256=KEPt_QdWVLyZhYMj3PNjwvb1gm6fXI9_FvWyMOvMrtw,2544
97
+ kumoai-2.13.0.dev202511261731.dist-info/WHEEL,sha256=KUuBC6lxAbHCKilKua8R9W_TM71_-9Sg5uEP3uDWcoU,101
98
+ kumoai-2.13.0.dev202511261731.dist-info/top_level.txt,sha256=YjU6UcmomoDx30vEXLsOU784ED7VztQOsFApk1SFwvs,7
99
+ kumoai-2.13.0.dev202511261731.dist-info/RECORD,,