nvidia-nat 1.3.0.dev2__py3-none-any.whl → 1.3.0rc1__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.
- aiq/__init__.py +2 -2
- nat/agent/base.py +24 -15
- nat/agent/dual_node.py +9 -4
- nat/agent/prompt_optimizer/prompt.py +68 -0
- nat/agent/prompt_optimizer/register.py +149 -0
- nat/agent/react_agent/agent.py +79 -47
- nat/agent/react_agent/register.py +41 -21
- nat/agent/reasoning_agent/reasoning_agent.py +11 -9
- nat/agent/register.py +1 -1
- nat/agent/rewoo_agent/agent.py +326 -148
- nat/agent/rewoo_agent/prompt.py +19 -22
- nat/agent/rewoo_agent/register.py +46 -26
- nat/agent/tool_calling_agent/agent.py +84 -28
- nat/agent/tool_calling_agent/register.py +51 -28
- nat/authentication/api_key/api_key_auth_provider.py +2 -2
- nat/authentication/credential_validator/bearer_token_validator.py +557 -0
- nat/authentication/http_basic_auth/http_basic_auth_provider.py +1 -1
- nat/authentication/interfaces.py +5 -2
- nat/authentication/oauth2/oauth2_auth_code_flow_provider.py +40 -20
- nat/authentication/oauth2/oauth2_resource_server_config.py +124 -0
- nat/authentication/register.py +0 -1
- nat/builder/builder.py +56 -24
- nat/builder/component_utils.py +9 -5
- nat/builder/context.py +46 -11
- nat/builder/eval_builder.py +16 -11
- nat/builder/framework_enum.py +1 -0
- nat/builder/front_end.py +1 -1
- nat/builder/function.py +378 -8
- nat/builder/function_base.py +3 -3
- nat/builder/function_info.py +6 -8
- nat/builder/user_interaction_manager.py +2 -2
- nat/builder/workflow.py +13 -1
- nat/builder/workflow_builder.py +281 -76
- nat/cli/cli_utils/config_override.py +2 -2
- nat/cli/commands/evaluate.py +1 -1
- nat/cli/commands/info/info.py +16 -6
- nat/cli/commands/info/list_channels.py +1 -1
- nat/cli/commands/info/list_components.py +7 -8
- nat/cli/commands/mcp/__init__.py +14 -0
- nat/cli/commands/mcp/mcp.py +986 -0
- nat/cli/commands/object_store/__init__.py +14 -0
- nat/cli/commands/object_store/object_store.py +227 -0
- nat/cli/commands/optimize.py +90 -0
- nat/cli/commands/registry/publish.py +2 -2
- nat/cli/commands/registry/pull.py +2 -2
- nat/cli/commands/registry/remove.py +2 -2
- nat/cli/commands/registry/search.py +15 -17
- nat/cli/commands/start.py +16 -5
- nat/cli/commands/uninstall.py +1 -1
- nat/cli/commands/workflow/templates/config.yml.j2 +0 -1
- nat/cli/commands/workflow/templates/pyproject.toml.j2 +4 -1
- nat/cli/commands/workflow/templates/register.py.j2 +0 -1
- nat/cli/commands/workflow/workflow_commands.py +9 -13
- nat/cli/entrypoint.py +8 -10
- nat/cli/register_workflow.py +38 -4
- nat/cli/type_registry.py +75 -6
- nat/control_flow/__init__.py +0 -0
- nat/control_flow/register.py +20 -0
- nat/control_flow/router_agent/__init__.py +0 -0
- nat/control_flow/router_agent/agent.py +329 -0
- nat/control_flow/router_agent/prompt.py +48 -0
- nat/control_flow/router_agent/register.py +91 -0
- nat/control_flow/sequential_executor.py +166 -0
- nat/data_models/agent.py +34 -0
- nat/data_models/api_server.py +10 -10
- nat/data_models/authentication.py +23 -9
- nat/data_models/common.py +1 -1
- nat/data_models/component.py +2 -0
- nat/data_models/component_ref.py +11 -0
- nat/data_models/config.py +41 -17
- nat/data_models/dataset_handler.py +1 -1
- nat/data_models/discovery_metadata.py +4 -4
- nat/data_models/evaluate.py +4 -1
- nat/data_models/function.py +34 -0
- nat/data_models/function_dependencies.py +14 -6
- nat/data_models/gated_field_mixin.py +242 -0
- nat/data_models/intermediate_step.py +3 -3
- nat/data_models/optimizable.py +119 -0
- nat/data_models/optimizer.py +149 -0
- nat/data_models/swe_bench_model.py +1 -1
- nat/data_models/temperature_mixin.py +44 -0
- nat/data_models/thinking_mixin.py +86 -0
- nat/data_models/top_p_mixin.py +44 -0
- nat/embedder/nim_embedder.py +1 -1
- nat/embedder/openai_embedder.py +1 -1
- nat/embedder/register.py +0 -1
- nat/eval/config.py +3 -1
- nat/eval/dataset_handler/dataset_handler.py +71 -7
- nat/eval/evaluate.py +86 -31
- nat/eval/evaluator/base_evaluator.py +1 -1
- nat/eval/evaluator/evaluator_model.py +13 -0
- nat/eval/intermediate_step_adapter.py +1 -1
- nat/eval/rag_evaluator/evaluate.py +2 -2
- nat/eval/rag_evaluator/register.py +3 -3
- nat/eval/register.py +4 -1
- nat/eval/remote_workflow.py +3 -3
- nat/eval/runtime_evaluator/__init__.py +14 -0
- nat/eval/runtime_evaluator/evaluate.py +123 -0
- nat/eval/runtime_evaluator/register.py +100 -0
- nat/eval/swe_bench_evaluator/evaluate.py +6 -6
- nat/eval/trajectory_evaluator/evaluate.py +1 -1
- nat/eval/trajectory_evaluator/register.py +1 -1
- nat/eval/tunable_rag_evaluator/evaluate.py +4 -7
- nat/eval/utils/eval_trace_ctx.py +89 -0
- nat/eval/utils/weave_eval.py +18 -9
- nat/experimental/decorators/experimental_warning_decorator.py +27 -7
- nat/experimental/test_time_compute/functions/plan_select_execute_function.py +7 -3
- nat/experimental/test_time_compute/functions/ttc_tool_orchestration_function.py +3 -3
- nat/experimental/test_time_compute/functions/ttc_tool_wrapper_function.py +1 -1
- nat/experimental/test_time_compute/models/strategy_base.py +5 -4
- nat/experimental/test_time_compute/register.py +0 -1
- nat/experimental/test_time_compute/selection/llm_based_output_merging_selector.py +1 -3
- nat/front_ends/console/authentication_flow_handler.py +82 -30
- nat/front_ends/console/console_front_end_plugin.py +8 -5
- nat/front_ends/fastapi/auth_flow_handlers/websocket_flow_handler.py +52 -17
- nat/front_ends/fastapi/dask_client_mixin.py +65 -0
- nat/front_ends/fastapi/fastapi_front_end_config.py +36 -5
- nat/front_ends/fastapi/fastapi_front_end_controller.py +4 -4
- nat/front_ends/fastapi/fastapi_front_end_plugin.py +135 -4
- nat/front_ends/fastapi/fastapi_front_end_plugin_worker.py +481 -281
- nat/front_ends/fastapi/job_store.py +518 -99
- nat/front_ends/fastapi/main.py +11 -19
- nat/front_ends/fastapi/message_handler.py +13 -14
- nat/front_ends/fastapi/message_validator.py +17 -19
- nat/front_ends/fastapi/response_helpers.py +4 -4
- nat/front_ends/fastapi/step_adaptor.py +2 -2
- nat/front_ends/fastapi/utils.py +57 -0
- nat/front_ends/mcp/introspection_token_verifier.py +73 -0
- nat/front_ends/mcp/mcp_front_end_config.py +10 -1
- nat/front_ends/mcp/mcp_front_end_plugin.py +45 -13
- nat/front_ends/mcp/mcp_front_end_plugin_worker.py +116 -8
- nat/front_ends/mcp/tool_converter.py +44 -14
- nat/front_ends/register.py +0 -1
- nat/front_ends/simple_base/simple_front_end_plugin_base.py +3 -1
- nat/llm/aws_bedrock_llm.py +24 -12
- nat/llm/azure_openai_llm.py +13 -6
- nat/llm/litellm_llm.py +69 -0
- nat/llm/nim_llm.py +20 -8
- nat/llm/openai_llm.py +14 -6
- nat/llm/register.py +4 -1
- nat/llm/utils/env_config_value.py +2 -3
- nat/llm/utils/thinking.py +215 -0
- nat/meta/pypi.md +9 -9
- nat/object_store/register.py +0 -1
- nat/observability/exporter/base_exporter.py +3 -3
- nat/observability/exporter/file_exporter.py +1 -1
- nat/observability/exporter/processing_exporter.py +309 -81
- nat/observability/exporter/span_exporter.py +1 -1
- nat/observability/exporter_manager.py +7 -7
- nat/observability/mixin/file_mixin.py +7 -7
- nat/observability/mixin/redaction_config_mixin.py +42 -0
- nat/observability/mixin/tagging_config_mixin.py +62 -0
- nat/observability/mixin/type_introspection_mixin.py +420 -107
- nat/observability/processor/batching_processor.py +5 -7
- nat/observability/processor/falsy_batch_filter_processor.py +55 -0
- nat/observability/processor/processor.py +3 -0
- nat/observability/processor/processor_factory.py +70 -0
- nat/observability/processor/redaction/__init__.py +24 -0
- nat/observability/processor/redaction/contextual_redaction_processor.py +125 -0
- nat/observability/processor/redaction/contextual_span_redaction_processor.py +66 -0
- nat/observability/processor/redaction/redaction_processor.py +177 -0
- nat/observability/processor/redaction/span_header_redaction_processor.py +92 -0
- nat/observability/processor/span_tagging_processor.py +68 -0
- nat/observability/register.py +6 -4
- nat/profiler/calc/calc_runner.py +3 -4
- nat/profiler/callbacks/agno_callback_handler.py +1 -1
- nat/profiler/callbacks/langchain_callback_handler.py +6 -6
- nat/profiler/callbacks/llama_index_callback_handler.py +3 -3
- nat/profiler/callbacks/semantic_kernel_callback_handler.py +3 -3
- nat/profiler/data_frame_row.py +1 -1
- nat/profiler/decorators/framework_wrapper.py +62 -13
- nat/profiler/decorators/function_tracking.py +160 -3
- nat/profiler/forecasting/models/forecasting_base_model.py +3 -1
- nat/profiler/inference_optimization/bottleneck_analysis/simple_stack_analysis.py +1 -1
- nat/profiler/inference_optimization/data_models.py +3 -3
- nat/profiler/inference_optimization/experimental/prefix_span_analysis.py +7 -8
- nat/profiler/inference_optimization/token_uniqueness.py +1 -1
- nat/profiler/parameter_optimization/__init__.py +0 -0
- nat/profiler/parameter_optimization/optimizable_utils.py +93 -0
- nat/profiler/parameter_optimization/optimizer_runtime.py +67 -0
- nat/profiler/parameter_optimization/parameter_optimizer.py +153 -0
- nat/profiler/parameter_optimization/parameter_selection.py +107 -0
- nat/profiler/parameter_optimization/pareto_visualizer.py +380 -0
- nat/profiler/parameter_optimization/prompt_optimizer.py +384 -0
- nat/profiler/parameter_optimization/update_helpers.py +66 -0
- nat/profiler/profile_runner.py +14 -9
- nat/profiler/utils.py +4 -2
- nat/registry_handlers/local/local_handler.py +2 -2
- nat/registry_handlers/package_utils.py +1 -2
- nat/registry_handlers/pypi/pypi_handler.py +23 -26
- nat/registry_handlers/register.py +3 -4
- nat/registry_handlers/rest/rest_handler.py +12 -13
- nat/retriever/milvus/retriever.py +2 -2
- nat/retriever/nemo_retriever/retriever.py +1 -1
- nat/retriever/register.py +0 -1
- nat/runtime/loader.py +2 -2
- nat/runtime/runner.py +3 -2
- nat/runtime/session.py +43 -8
- nat/settings/global_settings.py +16 -5
- nat/tool/chat_completion.py +5 -2
- nat/tool/code_execution/local_sandbox/local_sandbox_server.py +3 -3
- nat/tool/datetime_tools.py +49 -9
- nat/tool/document_search.py +2 -2
- nat/tool/github_tools.py +450 -0
- nat/tool/nvidia_rag.py +1 -1
- nat/tool/register.py +2 -9
- nat/tool/retriever.py +3 -2
- nat/utils/callable_utils.py +70 -0
- nat/utils/data_models/schema_validator.py +3 -3
- nat/utils/exception_handlers/automatic_retries.py +104 -51
- nat/utils/exception_handlers/schemas.py +1 -1
- nat/utils/io/yaml_tools.py +2 -2
- nat/utils/log_levels.py +25 -0
- nat/utils/reactive/base/observable_base.py +2 -2
- nat/utils/reactive/base/observer_base.py +1 -1
- nat/utils/reactive/observable.py +2 -2
- nat/utils/reactive/observer.py +4 -4
- nat/utils/reactive/subscription.py +1 -1
- nat/utils/settings/global_settings.py +6 -8
- nat/utils/type_converter.py +4 -3
- nat/utils/type_utils.py +9 -5
- {nvidia_nat-1.3.0.dev2.dist-info → nvidia_nat-1.3.0rc1.dist-info}/METADATA +42 -16
- {nvidia_nat-1.3.0.dev2.dist-info → nvidia_nat-1.3.0rc1.dist-info}/RECORD +230 -189
- {nvidia_nat-1.3.0.dev2.dist-info → nvidia_nat-1.3.0rc1.dist-info}/entry_points.txt +1 -0
- nat/cli/commands/info/list_mcp.py +0 -304
- nat/tool/github_tools/create_github_commit.py +0 -133
- nat/tool/github_tools/create_github_issue.py +0 -87
- nat/tool/github_tools/create_github_pr.py +0 -106
- nat/tool/github_tools/get_github_file.py +0 -106
- nat/tool/github_tools/get_github_issue.py +0 -166
- nat/tool/github_tools/get_github_pr.py +0 -256
- nat/tool/github_tools/update_github_issue.py +0 -100
- nat/tool/mcp/exceptions.py +0 -142
- nat/tool/mcp/mcp_client.py +0 -255
- nat/tool/mcp/mcp_tool.py +0 -96
- nat/utils/exception_handlers/mcp.py +0 -211
- /nat/{tool/github_tools → agent/prompt_optimizer}/__init__.py +0 -0
- /nat/{tool/mcp → authentication/credential_validator}/__init__.py +0 -0
- {nvidia_nat-1.3.0.dev2.dist-info → nvidia_nat-1.3.0rc1.dist-info}/WHEEL +0 -0
- {nvidia_nat-1.3.0.dev2.dist-info → nvidia_nat-1.3.0rc1.dist-info}/licenses/LICENSE-3rd-party.txt +0 -0
- {nvidia_nat-1.3.0.dev2.dist-info → nvidia_nat-1.3.0rc1.dist-info}/licenses/LICENSE.md +0 -0
- {nvidia_nat-1.3.0.dev2.dist-info → nvidia_nat-1.3.0rc1.dist-info}/top_level.txt +0 -0
|
@@ -177,7 +177,7 @@ class DiscoveryMetadata(BaseModel):
|
|
|
177
177
|
logger.warning("Package metadata not found for %s", distro_name)
|
|
178
178
|
version = ""
|
|
179
179
|
except Exception as e:
|
|
180
|
-
logger.exception("Encountered issue extracting module metadata for %s: %s", config_type, e
|
|
180
|
+
logger.exception("Encountered issue extracting module metadata for %s: %s", config_type, e)
|
|
181
181
|
return DiscoveryMetadata(status=DiscoveryStatusEnum.FAILURE)
|
|
182
182
|
|
|
183
183
|
description = generate_config_type_docs(config_type=config_type)
|
|
@@ -217,7 +217,7 @@ class DiscoveryMetadata(BaseModel):
|
|
|
217
217
|
logger.warning("Package metadata not found for %s", distro_name)
|
|
218
218
|
version = ""
|
|
219
219
|
except Exception as e:
|
|
220
|
-
logger.exception("Encountered issue extracting module metadata for %s: %s", fn, e
|
|
220
|
+
logger.exception("Encountered issue extracting module metadata for %s: %s", fn, e)
|
|
221
221
|
return DiscoveryMetadata(status=DiscoveryStatusEnum.FAILURE)
|
|
222
222
|
|
|
223
223
|
if isinstance(wrapper_type, LLMFrameworkEnum):
|
|
@@ -252,7 +252,7 @@ class DiscoveryMetadata(BaseModel):
|
|
|
252
252
|
description = ""
|
|
253
253
|
package_version = package_version or ""
|
|
254
254
|
except Exception as e:
|
|
255
|
-
logger.exception("Encountered issue extracting module metadata for %s: %s", package_name, e
|
|
255
|
+
logger.exception("Encountered issue extracting module metadata for %s: %s", package_name, e)
|
|
256
256
|
return DiscoveryMetadata(status=DiscoveryStatusEnum.FAILURE)
|
|
257
257
|
|
|
258
258
|
return DiscoveryMetadata(package=package_name,
|
|
@@ -290,7 +290,7 @@ class DiscoveryMetadata(BaseModel):
|
|
|
290
290
|
logger.warning("Package metadata not found for %s", distro_name)
|
|
291
291
|
version = ""
|
|
292
292
|
except Exception as e:
|
|
293
|
-
logger.exception("Encountered issue extracting module metadata for %s: %s", config_type, e
|
|
293
|
+
logger.exception("Encountered issue extracting module metadata for %s: %s", config_type, e)
|
|
294
294
|
return DiscoveryMetadata(status=DiscoveryStatusEnum.FAILURE)
|
|
295
295
|
|
|
296
296
|
wrapper_type = wrapper_type.value if isinstance(wrapper_type, LLMFrameworkEnum) else wrapper_type
|
nat/data_models/evaluate.py
CHANGED
|
@@ -57,6 +57,9 @@ class EvalOutputConfig(BaseModel):
|
|
|
57
57
|
dir: Path = Path("./.tmp/nat/examples/default/")
|
|
58
58
|
# S3 prefix for the workflow and evaluation results
|
|
59
59
|
remote_dir: str | None = None
|
|
60
|
+
# Custom function to pre-evaluation process the eval input
|
|
61
|
+
# Format: "module.path.function_name"
|
|
62
|
+
custom_pre_eval_process_function: str | None = None
|
|
60
63
|
# Custom scripts to run after the workflow and evaluation results are saved
|
|
61
64
|
custom_scripts: dict[str, EvalCustomScriptConfig] = {}
|
|
62
65
|
# S3 config for uploading the contents of the output directory
|
|
@@ -108,7 +111,7 @@ class EvalConfig(BaseModel):
|
|
|
108
111
|
@classmethod
|
|
109
112
|
def rebuild_annotations(cls):
|
|
110
113
|
|
|
111
|
-
from nat.cli.type_registry import GlobalTypeRegistry
|
|
114
|
+
from nat.cli.type_registry import GlobalTypeRegistry
|
|
112
115
|
|
|
113
116
|
type_registry = GlobalTypeRegistry.get()
|
|
114
117
|
|
nat/data_models/function.py
CHANGED
|
@@ -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)
|
|
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)
|
|
68
|
+
self.llms.add(llm)
|
|
61
69
|
|
|
62
70
|
def add_embedder(self, embedder: str):
|
|
63
|
-
self.embedders.add(embedder)
|
|
71
|
+
self.embedders.add(embedder)
|
|
64
72
|
|
|
65
73
|
def add_memory_client(self, memory_client: str):
|
|
66
|
-
self.memory_clients.add(memory_client)
|
|
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)
|
|
77
|
+
self.object_stores.add(object_store)
|
|
70
78
|
|
|
71
79
|
def add_retriever(self, retriever: str):
|
|
72
|
-
self.retrievers.add(retriever)
|
|
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:
|
|
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:
|
|
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:
|
|
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
|
+
)
|