nvidia-nat 1.3.0a20250909__py3-none-any.whl → 1.3.0a20250917__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.
- nat/agent/base.py +11 -6
- nat/agent/dual_node.py +2 -2
- nat/agent/prompt_optimizer/prompt.py +68 -0
- nat/agent/prompt_optimizer/register.py +149 -0
- nat/agent/react_agent/agent.py +1 -1
- nat/agent/react_agent/register.py +17 -7
- nat/agent/reasoning_agent/reasoning_agent.py +6 -1
- nat/agent/register.py +2 -0
- nat/agent/rewoo_agent/agent.py +6 -3
- nat/agent/rewoo_agent/register.py +16 -10
- nat/agent/router_agent/__init__.py +0 -0
- nat/agent/router_agent/agent.py +329 -0
- nat/agent/router_agent/prompt.py +48 -0
- nat/agent/router_agent/register.py +97 -0
- nat/agent/tool_calling_agent/agent.py +69 -7
- nat/agent/tool_calling_agent/register.py +17 -9
- nat/builder/builder.py +27 -4
- nat/builder/component_utils.py +7 -3
- nat/builder/function.py +167 -0
- nat/builder/function_info.py +1 -1
- nat/builder/workflow.py +5 -0
- nat/builder/workflow_builder.py +213 -16
- nat/cli/commands/optimize.py +90 -0
- nat/cli/commands/workflow/templates/config.yml.j2 +0 -1
- nat/cli/commands/workflow/workflow_commands.py +5 -8
- nat/cli/entrypoint.py +2 -0
- nat/cli/register_workflow.py +38 -4
- nat/cli/type_registry.py +71 -0
- nat/data_models/api_server.py +1 -1
- nat/data_models/component.py +2 -0
- nat/data_models/component_ref.py +11 -0
- nat/data_models/config.py +40 -16
- nat/data_models/function.py +34 -0
- nat/data_models/function_dependencies.py +8 -0
- nat/data_models/optimizable.py +119 -0
- nat/data_models/optimizer.py +149 -0
- nat/data_models/temperature_mixin.py +4 -3
- nat/data_models/top_p_mixin.py +4 -3
- nat/embedder/nim_embedder.py +1 -1
- nat/embedder/openai_embedder.py +1 -1
- nat/eval/config.py +1 -1
- nat/eval/evaluate.py +5 -1
- nat/eval/register.py +4 -0
- 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/experimental/test_time_compute/functions/plan_select_execute_function.py +5 -1
- nat/front_ends/fastapi/dask_client_mixin.py +43 -0
- nat/front_ends/fastapi/fastapi_front_end_config.py +14 -3
- nat/front_ends/fastapi/fastapi_front_end_plugin.py +111 -3
- nat/front_ends/fastapi/fastapi_front_end_plugin_worker.py +243 -228
- nat/front_ends/fastapi/job_store.py +518 -99
- nat/front_ends/fastapi/main.py +11 -19
- nat/front_ends/fastapi/utils.py +57 -0
- nat/front_ends/mcp/mcp_front_end_plugin_worker.py +3 -2
- nat/llm/aws_bedrock_llm.py +15 -4
- nat/llm/nim_llm.py +14 -3
- nat/llm/openai_llm.py +8 -1
- nat/observability/exporter/processing_exporter.py +29 -55
- nat/observability/mixin/redaction_config_mixin.py +5 -4
- nat/observability/mixin/tagging_config_mixin.py +26 -14
- nat/observability/mixin/type_introspection_mixin.py +401 -107
- nat/observability/processor/processor.py +3 -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 +21 -14
- nat/profiler/decorators/framework_wrapper.py +9 -6
- 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 +149 -0
- nat/profiler/parameter_optimization/parameter_selection.py +108 -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/utils.py +3 -1
- nat/tool/chat_completion.py +5 -2
- nat/tool/document_search.py +1 -1
- nat/tool/github_tools.py +450 -0
- nat/tool/register.py +2 -7
- nat/utils/callable_utils.py +70 -0
- nat/utils/exception_handlers/automatic_retries.py +103 -48
- nat/utils/type_utils.py +4 -0
- {nvidia_nat-1.3.0a20250909.dist-info → nvidia_nat-1.3.0a20250917.dist-info}/METADATA +8 -1
- {nvidia_nat-1.3.0a20250909.dist-info → nvidia_nat-1.3.0a20250917.dist-info}/RECORD +94 -74
- nat/observability/processor/header_redaction_processor.py +0 -123
- nat/observability/processor/redaction_processor.py +0 -77
- 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/github_tools → agent/prompt_optimizer}/__init__.py +0 -0
- {nvidia_nat-1.3.0a20250909.dist-info → nvidia_nat-1.3.0a20250917.dist-info}/WHEEL +0 -0
- {nvidia_nat-1.3.0a20250909.dist-info → nvidia_nat-1.3.0a20250917.dist-info}/entry_points.txt +0 -0
- {nvidia_nat-1.3.0a20250909.dist-info → nvidia_nat-1.3.0a20250917.dist-info}/licenses/LICENSE-3rd-party.txt +0 -0
- {nvidia_nat-1.3.0a20250909.dist-info → nvidia_nat-1.3.0a20250917.dist-info}/licenses/LICENSE.md +0 -0
- {nvidia_nat-1.3.0a20250909.dist-info → nvidia_nat-1.3.0a20250917.dist-info}/top_level.txt +0 -0
nat/agent/base.py
CHANGED
|
@@ -27,9 +27,10 @@ from langchain_core.language_models import BaseChatModel
|
|
|
27
27
|
from langchain_core.messages import AIMessage
|
|
28
28
|
from langchain_core.messages import BaseMessage
|
|
29
29
|
from langchain_core.messages import ToolMessage
|
|
30
|
+
from langchain_core.runnables import Runnable
|
|
30
31
|
from langchain_core.runnables import RunnableConfig
|
|
31
32
|
from langchain_core.tools import BaseTool
|
|
32
|
-
from langgraph.graph.
|
|
33
|
+
from langgraph.graph.state import CompiledStateGraph
|
|
33
34
|
|
|
34
35
|
logger = logging.getLogger(__name__)
|
|
35
36
|
|
|
@@ -107,21 +108,25 @@ class BaseAgent(ABC):
|
|
|
107
108
|
|
|
108
109
|
return AIMessage(content=output_message)
|
|
109
110
|
|
|
110
|
-
async def _call_llm(self,
|
|
111
|
+
async def _call_llm(self, llm: Runnable, inputs: dict[str, Any], config: RunnableConfig | None = None) -> AIMessage:
|
|
111
112
|
"""
|
|
112
113
|
Call the LLM directly. Retry logic is handled automatically by the underlying LLM client.
|
|
113
114
|
|
|
114
115
|
Parameters
|
|
115
116
|
----------
|
|
116
|
-
|
|
117
|
-
The
|
|
117
|
+
llm : Runnable
|
|
118
|
+
The LLM runnable (prompt | llm or similar)
|
|
119
|
+
inputs : dict[str, Any]
|
|
120
|
+
The inputs to pass to the runnable
|
|
121
|
+
config : RunnableConfig | None
|
|
122
|
+
The config to pass to the runnable (should include callbacks)
|
|
118
123
|
|
|
119
124
|
Returns
|
|
120
125
|
-------
|
|
121
126
|
AIMessage
|
|
122
127
|
The LLM response
|
|
123
128
|
"""
|
|
124
|
-
response = await
|
|
129
|
+
response = await llm.ainvoke(inputs, config=config)
|
|
125
130
|
return AIMessage(content=str(response.content))
|
|
126
131
|
|
|
127
132
|
async def _call_tool(self,
|
|
@@ -256,5 +261,5 @@ class BaseAgent(ABC):
|
|
|
256
261
|
return "\n".join([f"{message.type}: {message.content}" for message in messages[:-1]])
|
|
257
262
|
|
|
258
263
|
@abstractmethod
|
|
259
|
-
async def _build_graph(self, state_schema: type) ->
|
|
264
|
+
async def _build_graph(self, state_schema: type) -> CompiledStateGraph:
|
|
260
265
|
pass
|
nat/agent/dual_node.py
CHANGED
|
@@ -20,7 +20,7 @@ from langchain_core.callbacks import AsyncCallbackHandler
|
|
|
20
20
|
from langchain_core.language_models import BaseChatModel
|
|
21
21
|
from langchain_core.tools import BaseTool
|
|
22
22
|
from langgraph.graph import StateGraph
|
|
23
|
-
from langgraph.graph.
|
|
23
|
+
from langgraph.graph.state import CompiledStateGraph
|
|
24
24
|
from pydantic import BaseModel
|
|
25
25
|
|
|
26
26
|
from .base import AgentDecision
|
|
@@ -55,7 +55,7 @@ class DualNodeAgent(BaseAgent):
|
|
|
55
55
|
async def conditional_edge(self, state: BaseModel) -> str:
|
|
56
56
|
pass
|
|
57
57
|
|
|
58
|
-
async def _build_graph(self, state_schema) ->
|
|
58
|
+
async def _build_graph(self, state_schema: type) -> CompiledStateGraph:
|
|
59
59
|
log.debug("Building and compiling the Agent Graph")
|
|
60
60
|
|
|
61
61
|
graph = StateGraph(state_schema)
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (c) 2024-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
|
+
# flake8: noqa W291
|
|
16
|
+
|
|
17
|
+
mutator_prompt = """
|
|
18
|
+
|
|
19
|
+
## CORE DIRECTIVES
|
|
20
|
+
- **Preserve the original objective and task.** Do not change what the prompt is meant to accomplish.
|
|
21
|
+
- **Keep the intent intact.** The improved prompt must solve the same problem as the original.
|
|
22
|
+
- **Do not invent new goals.** Only improve clarity, structure, constraints, and usability.
|
|
23
|
+
- **Do not drop critical instructions.** Everything essential from the original prompt must remain.
|
|
24
|
+
- **Return only the mutated prompt text.** No rationale, no diffs, no explanations.
|
|
25
|
+
- **Be Creative within bounds.** You may rephrase, reorganize, and enhance, but not alter meaning.
|
|
26
|
+
- **DO NOT use curly braces in your prompt** for anything other than existing variables in the prompt as the string
|
|
27
|
+
will be treated as an f-string.
|
|
28
|
+
- **Examples are a good idea** if the original prompt lacks them. They help clarify expected output.
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## IMPROVEMENT HINTS
|
|
33
|
+
When modifying, apply these principles:
|
|
34
|
+
1. **Clarity & Precision** – remove vague language, strengthen directives.
|
|
35
|
+
2. **Structure & Flow** – order sections as: *Objective → Constraints → Tools → Steps → Output Schema → Examples*.
|
|
36
|
+
3. **Schema Adherence** – enforce a single canonical output schema (JSON/XML) with `schema_version`.
|
|
37
|
+
4. **Tool Governance** – clarify when/how tools are used, their inputs/outputs, and fallback behavior.
|
|
38
|
+
5. **Error Handling** – specify behavior if tools fail or inputs are insufficient.
|
|
39
|
+
6. **Budget Awareness** – minimize verbosity, respect token/latency limits.
|
|
40
|
+
7. **Safety** – include refusals for unsafe requests, enforce compliance with rules.
|
|
41
|
+
8. **Consistency** – avoid format drift; always maintain the same schema.
|
|
42
|
+
9. **Integrity** – confirm the task, objective, and intent are preserved.
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## MUTATION OPERATORS
|
|
47
|
+
You may:
|
|
48
|
+
- **Tighten** (remove fluff, redundancies)
|
|
49
|
+
- **Reorder** (improve logical flow)
|
|
50
|
+
- **Constrain** (add explicit rules/limits)
|
|
51
|
+
- **Harden** (improve error handling/fallbacks)
|
|
52
|
+
- **Defuse** (replace ambiguous verbs with measurable actions)
|
|
53
|
+
- **Format-lock** (wrap outputs in JSON/XML fenced blocks)
|
|
54
|
+
- **Example-ify** (add examples if missing or weak)
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## INPUT
|
|
59
|
+
Here is the prompt to mutate:
|
|
60
|
+
{original_prompt}
|
|
61
|
+
|
|
62
|
+
## OBJECTIVE
|
|
63
|
+
The prompt must acheive the following objective:
|
|
64
|
+
{objective}
|
|
65
|
+
|
|
66
|
+
The modified prompt is: \n
|
|
67
|
+
|
|
68
|
+
"""
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (c) 2021-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
|
|
16
|
+
from pydantic import Field
|
|
17
|
+
|
|
18
|
+
from nat.builder.builder import Builder
|
|
19
|
+
from nat.builder.framework_enum import LLMFrameworkEnum
|
|
20
|
+
from nat.builder.function_info import FunctionInfo
|
|
21
|
+
from nat.cli.register_workflow import register_function
|
|
22
|
+
from nat.data_models.component_ref import LLMRef
|
|
23
|
+
from nat.data_models.function import FunctionBaseConfig
|
|
24
|
+
from nat.profiler.parameter_optimization.prompt_optimizer import PromptOptimizerInputSchema
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class PromptOptimizerConfig(FunctionBaseConfig, name="prompt_init"):
|
|
28
|
+
|
|
29
|
+
optimizer_llm: LLMRef = Field(description="LLM to use for prompt optimization")
|
|
30
|
+
optimizer_prompt: str = Field(
|
|
31
|
+
description="Prompt template for the optimizer",
|
|
32
|
+
default=(
|
|
33
|
+
"You are an expert at optimizing prompts for LLMs. "
|
|
34
|
+
"Your task is to take a given prompt and suggest an optimized version of it. "
|
|
35
|
+
"Note that the prompt might be a template with variables and curly braces. Remember to always keep the "
|
|
36
|
+
"variables and curly braces in the prompt the same. Only modify the instructions in the prompt that are"
|
|
37
|
+
"not variables. The system is meant to achieve the following objective\n"
|
|
38
|
+
"{system_objective}\n Of which, the prompt is one part. The details of the prompt and context as below.\n"))
|
|
39
|
+
system_objective: str = Field(description="Objective of the workflow")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@register_function(config_type=PromptOptimizerConfig, framework_wrappers=[LLMFrameworkEnum.LANGCHAIN])
|
|
43
|
+
async def prompt_optimizer_function(config: PromptOptimizerConfig, builder: Builder):
|
|
44
|
+
"""
|
|
45
|
+
Function to optimize prompts for LLMs.
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
try:
|
|
49
|
+
from langchain_core.prompts import PromptTemplate
|
|
50
|
+
|
|
51
|
+
from .prompt import mutator_prompt
|
|
52
|
+
except ImportError as exc:
|
|
53
|
+
raise ImportError("langchain-core is not installed. Please install it to use MultiLLMPlanner.\n"
|
|
54
|
+
"This error can be resolve by installing nvidia-nat[langchain]") from exc
|
|
55
|
+
|
|
56
|
+
llm = await builder.get_llm(config.optimizer_llm, wrapper_type=LLMFrameworkEnum.LANGCHAIN)
|
|
57
|
+
|
|
58
|
+
template = PromptTemplate(template=config.optimizer_prompt,
|
|
59
|
+
input_variables=["system_objective"],
|
|
60
|
+
validate_template=True)
|
|
61
|
+
|
|
62
|
+
base_prompt: str = (await template.ainvoke(input={"system_objective": config.system_objective})).to_string()
|
|
63
|
+
prompt_extension_template = PromptTemplate(template=mutator_prompt,
|
|
64
|
+
input_variables=["original_prompt", "objective"],
|
|
65
|
+
validate_template=True)
|
|
66
|
+
|
|
67
|
+
async def _inner(input_message: PromptOptimizerInputSchema) -> str:
|
|
68
|
+
"""
|
|
69
|
+
Optimize the prompt using the provided LLM.
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
original_prompt = input_message.original_prompt
|
|
73
|
+
prompt_objective = input_message.objective
|
|
74
|
+
|
|
75
|
+
prompt_extension = (await prompt_extension_template.ainvoke(input={
|
|
76
|
+
"original_prompt": original_prompt,
|
|
77
|
+
"objective": prompt_objective,
|
|
78
|
+
})).to_string()
|
|
79
|
+
|
|
80
|
+
prompt = f"{base_prompt}\n\n{prompt_extension}"
|
|
81
|
+
|
|
82
|
+
optimized_prompt = await llm.ainvoke(prompt)
|
|
83
|
+
return optimized_prompt.content
|
|
84
|
+
|
|
85
|
+
yield FunctionInfo.from_fn(
|
|
86
|
+
fn=_inner,
|
|
87
|
+
description="Optimize prompts for LLMs using a feedback LLM.",
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class PromptRecombinerConfig(FunctionBaseConfig, name="prompt_recombiner"):
|
|
92
|
+
|
|
93
|
+
optimizer_llm: LLMRef = Field(description="LLM to use for prompt recombination")
|
|
94
|
+
optimizer_prompt: str = Field(
|
|
95
|
+
description="Prompt template for the recombiner",
|
|
96
|
+
default=("You are an expert at combining prompt instructions for LLMs. "
|
|
97
|
+
"Your task is to merge two prompts for the same objective into a single, stronger prompt. "
|
|
98
|
+
"Do not introduce new variables or modify existing placeholders."),
|
|
99
|
+
)
|
|
100
|
+
system_objective: str = Field(description="Objective of the workflow")
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
@register_function(config_type=PromptRecombinerConfig, framework_wrappers=[LLMFrameworkEnum.LANGCHAIN])
|
|
104
|
+
async def prompt_recombiner_function(config: PromptRecombinerConfig, builder: Builder):
|
|
105
|
+
"""
|
|
106
|
+
Function to recombine two parent prompts into a child prompt using the optimizer LLM.
|
|
107
|
+
Uses the same base template and objective instructions.
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
try:
|
|
111
|
+
from langchain_core.prompts import PromptTemplate
|
|
112
|
+
except ImportError as exc:
|
|
113
|
+
raise ImportError("langchain-core is not installed. Please install it to use MultiLLMPlanner.\n"
|
|
114
|
+
"This error can be resolve by installing nvidia-nat[langchain].") from exc
|
|
115
|
+
|
|
116
|
+
llm = await builder.get_llm(config.optimizer_llm, wrapper_type=LLMFrameworkEnum.LANGCHAIN)
|
|
117
|
+
|
|
118
|
+
template = PromptTemplate(template=config.optimizer_prompt,
|
|
119
|
+
input_variables=["system_objective"],
|
|
120
|
+
validate_template=True)
|
|
121
|
+
|
|
122
|
+
base_prompt: str = (await template.ainvoke(input={"system_objective": config.system_objective})).to_string()
|
|
123
|
+
|
|
124
|
+
class RecombineSchema(PromptOptimizerInputSchema):
|
|
125
|
+
parent_b: str | None = None
|
|
126
|
+
|
|
127
|
+
async def _inner(input_message: RecombineSchema) -> str:
|
|
128
|
+
parent_a = input_message.original_prompt
|
|
129
|
+
parent_b = input_message.parent_b or ""
|
|
130
|
+
prompt_objective = input_message.objective
|
|
131
|
+
|
|
132
|
+
prompt = (
|
|
133
|
+
f"{base_prompt}\n\n"
|
|
134
|
+
"We are performing genetic recombination between two prompts that satisfy the same objective.\n"
|
|
135
|
+
f"Objective: {prompt_objective}\n\n"
|
|
136
|
+
f"Parent A:\n{parent_a}\n\n"
|
|
137
|
+
f"Parent B:\n{parent_b}\n\n"
|
|
138
|
+
"Combine the strongest instructions and phrasing from both parents to produce a single, coherent child "
|
|
139
|
+
"prompt.\n"
|
|
140
|
+
"Maintain variables and placeholders unchanged.\n"
|
|
141
|
+
"Return only the child prompt text, with no additional commentary.")
|
|
142
|
+
|
|
143
|
+
child_prompt = await llm.ainvoke(prompt)
|
|
144
|
+
return child_prompt.content
|
|
145
|
+
|
|
146
|
+
yield FunctionInfo.from_fn(
|
|
147
|
+
fn=_inner,
|
|
148
|
+
description="Recombine two prompts into a stronger child prompt.",
|
|
149
|
+
)
|
nat/agent/react_agent/agent.py
CHANGED
|
@@ -25,22 +25,26 @@ from nat.builder.function_info import FunctionInfo
|
|
|
25
25
|
from nat.cli.register_workflow import register_function
|
|
26
26
|
from nat.data_models.api_server import ChatRequest
|
|
27
27
|
from nat.data_models.api_server import ChatResponse
|
|
28
|
+
from nat.data_models.component_ref import FunctionGroupRef
|
|
28
29
|
from nat.data_models.component_ref import FunctionRef
|
|
29
30
|
from nat.data_models.component_ref import LLMRef
|
|
30
31
|
from nat.data_models.function import FunctionBaseConfig
|
|
32
|
+
from nat.data_models.optimizable import OptimizableField
|
|
33
|
+
from nat.data_models.optimizable import OptimizableMixin
|
|
34
|
+
from nat.data_models.optimizable import SearchSpace
|
|
31
35
|
from nat.utils.type_converter import GlobalTypeConverter
|
|
32
36
|
|
|
33
37
|
logger = logging.getLogger(__name__)
|
|
34
38
|
|
|
35
39
|
|
|
36
|
-
class ReActAgentWorkflowConfig(FunctionBaseConfig, name="react_agent"):
|
|
40
|
+
class ReActAgentWorkflowConfig(FunctionBaseConfig, OptimizableMixin, name="react_agent"):
|
|
37
41
|
"""
|
|
38
42
|
Defines a NAT function that uses a ReAct Agent performs reasoning inbetween tool calls, and utilizes the
|
|
39
43
|
tool names and descriptions to select the optimal tool.
|
|
40
44
|
"""
|
|
41
45
|
|
|
42
|
-
tool_names: list[FunctionRef] = Field(
|
|
43
|
-
|
|
46
|
+
tool_names: list[FunctionRef | FunctionGroupRef] = Field(
|
|
47
|
+
default_factory=list, description="The list of tools to provide to the react agent.")
|
|
44
48
|
llm_name: LLMRef = Field(description="The LLM model to use with the react agent.")
|
|
45
49
|
verbose: bool = Field(default=False, description="Set the verbosity of the react agent's logging.")
|
|
46
50
|
retry_agent_response_parsing_errors: bool = Field(
|
|
@@ -75,15 +79,21 @@ class ReActAgentWorkflowConfig(FunctionBaseConfig, name="react_agent"):
|
|
|
75
79
|
use_openai_api: bool = Field(default=False,
|
|
76
80
|
description=("Use OpenAI API for the input/output types to the function. "
|
|
77
81
|
"If False, strings will be used."))
|
|
78
|
-
additional_instructions: str | None =
|
|
79
|
-
default=None,
|
|
82
|
+
additional_instructions: str | None = OptimizableField(
|
|
83
|
+
default=None,
|
|
84
|
+
description="Additional instructions to provide to the agent in addition to the base prompt.",
|
|
85
|
+
space=SearchSpace(
|
|
86
|
+
is_prompt=True,
|
|
87
|
+
prompt="No additional instructions.",
|
|
88
|
+
prompt_purpose="Additional instructions to provide to the agent in addition to the base prompt.",
|
|
89
|
+
))
|
|
80
90
|
|
|
81
91
|
|
|
82
92
|
@register_function(config_type=ReActAgentWorkflowConfig, framework_wrappers=[LLMFrameworkEnum.LANGCHAIN])
|
|
83
93
|
async def react_agent_workflow(config: ReActAgentWorkflowConfig, builder: Builder):
|
|
84
94
|
from langchain.schema import BaseMessage
|
|
85
95
|
from langchain_core.messages import trim_messages
|
|
86
|
-
from langgraph.graph.
|
|
96
|
+
from langgraph.graph.state import CompiledStateGraph
|
|
87
97
|
|
|
88
98
|
from nat.agent.base import AGENT_LOG_PREFIX
|
|
89
99
|
from nat.agent.react_agent.agent import ReActAgentGraph
|
|
@@ -101,7 +111,7 @@ async def react_agent_workflow(config: ReActAgentWorkflowConfig, builder: Builde
|
|
|
101
111
|
raise ValueError(f"No tools specified for ReAct Agent '{config.llm_name}'")
|
|
102
112
|
# configure callbacks, for sending intermediate steps
|
|
103
113
|
# construct the ReAct Agent Graph from the configured llm, prompt, and tools
|
|
104
|
-
graph:
|
|
114
|
+
graph: CompiledStateGraph = await ReActAgentGraph(
|
|
105
115
|
llm=llm,
|
|
106
116
|
prompt=prompt,
|
|
107
117
|
tools=tools,
|
|
@@ -113,7 +113,12 @@ async def build_reasoning_function(config: ReasoningFunctionConfig, builder: Bui
|
|
|
113
113
|
f"function without a description.")
|
|
114
114
|
|
|
115
115
|
# Get the function dependencies of the augmented function
|
|
116
|
-
|
|
116
|
+
function_dependencies = builder.get_function_dependencies(config.augmented_fn)
|
|
117
|
+
function_used_tools = set()
|
|
118
|
+
function_used_tools.update(function_dependencies.functions)
|
|
119
|
+
for function_group in function_dependencies.function_groups:
|
|
120
|
+
function_used_tools.update(builder.get_function_group_dependencies(function_group).functions)
|
|
121
|
+
|
|
117
122
|
tool_names_with_desc: list[tuple[str, str]] = []
|
|
118
123
|
|
|
119
124
|
for tool in function_used_tools:
|
nat/agent/register.py
CHANGED
|
@@ -16,7 +16,9 @@
|
|
|
16
16
|
# flake8: noqa
|
|
17
17
|
|
|
18
18
|
# Import any workflows which need to be automatically registered here
|
|
19
|
+
from .prompt_optimizer import register as prompt_optimizer
|
|
19
20
|
from .react_agent import register as react_agent
|
|
20
21
|
from .reasoning_agent import reasoning_agent
|
|
21
22
|
from .rewoo_agent import register as rewoo_agent
|
|
23
|
+
from .router_agent import register as router_agent
|
|
22
24
|
from .tool_calling_agent import register as tool_calling_agent
|
nat/agent/rewoo_agent/agent.py
CHANGED
|
@@ -27,6 +27,7 @@ from langchain_core.prompts.chat import ChatPromptTemplate
|
|
|
27
27
|
from langchain_core.runnables.config import RunnableConfig
|
|
28
28
|
from langchain_core.tools import BaseTool
|
|
29
29
|
from langgraph.graph import StateGraph
|
|
30
|
+
from langgraph.graph.state import CompiledStateGraph
|
|
30
31
|
from pydantic import BaseModel
|
|
31
32
|
from pydantic import Field
|
|
32
33
|
|
|
@@ -67,7 +68,8 @@ class ReWOOAgentGraph(BaseAgent):
|
|
|
67
68
|
use_tool_schema: bool = True,
|
|
68
69
|
callbacks: list[AsyncCallbackHandler] | None = None,
|
|
69
70
|
detailed_logs: bool = False,
|
|
70
|
-
log_response_max_chars: int = 1000
|
|
71
|
+
log_response_max_chars: int = 1000,
|
|
72
|
+
tool_call_max_retries: int = 3):
|
|
71
73
|
super().__init__(llm=llm,
|
|
72
74
|
tools=tools,
|
|
73
75
|
callbacks=callbacks,
|
|
@@ -93,6 +95,7 @@ class ReWOOAgentGraph(BaseAgent):
|
|
|
93
95
|
self.planner_prompt = planner_prompt.partial(tools=tool_names_and_descriptions, tool_names=tool_names)
|
|
94
96
|
self.solver_prompt = solver_prompt
|
|
95
97
|
self.tools_dict = {tool.name: tool for tool in tools}
|
|
98
|
+
self.tool_call_max_retries = tool_call_max_retries
|
|
96
99
|
|
|
97
100
|
logger.debug("%s Initialized ReWOO Agent Graph", AGENT_LOG_PREFIX)
|
|
98
101
|
|
|
@@ -268,7 +271,7 @@ class ReWOOAgentGraph(BaseAgent):
|
|
|
268
271
|
tool_response = await self._call_tool(requested_tool,
|
|
269
272
|
tool_input_parsed,
|
|
270
273
|
RunnableConfig(callbacks=self.callbacks),
|
|
271
|
-
max_retries=
|
|
274
|
+
max_retries=self.tool_call_max_retries)
|
|
272
275
|
|
|
273
276
|
if self.detailed_logs:
|
|
274
277
|
self._log_tool_response(requested_tool.name, tool_input_parsed, str(tool_response))
|
|
@@ -341,7 +344,7 @@ class ReWOOAgentGraph(BaseAgent):
|
|
|
341
344
|
logger.warning("%s Ending graph traversal", AGENT_LOG_PREFIX)
|
|
342
345
|
return AgentDecision.END
|
|
343
346
|
|
|
344
|
-
async def _build_graph(self, state_schema):
|
|
347
|
+
async def _build_graph(self, state_schema: type) -> CompiledStateGraph:
|
|
345
348
|
try:
|
|
346
349
|
logger.debug("%s Building and compiling the ReWOO Graph", AGENT_LOG_PREFIX)
|
|
347
350
|
|
|
@@ -25,6 +25,7 @@ from nat.builder.function_info import FunctionInfo
|
|
|
25
25
|
from nat.cli.register_workflow import register_function
|
|
26
26
|
from nat.data_models.api_server import ChatRequest
|
|
27
27
|
from nat.data_models.api_server import ChatResponse
|
|
28
|
+
from nat.data_models.component_ref import FunctionGroupRef
|
|
28
29
|
from nat.data_models.component_ref import FunctionRef
|
|
29
30
|
from nat.data_models.component_ref import LLMRef
|
|
30
31
|
from nat.data_models.function import FunctionBaseConfig
|
|
@@ -39,8 +40,8 @@ class ReWOOAgentWorkflowConfig(FunctionBaseConfig, name="rewoo_agent"):
|
|
|
39
40
|
tool names and descriptions to select the optimal tool.
|
|
40
41
|
"""
|
|
41
42
|
|
|
42
|
-
tool_names: list[FunctionRef] = Field(
|
|
43
|
-
|
|
43
|
+
tool_names: list[FunctionRef | FunctionGroupRef] = Field(
|
|
44
|
+
default_factory=list, description="The list of tools to provide to the rewoo agent.")
|
|
44
45
|
llm_name: LLMRef = Field(description="The LLM model to use with the rewoo agent.")
|
|
45
46
|
verbose: bool = Field(default=False, description="Set the verbosity of the rewoo agent's logging.")
|
|
46
47
|
include_tool_input_schema_in_tool_description: bool = Field(
|
|
@@ -52,6 +53,9 @@ class ReWOOAgentWorkflowConfig(FunctionBaseConfig, name="rewoo_agent"):
|
|
|
52
53
|
solver_prompt: str | None = Field(
|
|
53
54
|
default=None,
|
|
54
55
|
description="Provides the SOLVER_PROMPT to use with the agent") # defaults to SOLVER_PROMPT in prompt.py
|
|
56
|
+
tool_call_max_retries: PositiveInt = Field(default=3,
|
|
57
|
+
description="The number of retries before raising a tool call error.",
|
|
58
|
+
ge=1)
|
|
55
59
|
max_history: int = Field(default=15, description="Maximum number of messages to keep in the conversation history.")
|
|
56
60
|
log_response_max_chars: PositiveInt = Field(
|
|
57
61
|
default=1000, description="Maximum number of characters to display in logs when logging tool responses.")
|
|
@@ -73,7 +77,7 @@ async def rewoo_agent_workflow(config: ReWOOAgentWorkflowConfig, builder: Builde
|
|
|
73
77
|
from langchain_core.messages import trim_messages
|
|
74
78
|
from langchain_core.messages.human import HumanMessage
|
|
75
79
|
from langchain_core.prompts import ChatPromptTemplate
|
|
76
|
-
from langgraph.graph.
|
|
80
|
+
from langgraph.graph.state import CompiledStateGraph
|
|
77
81
|
|
|
78
82
|
from nat.agent.rewoo_agent.prompt import PLANNER_SYSTEM_PROMPT
|
|
79
83
|
from nat.agent.rewoo_agent.prompt import PLANNER_USER_PROMPT
|
|
@@ -111,13 +115,15 @@ async def rewoo_agent_workflow(config: ReWOOAgentWorkflowConfig, builder: Builde
|
|
|
111
115
|
raise ValueError(f"No tools specified for ReWOO Agent '{config.llm_name}'")
|
|
112
116
|
|
|
113
117
|
# construct the ReWOO Agent Graph from the configured llm, prompt, and tools
|
|
114
|
-
graph:
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
118
|
+
graph: CompiledStateGraph = await ReWOOAgentGraph(
|
|
119
|
+
llm=llm,
|
|
120
|
+
planner_prompt=planner_prompt,
|
|
121
|
+
solver_prompt=solver_prompt,
|
|
122
|
+
tools=tools,
|
|
123
|
+
use_tool_schema=config.include_tool_input_schema_in_tool_description,
|
|
124
|
+
detailed_logs=config.verbose,
|
|
125
|
+
log_response_max_chars=config.log_response_max_chars,
|
|
126
|
+
tool_call_max_retries=config.tool_call_max_retries).build_graph()
|
|
121
127
|
|
|
122
128
|
async def _response_fn(input_message: ChatRequest) -> ChatResponse:
|
|
123
129
|
try:
|
|
File without changes
|