aiqtoolkit 1.2.0a20250706__py3-none-any.whl → 1.2.0a20250730__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.

Potentially problematic release.


This version of aiqtoolkit might be problematic. Click here for more details.

Files changed (197) hide show
  1. aiq/agent/base.py +171 -8
  2. aiq/agent/dual_node.py +1 -1
  3. aiq/agent/react_agent/agent.py +113 -113
  4. aiq/agent/react_agent/register.py +31 -14
  5. aiq/agent/rewoo_agent/agent.py +36 -35
  6. aiq/agent/rewoo_agent/register.py +2 -2
  7. aiq/agent/tool_calling_agent/agent.py +3 -7
  8. aiq/authentication/__init__.py +14 -0
  9. aiq/authentication/api_key/__init__.py +14 -0
  10. aiq/authentication/api_key/api_key_auth_provider.py +92 -0
  11. aiq/authentication/api_key/api_key_auth_provider_config.py +124 -0
  12. aiq/authentication/api_key/register.py +26 -0
  13. aiq/authentication/exceptions/__init__.py +14 -0
  14. aiq/authentication/exceptions/api_key_exceptions.py +38 -0
  15. aiq/authentication/exceptions/auth_code_grant_exceptions.py +86 -0
  16. aiq/authentication/exceptions/call_back_exceptions.py +38 -0
  17. aiq/authentication/exceptions/request_exceptions.py +54 -0
  18. aiq/authentication/http_basic_auth/__init__.py +0 -0
  19. aiq/authentication/http_basic_auth/http_basic_auth_provider.py +81 -0
  20. aiq/authentication/http_basic_auth/register.py +30 -0
  21. aiq/authentication/interfaces.py +93 -0
  22. aiq/authentication/oauth2/__init__.py +14 -0
  23. aiq/authentication/oauth2/oauth2_auth_code_flow_provider.py +107 -0
  24. aiq/authentication/oauth2/oauth2_auth_code_flow_provider_config.py +39 -0
  25. aiq/authentication/oauth2/register.py +25 -0
  26. aiq/authentication/register.py +21 -0
  27. aiq/builder/builder.py +64 -2
  28. aiq/builder/component_utils.py +16 -3
  29. aiq/builder/context.py +26 -0
  30. aiq/builder/eval_builder.py +43 -2
  31. aiq/builder/function.py +32 -4
  32. aiq/builder/function_base.py +1 -1
  33. aiq/builder/intermediate_step_manager.py +6 -8
  34. aiq/builder/user_interaction_manager.py +3 -0
  35. aiq/builder/workflow.py +23 -18
  36. aiq/builder/workflow_builder.py +420 -73
  37. aiq/cli/commands/info/list_mcp.py +103 -16
  38. aiq/cli/commands/sizing/__init__.py +14 -0
  39. aiq/cli/commands/sizing/calc.py +294 -0
  40. aiq/cli/commands/sizing/sizing.py +27 -0
  41. aiq/cli/commands/start.py +1 -0
  42. aiq/cli/entrypoint.py +2 -0
  43. aiq/cli/register_workflow.py +80 -0
  44. aiq/cli/type_registry.py +151 -30
  45. aiq/data_models/api_server.py +117 -11
  46. aiq/data_models/authentication.py +231 -0
  47. aiq/data_models/common.py +35 -7
  48. aiq/data_models/component.py +17 -9
  49. aiq/data_models/component_ref.py +33 -0
  50. aiq/data_models/config.py +60 -3
  51. aiq/data_models/embedder.py +1 -0
  52. aiq/data_models/function_dependencies.py +8 -0
  53. aiq/data_models/interactive.py +10 -1
  54. aiq/data_models/intermediate_step.py +15 -5
  55. aiq/data_models/its_strategy.py +30 -0
  56. aiq/data_models/llm.py +1 -0
  57. aiq/data_models/memory.py +1 -0
  58. aiq/data_models/object_store.py +44 -0
  59. aiq/data_models/retry_mixin.py +35 -0
  60. aiq/data_models/span.py +187 -0
  61. aiq/data_models/telemetry_exporter.py +2 -2
  62. aiq/embedder/nim_embedder.py +2 -1
  63. aiq/embedder/openai_embedder.py +2 -1
  64. aiq/eval/config.py +19 -1
  65. aiq/eval/dataset_handler/dataset_handler.py +75 -1
  66. aiq/eval/evaluate.py +53 -10
  67. aiq/eval/rag_evaluator/evaluate.py +23 -12
  68. aiq/eval/remote_workflow.py +7 -2
  69. aiq/eval/runners/__init__.py +14 -0
  70. aiq/eval/runners/config.py +39 -0
  71. aiq/eval/runners/multi_eval_runner.py +54 -0
  72. aiq/eval/usage_stats.py +6 -0
  73. aiq/eval/utils/weave_eval.py +5 -1
  74. aiq/experimental/__init__.py +0 -0
  75. aiq/experimental/decorators/__init__.py +0 -0
  76. aiq/experimental/decorators/experimental_warning_decorator.py +130 -0
  77. aiq/experimental/inference_time_scaling/__init__.py +0 -0
  78. aiq/experimental/inference_time_scaling/editing/__init__.py +0 -0
  79. aiq/experimental/inference_time_scaling/editing/iterative_plan_refinement_editor.py +147 -0
  80. aiq/experimental/inference_time_scaling/editing/llm_as_a_judge_editor.py +204 -0
  81. aiq/experimental/inference_time_scaling/editing/motivation_aware_summarization.py +107 -0
  82. aiq/experimental/inference_time_scaling/functions/__init__.py +0 -0
  83. aiq/experimental/inference_time_scaling/functions/execute_score_select_function.py +105 -0
  84. aiq/experimental/inference_time_scaling/functions/its_tool_orchestration_function.py +205 -0
  85. aiq/experimental/inference_time_scaling/functions/its_tool_wrapper_function.py +146 -0
  86. aiq/experimental/inference_time_scaling/functions/plan_select_execute_function.py +224 -0
  87. aiq/experimental/inference_time_scaling/models/__init__.py +0 -0
  88. aiq/experimental/inference_time_scaling/models/editor_config.py +132 -0
  89. aiq/experimental/inference_time_scaling/models/its_item.py +48 -0
  90. aiq/experimental/inference_time_scaling/models/scoring_config.py +112 -0
  91. aiq/experimental/inference_time_scaling/models/search_config.py +120 -0
  92. aiq/experimental/inference_time_scaling/models/selection_config.py +154 -0
  93. aiq/experimental/inference_time_scaling/models/stage_enums.py +43 -0
  94. aiq/experimental/inference_time_scaling/models/strategy_base.py +66 -0
  95. aiq/experimental/inference_time_scaling/models/tool_use_config.py +41 -0
  96. aiq/experimental/inference_time_scaling/register.py +36 -0
  97. aiq/experimental/inference_time_scaling/scoring/__init__.py +0 -0
  98. aiq/experimental/inference_time_scaling/scoring/llm_based_agent_scorer.py +168 -0
  99. aiq/experimental/inference_time_scaling/scoring/llm_based_plan_scorer.py +168 -0
  100. aiq/experimental/inference_time_scaling/scoring/motivation_aware_scorer.py +111 -0
  101. aiq/experimental/inference_time_scaling/search/__init__.py +0 -0
  102. aiq/experimental/inference_time_scaling/search/multi_llm_planner.py +128 -0
  103. aiq/experimental/inference_time_scaling/search/multi_query_retrieval_search.py +122 -0
  104. aiq/experimental/inference_time_scaling/search/single_shot_multi_plan_planner.py +128 -0
  105. aiq/experimental/inference_time_scaling/selection/__init__.py +0 -0
  106. aiq/experimental/inference_time_scaling/selection/best_of_n_selector.py +63 -0
  107. aiq/experimental/inference_time_scaling/selection/llm_based_agent_output_selector.py +131 -0
  108. aiq/experimental/inference_time_scaling/selection/llm_based_output_merging_selector.py +159 -0
  109. aiq/experimental/inference_time_scaling/selection/llm_based_plan_selector.py +128 -0
  110. aiq/experimental/inference_time_scaling/selection/threshold_selector.py +58 -0
  111. aiq/front_ends/console/authentication_flow_handler.py +233 -0
  112. aiq/front_ends/console/console_front_end_plugin.py +11 -2
  113. aiq/front_ends/fastapi/auth_flow_handlers/__init__.py +0 -0
  114. aiq/front_ends/fastapi/auth_flow_handlers/http_flow_handler.py +27 -0
  115. aiq/front_ends/fastapi/auth_flow_handlers/websocket_flow_handler.py +107 -0
  116. aiq/front_ends/fastapi/fastapi_front_end_config.py +20 -0
  117. aiq/front_ends/fastapi/fastapi_front_end_controller.py +68 -0
  118. aiq/front_ends/fastapi/fastapi_front_end_plugin.py +14 -1
  119. aiq/front_ends/fastapi/fastapi_front_end_plugin_worker.py +353 -31
  120. aiq/front_ends/fastapi/html_snippets/__init__.py +14 -0
  121. aiq/front_ends/fastapi/html_snippets/auth_code_grant_success.py +35 -0
  122. aiq/front_ends/fastapi/main.py +2 -0
  123. aiq/front_ends/fastapi/message_handler.py +102 -84
  124. aiq/front_ends/fastapi/step_adaptor.py +2 -1
  125. aiq/llm/aws_bedrock_llm.py +2 -1
  126. aiq/llm/nim_llm.py +2 -1
  127. aiq/llm/openai_llm.py +2 -1
  128. aiq/object_store/__init__.py +20 -0
  129. aiq/object_store/in_memory_object_store.py +74 -0
  130. aiq/object_store/interfaces.py +84 -0
  131. aiq/object_store/models.py +36 -0
  132. aiq/object_store/register.py +20 -0
  133. aiq/observability/__init__.py +14 -0
  134. aiq/observability/exporter/__init__.py +14 -0
  135. aiq/observability/exporter/base_exporter.py +449 -0
  136. aiq/observability/exporter/exporter.py +78 -0
  137. aiq/observability/exporter/file_exporter.py +33 -0
  138. aiq/observability/exporter/processing_exporter.py +269 -0
  139. aiq/observability/exporter/raw_exporter.py +52 -0
  140. aiq/observability/exporter/span_exporter.py +264 -0
  141. aiq/observability/exporter_manager.py +335 -0
  142. aiq/observability/mixin/__init__.py +14 -0
  143. aiq/observability/mixin/batch_config_mixin.py +26 -0
  144. aiq/observability/mixin/collector_config_mixin.py +23 -0
  145. aiq/observability/mixin/file_mixin.py +288 -0
  146. aiq/observability/mixin/file_mode.py +23 -0
  147. aiq/observability/mixin/resource_conflict_mixin.py +134 -0
  148. aiq/observability/mixin/serialize_mixin.py +61 -0
  149. aiq/observability/mixin/type_introspection_mixin.py +183 -0
  150. aiq/observability/processor/__init__.py +14 -0
  151. aiq/observability/processor/batching_processor.py +316 -0
  152. aiq/observability/processor/intermediate_step_serializer.py +28 -0
  153. aiq/observability/processor/processor.py +68 -0
  154. aiq/observability/register.py +32 -116
  155. aiq/observability/utils/__init__.py +14 -0
  156. aiq/observability/utils/dict_utils.py +236 -0
  157. aiq/observability/utils/time_utils.py +31 -0
  158. aiq/profiler/calc/__init__.py +14 -0
  159. aiq/profiler/calc/calc_runner.py +623 -0
  160. aiq/profiler/calc/calculations.py +288 -0
  161. aiq/profiler/calc/data_models.py +176 -0
  162. aiq/profiler/calc/plot.py +345 -0
  163. aiq/profiler/data_models.py +2 -0
  164. aiq/profiler/profile_runner.py +16 -13
  165. aiq/runtime/loader.py +8 -2
  166. aiq/runtime/runner.py +23 -9
  167. aiq/runtime/session.py +16 -5
  168. aiq/tool/chat_completion.py +74 -0
  169. aiq/tool/code_execution/README.md +152 -0
  170. aiq/tool/code_execution/code_sandbox.py +151 -72
  171. aiq/tool/code_execution/local_sandbox/.gitignore +1 -0
  172. aiq/tool/code_execution/local_sandbox/local_sandbox_server.py +139 -24
  173. aiq/tool/code_execution/local_sandbox/sandbox.requirements.txt +3 -1
  174. aiq/tool/code_execution/local_sandbox/start_local_sandbox.sh +27 -2
  175. aiq/tool/code_execution/register.py +7 -3
  176. aiq/tool/code_execution/test_code_execution_sandbox.py +414 -0
  177. aiq/tool/mcp/exceptions.py +142 -0
  178. aiq/tool/mcp/mcp_client.py +17 -3
  179. aiq/tool/mcp/mcp_tool.py +1 -1
  180. aiq/tool/register.py +1 -0
  181. aiq/tool/server_tools.py +2 -2
  182. aiq/utils/exception_handlers/automatic_retries.py +289 -0
  183. aiq/utils/exception_handlers/mcp.py +211 -0
  184. aiq/utils/io/model_processing.py +28 -0
  185. aiq/utils/log_utils.py +37 -0
  186. aiq/utils/string_utils.py +38 -0
  187. aiq/utils/type_converter.py +18 -2
  188. aiq/utils/type_utils.py +87 -0
  189. {aiqtoolkit-1.2.0a20250706.dist-info → aiqtoolkit-1.2.0a20250730.dist-info}/METADATA +37 -9
  190. {aiqtoolkit-1.2.0a20250706.dist-info → aiqtoolkit-1.2.0a20250730.dist-info}/RECORD +195 -80
  191. {aiqtoolkit-1.2.0a20250706.dist-info → aiqtoolkit-1.2.0a20250730.dist-info}/entry_points.txt +3 -0
  192. aiq/front_ends/fastapi/websocket.py +0 -153
  193. aiq/observability/async_otel_listener.py +0 -470
  194. {aiqtoolkit-1.2.0a20250706.dist-info → aiqtoolkit-1.2.0a20250730.dist-info}/WHEEL +0 -0
  195. {aiqtoolkit-1.2.0a20250706.dist-info → aiqtoolkit-1.2.0a20250730.dist-info}/licenses/LICENSE-3rd-party.txt +0 -0
  196. {aiqtoolkit-1.2.0a20250706.dist-info → aiqtoolkit-1.2.0a20250730.dist-info}/licenses/LICENSE.md +0 -0
  197. {aiqtoolkit-1.2.0a20250706.dist-info → aiqtoolkit-1.2.0a20250730.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,146 @@
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
+ import logging
17
+
18
+ from pydantic import BaseModel
19
+ from pydantic import Field
20
+
21
+ from aiq.builder.builder import Builder
22
+ from aiq.builder.framework_enum import LLMFrameworkEnum
23
+ from aiq.builder.function import Function
24
+ from aiq.builder.function_info import FunctionInfo
25
+ from aiq.cli.register_workflow import register_function
26
+ from aiq.data_models.component_ref import FunctionRef
27
+ from aiq.data_models.component_ref import LLMRef
28
+ from aiq.data_models.function import FunctionBaseConfig
29
+ from aiq.utils.string_utils import convert_to_str
30
+
31
+ logger = logging.getLogger(__name__)
32
+
33
+
34
+ class ITSToolWrapperFunctionConfig(FunctionBaseConfig, name="its_tool_wrapper"):
35
+ """
36
+ Configuration for the ITSToolWrapperFunction, which is used to wrap a function that will be executed
37
+ in the inference time scaling pipeline.
38
+
39
+ This function is responsible for turning an 'objective' or description for the tool into tool input.
40
+
41
+ NOTE: Only supports LLMs with structured output.
42
+ """
43
+
44
+ augmented_fn: FunctionRef = Field(description="The name of the function to reason on.")
45
+
46
+ input_llm: LLMRef = Field(description="The LLM that will generate input to the function.")
47
+ verbose: bool = Field(default=False, description="Whether to log detailed information.")
48
+
49
+ downstream_template: str = Field(
50
+ description="The template for the input LLM to generate structured input to the function.",
51
+ default=("You are highly sophisticated generalist AI assistant. Your objective is to act as a"
52
+ " conduit between a user's task for a function and the function itself. You will be given a general "
53
+ "description of the task, or pseudo input for a function. You will also be provided with description "
54
+ "of the function, its input schema, and the output schema. Your task is to generate structured input "
55
+ "to the function based on the description of the task and the function's input schema. If you do not "
56
+ "have enough information to generate structured input, you should respond with 'NOT ENOUGH "
57
+ "INFORMATION'. \n\n The description of the function is: {function_description}\n\n"
58
+ "The input schema of the function is: {input_schema}\n\n"
59
+ "The output schema of the function is: {output_schema}\n\n"
60
+ "The description of the task is: {task_description}\n\n"
61
+ "The structured input to the function is: "))
62
+
63
+ tool_description: str | None = Field(description="The description of the tool to be used for the function.",
64
+ default=None)
65
+
66
+
67
+ @register_function(config_type=ITSToolWrapperFunctionConfig, framework_wrappers=[LLMFrameworkEnum.LANGCHAIN])
68
+ async def register_its_tool_wrapper_function(
69
+ config: ITSToolWrapperFunctionConfig,
70
+ builder: Builder,
71
+ ):
72
+ """
73
+ Register the ITSToolWrapperFunction with the provided builder and configuration.
74
+ """
75
+
76
+ try:
77
+ from langchain_core.language_models import BaseChatModel
78
+ from langchain_core.prompts import PromptTemplate
79
+ except ImportError:
80
+ raise ImportError("langchain-core is not installed. Please install it to use SingleShotMultiPlanPlanner.\n"
81
+ "This error can be resolved by installing aiqtoolkit-langchain.")
82
+
83
+ augmented_function: Function = builder.get_function(config.augmented_fn)
84
+ input_llm: BaseChatModel = await builder.get_llm(config.input_llm, wrapper_type=LLMFrameworkEnum.LANGCHAIN)
85
+
86
+ if not augmented_function.has_single_output:
87
+ raise ValueError("ITSToolWrapperFunction only supports functions with a single output.")
88
+
89
+ if not augmented_function.has_single_output:
90
+ raise ValueError("ITSToolWrapperFunction only supports functions with a single output.")
91
+
92
+ if augmented_function.description and augmented_function.description != "":
93
+ augmented_function_desc = augmented_function.description
94
+ else:
95
+ if not config.tool_description:
96
+ raise ValueError(f"Function {config.augmented_fn} does not have a description. Cannot augment "
97
+ f"function without a description and without a tool description.")
98
+
99
+ augmented_function_desc = config.tool_description
100
+
101
+ fn_input_schema: BaseModel = augmented_function.input_schema
102
+ fn_output_schema: BaseModel = augmented_function.single_output_schema
103
+
104
+ runnable_llm = input_llm.with_structured_output(schema=fn_input_schema)
105
+
106
+ template = PromptTemplate(
107
+ template=config.downstream_template,
108
+ input_variables=["function_description", "input_schema", "output_schema", "task_description"],
109
+ validate_template=True)
110
+
111
+ function_description = (f"\nDescription: {augmented_function_desc}\n" +
112
+ "\n Input should be a thorough description with all relevant information on what "
113
+ f"the tool should do. The tool requires information about "
114
+ f"{fn_input_schema.model_fields}")
115
+
116
+ async def single_inner(input_message: str) -> fn_output_schema:
117
+ """
118
+ Inner function to handle the streaming output of the ITSToolWrapperFunction.
119
+ It generates structured input for the augmented function based on the input message.
120
+ """
121
+
122
+ prompt = await template.ainvoke(
123
+ input={
124
+ "function_description": augmented_function_desc,
125
+ "input_schema": fn_input_schema,
126
+ "output_schema": fn_output_schema,
127
+ "task_description": input_message
128
+ })
129
+
130
+ prompt = prompt.to_string()
131
+
132
+ if config.verbose:
133
+ logger.info("ITSToolWrapperFunction: Generated prompt: %s", prompt)
134
+
135
+ llm_parsed = await runnable_llm.ainvoke(prompt)
136
+
137
+ if not llm_parsed:
138
+ logger.warning("ITSToolWrapperFunction: LLM parsing error")
139
+ return "Not enough information"
140
+
141
+ # Call the augmented function with the structured input
142
+ result = await augmented_function.acall_invoke(llm_parsed)
143
+
144
+ return result
145
+
146
+ yield FunctionInfo.from_fn(fn=single_inner, description=function_description, converters=[convert_to_str])
@@ -0,0 +1,224 @@
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
+ import logging
17
+ from collections.abc import AsyncGenerator
18
+
19
+ from pydantic import Field
20
+
21
+ from aiq.builder.builder import Builder
22
+ from aiq.builder.framework_enum import LLMFrameworkEnum
23
+ from aiq.builder.function_info import FunctionInfo
24
+ from aiq.cli.register_workflow import register_function
25
+ from aiq.data_models.api_server import AIQChatRequest
26
+ from aiq.data_models.component_ref import FunctionRef
27
+ from aiq.data_models.component_ref import ITSStrategyRef
28
+ from aiq.data_models.function import FunctionBaseConfig
29
+ from aiq.experimental.inference_time_scaling.models.its_item import ITSItem
30
+ from aiq.experimental.inference_time_scaling.models.stage_enums import PipelineTypeEnum
31
+ from aiq.experimental.inference_time_scaling.models.stage_enums import StageTypeEnum
32
+
33
+ logger = logging.getLogger(__name__)
34
+
35
+
36
+ class PlanSelectExecuteFunctionConfig(FunctionBaseConfig, name="plan_select_execute_function"):
37
+ """
38
+ Defines an aiqtoolkit function that performs reasoning on the input data.
39
+ Output is passed to the next function in the workflow.
40
+
41
+ Designed to be used with an InterceptingFunction.
42
+ """
43
+
44
+ augmented_fn: FunctionRef = Field(description="The name of the function to reason on.")
45
+
46
+ planner: ITSStrategyRef = Field(description="The configuration for the planner.")
47
+ editor: ITSStrategyRef | None = Field(description="The configuration for the editor.", default=None)
48
+ scorer: ITSStrategyRef | None = Field(description="The configuration for the scorer.", default=None)
49
+ selector: ITSStrategyRef = Field(description="The configuration for the selector.")
50
+
51
+ verbose: bool = Field(default=False, description="Whether to log detailed information.")
52
+ agent_context_prompt_template: str = Field(
53
+ description="The template for the agent context prompt. This prompt is used to provide context about the agent",
54
+ default=("\nThe agent system has the following description:\n"
55
+ "{description}\n"
56
+ "And has access to the following tools with functionality:\n"
57
+ "{tools}\n\n"))
58
+
59
+ downstream_template: str = Field(
60
+ description=("The template for the downstream prompt. This prompt is used to provide the reasoning output to"
61
+ " the executing agent"),
62
+ default=("Answer the following question based on message history: {input_text}"
63
+ "\n\nHere is a plan for execution that you could use to guide you if you wanted to:"
64
+ "\n\n{reasoning_output}"
65
+ "\n\nNOTE: Remember to follow your guidance on how to format output, etc."
66
+ "\n\n You must respond with the answer to the original question directly to the user."))
67
+
68
+
69
+ @register_function(config_type=PlanSelectExecuteFunctionConfig, framework_wrappers=[LLMFrameworkEnum.LANGCHAIN])
70
+ async def plan_select_execute_function(config: PlanSelectExecuteFunctionConfig, builder: Builder):
71
+ """
72
+ Build a ExecutionPlanningFunction from the provided config.
73
+
74
+ Args:
75
+ config (ExecutionPlanningFunctionConfig): The config for the ExecutionPlanningFunction.
76
+ builder (Builder): The Builder instance to use for building the function.
77
+
78
+ Returns:
79
+ ExecutionPlanningFunction: The built ExecutionPlanningFunction.
80
+ """
81
+
82
+ try:
83
+ from langchain_core.prompts import PromptTemplate
84
+ except ImportError:
85
+ raise ImportError("langchain-core is not installed. Please install it to use SingleShotMultiPlanPlanner.\n"
86
+ "This error can be resolved by installing aiqtoolkit-langchain.")
87
+
88
+ # Get the augmented function's description
89
+ augmented_function = builder.get_function(config.augmented_fn)
90
+
91
+ # For now, we rely on runtime checking for type conversion
92
+
93
+ if augmented_function.description and augmented_function.description != "":
94
+ augmented_function_desc = augmented_function.description
95
+ else:
96
+ raise ValueError(f"Function {config.augmented_fn} does not have a description. Cannot augment "
97
+ f"function without a description.")
98
+
99
+ # Get the function dependencies of the augmented function
100
+ function_used_tools = builder.get_function_dependencies(config.augmented_fn).functions
101
+ tool_list = "Tool: Description\n"
102
+
103
+ for tool in function_used_tools:
104
+ tool_impl = builder.get_function(tool)
105
+ tool_list += f"- {tool}: {tool_impl.description if hasattr(tool_impl, 'description') else ''}\n"
106
+
107
+ # Draft the reasoning prompt for the augmented function
108
+ template = PromptTemplate(template=config.agent_context_prompt_template,
109
+ input_variables=["description", "tools"],
110
+ validate_template=True)
111
+
112
+ downstream_template = PromptTemplate(template=config.downstream_template,
113
+ input_variables=["input_text", "reasoning_output"],
114
+ validate_template=True)
115
+
116
+ planner = await builder.get_its_strategy(strategy_name=config.planner,
117
+ pipeline_type=PipelineTypeEnum.PLANNING,
118
+ stage_type=StageTypeEnum.SEARCH)
119
+
120
+ selector = await builder.get_its_strategy(strategy_name=config.selector,
121
+ pipeline_type=PipelineTypeEnum.PLANNING,
122
+ stage_type=StageTypeEnum.SELECTION)
123
+
124
+ if config.editor:
125
+ editor = await builder.get_its_strategy(strategy_name=config.editor,
126
+ pipeline_type=PipelineTypeEnum.PLANNING,
127
+ stage_type=StageTypeEnum.EDITING)
128
+ else:
129
+ editor = None
130
+
131
+ if config.scorer:
132
+ scorer = await builder.get_its_strategy(strategy_name=config.scorer,
133
+ pipeline_type=PipelineTypeEnum.PLANNING,
134
+ stage_type=StageTypeEnum.SCORING)
135
+ else:
136
+ scorer = None
137
+
138
+ async def planning_pipeline(prompt, context):
139
+
140
+ plans = await planner.ainvoke([ITSItem()], prompt, context)
141
+
142
+ if editor:
143
+ plans = await editor.ainvoke(plans, prompt, context)
144
+ if scorer:
145
+ plans = await scorer.ainvoke(plans, prompt, context)
146
+
147
+ selected_plan = (await selector.ainvoke(plans, prompt, context))[0]
148
+
149
+ return selected_plan
150
+
151
+ streaming_inner_fn = None
152
+ single_inner_fn = None
153
+
154
+ if augmented_function.has_streaming_output:
155
+
156
+ async def streaming_inner(
157
+ input_message: AIQChatRequest) -> AsyncGenerator[augmented_function.streaming_output_type]:
158
+ """
159
+ Perform reasoning on the input text.
160
+
161
+ Args:
162
+ input_message (AIQChatRequest): The input text to reason on.
163
+ """
164
+
165
+ input_text = "".join([str(message.model_dump()) + "\n" for message in input_message.messages])
166
+
167
+ context_prompt = await template.ainvoke(input={"description": augmented_function_desc, "tools": tool_list})
168
+
169
+ context_prompt = context_prompt.to_string()
170
+
171
+ # Run the ITS pipeline
172
+ planning_item: ITSItem = await planning_pipeline(prompt=input_text, context=context_prompt)
173
+
174
+ output = await downstream_template.ainvoke(input={
175
+ "input_text": input_text, "reasoning_output": planning_item.plan
176
+ })
177
+
178
+ output = output.to_string()
179
+
180
+ if config.verbose:
181
+ logger.info("Reasoning plan and input to agent: \n\n%s", output)
182
+
183
+ async for chunk in augmented_function.acall_stream(output):
184
+ yield chunk
185
+
186
+ streaming_inner_fn = streaming_inner
187
+
188
+ if augmented_function.has_single_output:
189
+
190
+ async def single_inner(input_message: AIQChatRequest) -> augmented_function.single_output_type:
191
+ """
192
+ Perform reasoning on the input text.
193
+
194
+ Args:
195
+ input_message (AIQChatRequest): The input text to reason on.
196
+ """
197
+
198
+ input_text = "".join([str(message.model_dump()) + "\n" for message in input_message.messages])
199
+
200
+ context_prompt = await template.ainvoke(input={"description": augmented_function_desc, "tools": tool_list})
201
+
202
+ context_prompt = context_prompt.to_string()
203
+
204
+ # Run the ITS pipeline
205
+ planning_item: ITSItem = await planning_pipeline(prompt=input_text, context=context_prompt)
206
+
207
+ output = await downstream_template.ainvoke(input={
208
+ "input_text": input_text, "reasoning_output": planning_item.plan
209
+ })
210
+
211
+ output = output.to_string()
212
+
213
+ if config.verbose:
214
+ logger.info("Reasoning plan and input to agent: \n\n%s", output)
215
+
216
+ return await augmented_function.acall_invoke(output)
217
+
218
+ single_inner_fn = single_inner
219
+
220
+ yield FunctionInfo.create(
221
+ single_fn=single_inner_fn,
222
+ stream_fn=streaming_inner_fn,
223
+ description=("Function that runs an ITS execution planner on input and sends plan downstream"),
224
+ converters=augmented_function.converter_list)
@@ -0,0 +1,132 @@
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
+ import typing
17
+
18
+ from pydantic import Field
19
+ from pydantic import model_validator
20
+
21
+ from aiq.data_models.component_ref import LLMRef
22
+ from aiq.data_models.its_strategy import ITSStrategyBaseConfig
23
+
24
+
25
+ class LLMAsAJudgeEditorConfig(ITSStrategyBaseConfig, name="llm_as_a_judge_editor"):
26
+ """
27
+ Configuration for the LLMAsAJudgeEditor.
28
+ """
29
+ num_feedback: int = Field(default=10,
30
+ description="Number of feedback items to generate for each plan during editing. "
31
+ "This can help in refining the plans based on feedback.")
32
+
33
+ # If strategy is provided, LLM must be
34
+ editing_llm: LLMRef | typing.Any | None = Field(
35
+ default=None,
36
+ description="The LLM to use for editing the plans. This can be a callable or an instance of an LLM client.")
37
+
38
+ # If strategy is LLM_AS_A_JUDGE, ensure that the feedback_llm is provided.
39
+ feedback_llm: LLMRef | typing.Any | None = Field(default=None,
40
+ description="The LLM to use for generating feedback on the plans."
41
+ " This can be a callable or an instance of an LLM client.")
42
+
43
+ editor_template: str = Field(default=(
44
+ "You are an expert at improving execution plans. You will be given a plan and feedback on that plan."
45
+ " Your task is to create an improved version of the plan that addresses the feedback "
46
+ "while maintaining its strengths.\n\n"
47
+ "Here is the context:\n\n"
48
+ "{context}\n\n"
49
+ "**Input:** \n{original_prompt}\n\n"
50
+ "**Original Plan:**\n{plan}\n\n"
51
+ "**Feedback on the Plan:**\n{feedback}\n\n"
52
+ "Please provide an improved version of the plan that addresses"
53
+ " the feedback points. Maintain the same structure and "
54
+ "step-by-step format, but enhance the content. Do not include explanations of your changes, just provide the "
55
+ "improved plan directly:\n\n"
56
+ "Begin the final improve plan with 'EDITED PLAN:'"),
57
+ description="The template to use for editing the planning items based on feedback.")
58
+
59
+ feedback_template: str = Field(
60
+ default=("You are an expert at evaluating execution plans. You will be given a plan and "
61
+ "need to provide {num_feedback} "
62
+ "specific points of feedback about its strengths and weaknesses.\n\n"
63
+ "Your feedback should cover aspects like:\n"
64
+ "- Comprehensiveness of the plan\n"
65
+ "- Logical flow and sequencing\n"
66
+ "- Appropriate use of available tools\n"
67
+ "- Potential edge cases or failure points\n"
68
+ "- Efficiency and optimization opportunities\n\n"
69
+ "Here is the context and plan to evaluate:\n\n"
70
+ "{context}\n\n"
71
+ "**Objective:** \n{original_prompt}\n\n"
72
+ "**Plan to Evaluate:**\n{plan}\n\n"
73
+ "Please provide exactly {num_feedback} numbered points of feedback, including "
74
+ "both strengths and areas for improvement. Begin the feedback with 'FEEDBACK:' and provide"
75
+ "{num_feedback} specific feedback points."),
76
+ description="The template to use for generating feedback for each planning item.")
77
+
78
+ @model_validator(mode="before")
79
+ def validate_strategies(cls, values: dict[str, typing.Any]) -> dict[str, typing.Any]:
80
+
81
+ if values.get('editing_llm') is None:
82
+ raise ValueError('editing_llm must be provided when editing_strategy is set.')
83
+ # If editing strategy is LLM_AS_A_JUDGE, feedback_llm must also be provided
84
+ if (values.get('feedback_llm') is None):
85
+ raise ValueError('feedback_llm must be provided when editing_strategy is LLM_AS_A_JUDGE.')
86
+
87
+ return values
88
+
89
+
90
+ class IterativePlanRefinementConfig(ITSStrategyBaseConfig, name="iterative_plan_refinement"):
91
+ """Configuration for an 'iterative plan refinement' strategy."""
92
+ editor_llm: LLMRef | typing.Any | None = Field(
93
+ default=None, description="The LLM to use for generating and refining the plan across multiple iterations.")
94
+ num_iterations: int = Field(default=3, description="How many refinement steps to perform.")
95
+ refinement_template: str = Field(
96
+ default=("You have the current plan:\n{current_plan}\n\n"
97
+ "The plan was generated to achieve the following objective:\n{original_prompt}\n\n"
98
+ "Using an agent system with the following description:\n{context}\n\n"
99
+ "Refine or improve it to achieve the objective better."
100
+ "Output the updated plan, beginning with:\nEDITED PLAN:\n"),
101
+ description="Prompt used in each iteration to refine the plan.")
102
+
103
+ @model_validator(mode="before")
104
+ def validate_iterative_strategies(cls, values: dict) -> dict:
105
+ if not values.get('editor_llm'):
106
+ raise ValueError('planning_llm must be provided for iterative plan refinement.')
107
+ if values.get('num_iterations', 0) < 1:
108
+ raise ValueError('num_iterations must be >= 1 for iterative plan refinement.')
109
+ return values
110
+
111
+
112
+ class MotivationAwareSummarizationConfig(ITSStrategyBaseConfig, name="motivation_aware_editing"):
113
+ """
114
+ Configuration for the MotivationAwareSummarization strategy.
115
+ """
116
+ editor_llm: LLMRef | typing.Any | None = Field(
117
+ default=None,
118
+ description="The LLM to use for editing the plans. This can be a callable or an instance of an LLM client.")
119
+
120
+ editor_template: str = Field(
121
+ default=("You are an expert at summarizing key information from relevant documents based on an input task"
122
+ "and motivation. Given a task and motivation, and documents, your task is to create a concise "
123
+ "a summarized response to the task and motivation grounded in the documents .\n\n"
124
+ "Here is the task:\n\n"
125
+ "{task}\n\n"
126
+ "Here is the motivation:\n\n"
127
+ "{motivation}\n\n"
128
+ "and here are the documents:\n\n"
129
+ "{output}\n\n"
130
+ "Please respond with a concise summary that addresses the task and motivation, in at most one"
131
+ "or two sentences. Do not include any other output except the summary. "),
132
+ description="The template to use for summarizing documents.")
@@ -0,0 +1,48 @@
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
+ import typing
17
+
18
+ from pydantic import BaseModel
19
+ from pydantic import ConfigDict
20
+ from pydantic import Field
21
+
22
+
23
+ class ITSItem(BaseModel):
24
+ """
25
+ Represents an item in the Inference Time Scaling (ITS) functions and pipelines
26
+ """
27
+ model_config = ConfigDict(extra="allow")
28
+
29
+ input: typing.Any | None = Field(default=None,
30
+ description="Input to the function or pipeline. "
31
+ "This can be a structured tool call, or other info.")
32
+ output: typing.Any | None = Field(default=None,
33
+ description="Output from the function or pipeline. "
34
+ "This can be a structured tool call, or other info.")
35
+ plan: typing.Any | None = Field(default=None, description="Search plan for downstream agent(s).")
36
+ feedback: str | None = Field(default=None,
37
+ description="Feedback "
38
+ "provided by feedback steps to improve the plan.")
39
+ score: float | None = Field(default=None,
40
+ description="Score of the plan based on feedback or other evaluation criteria. "
41
+ "This can be used to rank plans.")
42
+ metadata: typing.Any | None = Field(default=None,
43
+ description="Additional information. This can be"
44
+ " a structured tool call, or other info not "
45
+ "in the plan.")
46
+ name: str | None = Field(default=None,
47
+ description="Name of the item or function"
48
+ ", used for identification in pipelines.")
@@ -0,0 +1,112 @@
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
+ import typing
17
+
18
+ from pydantic import Field
19
+ from pydantic import model_validator
20
+
21
+ from aiq.data_models.component_ref import LLMRef
22
+ from aiq.data_models.its_strategy import ITSStrategyBaseConfig
23
+
24
+
25
+ class LLMBasedPlanScoringConfig(ITSStrategyBaseConfig, name="llm_based_plan_scoring"):
26
+ """
27
+ Configuration for LLMBasedScoring.
28
+ """
29
+ scoring_llm: LLMRef | typing.Any | None = Field(
30
+ default=None,
31
+ description="The LLM to use for scoring the plans. This can be a callable or an instance of an LLM client.")
32
+
33
+ scoring_template: str = Field(
34
+ default=("You are an expert reasoning model tasked with scoring the following execution plan based on its"
35
+ "quality and relevance to the provided input to an agent system.\n\n"
36
+ "The agent system's role is:\n{context}\n\n"
37
+ "It has been tasked with achieving the following goal: \n{original_prompt}\n\n"
38
+ "The following plan has been generated to achieve this goal:\n\n{plan}\n\n"
39
+ "Score the plan on a scale from 1 to 10, where 10 is the best. "
40
+ "Return the final score as a floating point number preceded by `FINAL SCORE:` without any "
41
+ "other text before or after it\n"),
42
+ description="The template to use for scoring the plans.")
43
+
44
+ @model_validator(mode="before")
45
+ def validate_strategies(cls, values: dict[str, typing.Any]) -> dict[str, typing.Any]:
46
+ """
47
+ Ensure that the scoring_llm is provided when using LLMBasedScoring.
48
+ """
49
+ if values.get('scoring_llm') is None:
50
+ raise ValueError('scoring_llm must be provided when scorer_type is set to LLM_BASED_SCORING.')
51
+
52
+ return values
53
+
54
+
55
+ class LLMBasedAgentScoringConfig(ITSStrategyBaseConfig, name="llm_based_agent_scoring"):
56
+ """
57
+ Configuration for LLMBasedScoring.
58
+ """
59
+ scoring_llm: LLMRef | typing.Any | None = Field(
60
+ default=None,
61
+ description="The LLM to use for scoring the plans. This can be a callable or an instance of an LLM client.")
62
+
63
+ scoring_template: str = Field(
64
+ description="Prompt template to use for scoring the function output",
65
+ default=("You are an expert reasoning model tasked with scoring the following "
66
+ "result of an agent system based on its input and objective. Judge"
67
+ " the quality and relevance of the answer to score it.\n\n"
68
+ "The agent system's objective is:\n{objective}\n\n"
69
+ "It has been tasked with achieving the following goal: \n{input}\n\n"
70
+ "The following output has been generated by the agent:\n\n{output}\n\n"
71
+ "Score the result on a scale from 1 to 10, where 10 is the best. "
72
+ "Return the final score as a floating point number preceded by `FINAL SCORE:` without any "
73
+ "other text before or after it\n"),
74
+ )
75
+
76
+ @model_validator(mode="before")
77
+ def validate_strategies(cls, values: dict[str, typing.Any]) -> dict[str, typing.Any]:
78
+ """
79
+ Ensure that the scoring_llm is provided when using LLMBasedScoring.
80
+ """
81
+ if values.get('scoring_llm') is None:
82
+ raise ValueError('scoring_llm must be provided when scorer_type is set to LLM_BASED_SCORING.')
83
+
84
+ return values
85
+
86
+
87
+ class MotivationAwareScoringConfig(ITSStrategyBaseConfig, name="motivation_aware_scoring"):
88
+ """
89
+ Configuration for a scoring strategy that considers both the original input (task)
90
+ and the motivation (from metadata) along with the current output.
91
+ """
92
+
93
+ scoring_llm: LLMRef | None = Field(
94
+ default=None, description="The LLM used to evaluate how well the output addresses the task plus motivation.")
95
+
96
+ scoring_template: str = Field(
97
+ default=("You are an expert at assessing the quality of an output in relation to its task and motivation.\n"
98
+ "Task: {task}\n"
99
+ "Motivation: {motivation}\n"
100
+ "Output: {output}\n"
101
+ "On a scale from 1 to 10 (10 being the best), how well does this output fulfill "
102
+ "the original task in the context "
103
+ "of the provided motivation? Note that the task might answer one part of a bigger question "
104
+ "which should count as a satisfactory response and should not receive a lower score.\n"
105
+ "Return the final score as a floating point number preceded by 'FINAL SCORE:'."),
106
+ description="The prompt template used to evaluate and score the output.")
107
+
108
+ @model_validator(mode="before")
109
+ def validate_scoring_llm(cls, values):
110
+ if values.get('scoring_llm') is None:
111
+ raise ValueError("A scoring_llm must be provided for motivation_aware_scoring.")
112
+ return values