Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/strands_tools/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ def memory(
next_token: Token for pagination in 'list' or 'retrieve' action (optional).
query: The search query for semantic search (required for 'retrieve' action).
min_score: Minimum relevance score threshold (0.0-1.0) for 'retrieve' action. Default is 0.4.
region_name: Optional AWS region name. If not provided, will use the AWS_REGION env variable.
region_name: Optional AWS region name. If not provided, will use the AWS_REGION env variable.
If AWS_REGION is not specified, it will default to us-west-2.

Returns:
Expand Down
38 changes: 32 additions & 6 deletions src/strands_tools/think.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,28 @@

agent = Agent(tools=[think, stop])

# Basic usage with default system prompt
# Basic usage with default system prompt (inherits all parent tools)
result = agent.tool.think(
thought="How might we improve renewable energy storage solutions?",
cycle_count=3,
system_prompt="You are an expert energy systems analyst."
)

# Advanced usage with custom system prompt
# Usage with specific tools filtered from parent agent
result = agent.tool.think(
thought="Calculate energy efficiency and analyze the data",
cycle_count=3,
system_prompt="You are an expert energy systems analyst.",
tools=["calculator", "file_read", "python_repl"]
)

# Usage with mixed tool filtering from parent agent
result = agent.tool.think(
thought="Analyze the implications of quantum computing on cryptography.",
cycle_count=5,
system_prompt="You are a specialist in quantum computing and cryptography. Analyze this topic deeply,
considering both technical and practical aspects."
considering both technical and practical aspects.",
tools=["retrieve", "calculator", "http_request"]
)
```

Expand Down Expand Up @@ -79,6 +88,7 @@ def process_cycle(
cycle: int,
total_cycles: int,
custom_system_prompt: str,
specified_tools=None,
**kwargs: Any,
) -> str:
"""Process a single thinking cycle."""
Expand All @@ -97,9 +107,21 @@ def process_cycle(
trace_attributes = {}
parent_agent = kwargs.get("agent")
if parent_agent:
tools = list(parent_agent.tool_registry.registry.values())
trace_attributes = parent_agent.trace_attributes

# If specific tools are provided, filter parent tools; otherwise inherit all tools from parent
if specified_tools is not None:
# Filter parent agent tools to only include specified tool names
filtered_tools = []
for tool_name in specified_tools:
if tool_name in parent_agent.tool_registry.registry:
filtered_tools.append(parent_agent.tool_registry.registry[tool_name])
else:
logger.warning(f"Tool '{tool_name}' not found in parent agent's tool registry")
tools = filtered_tools
else:
tools = list(parent_agent.tool_registry.registry.values())

# Initialize the new Agent with provided parameters
agent = Agent(messages=[], tools=tools, system_prompt=custom_system_prompt, trace_attributes=trace_attributes)

Expand All @@ -122,7 +144,7 @@ def process_cycle(


@tool
def think(thought: str, cycle_count: int, system_prompt: str, agent: Any) -> Dict[str, Any]:
def think(thought: str, cycle_count: int, system_prompt: str, tools: list = None, agent: Any = None) -> Dict[str, Any]:
"""
Recursive thinking tool for sophisticated thought generation, learning, and self-reflection.

Expand Down Expand Up @@ -162,7 +184,10 @@ def think(thought: str, cycle_count: int, system_prompt: str, agent: Any) -> Dic
provide a good balance of depth and efficiency.
system_prompt: Custom system prompt to use for the LLM thinking process. This should
specify the expertise domain and thinking approach for processing the thought.
**kwargs: Additional keyword arguments passed to the underlying LLM processing.
tools: List of tool names to make available to the nested agent. Tool names must
exist in the parent agent's tool registry. Examples: ["calculator", "file_read", "retrieve"]
If not provided, inherits all tools from the parent agent.
agent: The parent agent (automatically passed by Strands framework)

Returns:
Dict containing status and response content in the format:
Expand Down Expand Up @@ -210,6 +235,7 @@ def think(thought: str, cycle_count: int, system_prompt: str, agent: Any) -> Dic
cycle,
cycle_count,
custom_system_prompt,
specified_tools=tools,
**cycle_kwargs,
)

Expand Down
45 changes: 42 additions & 3 deletions src/strands_tools/use_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,26 @@

agent = Agent(tools=[use_llm])

# Basic usage with just a prompt and system prompt
# Basic usage with just a prompt and system prompt (inherits all parent tools)
result = agent.tool.use_llm(
prompt="Tell me about the advantages of tool-building in AI agents",
system_prompt="You are a helpful AI assistant specializing in AI development concepts."
)

# Usage with specific tools filtered from parent agent
result = agent.tool.use_llm(
prompt="Calculate 2 + 2 and retrieve some information",
system_prompt="You are a helpful assistant.",
tools=["calculator", "retrieve"]
)

# Usage with mixed tool filtering from parent agent
result = agent.tool.use_llm(
prompt="Analyze this data file",
system_prompt="You are a data analyst.",
tools=["file_read", "calculator", "python_repl"]
)

# The response is available in the returned object
print(result["content"][0]["text"]) # Prints the response text
```
Expand Down Expand Up @@ -52,6 +66,13 @@
"type": "string",
"description": "System prompt for the new event loop",
},
"tools": {
"type": "array",
"description": "List of tool names to make available to the nested agent"
+ "Tool names must exist in the parent agent's tool registry."
+ "If not provided, inherits all tools from parent agent.",
"items": {"type": "string"},
},
},
"required": ["prompt", "system_prompt"],
}
Expand Down Expand Up @@ -92,7 +113,11 @@ def use_llm(tool: ToolUse, **kwargs: Any) -> ToolResult:
Args:
tool (ToolUse): Tool use object containing the following:
- prompt (str): The prompt to process with the new agent instance
- system_prompt (str, optional): Custom system prompt for the agent
- system_prompt (str): Custom system prompt for the agent
- tools (List[str], optional): List of tool names to make available to the nested agent.
Tool names must exist in the parent agent's tool registry.
Examples: ["calculator", "file_read", "retrieve"]
If not provided, inherits all tools from the parent agent.
**kwargs (Any): Additional keyword arguments

Returns:
Expand All @@ -116,16 +141,30 @@ def use_llm(tool: ToolUse, **kwargs: Any) -> ToolResult:

prompt = tool_input["prompt"]
tool_system_prompt = tool_input.get("system_prompt")
specified_tools = tool_input.get("tools")

tools = []
trace_attributes = {}

extra_kwargs = {}
parent_agent = kwargs.get("agent")
if parent_agent:
tools = list(parent_agent.tool_registry.registry.values())
trace_attributes = parent_agent.trace_attributes
extra_kwargs["callback_handler"] = parent_agent.callback_handler

# If specific tools are provided, filter parent tools; otherwise inherit all tools from parent
if specified_tools is not None:
# Filter parent agent tools to only include specified tool names
filtered_tools = []
for tool_name in specified_tools:
if tool_name in parent_agent.tool_registry.registry:
filtered_tools.append(parent_agent.tool_registry.registry[tool_name])
else:
logger.warning(f"Tool '{tool_name}' not found in parent agent's tool registry")
tools = filtered_tools
else:
tools = list(parent_agent.tool_registry.registry.values())

if "callback_handler" in kwargs:
extra_kwargs["callback_handler"] = kwargs["callback_handler"]

Expand Down
Loading