maitai-python 1.1.28b0__tar.gz → 1.1.29b0__tar.gz

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.
Files changed (51) hide show
  1. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/PKG-INFO +1 -1
  2. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai/_config.py +90 -33
  3. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_common/version.py +1 -1
  4. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_python.egg-info/PKG-INFO +1 -1
  5. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/README.md +0 -0
  6. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai/__init__.py +0 -0
  7. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai/_azure.py +0 -0
  8. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai/_config_listener_thread.py +0 -0
  9. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai/_context.py +0 -0
  10. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai/_evaluator.py +0 -0
  11. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai/_inference.py +0 -0
  12. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai/_maitai.py +0 -0
  13. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai/_maitai_async.py +0 -0
  14. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai/_maitai_client.py +0 -0
  15. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai/_types.py +0 -0
  16. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai/_utils.py +0 -0
  17. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai/tools/__init__.py +0 -0
  18. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_common/__init__.py +0 -0
  19. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_common/config/__init__.py +0 -0
  20. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_common/config/config_service.py +0 -0
  21. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_common/processes/__init__.py +0 -0
  22. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_common/processes/io_thread.py +0 -0
  23. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_common/processes/websocket_listener_thread.py +0 -0
  24. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_common/utils/__init__.py +0 -0
  25. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_common/utils/proto_utils.py +0 -0
  26. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_common/utils/types.py +0 -0
  27. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/__init__.py +0 -0
  28. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/annotation/__init__.py +0 -0
  29. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/application/__init__.py +0 -0
  30. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/backtest/__init__.py +0 -0
  31. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/billing/__init__.py +0 -0
  32. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/chat/__init__.py +0 -0
  33. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/company/__init__.py +0 -0
  34. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/config/__init__.py +0 -0
  35. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/embedding/__init__.py +0 -0
  36. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/event/__init__.py +0 -0
  37. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/inference/__init__.py +0 -0
  38. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/key/__init__.py +0 -0
  39. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/metric/__init__.py +0 -0
  40. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/model/__init__.py +0 -0
  41. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/notification/__init__.py +0 -0
  42. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/rates/__init__.py +0 -0
  43. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/sandbox/__init__.py +0 -0
  44. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/sentinel/__init__.py +0 -0
  45. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_gen/session/__init__.py +0 -0
  46. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_python.egg-info/SOURCES.txt +0 -0
  47. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_python.egg-info/dependency_links.txt +0 -0
  48. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_python.egg-info/requires.txt +0 -0
  49. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/maitai_python.egg-info/top_level.txt +0 -0
  50. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/setup.cfg +0 -0
  51. {maitai_python-1.1.28b0 → maitai_python-1.1.29b0}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: maitai-python
3
- Version: 1.1.28b0
3
+ Version: 1.1.29b0
4
4
  Summary: Maitai SDK for Python
5
5
  Home-page: https://docs.trymaitai.ai
6
6
  Author: Maitai
@@ -21,19 +21,20 @@ def _get_aws_instance_metadata(url, timeout=2):
21
21
  token_url = "http://169.254.169.254/latest/api/token"
22
22
  token_headers = {"X-aws-ec2-metadata-token-ttl-seconds": "21600"}
23
23
  token_response = requests.put(token_url, headers=token_headers, timeout=timeout)
24
-
24
+
25
25
  if token_response.status_code == 200:
26
26
  token = token_response.text
27
27
  headers = {"X-aws-ec2-metadata-token": token}
28
28
  else:
29
29
  headers = None
30
-
30
+
31
31
  response = requests.get(url, headers=headers, timeout=timeout)
32
32
  response.raise_for_status()
33
33
  return response.text
34
34
  except requests.RequestException:
35
35
  return None
36
36
 
37
+
37
38
  def _get_gcp_instance_metadata(url, timeout=2):
38
39
  try:
39
40
  headers = {"Metadata-Flavor": "Google"}
@@ -43,32 +44,40 @@ def _get_gcp_instance_metadata(url, timeout=2):
43
44
  except requests.RequestException:
44
45
  return None
45
46
 
47
+
46
48
  def _determine_maitai_host():
47
49
  if maitai_host := os.environ.get("MAITAI_HOST"):
48
- return maitai_host.rstrip('/')
49
-
50
- if _get_aws_instance_metadata("http://169.254.169.254/latest/meta-data/placement/region") == "us-west-2":
50
+ return maitai_host.rstrip("/")
51
+
52
+ if (
53
+ _get_aws_instance_metadata(
54
+ "http://169.254.169.254/latest/meta-data/placement/region"
55
+ )
56
+ == "us-west-2"
57
+ ):
51
58
  return "https://api.aws.us-west-2.trymaitai.ai"
52
-
53
- gcp_zone = _get_gcp_instance_metadata("http://metadata.google.internal/computeMetadata/v1/instance/zone")
59
+
60
+ gcp_zone = _get_gcp_instance_metadata(
61
+ "http://metadata.google.internal/computeMetadata/v1/instance/zone"
62
+ )
54
63
  if gcp_zone:
55
64
  if "us-west1" in gcp_zone:
56
65
  return "https://api.gcp.us-west1.trymaitai.ai"
57
66
  elif "us-central1" in gcp_zone:
58
67
  return "https://api.gcp.us-central1.trymaitai.ai"
59
-
68
+
60
69
  return "https://api.trymaitai.ai"
61
70
 
71
+
62
72
  class Config:
63
73
  maitai_host = _determine_maitai_host()
64
- maitai_ws = os.environ.get("MAITAI_WS", 'wss://09hidyy627.execute-api.us-west-2.amazonaws.com/production')
74
+ maitai_ws = os.environ.get(
75
+ "MAITAI_WS", "wss://09hidyy627.execute-api.us-west-2.amazonaws.com/production"
76
+ )
65
77
  config_dir = os.path.expanduser(os.environ.get("MAITAI_CONFIG_DIR", "~/.maitai"))
66
78
 
67
79
  def __init__(self):
68
- retry_strategy = Retry(
69
- total=3,
70
- backoff_factor=1
71
- )
80
+ retry_strategy = Retry(total=3, backoff_factor=1)
72
81
  adapter = HTTPAdapter(max_retries=retry_strategy)
73
82
 
74
83
  self._session = requests.Session()
@@ -87,16 +96,22 @@ class Config:
87
96
  groq_api_key=Key(id=-1, key_value=os.environ.get("GROQ_API_KEY")),
88
97
  anthropic_api_key=Key(id=-1, key_value=os.environ.get("ANTHROPIC_API_KEY")),
89
98
  )
99
+ self.refresh_timer = None
100
+ self.refresh_interval = 300 # 5 minutes in seconds
90
101
 
91
102
  @property
92
103
  def api_key(self):
93
104
  if self._api_key is None:
94
105
  if self.initialized:
95
- raise ValueError("Maitai API Key has not been set. Either pass it directly into the client, or by setting the environment variable MAITAI_API_KEY.")
106
+ raise ValueError(
107
+ "Maitai API Key has not been set. Either pass it directly into the client, or by setting the environment variable MAITAI_API_KEY."
108
+ )
96
109
  else:
97
110
  api_key = os.environ.get("MAITAI_API_KEY")
98
111
  if not api_key:
99
- raise ValueError("Maitai API Key has not been set. Either pass it directly into the client, or by setting the environment variable MAITAI_API_KEY.")
112
+ raise ValueError(
113
+ "Maitai API Key has not been set. Either pass it directly into the client, or by setting the environment variable MAITAI_API_KEY."
114
+ )
100
115
  self.initialize(api_key=api_key)
101
116
  return self._api_key
102
117
 
@@ -113,6 +128,7 @@ class Config:
113
128
  self._initialize_company()
114
129
  self.refresh_applications()
115
130
  self._initialize_websocket()
131
+ self._start_refresh_timer()
116
132
  except Exception as e:
117
133
  try:
118
134
  self._load_config_from_file()
@@ -120,23 +136,46 @@ class Config:
120
136
  raise e
121
137
  self.initialized = False
122
138
  if retry < 5:
123
- t = threading.Timer(interval=2 ** retry, function=self.initialize, args=(api_key, retry))
139
+ t = threading.Timer(
140
+ interval=2**retry, function=self.initialize, args=(api_key, retry)
141
+ )
124
142
  t.daemon = True
125
143
  t.start()
126
144
 
145
+ def _start_refresh_timer(self):
146
+ if self.refresh_timer:
147
+ self.refresh_timer.cancel()
148
+ self.refresh_timer = threading.Timer(
149
+ self.refresh_interval, self._refresh_and_reschedule
150
+ )
151
+ self.refresh_timer.daemon = True
152
+ self.refresh_timer.start()
153
+
154
+ def _refresh_and_reschedule(self):
155
+ try:
156
+ self.refresh_applications()
157
+ except Exception as e:
158
+ pass
159
+ finally:
160
+ self._start_refresh_timer()
161
+
127
162
  def get_application(self, application_ref_name: str) -> Application:
128
163
  return self._applications.get(application_ref_name)
129
164
 
130
- def get_application_action_config(self, application_ref_name: str, action_type: str) -> ActionConfig:
131
- return self._application_action_configs.get(application_ref_name, {}).get(action_type, config_service.get_default_config())
165
+ def get_application_action_config(
166
+ self, application_ref_name: str, action_type: str
167
+ ) -> ActionConfig:
168
+ return self._application_action_configs.get(application_ref_name, {}).get(
169
+ action_type, config_service.get_default_config()
170
+ )
132
171
 
133
172
  def _initialize_company(self):
134
173
  host = self.maitai_host
135
- url = f'{host}/company/'
174
+ url = f"{host}/company/"
136
175
  headers = {
137
- 'Content-Type': 'application/json',
138
- 'x-api-key': self.api_key,
139
- 'x-client-version': version,
176
+ "Content-Type": "application/json",
177
+ "x-api-key": self.api_key,
178
+ "x-client-version": version,
140
179
  }
141
180
 
142
181
  response = self._session.get(url, headers=headers, timeout=15)
@@ -148,27 +187,36 @@ class Config:
148
187
 
149
188
  def refresh_applications(self):
150
189
  host = self.maitai_host
151
- url = f'{host}/application/'
190
+ url = f"{host}/application/"
152
191
  headers = {
153
- 'Content-Type': 'application/json',
154
- 'x-api-key': self.api_key,
155
- 'x-client-version': version,
192
+ "Content-Type": "application/json",
193
+ "x-api-key": self.api_key,
194
+ "x-client-version": version,
156
195
  }
157
196
 
158
197
  response = self._session.get(url, headers=headers, timeout=15)
159
198
  self._session.close()
160
199
  if response.status_code != 200:
161
200
  raise Exception(f"Failed to initialize Maitai client: {response.text}")
162
- applications = [Application().from_dict(app_json) for app_json in response.json()]
201
+ applications = [
202
+ Application().from_dict(app_json) for app_json in response.json()
203
+ ]
163
204
  return self.store_application_metadata(applications)
164
205
 
165
206
  def store_application_metadata(self, applications: List[Application]):
166
207
  for application in applications:
167
208
  self._applications[application.application_ref_name] = application
168
209
  for action_type in application.action_types:
169
- if application.application_ref_name not in self._application_action_configs:
170
- self._application_action_configs[application.application_ref_name] = {}
171
- self._application_action_configs[application.application_ref_name][action_type.action_type] = action_type.meta
210
+ if (
211
+ application.application_ref_name
212
+ not in self._application_action_configs
213
+ ):
214
+ self._application_action_configs[
215
+ application.application_ref_name
216
+ ] = {}
217
+ self._application_action_configs[application.application_ref_name][
218
+ action_type.action_type
219
+ ] = action_type.meta
172
220
  self._dump_application_metadata()
173
221
 
174
222
  def _dump_application_metadata(self):
@@ -179,7 +227,9 @@ class Config:
179
227
  for ref_name, intent_map in self._application_action_configs.items():
180
228
  config_object[ref_name] = {}
181
229
  for intent_name, intent_config in intent_map.items():
182
- config_object[ref_name][intent_name] = intent_config.to_pydict(casing=Casing.SNAKE, include_default_values=False)
230
+ config_object[ref_name][intent_name] = intent_config.to_pydict(
231
+ casing=Casing.SNAKE, include_default_values=False
232
+ )
183
233
  f.write(json.dumps(config_object, indent=2))
184
234
 
185
235
  def _load_config_from_file(self):
@@ -190,10 +240,14 @@ class Config:
190
240
  if ref_name not in self._application_action_configs:
191
241
  self._application_action_configs[ref_name] = {}
192
242
  for intent_name, intent_config in intent_map.items():
193
- self._application_action_configs[ref_name][intent_name] = ActionConfig().from_pydict(intent_config)
243
+ self._application_action_configs[ref_name][
244
+ intent_name
245
+ ] = ActionConfig().from_pydict(intent_config)
194
246
 
195
247
  def _initialize_websocket(self):
196
- self.config_listener_thread = ConfigListenerThread(self, self.maitai_ws, "APPLICATION_CONFIG_CHANGE", self._company_id)
248
+ self.config_listener_thread = ConfigListenerThread(
249
+ self, self.maitai_ws, "APPLICATION_CONFIG_CHANGE", self._company_id
250
+ )
197
251
  self.config_listener_thread.daemon = True
198
252
  self.config_listener_thread.start()
199
253
 
@@ -201,6 +255,9 @@ class Config:
201
255
  if self.config_listener_thread:
202
256
  self.config_listener_thread.terminate()
203
257
  self.config_listener_thread = None
258
+ if self.refresh_timer:
259
+ self.refresh_timer.cancel()
260
+ self.refresh_timer = None
204
261
 
205
262
 
206
263
  config = Config()
@@ -1,3 +1,3 @@
1
- version = "1.1.28b0"
1
+ version = "1.1.29b0"
2
2
  if __name__ == "__main__":
3
3
  print(version)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: maitai-python
3
- Version: 1.1.28b0
3
+ Version: 1.1.29b0
4
4
  Summary: Maitai SDK for Python
5
5
  Home-page: https://docs.trymaitai.ai
6
6
  Author: Maitai