kumoai 2.12.0.dev202511021731__cp310-cp310-macosx_11_0_arm64.whl → 2.13.0.dev202511191731__cp310-cp310-macosx_11_0_arm64.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.dev202511021731'
1
+ __version__ = '2.13.0.dev202511191731'
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()
@@ -147,3 +147,4 @@ class RFMEndpoints:
147
147
  explain = Endpoint(f"{BASE}/explain", HTTPMethod.POST)
148
148
  evaluate = Endpoint(f"{BASE}/evaluate", HTTPMethod.POST)
149
149
  validate_query = Endpoint(f"{BASE}/validate_query", HTTPMethod.POST)
150
+ parse_query = Endpoint(f"{BASE}/parse_query", HTTPMethod.POST)
kumoai/client/rfm.py CHANGED
@@ -1,7 +1,11 @@
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,
4
6
  RFMExplanationResponse,
7
+ RFMParseQueryRequest,
8
+ RFMParseQueryResponse,
5
9
  RFMPredictResponse,
6
10
  RFMValidateQueryRequest,
7
11
  RFMValidateQueryResponse,
@@ -26,25 +30,32 @@ class RFMAPI:
26
30
  Returns:
27
31
  RFMPredictResponse containing the predictions
28
32
  """
29
- # Send binary data to the predict endpoint
30
33
  response = self._client._request(
31
- RFMEndpoints.predict, data=request,
32
- headers={'Content-Type': 'application/x-protobuf'})
34
+ RFMEndpoints.predict,
35
+ data=request,
36
+ headers={'Content-Type': 'application/x-protobuf'},
37
+ )
33
38
  raise_on_error(response)
34
39
  return parse_response(RFMPredictResponse, response)
35
40
 
36
- def explain(self, request: bytes) -> RFMExplanationResponse:
41
+ def explain(
42
+ self,
43
+ request: bytes,
44
+ skip_summary: bool = False,
45
+ ) -> RFMExplanationResponse:
37
46
  """Explain the RFM model on the given context.
38
47
 
39
48
  Args:
40
49
  request: The predict request as serialized protobuf.
50
+ skip_summary: Whether to skip generating a human-readable summary
51
+ of the explanation.
41
52
 
42
53
  Returns:
43
54
  RFMPredictResponse containing the explanations
44
55
  """
45
- # Send binary data to the explain endpoint
56
+ params: dict[str, Any] = {'generate_summary': not skip_summary}
46
57
  response = self._client._request(
47
- RFMEndpoints.explain, data=request,
58
+ RFMEndpoints.explain, data=request, params=params,
48
59
  headers={'Content-Type': 'application/x-protobuf'})
49
60
  raise_on_error(response)
50
61
  return parse_response(RFMExplanationResponse, response)
@@ -58,7 +69,6 @@ class RFMAPI:
58
69
  Returns:
59
70
  RFMEvaluateResponse containing the computed metrics
60
71
  """
61
- # Send binary data to the evaluate endpoint
62
72
  response = self._client._request(
63
73
  RFMEndpoints.evaluate, data=request,
64
74
  headers={'Content-Type': 'application/x-protobuf'})
@@ -82,3 +92,21 @@ class RFMAPI:
82
92
  json=to_json_dict(request))
83
93
  raise_on_error(response)
84
94
  return parse_response(RFMValidateQueryResponse, response)
95
+
96
+ def parse_query(
97
+ self,
98
+ request: RFMParseQueryRequest,
99
+ ) -> RFMParseQueryResponse:
100
+ """Validate a predictive query against a graph.
101
+
102
+ Args:
103
+ request: The request object containing
104
+ the query and graph definition
105
+
106
+ Returns:
107
+ RFMParseQueryResponse containing the QueryDefinition
108
+ """
109
+ response = self._client._request(RFMEndpoints.parse_query,
110
+ json=to_json_dict(request))
111
+ raise_on_error(response)
112
+ return parse_response(RFMParseQueryResponse, response)
@@ -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: