nvidia-nat 1.3.0a20250910__py3-none-any.whl → 1.4.0a20251112__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 (213) hide show
  1. nat/agent/base.py +13 -8
  2. nat/agent/prompt_optimizer/prompt.py +68 -0
  3. nat/agent/prompt_optimizer/register.py +149 -0
  4. nat/agent/react_agent/agent.py +6 -5
  5. nat/agent/react_agent/register.py +49 -39
  6. nat/agent/reasoning_agent/reasoning_agent.py +17 -15
  7. nat/agent/register.py +2 -0
  8. nat/agent/responses_api_agent/__init__.py +14 -0
  9. nat/agent/responses_api_agent/register.py +126 -0
  10. nat/agent/rewoo_agent/agent.py +304 -117
  11. nat/agent/rewoo_agent/prompt.py +19 -22
  12. nat/agent/rewoo_agent/register.py +51 -38
  13. nat/agent/tool_calling_agent/agent.py +75 -17
  14. nat/agent/tool_calling_agent/register.py +46 -23
  15. nat/authentication/api_key/api_key_auth_provider.py +6 -11
  16. nat/authentication/api_key/api_key_auth_provider_config.py +8 -5
  17. nat/authentication/credential_validator/__init__.py +14 -0
  18. nat/authentication/credential_validator/bearer_token_validator.py +557 -0
  19. nat/authentication/http_basic_auth/http_basic_auth_provider.py +1 -1
  20. nat/authentication/interfaces.py +5 -2
  21. nat/authentication/oauth2/oauth2_auth_code_flow_provider.py +69 -36
  22. nat/authentication/oauth2/oauth2_auth_code_flow_provider_config.py +2 -1
  23. nat/authentication/oauth2/oauth2_resource_server_config.py +125 -0
  24. nat/builder/builder.py +55 -23
  25. nat/builder/component_utils.py +9 -5
  26. nat/builder/context.py +54 -15
  27. nat/builder/eval_builder.py +14 -9
  28. nat/builder/framework_enum.py +1 -0
  29. nat/builder/front_end.py +1 -1
  30. nat/builder/function.py +370 -0
  31. nat/builder/function_info.py +1 -1
  32. nat/builder/intermediate_step_manager.py +38 -2
  33. nat/builder/workflow.py +5 -0
  34. nat/builder/workflow_builder.py +306 -54
  35. nat/cli/cli_utils/config_override.py +1 -1
  36. nat/cli/commands/info/info.py +16 -6
  37. nat/cli/commands/mcp/__init__.py +14 -0
  38. nat/cli/commands/mcp/mcp.py +986 -0
  39. nat/cli/commands/optimize.py +90 -0
  40. nat/cli/commands/start.py +1 -1
  41. nat/cli/commands/workflow/templates/config.yml.j2 +14 -13
  42. nat/cli/commands/workflow/templates/register.py.j2 +2 -2
  43. nat/cli/commands/workflow/templates/workflow.py.j2 +35 -21
  44. nat/cli/commands/workflow/workflow_commands.py +60 -18
  45. nat/cli/entrypoint.py +15 -11
  46. nat/cli/main.py +3 -0
  47. nat/cli/register_workflow.py +38 -4
  48. nat/cli/type_registry.py +72 -1
  49. nat/control_flow/__init__.py +0 -0
  50. nat/control_flow/register.py +20 -0
  51. nat/control_flow/router_agent/__init__.py +0 -0
  52. nat/control_flow/router_agent/agent.py +329 -0
  53. nat/control_flow/router_agent/prompt.py +48 -0
  54. nat/control_flow/router_agent/register.py +91 -0
  55. nat/control_flow/sequential_executor.py +166 -0
  56. nat/data_models/agent.py +34 -0
  57. nat/data_models/api_server.py +199 -69
  58. nat/data_models/authentication.py +23 -9
  59. nat/data_models/common.py +47 -0
  60. nat/data_models/component.py +2 -0
  61. nat/data_models/component_ref.py +11 -0
  62. nat/data_models/config.py +41 -17
  63. nat/data_models/dataset_handler.py +4 -3
  64. nat/data_models/function.py +34 -0
  65. nat/data_models/function_dependencies.py +8 -0
  66. nat/data_models/intermediate_step.py +9 -1
  67. nat/data_models/llm.py +15 -1
  68. nat/data_models/openai_mcp.py +46 -0
  69. nat/data_models/optimizable.py +208 -0
  70. nat/data_models/optimizer.py +161 -0
  71. nat/data_models/span.py +41 -3
  72. nat/data_models/thinking_mixin.py +2 -2
  73. nat/embedder/azure_openai_embedder.py +2 -1
  74. nat/embedder/nim_embedder.py +3 -2
  75. nat/embedder/openai_embedder.py +3 -2
  76. nat/eval/config.py +1 -1
  77. nat/eval/dataset_handler/dataset_downloader.py +3 -2
  78. nat/eval/dataset_handler/dataset_filter.py +34 -2
  79. nat/eval/evaluate.py +10 -3
  80. nat/eval/evaluator/base_evaluator.py +1 -1
  81. nat/eval/rag_evaluator/evaluate.py +7 -4
  82. nat/eval/register.py +4 -0
  83. nat/eval/runtime_evaluator/__init__.py +14 -0
  84. nat/eval/runtime_evaluator/evaluate.py +123 -0
  85. nat/eval/runtime_evaluator/register.py +100 -0
  86. nat/eval/swe_bench_evaluator/evaluate.py +1 -1
  87. nat/eval/trajectory_evaluator/register.py +1 -1
  88. nat/eval/tunable_rag_evaluator/evaluate.py +1 -1
  89. nat/eval/usage_stats.py +2 -0
  90. nat/eval/utils/output_uploader.py +3 -2
  91. nat/eval/utils/weave_eval.py +17 -3
  92. nat/experimental/decorators/experimental_warning_decorator.py +27 -7
  93. nat/experimental/test_time_compute/functions/execute_score_select_function.py +1 -1
  94. nat/experimental/test_time_compute/functions/plan_select_execute_function.py +7 -3
  95. nat/experimental/test_time_compute/functions/ttc_tool_orchestration_function.py +1 -1
  96. nat/experimental/test_time_compute/functions/ttc_tool_wrapper_function.py +3 -3
  97. nat/experimental/test_time_compute/models/strategy_base.py +2 -2
  98. nat/experimental/test_time_compute/selection/llm_based_output_merging_selector.py +1 -1
  99. nat/front_ends/console/authentication_flow_handler.py +82 -30
  100. nat/front_ends/console/console_front_end_plugin.py +19 -7
  101. nat/front_ends/fastapi/auth_flow_handlers/http_flow_handler.py +1 -1
  102. nat/front_ends/fastapi/auth_flow_handlers/websocket_flow_handler.py +52 -17
  103. nat/front_ends/fastapi/dask_client_mixin.py +65 -0
  104. nat/front_ends/fastapi/fastapi_front_end_config.py +25 -3
  105. nat/front_ends/fastapi/fastapi_front_end_plugin.py +140 -3
  106. nat/front_ends/fastapi/fastapi_front_end_plugin_worker.py +445 -265
  107. nat/front_ends/fastapi/job_store.py +518 -99
  108. nat/front_ends/fastapi/main.py +11 -19
  109. nat/front_ends/fastapi/message_handler.py +69 -44
  110. nat/front_ends/fastapi/message_validator.py +8 -7
  111. nat/front_ends/fastapi/utils.py +57 -0
  112. nat/front_ends/mcp/introspection_token_verifier.py +73 -0
  113. nat/front_ends/mcp/mcp_front_end_config.py +71 -3
  114. nat/front_ends/mcp/mcp_front_end_plugin.py +85 -21
  115. nat/front_ends/mcp/mcp_front_end_plugin_worker.py +248 -29
  116. nat/front_ends/mcp/memory_profiler.py +320 -0
  117. nat/front_ends/mcp/tool_converter.py +78 -25
  118. nat/front_ends/simple_base/simple_front_end_plugin_base.py +3 -1
  119. nat/llm/aws_bedrock_llm.py +21 -8
  120. nat/llm/azure_openai_llm.py +14 -5
  121. nat/llm/litellm_llm.py +80 -0
  122. nat/llm/nim_llm.py +23 -9
  123. nat/llm/openai_llm.py +19 -7
  124. nat/llm/register.py +4 -0
  125. nat/llm/utils/thinking.py +1 -1
  126. nat/observability/exporter/base_exporter.py +1 -1
  127. nat/observability/exporter/processing_exporter.py +29 -55
  128. nat/observability/exporter/span_exporter.py +43 -15
  129. nat/observability/exporter_manager.py +2 -2
  130. nat/observability/mixin/redaction_config_mixin.py +5 -4
  131. nat/observability/mixin/tagging_config_mixin.py +26 -14
  132. nat/observability/mixin/type_introspection_mixin.py +420 -107
  133. nat/observability/processor/batching_processor.py +1 -1
  134. nat/observability/processor/processor.py +3 -0
  135. nat/observability/processor/redaction/__init__.py +24 -0
  136. nat/observability/processor/redaction/contextual_redaction_processor.py +125 -0
  137. nat/observability/processor/redaction/contextual_span_redaction_processor.py +66 -0
  138. nat/observability/processor/redaction/redaction_processor.py +177 -0
  139. nat/observability/processor/redaction/span_header_redaction_processor.py +92 -0
  140. nat/observability/processor/span_tagging_processor.py +21 -14
  141. nat/observability/register.py +16 -0
  142. nat/profiler/callbacks/langchain_callback_handler.py +32 -7
  143. nat/profiler/callbacks/llama_index_callback_handler.py +36 -2
  144. nat/profiler/callbacks/token_usage_base_model.py +2 -0
  145. nat/profiler/decorators/framework_wrapper.py +61 -9
  146. nat/profiler/decorators/function_tracking.py +35 -3
  147. nat/profiler/forecasting/models/linear_model.py +1 -1
  148. nat/profiler/forecasting/models/random_forest_regressor.py +1 -1
  149. nat/profiler/inference_optimization/bottleneck_analysis/nested_stack_analysis.py +1 -1
  150. nat/profiler/inference_optimization/experimental/prefix_span_analysis.py +1 -1
  151. nat/profiler/parameter_optimization/__init__.py +0 -0
  152. nat/profiler/parameter_optimization/optimizable_utils.py +93 -0
  153. nat/profiler/parameter_optimization/optimizer_runtime.py +67 -0
  154. nat/profiler/parameter_optimization/parameter_optimizer.py +189 -0
  155. nat/profiler/parameter_optimization/parameter_selection.py +107 -0
  156. nat/profiler/parameter_optimization/pareto_visualizer.py +460 -0
  157. nat/profiler/parameter_optimization/prompt_optimizer.py +384 -0
  158. nat/profiler/parameter_optimization/update_helpers.py +66 -0
  159. nat/profiler/utils.py +3 -1
  160. nat/registry_handlers/pypi/register_pypi.py +5 -3
  161. nat/registry_handlers/rest/register_rest.py +5 -3
  162. nat/retriever/milvus/retriever.py +1 -1
  163. nat/retriever/nemo_retriever/register.py +2 -1
  164. nat/runtime/loader.py +1 -1
  165. nat/runtime/runner.py +111 -6
  166. nat/runtime/session.py +49 -3
  167. nat/settings/global_settings.py +2 -2
  168. nat/tool/chat_completion.py +4 -1
  169. nat/tool/code_execution/code_sandbox.py +3 -6
  170. nat/tool/code_execution/local_sandbox/Dockerfile.sandbox +19 -32
  171. nat/tool/code_execution/local_sandbox/local_sandbox_server.py +6 -1
  172. nat/tool/code_execution/local_sandbox/sandbox.requirements.txt +2 -0
  173. nat/tool/code_execution/local_sandbox/start_local_sandbox.sh +10 -4
  174. nat/tool/datetime_tools.py +1 -1
  175. nat/tool/github_tools.py +450 -0
  176. nat/tool/memory_tools/add_memory_tool.py +3 -3
  177. nat/tool/memory_tools/delete_memory_tool.py +3 -4
  178. nat/tool/memory_tools/get_memory_tool.py +4 -4
  179. nat/tool/register.py +2 -7
  180. nat/tool/server_tools.py +15 -2
  181. nat/utils/__init__.py +76 -0
  182. nat/utils/callable_utils.py +70 -0
  183. nat/utils/data_models/schema_validator.py +1 -1
  184. nat/utils/decorators.py +210 -0
  185. nat/utils/exception_handlers/automatic_retries.py +278 -72
  186. nat/utils/io/yaml_tools.py +73 -3
  187. nat/utils/log_levels.py +25 -0
  188. nat/utils/responses_api.py +26 -0
  189. nat/utils/string_utils.py +16 -0
  190. nat/utils/type_converter.py +12 -3
  191. nat/utils/type_utils.py +6 -2
  192. nvidia_nat-1.4.0a20251112.dist-info/METADATA +197 -0
  193. {nvidia_nat-1.3.0a20250910.dist-info → nvidia_nat-1.4.0a20251112.dist-info}/RECORD +199 -165
  194. {nvidia_nat-1.3.0a20250910.dist-info → nvidia_nat-1.4.0a20251112.dist-info}/entry_points.txt +1 -0
  195. nat/cli/commands/info/list_mcp.py +0 -461
  196. nat/data_models/temperature_mixin.py +0 -43
  197. nat/data_models/top_p_mixin.py +0 -43
  198. nat/observability/processor/header_redaction_processor.py +0 -123
  199. nat/observability/processor/redaction_processor.py +0 -77
  200. nat/tool/code_execution/test_code_execution_sandbox.py +0 -414
  201. nat/tool/github_tools/create_github_commit.py +0 -133
  202. nat/tool/github_tools/create_github_issue.py +0 -87
  203. nat/tool/github_tools/create_github_pr.py +0 -106
  204. nat/tool/github_tools/get_github_file.py +0 -106
  205. nat/tool/github_tools/get_github_issue.py +0 -166
  206. nat/tool/github_tools/get_github_pr.py +0 -256
  207. nat/tool/github_tools/update_github_issue.py +0 -100
  208. nvidia_nat-1.3.0a20250910.dist-info/METADATA +0 -373
  209. /nat/{tool/github_tools → agent/prompt_optimizer}/__init__.py +0 -0
  210. {nvidia_nat-1.3.0a20250910.dist-info → nvidia_nat-1.4.0a20251112.dist-info}/WHEEL +0 -0
  211. {nvidia_nat-1.3.0a20250910.dist-info → nvidia_nat-1.4.0a20251112.dist-info}/licenses/LICENSE-3rd-party.txt +0 -0
  212. {nvidia_nat-1.3.0a20250910.dist-info → nvidia_nat-1.4.0a20251112.dist-info}/licenses/LICENSE.md +0 -0
  213. {nvidia_nat-1.3.0a20250910.dist-info → nvidia_nat-1.4.0a20251112.dist-info}/top_level.txt +0 -0
@@ -23,41 +23,38 @@ from nat.builder.builder import Builder
23
23
  from nat.builder.framework_enum import LLMFrameworkEnum
24
24
  from nat.builder.function_info import FunctionInfo
25
25
  from nat.cli.register_workflow import register_function
26
+ from nat.data_models.agent import AgentBaseConfig
26
27
  from nat.data_models.api_server import ChatRequest
28
+ from nat.data_models.api_server import ChatRequestOrMessage
27
29
  from nat.data_models.api_server import ChatResponse
30
+ from nat.data_models.api_server import Usage
31
+ from nat.data_models.component_ref import FunctionGroupRef
28
32
  from nat.data_models.component_ref import FunctionRef
29
- from nat.data_models.component_ref import LLMRef
30
- from nat.data_models.function import FunctionBaseConfig
31
33
  from nat.utils.type_converter import GlobalTypeConverter
32
34
 
33
35
  logger = logging.getLogger(__name__)
34
36
 
35
37
 
36
- class ReWOOAgentWorkflowConfig(FunctionBaseConfig, name="rewoo_agent"):
38
+ class ReWOOAgentWorkflowConfig(AgentBaseConfig, name="rewoo_agent"):
37
39
  """
38
40
  Defines a NAT function that uses a ReWOO Agent performs reasoning inbetween tool calls, and utilizes the
39
41
  tool names and descriptions to select the optimal tool.
40
42
  """
41
-
42
- tool_names: list[FunctionRef] = Field(default_factory=list,
43
- description="The list of tools to provide to the rewoo agent.")
44
- llm_name: LLMRef = Field(description="The LLM model to use with the rewoo agent.")
45
- verbose: bool = Field(default=False, description="Set the verbosity of the rewoo agent's logging.")
43
+ description: str = Field(default="ReWOO Agent Workflow", description="The description of this functions use.")
44
+ tool_names: list[FunctionRef | FunctionGroupRef] = Field(
45
+ default_factory=list, description="The list of tools to provide to the rewoo agent.")
46
46
  include_tool_input_schema_in_tool_description: bool = Field(
47
47
  default=True, description="Specify inclusion of tool input schemas in the prompt.")
48
- description: str = Field(default="ReWOO Agent Workflow", description="The description of this functions use.")
49
48
  planner_prompt: str | None = Field(
50
49
  default=None,
51
50
  description="Provides the PLANNER_PROMPT to use with the agent") # defaults to PLANNER_PROMPT in prompt.py
52
51
  solver_prompt: str | None = Field(
53
52
  default=None,
54
53
  description="Provides the SOLVER_PROMPT to use with the agent") # defaults to SOLVER_PROMPT in prompt.py
54
+ tool_call_max_retries: PositiveInt = Field(default=3,
55
+ description="The number of retries before raising a tool call error.",
56
+ ge=1)
55
57
  max_history: int = Field(default=15, description="Maximum number of messages to keep in the conversation history.")
56
- log_response_max_chars: PositiveInt = Field(
57
- default=1000, description="Maximum number of characters to display in logs when logging tool responses.")
58
- use_openai_api: bool = Field(default=False,
59
- description=("Use OpenAI API for the input/output types to the function. "
60
- "If False, strings will be used."))
61
58
  additional_planner_instructions: str | None = Field(
62
59
  default=None,
63
60
  validation_alias=AliasChoices("additional_planner_instructions", "additional_instructions"),
@@ -65,12 +62,16 @@ class ReWOOAgentWorkflowConfig(FunctionBaseConfig, name="rewoo_agent"):
65
62
  additional_solver_instructions: str | None = Field(
66
63
  default=None,
67
64
  description="Additional instructions to provide to the agent in addition to the base solver prompt.")
65
+ raise_tool_call_error: bool = Field(default=True,
66
+ description="Whether to raise a exception immediately if a tool"
67
+ "call fails. If set to False, the tool call error message will be included in"
68
+ "the tool response and passed to the next tool.")
68
69
 
69
70
 
70
71
  @register_function(config_type=ReWOOAgentWorkflowConfig, framework_wrappers=[LLMFrameworkEnum.LANGCHAIN])
71
72
  async def rewoo_agent_workflow(config: ReWOOAgentWorkflowConfig, builder: Builder):
72
- from langchain.schema import BaseMessage
73
73
  from langchain_core.messages import trim_messages
74
+ from langchain_core.messages.base import BaseMessage
74
75
  from langchain_core.messages.human import HumanMessage
75
76
  from langchain_core.prompts import ChatPromptTemplate
76
77
  from langgraph.graph.state import CompiledStateGraph
@@ -106,7 +107,7 @@ async def rewoo_agent_workflow(config: ReWOOAgentWorkflowConfig, builder: Builde
106
107
 
107
108
  # the agent can run any installed tool, simply install the tool and add it to the config file
108
109
  # the sample tool provided can easily be copied or changed
109
- tools = builder.get_tools(tool_names=config.tool_names, wrapper_type=LLMFrameworkEnum.LANGCHAIN)
110
+ tools = await builder.get_tools(tool_names=config.tool_names, wrapper_type=LLMFrameworkEnum.LANGCHAIN)
110
111
  if not tools:
111
112
  raise ValueError(f"No tools specified for ReWOO Agent '{config.llm_name}'")
112
113
 
@@ -118,12 +119,27 @@ async def rewoo_agent_workflow(config: ReWOOAgentWorkflowConfig, builder: Builde
118
119
  tools=tools,
119
120
  use_tool_schema=config.include_tool_input_schema_in_tool_description,
120
121
  detailed_logs=config.verbose,
121
- log_response_max_chars=config.log_response_max_chars).build_graph()
122
+ log_response_max_chars=config.log_response_max_chars,
123
+ tool_call_max_retries=config.tool_call_max_retries,
124
+ raise_tool_call_error=config.raise_tool_call_error).build_graph()
125
+
126
+ async def _response_fn(chat_request_or_message: ChatRequestOrMessage) -> ChatResponse | str:
127
+ """
128
+ Main workflow entry function for the ReWOO Agent.
129
+
130
+ This function invokes the ReWOO Agent Graph and returns the response.
131
+
132
+ Args:
133
+ chat_request_or_message (ChatRequestOrMessage): The input message to process
122
134
 
123
- async def _response_fn(input_message: ChatRequest) -> ChatResponse:
135
+ Returns:
136
+ ChatResponse | str: The response from the agent or error message
137
+ """
124
138
  try:
139
+ message = GlobalTypeConverter.get().convert(chat_request_or_message, to_type=ChatRequest)
140
+
125
141
  # initialize the starting state with the user query
126
- messages: list[BaseMessage] = trim_messages(messages=[m.model_dump() for m in input_message.messages],
142
+ messages: list[BaseMessage] = trim_messages(messages=[m.model_dump() for m in message.messages],
127
143
  max_tokens=config.max_history,
128
144
  strategy="last",
129
145
  token_counter=len,
@@ -139,24 +155,21 @@ async def rewoo_agent_workflow(config: ReWOOAgentWorkflowConfig, builder: Builde
139
155
  # get and return the output from the state
140
156
  state = ReWOOGraphState(**state)
141
157
  output_message = state.result.content
142
- return ChatResponse.from_string(output_message)
143
-
158
+ # Ensure output_message is a string
159
+ if isinstance(output_message, list | dict):
160
+ output_message = str(output_message)
161
+
162
+ # Create usage statistics for the response
163
+ prompt_tokens = sum(len(str(msg.content).split()) for msg in message.messages)
164
+ completion_tokens = len(output_message.split()) if output_message else 0
165
+ total_tokens = prompt_tokens + completion_tokens
166
+ usage = Usage(prompt_tokens=prompt_tokens, completion_tokens=completion_tokens, total_tokens=total_tokens)
167
+ response = ChatResponse.from_string(output_message, usage=usage)
168
+ if chat_request_or_message.is_string:
169
+ return GlobalTypeConverter.get().convert(response, to_type=str)
170
+ return response
144
171
  except Exception as ex:
145
- logger.exception("ReWOO Agent failed with exception: %s", ex)
146
- # here, we can implement custom error messages
147
- if config.verbose:
148
- return ChatResponse.from_string(str(ex))
149
- return ChatResponse.from_string("I seem to be having a problem.")
150
-
151
- if (config.use_openai_api):
152
- yield FunctionInfo.from_fn(_response_fn, description=config.description)
153
-
154
- else:
155
-
156
- async def _str_api_fn(input_message: str) -> str:
157
- oai_input = GlobalTypeConverter.get().try_convert(input_message, to_type=ChatRequest)
158
- oai_output = await _response_fn(oai_input)
159
-
160
- return GlobalTypeConverter.get().try_convert(oai_output, to_type=str)
172
+ logger.error("ReWOO Agent failed with exception: %s", ex)
173
+ raise
161
174
 
162
- yield FunctionInfo.from_fn(_str_api_fn, description=config.description)
175
+ yield FunctionInfo.from_fn(_response_fn, description=config.description)
@@ -19,10 +19,13 @@ import typing
19
19
  from langchain_core.callbacks.base import AsyncCallbackHandler
20
20
  from langchain_core.language_models import BaseChatModel
21
21
  from langchain_core.messages import SystemMessage
22
+ from langchain_core.messages import ToolMessage
22
23
  from langchain_core.messages.base import BaseMessage
23
24
  from langchain_core.runnables import RunnableLambda
24
25
  from langchain_core.runnables.config import RunnableConfig
25
26
  from langchain_core.tools import BaseTool
27
+ from langgraph.graph import StateGraph
28
+ from langgraph.graph.state import CompiledStateGraph
26
29
  from langgraph.prebuilt import ToolNode
27
30
  from pydantic import BaseModel
28
31
  from pydantic import Field
@@ -57,12 +60,14 @@ class ToolCallAgentGraph(DualNodeAgent):
57
60
  detailed_logs: bool = False,
58
61
  log_response_max_chars: int = 1000,
59
62
  handle_tool_errors: bool = True,
63
+ return_direct: list[BaseTool] | None = None,
60
64
  ):
61
65
  super().__init__(llm=llm,
62
66
  tools=tools,
63
67
  callbacks=callbacks,
64
68
  detailed_logs=detailed_logs,
65
69
  log_response_max_chars=log_response_max_chars)
70
+
66
71
  # some LLMs support tool calling
67
72
  # these models accept the tool's input schema and decide when to use a tool based on the input's relevance
68
73
  try:
@@ -85,8 +90,8 @@ class ToolCallAgentGraph(DualNodeAgent):
85
90
  )
86
91
 
87
92
  self.agent = prompt_runnable | self.bound_llm
88
-
89
93
  self.tool_caller = ToolNode(tools, handle_tool_errors=handle_tool_errors)
94
+ self.return_direct = [tool.name for tool in return_direct] if return_direct else []
90
95
  logger.debug("%s Initialized Tool Calling Agent Graph", AGENT_LOG_PREFIX)
91
96
 
92
97
  async def agent_node(self, state: ToolCallAgentGraphState):
@@ -146,13 +151,70 @@ class ToolCallAgentGraph(DualNodeAgent):
146
151
  logger.error("%s Failed to call tool_node: %s", AGENT_LOG_PREFIX, ex)
147
152
  raise
148
153
 
149
- async def build_graph(self):
154
+ async def tool_conditional_edge(self, state: ToolCallAgentGraphState) -> AgentDecision:
155
+ """
156
+ Determines whether to continue to the agent or end graph execution after a tool call.
157
+
158
+ Args:
159
+ state: The current state of the Tool Calling Agent graph containing messages and tool responses.
160
+
161
+ Returns:
162
+ AgentDecision: TOOL to continue to agent for processing, or END to terminate graph execution.
163
+ Returns END if the tool is in return_direct list, otherwise returns TOOL to continue processing.
164
+ """
150
165
  try:
151
- await super()._build_graph(state_schema=ToolCallAgentGraphState)
152
- logger.debug(
153
- "%s Tool Calling Agent Graph built and compiled successfully",
154
- AGENT_LOG_PREFIX,
155
- )
166
+ logger.debug("%s Starting the Tool Conditional Edge", AGENT_LOG_PREFIX)
167
+ if not state.messages:
168
+ logger.debug("%s No messages in state; routing to agent", AGENT_LOG_PREFIX)
169
+ return AgentDecision.TOOL
170
+
171
+ last_message = state.messages[-1]
172
+ # Return directly if this tool is in the return_direct set
173
+ if (self.return_direct and isinstance(last_message, ToolMessage) and last_message.name
174
+ and last_message.name in self.return_direct):
175
+ # Return directly if this tool is in the return_direct list
176
+ logger.debug("%s Tool %s is set to return directly", AGENT_LOG_PREFIX, last_message.name)
177
+ return AgentDecision.END
178
+ else:
179
+ # Continue to agent for processing
180
+ logger.debug("%s Tool response will be processed by agent", AGENT_LOG_PREFIX)
181
+ return AgentDecision.TOOL
182
+ except Exception as ex:
183
+ logger.exception("%s Failed to determine tool conditional edge: %s", AGENT_LOG_PREFIX, ex)
184
+ logger.warning("%s Continuing to agent for processing", AGENT_LOG_PREFIX)
185
+ return AgentDecision.TOOL
186
+
187
+ async def _build_graph(self, state_schema: type) -> CompiledStateGraph:
188
+ try:
189
+ logger.debug("%s Building and compiling the Tool Calling Agent Graph", AGENT_LOG_PREFIX)
190
+
191
+ graph = StateGraph(state_schema)
192
+ graph.add_node("agent", self.agent_node)
193
+ graph.add_node("tool", self.tool_node)
194
+
195
+ if self.return_direct:
196
+ # go to end of graph if tool is set to return directly
197
+ tool_conditional_edge_possible_outputs = {AgentDecision.END: "__end__", AgentDecision.TOOL: "agent"}
198
+ graph.add_conditional_edges("tool", self.tool_conditional_edge, tool_conditional_edge_possible_outputs)
199
+ else:
200
+ # otherwise return to agent after tool call
201
+ graph.add_edge("tool", "agent")
202
+
203
+ conditional_edge_possible_outputs = {AgentDecision.TOOL: "tool", AgentDecision.END: "__end__"}
204
+ graph.add_conditional_edges("agent", self.conditional_edge, conditional_edge_possible_outputs)
205
+
206
+ graph.set_entry_point("agent")
207
+ self.graph = graph.compile()
208
+
209
+ return self.graph
210
+ except Exception as ex:
211
+ logger.error("%s Failed to build Tool Calling Agent Graph: %s", AGENT_LOG_PREFIX, ex)
212
+ raise
213
+
214
+ async def build_graph(self) -> CompiledStateGraph:
215
+ try:
216
+ await self._build_graph(state_schema=ToolCallAgentGraphState)
217
+ logger.debug("%s Tool Calling Agent Graph built and compiled successfully", AGENT_LOG_PREFIX)
156
218
  return self.graph
157
219
  except Exception as ex:
158
220
  logger.error("%s Failed to build Tool Calling Agent Graph: %s", AGENT_LOG_PREFIX, ex)
@@ -171,14 +233,10 @@ def create_tool_calling_agent_prompt(config: "ToolCallAgentWorkflowConfig") -> s
171
233
  """
172
234
  # the Tool Calling Agent prompt can be customized via config option system_prompt and additional_instructions.
173
235
 
174
- if config.system_prompt:
175
- prompt_str = config.system_prompt
176
- else:
177
- prompt_str = ""
178
-
179
- if config.additional_instructions:
180
- prompt_str += f" {config.additional_instructions}"
181
-
182
- if len(prompt_str) > 0:
183
- return prompt_str
236
+ prompt_strs = []
237
+ for msg in [config.system_prompt, config.additional_instructions]:
238
+ if msg is not None:
239
+ prompt_strs.append(msg)
240
+ if prompt_strs:
241
+ return " ".join(prompt_strs)
184
242
  return None
@@ -16,42 +16,44 @@
16
16
  import logging
17
17
 
18
18
  from pydantic import Field
19
- from pydantic import PositiveInt
20
19
 
21
20
  from nat.builder.builder import Builder
22
21
  from nat.builder.framework_enum import LLMFrameworkEnum
23
22
  from nat.builder.function_info import FunctionInfo
24
23
  from nat.cli.register_workflow import register_function
24
+ from nat.data_models.agent import AgentBaseConfig
25
+ from nat.data_models.api_server import ChatRequest
26
+ from nat.data_models.api_server import ChatRequestOrMessage
27
+ from nat.data_models.component_ref import FunctionGroupRef
25
28
  from nat.data_models.component_ref import FunctionRef
26
- from nat.data_models.component_ref import LLMRef
27
- from nat.data_models.function import FunctionBaseConfig
29
+ from nat.utils.type_converter import GlobalTypeConverter
28
30
 
29
31
  logger = logging.getLogger(__name__)
30
32
 
31
33
 
32
- class ToolCallAgentWorkflowConfig(FunctionBaseConfig, name="tool_calling_agent"):
34
+ class ToolCallAgentWorkflowConfig(AgentBaseConfig, name="tool_calling_agent"):
33
35
  """
34
36
  A Tool Calling Agent requires an LLM which supports tool calling. A tool Calling Agent utilizes the tool
35
37
  input parameters to select the optimal tool. Supports handling tool errors.
36
38
  """
37
-
38
- tool_names: list[FunctionRef] = Field(default_factory=list,
39
- description="The list of tools to provide to the tool calling agent.")
40
- llm_name: LLMRef = Field(description="The LLM model to use with the tool calling agent.")
41
- verbose: bool = Field(default=False, description="Set the verbosity of the tool calling agent's logging.")
42
- handle_tool_errors: bool = Field(default=True, description="Specify ability to handle tool calling errors.")
43
39
  description: str = Field(default="Tool Calling Agent Workflow", description="Description of this functions use.")
40
+ tool_names: list[FunctionRef | FunctionGroupRef] = Field(
41
+ default_factory=list, description="The list of tools to provide to the tool calling agent.")
42
+ handle_tool_errors: bool = Field(default=True, description="Specify ability to handle tool calling errors.")
44
43
  max_iterations: int = Field(default=15, description="Number of tool calls before stoping the tool calling agent.")
45
- log_response_max_chars: PositiveInt = Field(
46
- default=1000, description="Maximum number of characters to display in logs when logging tool responses.")
44
+ max_history: int = Field(default=15, description="Maximum number of messages to keep in the conversation history.")
45
+
47
46
  system_prompt: str | None = Field(default=None, description="Provides the system prompt to use with the agent.")
48
47
  additional_instructions: str | None = Field(default=None,
49
48
  description="Additional instructions appended to the system prompt.")
49
+ return_direct: list[FunctionRef] | None = Field(
50
+ default=None, description="List of tool names that should return responses directly without LLM processing.")
50
51
 
51
52
 
52
53
  @register_function(config_type=ToolCallAgentWorkflowConfig, framework_wrappers=[LLMFrameworkEnum.LANGCHAIN])
53
54
  async def tool_calling_agent_workflow(config: ToolCallAgentWorkflowConfig, builder: Builder):
54
- from langchain_core.messages.human import HumanMessage
55
+ from langchain_core.messages import trim_messages
56
+ from langchain_core.messages.base import BaseMessage
55
57
  from langgraph.graph.state import CompiledStateGraph
56
58
 
57
59
  from nat.agent.base import AGENT_LOG_PREFIX
@@ -64,23 +66,46 @@ async def tool_calling_agent_workflow(config: ToolCallAgentWorkflowConfig, build
64
66
  llm = await builder.get_llm(config.llm_name, wrapper_type=LLMFrameworkEnum.LANGCHAIN)
65
67
  # the agent can run any installed tool, simply install the tool and add it to the config file
66
68
  # the sample tools provided can easily be copied or changed
67
- tools = builder.get_tools(tool_names=config.tool_names, wrapper_type=LLMFrameworkEnum.LANGCHAIN)
69
+ tools = await builder.get_tools(tool_names=config.tool_names, wrapper_type=LLMFrameworkEnum.LANGCHAIN)
68
70
  if not tools:
69
71
  raise ValueError(f"No tools specified for Tool Calling Agent '{config.llm_name}'")
70
72
 
73
+ # convert return_direct FunctionRef objects to BaseTool objects
74
+ return_direct_tools = await builder.get_tools(
75
+ tool_names=config.return_direct, wrapper_type=LLMFrameworkEnum.LANGCHAIN) if config.return_direct else None
76
+
71
77
  # construct the Tool Calling Agent Graph from the configured llm, and tools
72
78
  graph: CompiledStateGraph = await ToolCallAgentGraph(llm=llm,
73
79
  tools=tools,
74
80
  prompt=prompt,
75
81
  detailed_logs=config.verbose,
76
82
  log_response_max_chars=config.log_response_max_chars,
77
- handle_tool_errors=config.handle_tool_errors).build_graph()
83
+ handle_tool_errors=config.handle_tool_errors,
84
+ return_direct=return_direct_tools).build_graph()
78
85
 
79
- async def _response_fn(input_message: str) -> str:
86
+ async def _response_fn(chat_request_or_message: ChatRequestOrMessage) -> str:
87
+ """
88
+ Main workflow entry function for the Tool Calling Agent.
89
+
90
+ This function invokes the Tool Calling Agent Graph and returns the response.
91
+
92
+ Args:
93
+ chat_request_or_message (ChatRequestOrMessage): The input message to process
94
+
95
+ Returns:
96
+ str: The response from the agent or error message
97
+ """
80
98
  try:
99
+ message = GlobalTypeConverter.get().convert(chat_request_or_message, to_type=ChatRequest)
100
+
81
101
  # initialize the starting state with the user query
82
- input_message = HumanMessage(content=input_message)
83
- state = ToolCallAgentGraphState(messages=[input_message])
102
+ messages: list[BaseMessage] = trim_messages(messages=[m.model_dump() for m in message.messages],
103
+ max_tokens=config.max_history,
104
+ strategy="last",
105
+ token_counter=len,
106
+ start_on="human",
107
+ include_system=True)
108
+ state = ToolCallAgentGraphState(messages=messages)
84
109
 
85
110
  # run the Tool Calling Agent Graph
86
111
  state = await graph.ainvoke(state, config={'recursion_limit': (config.max_iterations + 1) * 2})
@@ -91,12 +116,10 @@ async def tool_calling_agent_workflow(config: ToolCallAgentWorkflowConfig, build
91
116
  # get and return the output from the state
92
117
  state = ToolCallAgentGraphState(**state)
93
118
  output_message = state.messages[-1]
94
- return output_message.content
119
+ return str(output_message.content)
95
120
  except Exception as ex:
96
- logger.exception("%s Tool Calling Agent failed with exception: %s", AGENT_LOG_PREFIX, ex)
97
- if config.verbose:
98
- return str(ex)
99
- return "I seem to be having a problem."
121
+ logger.error("%s Tool Calling Agent failed with exception: %s", AGENT_LOG_PREFIX, ex)
122
+ raise
100
123
 
101
124
  try:
102
125
  yield FunctionInfo.from_fn(_response_fn, description=config.description)
@@ -15,8 +15,6 @@
15
15
 
16
16
  import logging
17
17
 
18
- from pydantic import SecretStr
19
-
20
18
  from nat.authentication.api_key.api_key_auth_provider_config import APIKeyAuthProviderConfig
21
19
  from nat.authentication.interfaces import AuthProviderBase
22
20
  from nat.data_models.authentication import AuthResult
@@ -29,11 +27,10 @@ logger = logging.getLogger(__name__)
29
27
  class APIKeyAuthProvider(AuthProviderBase[APIKeyAuthProviderConfig]):
30
28
 
31
29
  # fmt: off
32
- def __init__(self,
33
- config: APIKeyAuthProviderConfig,
34
- config_name: str | None = None) -> None:
30
+ def __init__(self, config: APIKeyAuthProviderConfig, config_name: str | None = None) -> None:
35
31
  assert isinstance(config, APIKeyAuthProviderConfig), ("Config is not APIKeyAuthProviderConfig")
36
32
  super().__init__(config)
33
+
37
34
  # fmt: on
38
35
 
39
36
  async def _construct_authentication_header(self) -> BearerTokenCred:
@@ -58,14 +55,12 @@ class APIKeyAuthProvider(AuthProviderBase[APIKeyAuthProviderConfig]):
58
55
  header_auth_scheme = config.auth_scheme
59
56
 
60
57
  if header_auth_scheme == HeaderAuthScheme.BEARER:
61
- return BearerTokenCred(token=SecretStr(f"{config.raw_key}"),
58
+ return BearerTokenCred(token=config.raw_key,
62
59
  scheme=HeaderAuthScheme.BEARER.value,
63
60
  header_name=AUTHORIZATION_HEADER)
64
61
 
65
62
  if header_auth_scheme == HeaderAuthScheme.X_API_KEY:
66
- return BearerTokenCred(token=SecretStr(f"{config.raw_key}"),
67
- scheme=HeaderAuthScheme.X_API_KEY.value,
68
- header_name='')
63
+ return BearerTokenCred(token=config.raw_key, scheme=HeaderAuthScheme.X_API_KEY.value, header_name='')
69
64
 
70
65
  if header_auth_scheme == HeaderAuthScheme.CUSTOM:
71
66
  if not config.custom_header_name:
@@ -74,13 +69,13 @@ class APIKeyAuthProvider(AuthProviderBase[APIKeyAuthProviderConfig]):
74
69
  if not config.custom_header_prefix:
75
70
  raise ValueError('custom_header_prefix required when using header_auth_scheme=CUSTOM')
76
71
 
77
- return BearerTokenCred(token=SecretStr(f"{config.raw_key}"),
72
+ return BearerTokenCred(token=config.raw_key,
78
73
  scheme=config.custom_header_prefix,
79
74
  header_name=config.custom_header_name)
80
75
 
81
76
  raise ValueError(f"Unsupported header auth scheme: {header_auth_scheme}")
82
77
 
83
- async def authenticate(self, user_id: str | None = None) -> AuthResult | None:
78
+ async def authenticate(self, user_id: str | None = None, **kwargs) -> AuthResult | None:
84
79
  """
85
80
  Authenticate the user using the API key credentials.
86
81
 
@@ -25,6 +25,7 @@ from nat.authentication.exceptions.api_key_exceptions import HeaderNameFieldErro
25
25
  from nat.authentication.exceptions.api_key_exceptions import HeaderPrefixFieldError
26
26
  from nat.data_models.authentication import AuthProviderBaseConfig
27
27
  from nat.data_models.authentication import HeaderAuthScheme
28
+ from nat.data_models.common import SerializableSecretStr
28
29
 
29
30
  logger = logging.getLogger(__name__)
30
31
 
@@ -37,8 +38,9 @@ class APIKeyAuthProviderConfig(AuthProviderBaseConfig, name="api_key"):
37
38
  API Key authentication configuration model.
38
39
  """
39
40
 
40
- raw_key: str = Field(description=("Raw API token or credential to be injected into the request parameter. "
41
- "Used for 'bearer','x-api-key','custom', and other schemes. "))
41
+ raw_key: SerializableSecretStr = Field(
42
+ description=("Raw API token or credential to be injected into the request parameter. "
43
+ "Used for 'bearer','x-api-key','custom', and other schemes. "))
42
44
 
43
45
  auth_scheme: HeaderAuthScheme = Field(default=HeaderAuthScheme.BEARER,
44
46
  description=("The HTTP authentication scheme to use. "
@@ -53,7 +55,7 @@ class APIKeyAuthProviderConfig(AuthProviderBaseConfig, name="api_key"):
53
55
 
54
56
  @field_validator('raw_key')
55
57
  @classmethod
56
- def validate_raw_key(cls, value: str) -> str:
58
+ def validate_raw_key(cls, value: SerializableSecretStr) -> SerializableSecretStr:
57
59
  if not value:
58
60
  raise APIKeyFieldError('value_missing', 'raw_key field value is required.')
59
61
 
@@ -63,11 +65,12 @@ class APIKeyAuthProviderConfig(AuthProviderBaseConfig, name="api_key"):
63
65
  'raw_key field value must be at least 8 characters long for security. '
64
66
  f'Got: {len(value)} characters.')
65
67
 
66
- if len(value.strip()) != len(value):
68
+ str_value = value.get_secret_value()
69
+ if len(str_value.strip()) != len(value):
67
70
  raise APIKeyFieldError('whitespace_found',
68
71
  'raw_key field value cannot have leading or trailing whitespace.')
69
72
 
70
- if any(c in string.whitespace for c in value):
73
+ if any(c in string.whitespace for c in str_value):
71
74
  raise APIKeyFieldError('contains_whitespace', 'raw_key must not contain any '
72
75
  'whitespace characters.')
73
76
 
@@ -0,0 +1,14 @@
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.