nvidia-nat 1.3.0.dev2__py3-none-any.whl → 1.3.0rc2__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 (250) hide show
  1. aiq/__init__.py +2 -2
  2. nat/agent/base.py +24 -15
  3. nat/agent/dual_node.py +9 -4
  4. nat/agent/prompt_optimizer/prompt.py +68 -0
  5. nat/agent/prompt_optimizer/register.py +149 -0
  6. nat/agent/react_agent/agent.py +79 -47
  7. nat/agent/react_agent/register.py +50 -22
  8. nat/agent/reasoning_agent/reasoning_agent.py +11 -9
  9. nat/agent/register.py +1 -1
  10. nat/agent/rewoo_agent/agent.py +326 -148
  11. nat/agent/rewoo_agent/prompt.py +19 -22
  12. nat/agent/rewoo_agent/register.py +54 -27
  13. nat/agent/tool_calling_agent/agent.py +84 -28
  14. nat/agent/tool_calling_agent/register.py +51 -28
  15. nat/authentication/api_key/api_key_auth_provider.py +2 -2
  16. nat/authentication/credential_validator/bearer_token_validator.py +557 -0
  17. nat/authentication/http_basic_auth/http_basic_auth_provider.py +1 -1
  18. nat/authentication/interfaces.py +5 -2
  19. nat/authentication/oauth2/oauth2_auth_code_flow_provider.py +69 -36
  20. nat/authentication/oauth2/oauth2_resource_server_config.py +124 -0
  21. nat/authentication/register.py +0 -1
  22. nat/builder/builder.py +56 -24
  23. nat/builder/component_utils.py +9 -5
  24. nat/builder/context.py +68 -17
  25. nat/builder/eval_builder.py +16 -11
  26. nat/builder/framework_enum.py +1 -0
  27. nat/builder/front_end.py +1 -1
  28. nat/builder/function.py +378 -8
  29. nat/builder/function_base.py +3 -3
  30. nat/builder/function_info.py +6 -8
  31. nat/builder/user_interaction_manager.py +2 -2
  32. nat/builder/workflow.py +13 -1
  33. nat/builder/workflow_builder.py +281 -76
  34. nat/cli/cli_utils/config_override.py +2 -2
  35. nat/cli/commands/evaluate.py +1 -1
  36. nat/cli/commands/info/info.py +16 -6
  37. nat/cli/commands/info/list_channels.py +1 -1
  38. nat/cli/commands/info/list_components.py +7 -8
  39. nat/cli/commands/mcp/__init__.py +14 -0
  40. nat/cli/commands/mcp/mcp.py +986 -0
  41. nat/cli/commands/object_store/__init__.py +14 -0
  42. nat/cli/commands/object_store/object_store.py +227 -0
  43. nat/cli/commands/optimize.py +90 -0
  44. nat/cli/commands/registry/publish.py +2 -2
  45. nat/cli/commands/registry/pull.py +2 -2
  46. nat/cli/commands/registry/remove.py +2 -2
  47. nat/cli/commands/registry/search.py +15 -17
  48. nat/cli/commands/start.py +16 -5
  49. nat/cli/commands/uninstall.py +1 -1
  50. nat/cli/commands/workflow/templates/config.yml.j2 +14 -13
  51. nat/cli/commands/workflow/templates/pyproject.toml.j2 +4 -1
  52. nat/cli/commands/workflow/templates/register.py.j2 +2 -3
  53. nat/cli/commands/workflow/templates/workflow.py.j2 +35 -21
  54. nat/cli/commands/workflow/workflow_commands.py +62 -22
  55. nat/cli/entrypoint.py +8 -10
  56. nat/cli/main.py +3 -0
  57. nat/cli/register_workflow.py +38 -4
  58. nat/cli/type_registry.py +75 -6
  59. nat/control_flow/__init__.py +0 -0
  60. nat/control_flow/register.py +20 -0
  61. nat/control_flow/router_agent/__init__.py +0 -0
  62. nat/control_flow/router_agent/agent.py +329 -0
  63. nat/control_flow/router_agent/prompt.py +48 -0
  64. nat/control_flow/router_agent/register.py +91 -0
  65. nat/control_flow/sequential_executor.py +166 -0
  66. nat/data_models/agent.py +34 -0
  67. nat/data_models/api_server.py +74 -66
  68. nat/data_models/authentication.py +23 -9
  69. nat/data_models/common.py +1 -1
  70. nat/data_models/component.py +2 -0
  71. nat/data_models/component_ref.py +11 -0
  72. nat/data_models/config.py +41 -17
  73. nat/data_models/dataset_handler.py +1 -1
  74. nat/data_models/discovery_metadata.py +4 -4
  75. nat/data_models/evaluate.py +4 -1
  76. nat/data_models/function.py +34 -0
  77. nat/data_models/function_dependencies.py +14 -6
  78. nat/data_models/gated_field_mixin.py +242 -0
  79. nat/data_models/intermediate_step.py +3 -3
  80. nat/data_models/optimizable.py +119 -0
  81. nat/data_models/optimizer.py +149 -0
  82. nat/data_models/span.py +41 -3
  83. nat/data_models/swe_bench_model.py +1 -1
  84. nat/data_models/temperature_mixin.py +44 -0
  85. nat/data_models/thinking_mixin.py +86 -0
  86. nat/data_models/top_p_mixin.py +44 -0
  87. nat/embedder/nim_embedder.py +1 -1
  88. nat/embedder/openai_embedder.py +1 -1
  89. nat/embedder/register.py +0 -1
  90. nat/eval/config.py +3 -1
  91. nat/eval/dataset_handler/dataset_handler.py +71 -7
  92. nat/eval/evaluate.py +86 -31
  93. nat/eval/evaluator/base_evaluator.py +1 -1
  94. nat/eval/evaluator/evaluator_model.py +13 -0
  95. nat/eval/intermediate_step_adapter.py +1 -1
  96. nat/eval/rag_evaluator/evaluate.py +2 -2
  97. nat/eval/rag_evaluator/register.py +3 -3
  98. nat/eval/register.py +4 -1
  99. nat/eval/remote_workflow.py +3 -3
  100. nat/eval/runtime_evaluator/__init__.py +14 -0
  101. nat/eval/runtime_evaluator/evaluate.py +123 -0
  102. nat/eval/runtime_evaluator/register.py +100 -0
  103. nat/eval/swe_bench_evaluator/evaluate.py +6 -6
  104. nat/eval/trajectory_evaluator/evaluate.py +1 -1
  105. nat/eval/trajectory_evaluator/register.py +1 -1
  106. nat/eval/tunable_rag_evaluator/evaluate.py +4 -7
  107. nat/eval/utils/eval_trace_ctx.py +89 -0
  108. nat/eval/utils/weave_eval.py +18 -9
  109. nat/experimental/decorators/experimental_warning_decorator.py +27 -7
  110. nat/experimental/test_time_compute/functions/plan_select_execute_function.py +7 -3
  111. nat/experimental/test_time_compute/functions/ttc_tool_orchestration_function.py +3 -3
  112. nat/experimental/test_time_compute/functions/ttc_tool_wrapper_function.py +1 -1
  113. nat/experimental/test_time_compute/models/strategy_base.py +5 -4
  114. nat/experimental/test_time_compute/register.py +0 -1
  115. nat/experimental/test_time_compute/selection/llm_based_output_merging_selector.py +1 -3
  116. nat/front_ends/console/authentication_flow_handler.py +82 -30
  117. nat/front_ends/console/console_front_end_plugin.py +8 -5
  118. nat/front_ends/fastapi/auth_flow_handlers/websocket_flow_handler.py +52 -17
  119. nat/front_ends/fastapi/dask_client_mixin.py +65 -0
  120. nat/front_ends/fastapi/fastapi_front_end_config.py +36 -5
  121. nat/front_ends/fastapi/fastapi_front_end_controller.py +4 -4
  122. nat/front_ends/fastapi/fastapi_front_end_plugin.py +135 -4
  123. nat/front_ends/fastapi/fastapi_front_end_plugin_worker.py +452 -282
  124. nat/front_ends/fastapi/job_store.py +518 -99
  125. nat/front_ends/fastapi/main.py +11 -19
  126. nat/front_ends/fastapi/message_handler.py +13 -14
  127. nat/front_ends/fastapi/message_validator.py +19 -19
  128. nat/front_ends/fastapi/response_helpers.py +4 -4
  129. nat/front_ends/fastapi/step_adaptor.py +2 -2
  130. nat/front_ends/fastapi/utils.py +57 -0
  131. nat/front_ends/mcp/introspection_token_verifier.py +73 -0
  132. nat/front_ends/mcp/mcp_front_end_config.py +10 -1
  133. nat/front_ends/mcp/mcp_front_end_plugin.py +45 -13
  134. nat/front_ends/mcp/mcp_front_end_plugin_worker.py +116 -8
  135. nat/front_ends/mcp/tool_converter.py +44 -14
  136. nat/front_ends/register.py +0 -1
  137. nat/front_ends/simple_base/simple_front_end_plugin_base.py +3 -1
  138. nat/llm/aws_bedrock_llm.py +24 -12
  139. nat/llm/azure_openai_llm.py +13 -6
  140. nat/llm/litellm_llm.py +69 -0
  141. nat/llm/nim_llm.py +20 -8
  142. nat/llm/openai_llm.py +14 -6
  143. nat/llm/register.py +4 -1
  144. nat/llm/utils/env_config_value.py +2 -3
  145. nat/llm/utils/thinking.py +215 -0
  146. nat/meta/pypi.md +9 -9
  147. nat/object_store/register.py +0 -1
  148. nat/observability/exporter/base_exporter.py +3 -3
  149. nat/observability/exporter/file_exporter.py +1 -1
  150. nat/observability/exporter/processing_exporter.py +309 -81
  151. nat/observability/exporter/span_exporter.py +35 -15
  152. nat/observability/exporter_manager.py +7 -7
  153. nat/observability/mixin/file_mixin.py +7 -7
  154. nat/observability/mixin/redaction_config_mixin.py +42 -0
  155. nat/observability/mixin/tagging_config_mixin.py +62 -0
  156. nat/observability/mixin/type_introspection_mixin.py +420 -107
  157. nat/observability/processor/batching_processor.py +5 -7
  158. nat/observability/processor/falsy_batch_filter_processor.py +55 -0
  159. nat/observability/processor/processor.py +3 -0
  160. nat/observability/processor/processor_factory.py +70 -0
  161. nat/observability/processor/redaction/__init__.py +24 -0
  162. nat/observability/processor/redaction/contextual_redaction_processor.py +125 -0
  163. nat/observability/processor/redaction/contextual_span_redaction_processor.py +66 -0
  164. nat/observability/processor/redaction/redaction_processor.py +177 -0
  165. nat/observability/processor/redaction/span_header_redaction_processor.py +92 -0
  166. nat/observability/processor/span_tagging_processor.py +68 -0
  167. nat/observability/register.py +6 -4
  168. nat/profiler/calc/calc_runner.py +3 -4
  169. nat/profiler/callbacks/agno_callback_handler.py +1 -1
  170. nat/profiler/callbacks/langchain_callback_handler.py +6 -6
  171. nat/profiler/callbacks/llama_index_callback_handler.py +3 -3
  172. nat/profiler/callbacks/semantic_kernel_callback_handler.py +3 -3
  173. nat/profiler/data_frame_row.py +1 -1
  174. nat/profiler/decorators/framework_wrapper.py +62 -13
  175. nat/profiler/decorators/function_tracking.py +160 -3
  176. nat/profiler/forecasting/models/forecasting_base_model.py +3 -1
  177. nat/profiler/forecasting/models/linear_model.py +1 -1
  178. nat/profiler/forecasting/models/random_forest_regressor.py +1 -1
  179. nat/profiler/inference_optimization/bottleneck_analysis/nested_stack_analysis.py +1 -1
  180. nat/profiler/inference_optimization/bottleneck_analysis/simple_stack_analysis.py +1 -1
  181. nat/profiler/inference_optimization/data_models.py +3 -3
  182. nat/profiler/inference_optimization/experimental/prefix_span_analysis.py +8 -9
  183. nat/profiler/inference_optimization/token_uniqueness.py +1 -1
  184. nat/profiler/parameter_optimization/__init__.py +0 -0
  185. nat/profiler/parameter_optimization/optimizable_utils.py +93 -0
  186. nat/profiler/parameter_optimization/optimizer_runtime.py +67 -0
  187. nat/profiler/parameter_optimization/parameter_optimizer.py +153 -0
  188. nat/profiler/parameter_optimization/parameter_selection.py +107 -0
  189. nat/profiler/parameter_optimization/pareto_visualizer.py +380 -0
  190. nat/profiler/parameter_optimization/prompt_optimizer.py +384 -0
  191. nat/profiler/parameter_optimization/update_helpers.py +66 -0
  192. nat/profiler/profile_runner.py +14 -9
  193. nat/profiler/utils.py +4 -2
  194. nat/registry_handlers/local/local_handler.py +2 -2
  195. nat/registry_handlers/package_utils.py +1 -2
  196. nat/registry_handlers/pypi/pypi_handler.py +23 -26
  197. nat/registry_handlers/register.py +3 -4
  198. nat/registry_handlers/rest/rest_handler.py +12 -13
  199. nat/retriever/milvus/retriever.py +2 -2
  200. nat/retriever/nemo_retriever/retriever.py +1 -1
  201. nat/retriever/register.py +0 -1
  202. nat/runtime/loader.py +2 -2
  203. nat/runtime/runner.py +106 -8
  204. nat/runtime/session.py +69 -8
  205. nat/settings/global_settings.py +16 -5
  206. nat/tool/chat_completion.py +5 -2
  207. nat/tool/code_execution/local_sandbox/local_sandbox_server.py +3 -3
  208. nat/tool/datetime_tools.py +49 -9
  209. nat/tool/document_search.py +2 -2
  210. nat/tool/github_tools.py +450 -0
  211. nat/tool/memory_tools/get_memory_tool.py +1 -1
  212. nat/tool/nvidia_rag.py +1 -1
  213. nat/tool/register.py +2 -9
  214. nat/tool/retriever.py +3 -2
  215. nat/utils/callable_utils.py +70 -0
  216. nat/utils/data_models/schema_validator.py +3 -3
  217. nat/utils/decorators.py +210 -0
  218. nat/utils/exception_handlers/automatic_retries.py +104 -51
  219. nat/utils/exception_handlers/schemas.py +1 -1
  220. nat/utils/io/yaml_tools.py +2 -2
  221. nat/utils/log_levels.py +25 -0
  222. nat/utils/reactive/base/observable_base.py +2 -2
  223. nat/utils/reactive/base/observer_base.py +1 -1
  224. nat/utils/reactive/observable.py +2 -2
  225. nat/utils/reactive/observer.py +4 -4
  226. nat/utils/reactive/subscription.py +1 -1
  227. nat/utils/settings/global_settings.py +6 -8
  228. nat/utils/type_converter.py +4 -3
  229. nat/utils/type_utils.py +9 -5
  230. {nvidia_nat-1.3.0.dev2.dist-info → nvidia_nat-1.3.0rc2.dist-info}/METADATA +42 -18
  231. {nvidia_nat-1.3.0.dev2.dist-info → nvidia_nat-1.3.0rc2.dist-info}/RECORD +238 -196
  232. {nvidia_nat-1.3.0.dev2.dist-info → nvidia_nat-1.3.0rc2.dist-info}/entry_points.txt +1 -0
  233. nat/cli/commands/info/list_mcp.py +0 -304
  234. nat/tool/github_tools/create_github_commit.py +0 -133
  235. nat/tool/github_tools/create_github_issue.py +0 -87
  236. nat/tool/github_tools/create_github_pr.py +0 -106
  237. nat/tool/github_tools/get_github_file.py +0 -106
  238. nat/tool/github_tools/get_github_issue.py +0 -166
  239. nat/tool/github_tools/get_github_pr.py +0 -256
  240. nat/tool/github_tools/update_github_issue.py +0 -100
  241. nat/tool/mcp/exceptions.py +0 -142
  242. nat/tool/mcp/mcp_client.py +0 -255
  243. nat/tool/mcp/mcp_tool.py +0 -96
  244. nat/utils/exception_handlers/mcp.py +0 -211
  245. /nat/{tool/github_tools → agent/prompt_optimizer}/__init__.py +0 -0
  246. /nat/{tool/mcp → authentication/credential_validator}/__init__.py +0 -0
  247. {nvidia_nat-1.3.0.dev2.dist-info → nvidia_nat-1.3.0rc2.dist-info}/WHEEL +0 -0
  248. {nvidia_nat-1.3.0.dev2.dist-info → nvidia_nat-1.3.0rc2.dist-info}/licenses/LICENSE-3rd-party.txt +0 -0
  249. {nvidia_nat-1.3.0.dev2.dist-info → nvidia_nat-1.3.0rc2.dist-info}/licenses/LICENSE.md +0 -0
  250. {nvidia_nat-1.3.0.dev2.dist-info → nvidia_nat-1.3.0rc2.dist-info}/top_level.txt +0 -0
@@ -15,6 +15,10 @@
15
15
 
16
16
  import typing
17
17
 
18
+ from pydantic import Field
19
+ from pydantic import field_validator
20
+ from pydantic import model_validator
21
+
18
22
  from .common import BaseModelRegistryTag
19
23
  from .common import TypedBaseModel
20
24
 
@@ -23,8 +27,38 @@ class FunctionBaseConfig(TypedBaseModel, BaseModelRegistryTag):
23
27
  pass
24
28
 
25
29
 
30
+ class FunctionGroupBaseConfig(TypedBaseModel, BaseModelRegistryTag):
31
+ """Base configuration for function groups.
32
+
33
+ Function groups enable sharing of configurations and resources across multiple functions.
34
+ """
35
+ include: list[str] = Field(
36
+ default_factory=list,
37
+ description="The list of function names which should be added to the global Function registry",
38
+ )
39
+ exclude: list[str] = Field(
40
+ default_factory=list,
41
+ description="The list of function names which should be excluded from default access to the group",
42
+ )
43
+
44
+ @field_validator("include", "exclude")
45
+ @classmethod
46
+ def _validate_fields_include_exclude(cls, value: list[str]) -> list[str]:
47
+ if len(set(value)) != len(value):
48
+ raise ValueError("Function names must be unique")
49
+ return sorted(value)
50
+
51
+ @model_validator(mode="after")
52
+ def _validate_include_exclude(self):
53
+ if self.include and self.exclude:
54
+ raise ValueError("include and exclude cannot be used together")
55
+ return self
56
+
57
+
26
58
  class EmptyFunctionConfig(FunctionBaseConfig, name="EmptyFunctionConfig"):
27
59
  pass
28
60
 
29
61
 
30
62
  FunctionConfigT = typing.TypeVar("FunctionConfigT", bound=FunctionBaseConfig)
63
+
64
+ FunctionGroupConfigT = typing.TypeVar("FunctionGroupConfigT", bound=FunctionGroupBaseConfig)
@@ -23,6 +23,7 @@ class FunctionDependencies(BaseModel):
23
23
  A class to represent the dependencies of a function.
24
24
  """
25
25
  functions: set[str] = Field(default_factory=set)
26
+ function_groups: set[str] = Field(default_factory=set)
26
27
  llms: set[str] = Field(default_factory=set)
27
28
  embedders: set[str] = Field(default_factory=set)
28
29
  memory_clients: set[str] = Field(default_factory=set)
@@ -33,6 +34,10 @@ class FunctionDependencies(BaseModel):
33
34
  def serialize_functions(self, v: set[str]) -> list[str]:
34
35
  return list(v)
35
36
 
37
+ @field_serializer("function_groups", when_used="json")
38
+ def serialize_function_groups(self, v: set[str]) -> list[str]:
39
+ return list(v)
40
+
36
41
  @field_serializer("llms", when_used="json")
37
42
  def serialize_llms(self, v: set[str]) -> list[str]:
38
43
  return list(v)
@@ -54,19 +59,22 @@ class FunctionDependencies(BaseModel):
54
59
  return list(v)
55
60
 
56
61
  def add_function(self, function: str):
57
- self.functions.add(function) # pylint: disable=no-member
62
+ self.functions.add(function)
63
+
64
+ def add_function_group(self, function_group: str):
65
+ self.function_groups.add(function_group) # pylint: disable=no-member
58
66
 
59
67
  def add_llm(self, llm: str):
60
- self.llms.add(llm) # pylint: disable=no-member
68
+ self.llms.add(llm)
61
69
 
62
70
  def add_embedder(self, embedder: str):
63
- self.embedders.add(embedder) # pylint: disable=no-member
71
+ self.embedders.add(embedder)
64
72
 
65
73
  def add_memory_client(self, memory_client: str):
66
- self.memory_clients.add(memory_client) # pylint: disable=no-member
74
+ self.memory_clients.add(memory_client)
67
75
 
68
76
  def add_object_store(self, object_store: str):
69
- self.object_stores.add(object_store) # pylint: disable=no-member
77
+ self.object_stores.add(object_store)
70
78
 
71
79
  def add_retriever(self, retriever: str):
72
- self.retrievers.add(retriever) # pylint: disable=no-member
80
+ self.retrievers.add(retriever)
@@ -0,0 +1,242 @@
1
+ # SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ from collections.abc import Sequence
17
+ from dataclasses import dataclass
18
+ from re import Pattern
19
+
20
+ from pydantic import model_validator
21
+
22
+
23
+ @dataclass
24
+ class GatedFieldMixinConfig:
25
+ """Configuration for a gated field mixin."""
26
+
27
+ field_name: str
28
+ default_if_supported: object | None
29
+ unsupported: Sequence[Pattern[str]] | None
30
+ supported: Sequence[Pattern[str]] | None
31
+ keys: Sequence[str]
32
+
33
+
34
+ class GatedFieldMixin:
35
+ """
36
+ A mixin that gates a field based on specified keys.
37
+
38
+ This should be used to automatically validate a field based on a given key.
39
+
40
+ Parameters
41
+ ----------
42
+ field_name: `str`
43
+ The name of the field.
44
+ default_if_supported: `object | None`
45
+ The default value of the field if it is supported for the key.
46
+ keys: `Sequence[str]`
47
+ A sequence of keys that are used to validate the field.
48
+ unsupported: `Sequence[Pattern[str]] | None`
49
+ A sequence of regex patterns that match the key names NOT supported for the field.
50
+ Defaults to None.
51
+ supported: `Sequence[Pattern[str]] | None`
52
+ A sequence of regex patterns that match the key names supported for the field.
53
+ Defaults to None.
54
+ """
55
+
56
+ def __init_subclass__(
57
+ cls,
58
+ field_name: str | None = None,
59
+ default_if_supported: object | None = None,
60
+ keys: Sequence[str] | None = None,
61
+ unsupported: Sequence[Pattern[str]] | None = None,
62
+ supported: Sequence[Pattern[str]] | None = None,
63
+ ) -> None:
64
+ """Store the class variables for the field and define the gated field validator."""
65
+ super().__init_subclass__()
66
+
67
+ # Check if this class directly inherits from GatedFieldMixin
68
+ has_gated_field_mixin = GatedFieldMixin in cls.__bases__
69
+
70
+ if has_gated_field_mixin:
71
+ if keys is None:
72
+ raise ValueError("keys must be provided when subclassing GatedFieldMixin")
73
+ if field_name is None:
74
+ raise ValueError("field_name must be provided when subclassing GatedFieldMixin")
75
+
76
+ cls._setup_direct_mixin(field_name, default_if_supported, unsupported, supported, keys)
77
+
78
+ # Always try to collect mixins and create validators for multiple inheritance
79
+ # This handles both direct inheritance and deep inheritance chains
80
+ all_mixins = cls._collect_all_mixin_configs()
81
+ if all_mixins:
82
+ cls._create_combined_validator(all_mixins)
83
+
84
+ @classmethod
85
+ def _setup_direct_mixin(
86
+ cls,
87
+ field_name: str,
88
+ default_if_supported: object | None,
89
+ unsupported: Sequence[Pattern[str]] | None,
90
+ supported: Sequence[Pattern[str]] | None,
91
+ keys: Sequence[str],
92
+ ) -> None:
93
+ """Set up a class that directly inherits from GatedFieldMixin."""
94
+ cls._validate_mixin_parameters(unsupported, supported, keys)
95
+
96
+ # Create and store validator
97
+ validator = cls._create_gated_field_validator(field_name, default_if_supported, unsupported, supported, keys)
98
+ validator_name = f"_gated_field_validator_{field_name}"
99
+ setattr(cls, validator_name, validator)
100
+
101
+ # Store mixin info for multiple inheritance
102
+ if not hasattr(cls, "_gated_field_mixins"):
103
+ cls._gated_field_mixins = []
104
+
105
+ cls._gated_field_mixins.append(
106
+ GatedFieldMixinConfig(
107
+ field_name,
108
+ default_if_supported,
109
+ unsupported,
110
+ supported,
111
+ keys,
112
+ ))
113
+
114
+ @classmethod
115
+ def _validate_mixin_parameters(
116
+ cls,
117
+ unsupported: Sequence[Pattern[str]] | None,
118
+ supported: Sequence[Pattern[str]] | None,
119
+ keys: Sequence[str],
120
+ ) -> None:
121
+ """Validate that all required parameters are provided."""
122
+ if unsupported is None and supported is None:
123
+ raise ValueError("Either unsupported or supported must be provided")
124
+ if unsupported is not None and supported is not None:
125
+ raise ValueError("Only one of unsupported or supported must be provided")
126
+ if len(keys) == 0:
127
+ raise ValueError("keys must be provided and non-empty when subclassing GatedFieldMixin")
128
+
129
+ @classmethod
130
+ def _create_gated_field_validator(
131
+ cls,
132
+ field_name: str,
133
+ default_if_supported: object | None,
134
+ unsupported: Sequence[Pattern[str]] | None,
135
+ supported: Sequence[Pattern[str]] | None,
136
+ keys: Sequence[str],
137
+ ):
138
+ """Create the model validator function."""
139
+
140
+ @model_validator(mode="after")
141
+ def gated_field_validator(self):
142
+ """Validate the gated field."""
143
+ current_value = getattr(self, field_name, None)
144
+ is_supported = cls._check_field_support(self, unsupported, supported, keys)
145
+ if not is_supported:
146
+ if current_value is not None:
147
+ blocking_key = cls._find_blocking_key(self, unsupported, supported, keys)
148
+ value = getattr(self, blocking_key, "<unknown>")
149
+ raise ValueError(f"{field_name} is not supported for {blocking_key}: {value}")
150
+ elif current_value is None:
151
+ setattr(self, field_name, default_if_supported)
152
+ return self
153
+
154
+ return gated_field_validator
155
+
156
+ @classmethod
157
+ def _check_field_support(
158
+ cls,
159
+ instance: object,
160
+ unsupported: Sequence[Pattern[str]] | None,
161
+ supported: Sequence[Pattern[str]] | None,
162
+ keys: Sequence[str],
163
+ ) -> bool:
164
+ """Check if a specific field is supported based on its configuration and keys."""
165
+ seen = False
166
+ for key in keys:
167
+ if not hasattr(instance, key):
168
+ continue
169
+ seen = True
170
+ value = str(getattr(instance, key))
171
+ if supported is not None:
172
+ if any(p.search(value) for p in supported):
173
+ return True
174
+ elif unsupported is not None:
175
+ if any(p.search(value) for p in unsupported):
176
+ return False
177
+ return True if not seen else (unsupported is not None)
178
+
179
+ @classmethod
180
+ def _find_blocking_key(
181
+ cls,
182
+ instance: object,
183
+ unsupported: Sequence[Pattern[str]] | None,
184
+ supported: Sequence[Pattern[str]] | None,
185
+ keys: Sequence[str],
186
+ ) -> str:
187
+ """Find which key is blocking the field."""
188
+ for key in keys:
189
+ if not hasattr(instance, key):
190
+ continue
191
+ value = str(getattr(instance, key))
192
+ if supported is not None:
193
+ if not any(p.search(value) for p in supported):
194
+ return key
195
+ elif unsupported is not None:
196
+ if any(p.search(value) for p in unsupported):
197
+ return key
198
+
199
+ return "<unknown>"
200
+
201
+ @classmethod
202
+ def _collect_all_mixin_configs(cls) -> list[GatedFieldMixinConfig]:
203
+ """Collect all mixin configurations from base classes."""
204
+ all_mixins = []
205
+ for base in cls.__bases__:
206
+ if hasattr(base, "_gated_field_mixins"):
207
+ all_mixins.extend(base._gated_field_mixins)
208
+ return all_mixins
209
+
210
+ @classmethod
211
+ def _create_combined_validator(cls, all_mixins: list[GatedFieldMixinConfig]) -> None:
212
+ """Create a combined validator that handles all fields."""
213
+
214
+ @model_validator(mode="after")
215
+ def combined_gated_field_validator(self):
216
+ """Validate all gated fields."""
217
+ for mixin_config in all_mixins:
218
+ field_name_local = mixin_config.field_name
219
+ current_value = getattr(self, field_name_local, None)
220
+ if not self._check_field_support_instance(mixin_config):
221
+ if current_value is not None:
222
+ blocking_key = self._find_blocking_key_instance(mixin_config)
223
+ value = getattr(self, blocking_key, "<unknown>")
224
+ raise ValueError(f"{field_name_local} is not supported for {blocking_key}: {value}")
225
+ elif current_value is None:
226
+ setattr(self, field_name_local, mixin_config.default_if_supported)
227
+
228
+ return self
229
+
230
+ cls._combined_gated_field_validator = combined_gated_field_validator
231
+
232
+ # Add helper methods
233
+ def _check_field_support_instance(self, mixin_config: GatedFieldMixinConfig) -> bool:
234
+ """Check if a specific field is supported based on its configuration and keys."""
235
+ return cls._check_field_support(self, mixin_config.unsupported, mixin_config.supported, mixin_config.keys)
236
+
237
+ def _find_blocking_key_instance(self, mixin_config: GatedFieldMixinConfig) -> str:
238
+ """Find which key is blocking the field."""
239
+ return cls._find_blocking_key(self, mixin_config.unsupported, mixin_config.supported, mixin_config.keys)
240
+
241
+ cls._check_field_support_instance = _check_field_support_instance
242
+ cls._find_blocking_key_instance = _find_blocking_key_instance
@@ -142,7 +142,7 @@ class IntermediateStepPayload(BaseModel):
142
142
  UUID: str = Field(default_factory=lambda: str(uuid.uuid4()))
143
143
 
144
144
  @property
145
- def event_category(self) -> IntermediateStepCategory: # pylint: disable=too-many-return-statements
145
+ def event_category(self) -> IntermediateStepCategory:
146
146
  match self.event_type:
147
147
  case IntermediateStepType.LLM_START:
148
148
  return IntermediateStepCategory.LLM
@@ -180,7 +180,7 @@ class IntermediateStepPayload(BaseModel):
180
180
  raise ValueError(f"Unknown event type: {self.event_type}")
181
181
 
182
182
  @property
183
- def event_state(self) -> IntermediateStepState: # pylint: disable=too-many-return-statements
183
+ def event_state(self) -> IntermediateStepState:
184
184
  match self.event_type:
185
185
  case IntermediateStepType.LLM_START:
186
186
  return IntermediateStepState.START
@@ -290,7 +290,7 @@ class IntermediateStep(BaseModel):
290
290
  return self.payload.usage_info
291
291
 
292
292
  @property
293
- def UUID(self) -> str: # pylint: disable=invalid-name
293
+ def UUID(self) -> str:
294
294
  return self.payload.UUID
295
295
 
296
296
  @property
@@ -0,0 +1,119 @@
1
+ # SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ from collections.abc import Sequence
17
+ from typing import Any
18
+ from typing import Generic
19
+ from typing import TypeVar
20
+
21
+ from optuna import Trial
22
+ from pydantic import BaseModel
23
+ from pydantic import ConfigDict
24
+ from pydantic import Field
25
+ from pydantic import model_validator
26
+
27
+ T = TypeVar("T", int, float, bool, str)
28
+
29
+
30
+ # --------------------------------------------------------------------- #
31
+ # 1. Hyper‑parameter metadata container #
32
+ # --------------------------------------------------------------------- #
33
+ class SearchSpace(BaseModel, Generic[T]):
34
+ values: Sequence[T] | None = None
35
+ low: T | None = None
36
+ high: T | None = None
37
+ log: bool = False # log scale
38
+ step: float | None = None
39
+ is_prompt: bool = False
40
+ prompt: str | None = None # prompt to optimize
41
+ prompt_purpose: str | None = None # purpose of the prompt
42
+
43
+ model_config = ConfigDict(protected_namespaces=(), extra="forbid")
44
+
45
+ @model_validator(mode="after")
46
+ def validate_search_space_parameters(self):
47
+ """Validate that either values is provided, or both high and low."""
48
+ if self.values is not None:
49
+ # If values is provided, we don't need high/low
50
+ if self.high is not None or self.low is not None:
51
+ raise ValueError("SearchSpace 'values' is mutually exclusive with 'high' and 'low'")
52
+ return self
53
+
54
+ return self
55
+
56
+ # Helper for Optuna Trials
57
+ def suggest(self, trial: Trial, name: str):
58
+ if self.is_prompt:
59
+ raise ValueError("Prompt optimization not currently supported using Optuna. "
60
+ "Use the genetic algorithm implementation instead.")
61
+ if self.values is not None:
62
+ return trial.suggest_categorical(name, self.values)
63
+ if isinstance(self.low, int):
64
+ return trial.suggest_int(name, self.low, self.high, log=self.log, step=self.step)
65
+ return trial.suggest_float(name, self.low, self.high, log=self.log, step=self.step)
66
+
67
+
68
+ def OptimizableField(
69
+ default: Any,
70
+ *,
71
+ space: SearchSpace | None = None,
72
+ merge_conflict: str = "overwrite",
73
+ **fld_kw,
74
+ ):
75
+ # 1. Pull out any user‑supplied extras (must be a dict)
76
+ user_extra = fld_kw.pop("json_schema_extra", None) or {}
77
+ if not isinstance(user_extra, dict):
78
+ raise TypeError("`json_schema_extra` must be a mapping.")
79
+
80
+ # 2. If the space is a prompt, ensure a concrete base prompt exists
81
+ if space is not None and getattr(space, "is_prompt", False):
82
+ if getattr(space, "prompt", None) is None:
83
+ if default is None:
84
+ raise ValueError("Prompt-optimized fields require a base prompt: provide a "
85
+ "non-None field default or set space.prompt.")
86
+ # Default prompt not provided in space; fall back to the field's default
87
+ space.prompt = default
88
+
89
+ # 3. Prepare our own metadata
90
+ ours = {"optimizable": True}
91
+ if space is not None:
92
+ ours["search_space"] = space
93
+
94
+ # 4. Merge with user extras according to merge_conflict policy
95
+ intersect = ours.keys() & user_extra.keys()
96
+ if intersect:
97
+ if merge_conflict == "error":
98
+ raise ValueError("`json_schema_extra` already contains reserved key(s): "
99
+ f"{', '.join(intersect)}")
100
+ if merge_conflict == "keep":
101
+ # remove the ones the user already set so we don't overwrite them
102
+ ours = {k: v for k, v in ours.items() if k not in intersect}
103
+
104
+ merged_extra = {**user_extra, **ours} # ours wins if 'overwrite'
105
+
106
+ # 5. Return a normal Pydantic Field with merged extras
107
+ return Field(default, json_schema_extra=merged_extra, **fld_kw)
108
+
109
+
110
+ class OptimizableMixin(BaseModel):
111
+ optimizable_params: list[str] = Field(default_factory=list,
112
+ description="List of parameters that can be optimized.",
113
+ exclude=True)
114
+
115
+ search_space: dict[str, SearchSpace] = Field(
116
+ default_factory=dict,
117
+ description="Optional search space overrides for optimizable parameters.",
118
+ exclude=True,
119
+ )
@@ -0,0 +1,149 @@
1
+ # SPDX-FileCopyrightText: Copyright (c) 2021-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ from pathlib import Path
17
+
18
+ from pydantic import BaseModel
19
+ from pydantic import Field
20
+
21
+
22
+ class OptimizerMetric(BaseModel):
23
+ """
24
+ Parameters used by the workflow optimizer to define a metric to optimize.
25
+ """
26
+ evaluator_name: str = Field(description="Name of the metric to optimize.")
27
+ direction: str = Field(description="Direction of the optimization. Can be 'maximize' or 'minimize'.")
28
+ weight: float = Field(description="Weight of the metric in the optimization process.", default=1.0)
29
+
30
+
31
+ class NumericOptimizationConfig(BaseModel):
32
+ """
33
+ Configuration for numeric/enum optimization (Optuna).
34
+ """
35
+ enabled: bool = Field(default=True, description="Enable numeric optimization")
36
+ n_trials: int = Field(description="Number of trials for numeric optimization.", default=20)
37
+
38
+
39
+ class PromptGAOptimizationConfig(BaseModel):
40
+ """
41
+ Configuration for prompt optimization using a Genetic Algorithm.
42
+ """
43
+ enabled: bool = Field(default=False, description="Enable GA-based prompt optimization")
44
+
45
+ # Prompt optimization function hooks
46
+ prompt_population_init_function: str | None = Field(
47
+ default=None,
48
+ description="Optional function name to initialize/mutate candidate prompts.",
49
+ )
50
+ prompt_recombination_function: str | None = Field(
51
+ default=None,
52
+ description="Optional function name to recombine two parent prompts into a child.",
53
+ )
54
+
55
+ # Genetic algorithm configuration
56
+ ga_population_size: int = Field(
57
+ description="Population size for genetic algorithm prompt optimization.",
58
+ default=24,
59
+ )
60
+ ga_generations: int = Field(
61
+ description="Number of generations to evolve in GA prompt optimization.",
62
+ default=15,
63
+ )
64
+ ga_offspring_size: int | None = Field(
65
+ description="Number of offspring to produce per generation. Defaults to population_size - elitism.",
66
+ default=None,
67
+ )
68
+ ga_crossover_rate: float = Field(
69
+ description="Probability of applying crossover during reproduction.",
70
+ default=0.8,
71
+ ge=0.0,
72
+ le=1.0,
73
+ )
74
+ ga_mutation_rate: float = Field(
75
+ description="Probability of mutating a child after crossover.",
76
+ default=0.3,
77
+ ge=0.0,
78
+ le=1.0,
79
+ )
80
+ ga_elitism: int = Field(
81
+ description="Number of top individuals carried over unchanged each generation.",
82
+ default=2,
83
+ )
84
+ ga_selection_method: str = Field(
85
+ description="Parent selection strategy: 'tournament' or 'roulette'.",
86
+ default="tournament",
87
+ )
88
+ ga_tournament_size: int = Field(
89
+ description="Tournament size when using tournament selection.",
90
+ default=3,
91
+ )
92
+ ga_parallel_evaluations: int = Field(
93
+ description="Max number of individuals to evaluate concurrently per generation.",
94
+ default=8,
95
+ )
96
+ ga_diversity_lambda: float = Field(
97
+ description="Strength of diversity penalty (0 disables). Penalizes identical/near-identical prompts.",
98
+ default=0.0,
99
+ ge=0.0,
100
+ )
101
+
102
+
103
+ class OptimizerConfig(BaseModel):
104
+ """
105
+ Parameters used by the workflow optimizer.
106
+ """
107
+ output_path: Path | None = Field(
108
+ default=None,
109
+ description="Path to the output directory where the results will be saved.",
110
+ )
111
+
112
+ eval_metrics: dict[str, OptimizerMetric] | None = Field(
113
+ description="List of evaluation metrics to optimize.",
114
+ default=None,
115
+ )
116
+
117
+ reps_per_param_set: int = Field(
118
+ default=3,
119
+ description="Number of repetitions per parameter set for the optimization.",
120
+ )
121
+
122
+ target: float | None = Field(
123
+ description=(
124
+ "Target value for the optimization. If set, the optimization will stop when this value is reached."),
125
+ default=None,
126
+ )
127
+
128
+ multi_objective_combination_mode: str = Field(
129
+ description="Method to combine multiple objectives into a single score.",
130
+ default="harmonic",
131
+ )
132
+
133
+ # Nested configs
134
+ numeric: NumericOptimizationConfig = NumericOptimizationConfig()
135
+ prompt: PromptGAOptimizationConfig = PromptGAOptimizationConfig()
136
+
137
+
138
+ class OptimizerRunConfig(BaseModel):
139
+ """
140
+ Parameters used for an Optimizer R=run
141
+ """
142
+ # Eval parameters
143
+
144
+ config_file: Path | BaseModel # allow for instantiated configs to be passed in
145
+ dataset: str | Path | None # dataset file path can be specified in the config file
146
+ result_json_path: str = "$"
147
+ endpoint: str | None = None # only used when running the workflow remotely
148
+ endpoint_timeout: int = 300
149
+ override: tuple[tuple[str, str], ...] = ()
nat/data_models/span.py CHANGED
@@ -128,10 +128,48 @@ class SpanStatus(BaseModel):
128
128
  message: str | None = Field(default=None, description="The status message of the span.")
129
129
 
130
130
 
131
+ def _generate_nonzero_trace_id() -> int:
132
+ """Generate a non-zero 128-bit trace ID."""
133
+ return uuid.uuid4().int
134
+
135
+
136
+ def _generate_nonzero_span_id() -> int:
137
+ """Generate a non-zero 64-bit span ID."""
138
+ return uuid.uuid4().int >> 64
139
+
140
+
131
141
  class SpanContext(BaseModel):
132
- trace_id: int = Field(default_factory=lambda: uuid.uuid4().int, description="The 128-bit trace ID of the span.")
133
- span_id: int = Field(default_factory=lambda: uuid.uuid4().int & ((1 << 64) - 1),
134
- description="The 64-bit span ID of the span.")
142
+ trace_id: int = Field(default_factory=_generate_nonzero_trace_id,
143
+ description="The OTel-syle 128-bit trace ID of the span.")
144
+ span_id: int = Field(default_factory=_generate_nonzero_span_id,
145
+ description="The OTel-syle 64-bit span ID of the span.")
146
+
147
+ @field_validator("trace_id", mode="before")
148
+ @classmethod
149
+ def _validate_trace_id(cls, v: int | str | None) -> int:
150
+ """Regenerate if trace_id is None; raise an exception if trace_id is invalid;"""
151
+ if isinstance(v, str):
152
+ v = uuid.UUID(v).int
153
+ if isinstance(v, type(None)):
154
+ v = _generate_nonzero_trace_id()
155
+ if v <= 0 or v >> 128:
156
+ raise ValueError(f"Invalid trace_id: must be a non-zero 128-bit integer, got {v}")
157
+ return v
158
+
159
+ @field_validator("span_id", mode="before")
160
+ @classmethod
161
+ def _validate_span_id(cls, v: int | str | None) -> int:
162
+ """Regenerate if span_id is None; raise an exception if span_id is invalid;"""
163
+ if isinstance(v, str):
164
+ try:
165
+ v = int(v, 16)
166
+ except ValueError:
167
+ raise ValueError(f"span_id unable to be parsed: {v}")
168
+ if isinstance(v, type(None)):
169
+ v = _generate_nonzero_span_id()
170
+ if v <= 0 or v >> 64:
171
+ raise ValueError(f"Invalid span_id: must be a non-zero 64-bit integer, got {v}")
172
+ return v
135
173
 
136
174
 
137
175
  class Span(BaseModel):