synth-ai 0.2.4.dev6__py3-none-any.whl → 0.2.4.dev8__py3-none-any.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.
Files changed (256) hide show
  1. synth_ai/__init__.py +18 -9
  2. synth_ai/cli/__init__.py +10 -5
  3. synth_ai/cli/balance.py +25 -32
  4. synth_ai/cli/calc.py +2 -3
  5. synth_ai/cli/demo.py +3 -5
  6. synth_ai/cli/legacy_root_backup.py +58 -32
  7. synth_ai/cli/man.py +22 -19
  8. synth_ai/cli/recent.py +9 -8
  9. synth_ai/cli/root.py +58 -13
  10. synth_ai/cli/status.py +13 -6
  11. synth_ai/cli/traces.py +45 -21
  12. synth_ai/cli/watch.py +40 -37
  13. synth_ai/config/base_url.py +47 -2
  14. synth_ai/core/experiment.py +1 -2
  15. synth_ai/environments/__init__.py +2 -6
  16. synth_ai/environments/environment/artifacts/base.py +3 -1
  17. synth_ai/environments/environment/db/sqlite.py +1 -1
  18. synth_ai/environments/environment/registry.py +19 -20
  19. synth_ai/environments/environment/resources/sqlite.py +2 -3
  20. synth_ai/environments/environment/rewards/core.py +3 -2
  21. synth_ai/environments/environment/tools/__init__.py +6 -4
  22. synth_ai/environments/examples/crafter_classic/__init__.py +1 -1
  23. synth_ai/environments/examples/crafter_classic/engine.py +13 -13
  24. synth_ai/environments/examples/crafter_classic/engine_deterministic_patch.py +1 -0
  25. synth_ai/environments/examples/crafter_classic/engine_helpers/action_map.py +2 -1
  26. synth_ai/environments/examples/crafter_classic/engine_helpers/serialization.py +2 -1
  27. synth_ai/environments/examples/crafter_classic/engine_serialization_patch_v3.py +3 -2
  28. synth_ai/environments/examples/crafter_classic/environment.py +16 -15
  29. synth_ai/environments/examples/crafter_classic/taskset.py +2 -2
  30. synth_ai/environments/examples/crafter_classic/trace_hooks_v3.py +2 -3
  31. synth_ai/environments/examples/crafter_classic/world_config_patch_simple.py +2 -1
  32. synth_ai/environments/examples/crafter_custom/crafter/__init__.py +2 -2
  33. synth_ai/environments/examples/crafter_custom/crafter/config.py +2 -2
  34. synth_ai/environments/examples/crafter_custom/crafter/env.py +1 -5
  35. synth_ai/environments/examples/crafter_custom/crafter/objects.py +1 -2
  36. synth_ai/environments/examples/crafter_custom/crafter/worldgen.py +1 -2
  37. synth_ai/environments/examples/crafter_custom/dataset_builder.py +5 -5
  38. synth_ai/environments/examples/crafter_custom/environment.py +13 -13
  39. synth_ai/environments/examples/crafter_custom/run_dataset.py +5 -5
  40. synth_ai/environments/examples/enron/art_helpers/email_search_tools.py +2 -2
  41. synth_ai/environments/examples/enron/art_helpers/local_email_db.py +5 -4
  42. synth_ai/environments/examples/enron/art_helpers/types_enron.py +2 -1
  43. synth_ai/environments/examples/enron/engine.py +18 -14
  44. synth_ai/environments/examples/enron/environment.py +12 -11
  45. synth_ai/environments/examples/enron/taskset.py +7 -7
  46. synth_ai/environments/examples/minigrid/__init__.py +6 -6
  47. synth_ai/environments/examples/minigrid/engine.py +6 -6
  48. synth_ai/environments/examples/minigrid/environment.py +6 -6
  49. synth_ai/environments/examples/minigrid/puzzle_loader.py +3 -2
  50. synth_ai/environments/examples/minigrid/taskset.py +13 -13
  51. synth_ai/environments/examples/nethack/achievements.py +1 -1
  52. synth_ai/environments/examples/nethack/engine.py +8 -7
  53. synth_ai/environments/examples/nethack/environment.py +10 -9
  54. synth_ai/environments/examples/nethack/helpers/__init__.py +8 -9
  55. synth_ai/environments/examples/nethack/helpers/action_mapping.py +1 -1
  56. synth_ai/environments/examples/nethack/helpers/nle_wrapper.py +2 -1
  57. synth_ai/environments/examples/nethack/helpers/observation_utils.py +1 -1
  58. synth_ai/environments/examples/nethack/helpers/recording_wrapper.py +3 -4
  59. synth_ai/environments/examples/nethack/helpers/trajectory_recorder.py +6 -5
  60. synth_ai/environments/examples/nethack/helpers/visualization/replay_viewer.py +5 -5
  61. synth_ai/environments/examples/nethack/helpers/visualization/visualizer.py +7 -6
  62. synth_ai/environments/examples/nethack/taskset.py +5 -5
  63. synth_ai/environments/examples/red/engine.py +9 -8
  64. synth_ai/environments/examples/red/engine_helpers/reward_components.py +2 -1
  65. synth_ai/environments/examples/red/engine_helpers/reward_library/__init__.py +7 -7
  66. synth_ai/environments/examples/red/engine_helpers/reward_library/adaptive_rewards.py +2 -1
  67. synth_ai/environments/examples/red/engine_helpers/reward_library/battle_rewards.py +2 -1
  68. synth_ai/environments/examples/red/engine_helpers/reward_library/composite_rewards.py +2 -1
  69. synth_ai/environments/examples/red/engine_helpers/reward_library/economy_rewards.py +2 -1
  70. synth_ai/environments/examples/red/engine_helpers/reward_library/efficiency_rewards.py +2 -1
  71. synth_ai/environments/examples/red/engine_helpers/reward_library/exploration_rewards.py +2 -1
  72. synth_ai/environments/examples/red/engine_helpers/reward_library/novelty_rewards.py +2 -1
  73. synth_ai/environments/examples/red/engine_helpers/reward_library/pallet_town_rewards.py +2 -1
  74. synth_ai/environments/examples/red/engine_helpers/reward_library/pokemon_rewards.py +2 -1
  75. synth_ai/environments/examples/red/engine_helpers/reward_library/social_rewards.py +2 -1
  76. synth_ai/environments/examples/red/engine_helpers/reward_library/story_rewards.py +2 -1
  77. synth_ai/environments/examples/red/engine_helpers/screen_analysis.py +3 -2
  78. synth_ai/environments/examples/red/engine_helpers/state_extraction.py +2 -1
  79. synth_ai/environments/examples/red/environment.py +18 -15
  80. synth_ai/environments/examples/red/taskset.py +5 -3
  81. synth_ai/environments/examples/sokoban/engine.py +16 -13
  82. synth_ai/environments/examples/sokoban/engine_helpers/room_utils.py +3 -2
  83. synth_ai/environments/examples/sokoban/engine_helpers/vendored/__init__.py +2 -1
  84. synth_ai/environments/examples/sokoban/engine_helpers/vendored/envs/__init__.py +1 -1
  85. synth_ai/environments/examples/sokoban/engine_helpers/vendored/envs/boxoban_env.py +7 -5
  86. synth_ai/environments/examples/sokoban/engine_helpers/vendored/envs/render_utils.py +1 -1
  87. synth_ai/environments/examples/sokoban/engine_helpers/vendored/envs/room_utils.py +2 -1
  88. synth_ai/environments/examples/sokoban/engine_helpers/vendored/envs/sokoban_env.py +5 -4
  89. synth_ai/environments/examples/sokoban/engine_helpers/vendored/envs/sokoban_env_fixed_targets.py +3 -2
  90. synth_ai/environments/examples/sokoban/engine_helpers/vendored/envs/sokoban_env_pull.py +2 -1
  91. synth_ai/environments/examples/sokoban/engine_helpers/vendored/envs/sokoban_env_two_player.py +5 -4
  92. synth_ai/environments/examples/sokoban/engine_helpers/vendored/envs/sokoban_env_variations.py +1 -1
  93. synth_ai/environments/examples/sokoban/environment.py +15 -14
  94. synth_ai/environments/examples/sokoban/generate_verified_puzzles.py +5 -3
  95. synth_ai/environments/examples/sokoban/puzzle_loader.py +3 -2
  96. synth_ai/environments/examples/sokoban/taskset.py +13 -10
  97. synth_ai/environments/examples/tictactoe/engine.py +6 -6
  98. synth_ai/environments/examples/tictactoe/environment.py +8 -7
  99. synth_ai/environments/examples/tictactoe/taskset.py +6 -5
  100. synth_ai/environments/examples/verilog/engine.py +4 -3
  101. synth_ai/environments/examples/verilog/environment.py +11 -10
  102. synth_ai/environments/examples/verilog/taskset.py +14 -12
  103. synth_ai/environments/examples/wordle/__init__.py +5 -5
  104. synth_ai/environments/examples/wordle/engine.py +32 -25
  105. synth_ai/environments/examples/wordle/environment.py +21 -16
  106. synth_ai/environments/examples/wordle/helpers/generate_instances_wordfreq.py +6 -6
  107. synth_ai/environments/examples/wordle/taskset.py +20 -12
  108. synth_ai/environments/reproducibility/core.py +1 -1
  109. synth_ai/environments/reproducibility/tree.py +21 -21
  110. synth_ai/environments/service/app.py +3 -2
  111. synth_ai/environments/service/core_routes.py +104 -110
  112. synth_ai/environments/service/external_registry.py +1 -2
  113. synth_ai/environments/service/registry.py +1 -1
  114. synth_ai/environments/stateful/core.py +1 -2
  115. synth_ai/environments/stateful/engine.py +1 -1
  116. synth_ai/environments/tasks/api.py +4 -4
  117. synth_ai/environments/tasks/core.py +14 -12
  118. synth_ai/environments/tasks/filters.py +6 -4
  119. synth_ai/environments/tasks/utils.py +13 -11
  120. synth_ai/evals/base.py +2 -3
  121. synth_ai/experimental/synth_oss.py +4 -4
  122. synth_ai/http.py +102 -0
  123. synth_ai/inference/__init__.py +7 -0
  124. synth_ai/inference/client.py +20 -0
  125. synth_ai/jobs/client.py +246 -0
  126. synth_ai/learning/__init__.py +24 -0
  127. synth_ai/learning/client.py +149 -0
  128. synth_ai/learning/config.py +43 -0
  129. synth_ai/learning/constants.py +29 -0
  130. synth_ai/learning/ft_client.py +59 -0
  131. synth_ai/learning/gateway.py +1 -3
  132. synth_ai/learning/health.py +43 -0
  133. synth_ai/learning/jobs.py +205 -0
  134. synth_ai/learning/prompts/banking77_injection_eval.py +15 -10
  135. synth_ai/learning/prompts/hello_world_in_context_injection_ex.py +26 -14
  136. synth_ai/learning/prompts/mipro.py +61 -52
  137. synth_ai/learning/prompts/random_search.py +42 -43
  138. synth_ai/learning/prompts/run_mipro_banking77.py +32 -20
  139. synth_ai/learning/prompts/run_random_search_banking77.py +71 -52
  140. synth_ai/learning/rl_client.py +256 -0
  141. synth_ai/learning/sse.py +58 -0
  142. synth_ai/learning/validators.py +48 -0
  143. synth_ai/lm/__init__.py +5 -5
  144. synth_ai/lm/caching/ephemeral.py +9 -9
  145. synth_ai/lm/caching/handler.py +20 -20
  146. synth_ai/lm/caching/persistent.py +10 -10
  147. synth_ai/lm/config.py +3 -3
  148. synth_ai/lm/constants.py +7 -7
  149. synth_ai/lm/core/all.py +17 -3
  150. synth_ai/lm/core/exceptions.py +0 -2
  151. synth_ai/lm/core/main.py +26 -41
  152. synth_ai/lm/core/main_v3.py +33 -10
  153. synth_ai/lm/core/synth_models.py +48 -0
  154. synth_ai/lm/core/vendor_clients.py +26 -22
  155. synth_ai/lm/injection.py +7 -8
  156. synth_ai/lm/overrides.py +21 -19
  157. synth_ai/lm/provider_support/__init__.py +1 -1
  158. synth_ai/lm/provider_support/anthropic.py +15 -15
  159. synth_ai/lm/provider_support/openai.py +23 -21
  160. synth_ai/lm/structured_outputs/handler.py +34 -32
  161. synth_ai/lm/structured_outputs/inject.py +24 -27
  162. synth_ai/lm/structured_outputs/rehabilitate.py +19 -15
  163. synth_ai/lm/tools/base.py +17 -16
  164. synth_ai/lm/unified_interface.py +17 -18
  165. synth_ai/lm/vendors/base.py +20 -18
  166. synth_ai/lm/vendors/core/anthropic_api.py +36 -27
  167. synth_ai/lm/vendors/core/gemini_api.py +31 -36
  168. synth_ai/lm/vendors/core/mistral_api.py +19 -19
  169. synth_ai/lm/vendors/core/openai_api.py +42 -13
  170. synth_ai/lm/vendors/openai_standard.py +158 -101
  171. synth_ai/lm/vendors/openai_standard_responses.py +74 -61
  172. synth_ai/lm/vendors/retries.py +9 -1
  173. synth_ai/lm/vendors/supported/custom_endpoint.py +38 -28
  174. synth_ai/lm/vendors/supported/deepseek.py +10 -10
  175. synth_ai/lm/vendors/supported/grok.py +8 -8
  176. synth_ai/lm/vendors/supported/ollama.py +2 -1
  177. synth_ai/lm/vendors/supported/openrouter.py +11 -9
  178. synth_ai/lm/vendors/synth_client.py +425 -75
  179. synth_ai/lm/warmup.py +8 -7
  180. synth_ai/rl/__init__.py +30 -0
  181. synth_ai/rl/contracts.py +32 -0
  182. synth_ai/rl/env_keys.py +137 -0
  183. synth_ai/rl/secrets.py +19 -0
  184. synth_ai/scripts/verify_rewards.py +100 -0
  185. synth_ai/task/__init__.py +10 -0
  186. synth_ai/task/contracts.py +120 -0
  187. synth_ai/task/health.py +28 -0
  188. synth_ai/task/validators.py +12 -0
  189. synth_ai/tracing/__init__.py +22 -10
  190. synth_ai/tracing_v1/__init__.py +22 -20
  191. synth_ai/tracing_v3/__init__.py +7 -7
  192. synth_ai/tracing_v3/abstractions.py +56 -52
  193. synth_ai/tracing_v3/config.py +4 -2
  194. synth_ai/tracing_v3/db_config.py +6 -8
  195. synth_ai/tracing_v3/decorators.py +29 -30
  196. synth_ai/tracing_v3/examples/basic_usage.py +12 -12
  197. synth_ai/tracing_v3/hooks.py +24 -22
  198. synth_ai/tracing_v3/llm_call_record_helpers.py +85 -98
  199. synth_ai/tracing_v3/lm_call_record_abstractions.py +2 -4
  200. synth_ai/tracing_v3/migration_helper.py +3 -5
  201. synth_ai/tracing_v3/replica_sync.py +30 -32
  202. synth_ai/tracing_v3/session_tracer.py +158 -31
  203. synth_ai/tracing_v3/storage/__init__.py +1 -1
  204. synth_ai/tracing_v3/storage/base.py +8 -7
  205. synth_ai/tracing_v3/storage/config.py +4 -4
  206. synth_ai/tracing_v3/storage/factory.py +4 -4
  207. synth_ai/tracing_v3/storage/utils.py +9 -9
  208. synth_ai/tracing_v3/turso/__init__.py +3 -3
  209. synth_ai/tracing_v3/turso/daemon.py +9 -9
  210. synth_ai/tracing_v3/turso/manager.py +278 -48
  211. synth_ai/tracing_v3/turso/models.py +77 -19
  212. synth_ai/tracing_v3/utils.py +5 -5
  213. synth_ai/v0/tracing/abstractions.py +28 -28
  214. synth_ai/v0/tracing/base_client.py +9 -9
  215. synth_ai/v0/tracing/client_manager.py +7 -7
  216. synth_ai/v0/tracing/config.py +7 -7
  217. synth_ai/v0/tracing/context.py +6 -6
  218. synth_ai/v0/tracing/decorators.py +6 -5
  219. synth_ai/v0/tracing/events/manage.py +1 -1
  220. synth_ai/v0/tracing/events/store.py +5 -4
  221. synth_ai/v0/tracing/immediate_client.py +4 -5
  222. synth_ai/v0/tracing/local.py +3 -3
  223. synth_ai/v0/tracing/log_client_base.py +4 -5
  224. synth_ai/v0/tracing/retry_queue.py +5 -6
  225. synth_ai/v0/tracing/trackers.py +25 -25
  226. synth_ai/v0/tracing/upload.py +6 -0
  227. synth_ai/v0/tracing_v1/__init__.py +1 -1
  228. synth_ai/v0/tracing_v1/abstractions.py +28 -28
  229. synth_ai/v0/tracing_v1/base_client.py +9 -9
  230. synth_ai/v0/tracing_v1/client_manager.py +7 -7
  231. synth_ai/v0/tracing_v1/config.py +7 -7
  232. synth_ai/v0/tracing_v1/context.py +6 -6
  233. synth_ai/v0/tracing_v1/decorators.py +7 -6
  234. synth_ai/v0/tracing_v1/events/manage.py +1 -1
  235. synth_ai/v0/tracing_v1/events/store.py +5 -4
  236. synth_ai/v0/tracing_v1/immediate_client.py +4 -5
  237. synth_ai/v0/tracing_v1/local.py +3 -3
  238. synth_ai/v0/tracing_v1/log_client_base.py +4 -5
  239. synth_ai/v0/tracing_v1/retry_queue.py +5 -6
  240. synth_ai/v0/tracing_v1/trackers.py +25 -25
  241. synth_ai/v0/tracing_v1/upload.py +25 -24
  242. synth_ai/zyk/__init__.py +1 -0
  243. synth_ai-0.2.4.dev8.dist-info/METADATA +635 -0
  244. synth_ai-0.2.4.dev8.dist-info/RECORD +317 -0
  245. synth_ai/tui/__init__.py +0 -1
  246. synth_ai/tui/__main__.py +0 -13
  247. synth_ai/tui/cli/__init__.py +0 -1
  248. synth_ai/tui/cli/query_experiments.py +0 -165
  249. synth_ai/tui/cli/query_experiments_v3.py +0 -165
  250. synth_ai/tui/dashboard.py +0 -329
  251. synth_ai-0.2.4.dev6.dist-info/METADATA +0 -203
  252. synth_ai-0.2.4.dev6.dist-info/RECORD +0 -299
  253. {synth_ai-0.2.4.dev6.dist-info → synth_ai-0.2.4.dev8.dist-info}/WHEEL +0 -0
  254. {synth_ai-0.2.4.dev6.dist-info → synth_ai-0.2.4.dev8.dist-info}/entry_points.txt +0 -0
  255. {synth_ai-0.2.4.dev6.dist-info → synth_ai-0.2.4.dev8.dist-info}/licenses/LICENSE +0 -0
  256. {synth_ai-0.2.4.dev6.dist-info → synth_ai-0.2.4.dev8.dist-info}/top_level.txt +0 -0
@@ -1,6 +1,8 @@
1
- from typing import Any, Collection, Optional
1
+ from collections.abc import Collection
2
2
  from dataclasses import dataclass
3
- from synth_ai.environments.tasks.core import TaskInstanceMetadataFilter, TaskInstance
3
+ from typing import Any
4
+
5
+ from synth_ai.environments.tasks.core import TaskInstance, TaskInstanceMetadataFilter
4
6
 
5
7
 
6
8
  @dataclass
@@ -18,8 +20,8 @@ class ValueFilter(TaskInstanceMetadataFilter):
18
20
  @dataclass
19
21
  class RangeFilter(TaskInstanceMetadataFilter):
20
22
  key: str
21
- min_val: Optional[float] = None
22
- max_val: Optional[float] = None
23
+ min_val: float | None = None
24
+ max_val: float | None = None
23
25
 
24
26
  def __call__(self, instance: TaskInstance) -> bool:
25
27
  instance_value = getattr(instance.metadata, self.key, None)
@@ -2,17 +2,19 @@
2
2
  Utility functions and generic filters for taskset creation.
3
3
  """
4
4
 
5
- from typing import Any, Collection, Optional, List, Set
5
+ from collections.abc import Collection
6
+ from typing import Any
6
7
  from uuid import UUID, uuid4
8
+
7
9
  from synth_ai.environments.tasks.core import (
8
- TaskInstanceMetadataFilter,
9
- TaskInstanceSet,
10
10
  SplitInfo,
11
11
  TaskInstance,
12
+ TaskInstanceMetadataFilter,
13
+ TaskInstanceSet,
12
14
  )
13
15
 
14
16
 
15
- def parse_or_new_uuid(raw_id: Optional[str]) -> UUID:
17
+ def parse_or_new_uuid(raw_id: str | None) -> UUID:
16
18
  """
17
19
  Parse a raw ID string into a UUID, or generate a new one if invalid or missing.
18
20
  """
@@ -43,8 +45,8 @@ class RangeFilter(TaskInstanceMetadataFilter):
43
45
  def __init__(
44
46
  self,
45
47
  key: str,
46
- min_value: Optional[float] = None,
47
- max_value: Optional[float] = None,
48
+ min_value: float | None = None,
49
+ max_value: float | None = None,
48
50
  ):
49
51
  self.key = key
50
52
  self.min_value = min_value
@@ -62,15 +64,15 @@ class RangeFilter(TaskInstanceMetadataFilter):
62
64
  def make_taskset(
63
65
  name: str,
64
66
  description: str,
65
- instances: List[TaskInstance],
66
- val_filter: Optional[TaskInstanceMetadataFilter] = None,
67
- test_filter: Optional[TaskInstanceMetadataFilter] = None,
67
+ instances: list[TaskInstance],
68
+ val_filter: TaskInstanceMetadataFilter | None = None,
69
+ test_filter: TaskInstanceMetadataFilter | None = None,
68
70
  ) -> TaskInstanceSet:
69
71
  """
70
72
  Assemble a TaskInstanceSet by applying optional validation and test filters.
71
73
  """
72
- val_ids: Set[Any] = set()
73
- test_ids: Set[Any] = set()
74
+ val_ids: set[Any] = set()
75
+ test_ids: set[Any] = set()
74
76
  if val_filter:
75
77
  val_ids = {inst.id for inst in instances if val_filter(inst)}
76
78
  if test_filter:
synth_ai/evals/base.py CHANGED
@@ -1,9 +1,8 @@
1
- from typing import List
2
1
 
3
2
 
4
3
  class Judgement:
5
4
  def __init__(
6
- self, criteria: str, score: float, reasoning: str = "", evidence: List[str] = None
5
+ self, criteria: str, score: float, reasoning: str = "", evidence: list[str] = None
7
6
  ):
8
7
  self.criteria = criteria
9
8
  self.score = score
@@ -12,5 +11,5 @@ class Judgement:
12
11
 
13
12
 
14
13
  class BaseEval:
15
- async def run(self, data: any) -> List[Judgement]:
14
+ async def run(self, data: any) -> list[Judgement]:
16
15
  pass
@@ -1,5 +1,5 @@
1
-
2
- """
1
+ # ruff: noqa
2
+ '''
3
3
  Synth OSS Integration Module
4
4
 
5
5
  This module provides integration with Synth's open-source inference and training APIs
@@ -336,7 +336,7 @@ Implementation sketch (backend == "synth")
336
336
  The method is a *no-op* for the default (OpenAI) backend so existing code keeps
337
337
  working.
338
338
 
339
- """
339
+ '''
340
340
 
341
341
 
342
342
  """
@@ -443,4 +443,4 @@ async def warmup(
443
443
  So: **the existing endpoint does not yet support GPU selection; we need to add
444
444
  the small change above on the `learning_v2` side and then LM.warmup can request
445
445
  specific GPUs.**
446
- """
446
+ """
synth_ai/http.py ADDED
@@ -0,0 +1,102 @@
1
+ from __future__ import annotations
2
+
3
+ import asyncio
4
+ from dataclasses import dataclass
5
+ from typing import Any, Dict, Optional
6
+
7
+ import aiohttp
8
+
9
+
10
+ @dataclass
11
+ class HTTPError(Exception):
12
+ status: int
13
+ url: str
14
+ message: str
15
+ body_snippet: str | None = None
16
+ detail: Any | None = None
17
+
18
+ def __str__(self) -> str: # pragma: no cover - trivial
19
+ base = f"HTTP {self.status} for {self.url}: {self.message}"
20
+ if self.body_snippet:
21
+ base += f" | body[0:200]={self.body_snippet[:200]}"
22
+ return base
23
+
24
+
25
+ class AsyncHttpClient:
26
+ def __init__(self, base_url: str, api_key: str, timeout: float = 30.0) -> None:
27
+ self._base_url = base_url.rstrip("/")
28
+ self._api_key = api_key
29
+ self._timeout = aiohttp.ClientTimeout(total=timeout)
30
+ self._session: Optional[aiohttp.ClientSession] = None
31
+
32
+ async def __aenter__(self) -> "AsyncHttpClient":
33
+ if self._session is None:
34
+ headers = {"authorization": f"Bearer {self._api_key}"}
35
+ self._session = aiohttp.ClientSession(headers=headers, timeout=self._timeout)
36
+ return self
37
+
38
+ async def __aexit__(self, exc_type, exc, tb) -> None: # noqa: ANN001
39
+ if self._session is not None:
40
+ await self._session.close()
41
+ self._session = None
42
+
43
+ def _abs(self, path: str) -> str:
44
+ if path.startswith("http://") or path.startswith("https://"):
45
+ return path
46
+ # If base_url already ends with /api and path starts with /api, remove duplicate
47
+ if self._base_url.endswith("/api") and path.startswith("/api"):
48
+ path = path[4:] # Remove leading /api
49
+ return f"{self._base_url}/{path.lstrip('/')}"
50
+
51
+ async def get(self, path: str, *, params: Optional[Dict[str, Any]] = None, headers: Optional[Dict[str, str]] = None) -> Any:
52
+ url = self._abs(path)
53
+ assert self._session is not None, "AsyncHttpClient must be used as an async context manager"
54
+ async with self._session.get(url, params=params, headers=headers) as resp:
55
+ return await self._handle_response(resp, url)
56
+
57
+ async def post_json(self, path: str, *, json: Dict[str, Any], headers: Optional[Dict[str, str]] = None) -> Any:
58
+ url = self._abs(path)
59
+ assert self._session is not None, "AsyncHttpClient must be used as an async context manager"
60
+ async with self._session.post(url, json=json, headers=headers) as resp:
61
+ return await self._handle_response(resp, url)
62
+
63
+ async def post_multipart(self, path: str, *, data: Dict[str, Any], files: Dict[str, tuple[str, bytes, str | None]], headers: Optional[Dict[str, str]] = None) -> Any:
64
+ url = self._abs(path)
65
+ assert self._session is not None, "AsyncHttpClient must be used as an async context manager"
66
+ form = aiohttp.FormData()
67
+ for k, v in data.items():
68
+ form.add_field(k, str(v))
69
+ for field, (filename, content, content_type) in files.items():
70
+ form.add_field(field, content, filename=filename, content_type=content_type or "application/octet-stream")
71
+ async with self._session.post(url, data=form, headers=headers) as resp:
72
+ return await self._handle_response(resp, url)
73
+
74
+ async def delete(self, path: str, *, headers: Optional[Dict[str, str]] = None) -> Any:
75
+ url = self._abs(path)
76
+ assert self._session is not None, "AsyncHttpClient must be used as an async context manager"
77
+ async with self._session.delete(url, headers=headers) as resp:
78
+ return await self._handle_response(resp, url)
79
+
80
+ async def _handle_response(self, resp: aiohttp.ClientResponse, url: str) -> Any:
81
+ text = await resp.text()
82
+ body_snippet = text[:200] if text else None
83
+ if 200 <= resp.status < 300:
84
+ ctype = resp.headers.get("content-type", "")
85
+ if "application/json" in ctype:
86
+ try:
87
+ return await resp.json()
88
+ except Exception:
89
+ # Fallback to text
90
+ return text
91
+ return text
92
+ # error
93
+ detail: Any | None = None
94
+ try:
95
+ detail = await resp.json()
96
+ except Exception:
97
+ detail = None
98
+ raise HTTPError(status=resp.status, url=url, message="request_failed", body_snippet=body_snippet, detail=detail)
99
+
100
+
101
+ async def sleep(seconds: float) -> None:
102
+ await asyncio.sleep(seconds)
@@ -0,0 +1,7 @@
1
+ from .client import InferenceClient
2
+
3
+ __all__ = [
4
+ "InferenceClient",
5
+ ]
6
+
7
+
@@ -0,0 +1,20 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any, Dict
4
+
5
+ from ..http import AsyncHttpClient
6
+
7
+
8
+ class InferenceClient:
9
+ def __init__(self, base_url: str, api_key: str, *, timeout: float = 30.0) -> None:
10
+ self._base_url = base_url.rstrip("/")
11
+ self._api_key = api_key
12
+ self._timeout = timeout
13
+
14
+ async def create_chat_completion(self, *, model: str, messages: list[dict], **kwargs: Any) -> Dict[str, Any]:
15
+ body: Dict[str, Any] = {"model": model, "messages": messages}
16
+ body.update(kwargs)
17
+ async with AsyncHttpClient(self._base_url, self._api_key, timeout=self._timeout) as http:
18
+ return await http.post_json("/v1/chat/completions", json=body)
19
+
20
+
@@ -0,0 +1,246 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any, Dict, Optional
4
+
5
+ from synth_ai.http import AsyncHttpClient
6
+
7
+
8
+ class FilesApi:
9
+ def __init__(self, http: AsyncHttpClient) -> None:
10
+ self._http = http
11
+
12
+ async def upload(self, *, filename: str, content: bytes, purpose: str, content_type: Optional[str] = None, idempotency_key: Optional[str] = None) -> Dict[str, Any]:
13
+ data = {"purpose": purpose}
14
+ files = {"file": (filename, content, content_type)}
15
+ headers = {"Idempotency-Key": idempotency_key} if idempotency_key else None
16
+ return await self._http.post_multipart("/api/files", data=data, files=files, headers=headers)
17
+
18
+ async def list(self, *, purpose: Optional[str] = None, after: Optional[str] = None, limit: int = 20) -> Dict[str, Any]:
19
+ params: Dict[str, Any] = {}
20
+ if purpose is not None:
21
+ params["purpose"] = purpose
22
+ if after is not None:
23
+ params["after"] = after
24
+ params["limit"] = limit
25
+ return await self._http.get("/api/files", params=params)
26
+
27
+ async def retrieve(self, file_id: str) -> Dict[str, Any]:
28
+ return await self._http.get(f"/api/files/{file_id}")
29
+
30
+ async def delete(self, file_id: str) -> Any:
31
+ return await self._http.delete(f"/api/files/{file_id}")
32
+
33
+ async def list_jobs(self, file_id: str, *, after: Optional[str] = None, limit: int = 20) -> Dict[str, Any]:
34
+ params: Dict[str, Any] = {"limit": limit}
35
+ if after is not None:
36
+ params["after"] = after
37
+ return await self._http.get(f"/api/files/{file_id}/jobs", params=params)
38
+
39
+
40
+ class SftJobsApi:
41
+ def __init__(self, http: AsyncHttpClient) -> None:
42
+ self._http = http
43
+
44
+ async def create(
45
+ self,
46
+ *,
47
+ training_file: str,
48
+ model: str,
49
+ validation_file: Optional[str] = None,
50
+ hyperparameters: Optional[Dict[str, Any]] = None,
51
+ suffix: Optional[str] = None,
52
+ integrations: Optional[Dict[str, Any]] = None,
53
+ metadata: Optional[Dict[str, Any]] = None,
54
+ idempotency_key: Optional[str] = None,
55
+ ) -> Dict[str, Any]:
56
+ payload: Dict[str, Any] = {
57
+ "training_file": training_file,
58
+ "model": model,
59
+ }
60
+ if validation_file is not None:
61
+ payload["validation_file"] = validation_file
62
+ if hyperparameters is not None:
63
+ payload["hyperparameters"] = hyperparameters
64
+ if suffix is not None:
65
+ payload["suffix"] = suffix
66
+ if integrations is not None:
67
+ payload["integrations"] = integrations
68
+ if metadata is not None:
69
+ payload["metadata"] = metadata
70
+ headers = {"Idempotency-Key": idempotency_key} if idempotency_key else None
71
+ return await self._http.post_json("/api/sft/jobs", json=payload, headers=headers)
72
+
73
+ async def list(
74
+ self,
75
+ *,
76
+ status: Optional[str] = None,
77
+ model: Optional[str] = None,
78
+ file_id: Optional[str] = None,
79
+ created_after: Optional[int] = None,
80
+ created_before: Optional[int] = None,
81
+ after: Optional[str] = None,
82
+ limit: int = 20,
83
+ ) -> Dict[str, Any]:
84
+ params: Dict[str, Any] = {"limit": limit}
85
+ if status is not None:
86
+ params["status"] = status
87
+ if model is not None:
88
+ params["model"] = model
89
+ if file_id is not None:
90
+ params["file_id"] = file_id
91
+ if created_after is not None:
92
+ params["created_after"] = created_after
93
+ if created_before is not None:
94
+ params["created_before"] = created_before
95
+ if after is not None:
96
+ params["after"] = after
97
+ return await self._http.get("/api/sft/jobs", params=params)
98
+
99
+ async def retrieve(self, job_id: str) -> Dict[str, Any]:
100
+ return await self._http.get(f"/api/sft/jobs/{job_id}")
101
+
102
+ async def cancel(self, job_id: str) -> Dict[str, Any]:
103
+ return await self._http.post_json(f"/api/sft/jobs/{job_id}/cancel", json={})
104
+
105
+ async def list_events(self, job_id: str, *, since_seq: int = 0, limit: int = 200) -> Dict[str, Any]:
106
+ params = {"since_seq": since_seq, "limit": limit}
107
+ return await self._http.get(f"/api/sft/jobs/{job_id}/events", params=params)
108
+
109
+ async def checkpoints(self, job_id: str, *, after: Optional[str] = None, limit: int = 10) -> Dict[str, Any]:
110
+ params: Dict[str, Any] = {"limit": limit}
111
+ if after is not None:
112
+ params["after"] = after
113
+ return await self._http.get(f"/api/sft/jobs/{job_id}/checkpoints", params=params)
114
+
115
+
116
+ class RlJobsApi:
117
+ def __init__(self, http: AsyncHttpClient) -> None:
118
+ self._http = http
119
+
120
+ async def create(
121
+ self,
122
+ *,
123
+ model: str,
124
+ endpoint_base_url: str,
125
+ trainer_id: str,
126
+ trainer: Optional[Dict[str, Any]] = None,
127
+ job_config_id: Optional[str] = None,
128
+ config: Optional[Dict[str, Any]] = None,
129
+ metadata: Optional[Dict[str, Any]] = None,
130
+ idempotency_key: Optional[str] = None,
131
+ ) -> Dict[str, Any]:
132
+ payload: Dict[str, Any] = {
133
+ "model": model,
134
+ "endpoint_base_url": endpoint_base_url,
135
+ "trainer_id": trainer_id,
136
+ }
137
+ if trainer is not None:
138
+ payload["trainer"] = trainer
139
+ if job_config_id is not None:
140
+ payload["job_config_id"] = job_config_id
141
+ if config is not None:
142
+ payload["config"] = config
143
+ if metadata is not None:
144
+ payload["metadata"] = metadata
145
+ headers = {"Idempotency-Key": idempotency_key} if idempotency_key else None
146
+ return await self._http.post_json("/api/rl/jobs", json=payload, headers=headers)
147
+
148
+ async def list(
149
+ self,
150
+ *,
151
+ status: Optional[str] = None,
152
+ model: Optional[str] = None,
153
+ created_after: Optional[int] = None,
154
+ created_before: Optional[int] = None,
155
+ after: Optional[str] = None,
156
+ limit: int = 20,
157
+ ) -> Dict[str, Any]:
158
+ params: Dict[str, Any] = {"limit": limit}
159
+ if status is not None:
160
+ params["status"] = status
161
+ if model is not None:
162
+ params["model"] = model
163
+ if created_after is not None:
164
+ params["created_after"] = created_after
165
+ if created_before is not None:
166
+ params["created_before"] = created_before
167
+ if after is not None:
168
+ params["after"] = after
169
+ return await self._http.get("/api/rl/jobs", params=params)
170
+
171
+ async def retrieve(self, job_id: str) -> Dict[str, Any]:
172
+ return await self._http.get(f"/api/rl/jobs/{job_id}")
173
+
174
+ async def cancel(self, job_id: str) -> Dict[str, Any]:
175
+ return await self._http.post_json(f"/api/rl/jobs/{job_id}/cancel", json={})
176
+
177
+ async def list_events(self, job_id: str, *, since_seq: int = 0, limit: int = 200) -> Dict[str, Any]:
178
+ params = {"since_seq": since_seq, "limit": limit}
179
+ return await self._http.get(f"/api/rl/jobs/{job_id}/events", params=params)
180
+
181
+ async def metrics(self, job_id: str, *, after_step: int = -1, limit: int = 200) -> Dict[str, Any]:
182
+ params = {"after_step": after_step, "limit": limit}
183
+ return await self._http.get(f"/api/rl/jobs/{job_id}/metrics", params=params)
184
+
185
+
186
+ class ModelsApi:
187
+ def __init__(self, http: AsyncHttpClient) -> None:
188
+ self._http = http
189
+
190
+ async def list(
191
+ self,
192
+ *,
193
+ source: Optional[str] = None,
194
+ base_model: Optional[str] = None,
195
+ status: Optional[str] = None,
196
+ after: Optional[str] = None,
197
+ limit: int = 20,
198
+ ) -> Dict[str, Any]:
199
+ params: Dict[str, Any] = {"limit": limit}
200
+ if source is not None:
201
+ params["source"] = source
202
+ if base_model is not None:
203
+ params["base_model"] = base_model
204
+ if status is not None:
205
+ params["status"] = status
206
+ if after is not None:
207
+ params["after"] = after
208
+ return await self._http.get("/api/models", params=params)
209
+
210
+ async def retrieve(self, model_id: str) -> Dict[str, Any]:
211
+ return await self._http.get(f"/api/models/{model_id}")
212
+
213
+ async def delete(self, model_id: str) -> Any:
214
+ return await self._http.delete(f"/api/models/{model_id}")
215
+
216
+ async def list_jobs(self, model_id: str, *, after: Optional[str] = None, limit: int = 20) -> Dict[str, Any]:
217
+ params: Dict[str, Any] = {"limit": limit}
218
+ if after is not None:
219
+ params["after"] = after
220
+ return await self._http.get(f"/api/models/{model_id}/jobs", params=params)
221
+
222
+
223
+ class JobsClient:
224
+ """High-level client aggregating job APIs.
225
+
226
+ Usage:
227
+ async with JobsClient(base_url, api_key) as c:
228
+ await c.files.list()
229
+ """
230
+
231
+ def __init__(self, base_url: str, api_key: str, timeout: float = 30.0, http: Optional[AsyncHttpClient] = None) -> None:
232
+ self._base_url = base_url
233
+ self._api_key = api_key
234
+ self._timeout = timeout
235
+ self._http = http or AsyncHttpClient(base_url, api_key, timeout=timeout)
236
+ self.files = FilesApi(self._http)
237
+ self.sft = SftJobsApi(self._http)
238
+ self.rl = RlJobsApi(self._http)
239
+ self.models = ModelsApi(self._http)
240
+
241
+ async def __aenter__(self) -> "JobsClient":
242
+ await self._http.__aenter__()
243
+ return self
244
+
245
+ async def __aexit__(self, exc_type, exc, tb) -> None: # noqa: ANN001
246
+ await self._http.__aexit__(exc_type, exc, tb)
@@ -0,0 +1,24 @@
1
+ from .client import LearningClient
2
+ from .rl_client import RlClient
3
+ from .ft_client import FtClient
4
+ from .validators import validate_training_jsonl, validate_trainer_cfg_rl
5
+ from synth_ai.task import validate_task_app_url, task_app_health
6
+ from .health import backend_health, pricing_preflight, balance_autumn_normalized
7
+ from .sse import stream_events as stream_job_events
8
+ from .jobs import JobHandle, JobsApiResolver
9
+
10
+ __all__ = [
11
+ "LearningClient",
12
+ "RlClient",
13
+ "FtClient",
14
+ "validate_training_jsonl",
15
+ "validate_trainer_cfg_rl",
16
+ "validate_task_app_url",
17
+ "backend_health",
18
+ "task_app_health",
19
+ "pricing_preflight",
20
+ "balance_autumn_normalized",
21
+ "stream_job_events",
22
+ "JobHandle",
23
+ "JobsApiResolver",
24
+ ]
@@ -0,0 +1,149 @@
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+ from typing import Any, Callable, Dict, List, Optional
5
+
6
+ from ..http import AsyncHttpClient, HTTPError, sleep
7
+
8
+
9
+ class LearningClient:
10
+ def __init__(self, base_url: str, api_key: str, *, timeout: float = 30.0) -> None:
11
+ self._base_url = base_url.rstrip("/")
12
+ self._api_key = api_key
13
+ self._timeout = timeout
14
+
15
+ async def upload_training_file(self, path: str | Path, *, purpose: str = "fine-tune") -> str:
16
+ p = Path(path)
17
+ content = p.read_bytes()
18
+ async with AsyncHttpClient(self._base_url, self._api_key, timeout=self._timeout) as http:
19
+ data = {"purpose": purpose}
20
+ files = {"file": (p.name, content, _infer_content_type(p.name))}
21
+ js = await http.post_multipart("/api/learning/files", data=data, files=files)
22
+ if not isinstance(js, dict) or "id" not in js:
23
+ raise HTTPError(status=500, url="/api/learning/files", message="invalid_upload_response", body_snippet=str(js)[:200])
24
+ return str(js["id"])
25
+
26
+ async def create_job(
27
+ self,
28
+ *,
29
+ training_type: str,
30
+ model: str,
31
+ training_file_id: str,
32
+ hyperparameters: Optional[Dict[str, Any]] = None,
33
+ metadata: Optional[Dict[str, Any]] = None,
34
+ ) -> Dict[str, Any]:
35
+ body = {
36
+ "training_type": training_type,
37
+ "model": model,
38
+ "training_file_id": training_file_id,
39
+ "hyperparameters": hyperparameters or {},
40
+ "metadata": metadata or {},
41
+ }
42
+ async with AsyncHttpClient(self._base_url, self._api_key, timeout=self._timeout) as http:
43
+ return await http.post_json("/api/learning/jobs", json=body)
44
+
45
+ async def start_job(self, job_id: str) -> Dict[str, Any]:
46
+ async with AsyncHttpClient(self._base_url, self._api_key, timeout=self._timeout) as http:
47
+ return await http.post_json(f"/api/learning/jobs/{job_id}/start", json={})
48
+
49
+ async def get_job(self, job_id: str) -> Dict[str, Any]:
50
+ async with AsyncHttpClient(self._base_url, self._api_key, timeout=self._timeout) as http:
51
+ return await http.get(f"/api/learning/jobs/{job_id}")
52
+
53
+ async def get_events(self, job_id: str, *, since_seq: int = 0, limit: int = 200) -> List[Dict[str, Any]]:
54
+ params = {"since_seq": since_seq, "limit": limit}
55
+ async with AsyncHttpClient(self._base_url, self._api_key, timeout=self._timeout) as http:
56
+ js = await http.get(f"/api/learning/jobs/{job_id}/events", params=params)
57
+ if isinstance(js, dict) and isinstance(js.get("events"), list):
58
+ return js["events"]
59
+ return []
60
+
61
+ async def get_metrics(self, job_id: str, *, name: str | None = None, after_step: int | None = None, limit: int = 500, run_id: str | None = None) -> List[Dict[str, Any]]:
62
+ params: Dict[str, Any] = {"limit": limit}
63
+ if name is not None:
64
+ params["name"] = name
65
+ if after_step is not None:
66
+ params["after_step"] = after_step
67
+ if run_id is not None:
68
+ params["run_id"] = run_id
69
+ async with AsyncHttpClient(self._base_url, self._api_key, timeout=self._timeout) as http:
70
+ js = await http.get(f"/api/learning/jobs/{job_id}/metrics", params=params)
71
+ if isinstance(js, dict) and isinstance(js.get("points"), list):
72
+ return js["points"]
73
+ return []
74
+
75
+ async def get_timeline(self, job_id: str, *, limit: int = 200) -> List[Dict[str, Any]]:
76
+ params = {"limit": limit}
77
+ async with AsyncHttpClient(self._base_url, self._api_key, timeout=self._timeout) as http:
78
+ js = await http.get(f"/api/learning/jobs/{job_id}/timeline", params=params)
79
+ if isinstance(js, dict) and isinstance(js.get("events"), list):
80
+ return js["events"]
81
+ return []
82
+
83
+ async def poll_until_terminal(
84
+ self,
85
+ job_id: str,
86
+ *,
87
+ interval_seconds: float = 2.0,
88
+ max_seconds: float | None = 3600,
89
+ on_event: Callable[[Dict[str, Any]], None] | None = None,
90
+ ) -> Dict[str, Any]:
91
+ last_seq = 0
92
+ elapsed = 0.0
93
+ while True:
94
+ # Events
95
+ events = await self.get_events(job_id, since_seq=last_seq, limit=200)
96
+ for e in events:
97
+ if isinstance(e, dict) and isinstance(e.get("seq"), int):
98
+ last_seq = max(last_seq, int(e["seq"]))
99
+ if on_event:
100
+ try:
101
+ on_event(e)
102
+ except Exception:
103
+ pass
104
+
105
+ # Status
106
+ job = await self.get_job(job_id)
107
+ status = str(job.get("status") or "").lower()
108
+ if status in {"succeeded", "failed", "canceled", "cancelled"}:
109
+ return job
110
+
111
+ # Sleep and time budget
112
+ await sleep(interval_seconds)
113
+ elapsed += interval_seconds
114
+ if max_seconds is not None and elapsed >= max_seconds:
115
+ raise TimeoutError(f"Polling timed out after {elapsed} seconds for job {job_id}")
116
+
117
+ # --- Optional diagnostics ---
118
+ async def pricing_preflight(self, *, job_type: str, gpu_type: str, estimated_seconds: float, container_count: int) -> Dict[str, Any]:
119
+ body = {
120
+ "job_type": job_type,
121
+ "gpu_type": gpu_type,
122
+ "estimated_seconds": float(estimated_seconds or 0.0),
123
+ "container_count": int(container_count or 1),
124
+ }
125
+ async with AsyncHttpClient(self._base_url, self._api_key, timeout=self._timeout) as http:
126
+ js = await http.post_json("/api/v1/pricing/preflight", json=body)
127
+ if not isinstance(js, dict):
128
+ raise HTTPError(status=500, url="/api/v1/pricing/preflight", message="invalid_preflight_response", body_snippet=str(js)[:200])
129
+ return js
130
+
131
+ async def balance_autumn_normalized(self) -> Dict[str, Any]:
132
+ async with AsyncHttpClient(self._base_url, self._api_key, timeout=self._timeout) as http:
133
+ js = await http.get("/api/v1/balance/autumn-normalized")
134
+ if not isinstance(js, dict):
135
+ raise HTTPError(status=500, url="/api/v1/balance/autumn-normalized", message="invalid_balance_response", body_snippet=str(js)[:200])
136
+ return js
137
+
138
+
139
+ def _infer_content_type(filename: str) -> str:
140
+ name = filename.lower()
141
+ if name.endswith(".jsonl"):
142
+ return "application/jsonl"
143
+ if name.endswith(".json"):
144
+ return "application/json"
145
+ if name.endswith(".txt"):
146
+ return "text/plain"
147
+ return "application/octet-stream"
148
+
149
+