Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.txt
*.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "8e394456",
"metadata": {},
"source": [
"# Loading Tools from Configuration Files\n",
"\n",
"This notebook demonstrates how to use the **ToolConfigLoader** to dynamically load and instantiate tools from YAML configuration files. This approach enables:\n",
"\n",
"- **Declarative Tool Management**: Define tools in configuration files rather than hardcoding them\n",
"- **Dynamic Tool Loading**: Load tools at runtime based on configuration\n",
"- **Flexible Tool Composition**: Mix custom tools with pre-built tools from strands-tools\n",
"\n",
"## What You'll Learn\n",
"\n",
"1. How to define tools in YAML configuration files\n",
"2. Using ToolConfigLoader to load custom and pre-built tools\n",
"3. Executing loaded tools with proper parameters\n",
"4. Best practices for tool configuration management\n",
"\n",
"## Prerequisites\n",
"\n",
"- Python 3.10 or later\n",
"- strands-agents and strands-agents-tools packages\n",
"- Basic understanding of YAML configuration files\n",
"\n",
"Let's explore tool loading from configuration!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f7b62fd",
"metadata": {},
"outputs": [],
"source": [
"# Install Strands using pip\n",
"!pip install strands-agents strands-agents-tools PyYAML"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6763a5b8",
"metadata": {},
"outputs": [],
"source": [
"import yaml\n",
"import sys\n",
"\n",
"with open('./configs/tools.strands.yml', 'r') as file:\n",
" config = yaml.safe_load(file)\n",
"print(config)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "51dba503",
"metadata": {},
"outputs": [],
"source": [
"from strands.experimental.config_loader.tools.tool_config_loader import ToolConfigLoader\n",
"\n",
"tool_loader = ToolConfigLoader()\n",
"weather = tool_loader.load_tool(tool=config[\"tools\"][0])\n",
"\n",
"print(weather)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e19068ac",
"metadata": {},
"outputs": [],
"source": [
"response = weather()\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f5cb951a",
"metadata": {},
"outputs": [],
"source": [
"# from strands_tools import file_write\n",
"tool_loader = ToolConfigLoader()\n",
"file_write = tool_loader.load_tool(tool=config[\"tools\"][1])\n",
"\n",
"print(file_write)"
]
},
{
"cell_type": "markdown",
"id": "demo_explanation",
"metadata": {},
"source": [
"## Demonstrating the file_write Tool\n",
"\n",
"Now let's use the `file_write` tool to create a simple text file. This demonstrates how to:\n",
"\n",
"1. Create a proper `ToolUse` request with the required parameters\n",
"2. Execute the tool asynchronously using the `stream` method\n",
"3. Handle the tool's response and verify the operation\n",
"\n",
"The `file_write` tool requires two parameters:\n",
"- `path`: The file path where content should be written\n",
"- `content`: The text content to write to the file"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b131235e",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"# Set environment variable to bypass interactive prompts\n",
"os.environ['BYPASS_TOOL_CONSENT'] = 'true'\n",
"\n",
"# Create a ToolUse request for the file_write tool\n",
"tool_use = {\n",
" 'toolUseId': 'demo-file-write',\n",
" 'name': 'file_write',\n",
" 'input': {\n",
" 'path': 'hello-strands.txt',\n",
" 'content': 'Hello Strands!'\n",
" }\n",
"}\n",
"\n",
"[result async for result in file_write.stream(tool_use, {})]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "dev",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "8e394456",
"metadata": {},
"source": [
"# Loading Agents from Configuration Files\n",
"\n",
"This notebook demonstrates how to create and configure Strands Agents using **YAML configuration files** and the **AgentConfigLoader**. This declarative approach provides several advantages:\n",
"\n",
"- **Configuration-Driven Development**: Define agent behavior, tools, and models in external config files\n",
"- **Environment Flexibility**: Easily switch between different configurations for development, testing, and production\n",
"- **Maintainability**: Separate agent logic from configuration, making updates easier\n",
"- **Reusability**: Share and version control agent configurations independently\n",
"\n",
"## What You'll Learn\n",
"\n",
"1. How to define agent configurations in YAML files\n",
"2. Loading agents using the AgentConfigLoader from configuration files or dictionaries\n",
"3. Configuring models, system prompts, and tools declaratively\n",
"4. Best practices for agent configuration management\n",
"\n",
"## Prerequisites\n",
"\n",
"- Python 3.10 or later\n",
"- AWS account configured with appropriate permissions for Bedrock\n",
"- strands-agents package installed\n",
"- Basic understanding of YAML syntax\n",
"\n",
"Let's build agents from configuration!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f7b62fd",
"metadata": {},
"outputs": [],
"source": [
"# Install Strands using pip\n",
"!pip install strands-agents strands-agents-tools python-dotenv PyYAML"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fda69d85",
"metadata": {},
"outputs": [],
"source": [
"from dotenv import load_dotenv\n",
"load_dotenv()"
]
},
{
"cell_type": "markdown",
"id": "79a45632",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"source": [
"\n",
"## Creating an Agent from Configuration\n",
"\n",
"Let's examine how to define and load a weather agent using YAML configuration and the AgentConfigLoader.\n",
"\n",
"### 1. Weather Agent Configuration:\n",
"\n",
"The configuration file defines:\n",
"- **Model**: Specifies which LLM to use (Claude 3.7 Sonnet via Amazon Bedrock)\n",
"- **System Prompt**: Sets the agent's behavior and capabilities\n",
"- **Tools**: Lists the tools available to the agent (weather_tool.weather)\n",
"\n",
"This creates a specialized weather assistant that can:\n",
"- Answer weather-related queries using the weather tool\n",
"- Perform simple calculations as specified in the system prompt\n",
"- Maintain consistent behavior across different environments\n",
"\n",
"<div style=\"text-align:center\">\n",
" <img src=\"images/simple_agent.png\" width=\"75%\" />\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f35b45a",
"metadata": {},
"outputs": [],
"source": [
"import yaml\n",
"\n",
"with open('./configs/weather-agent.strands.yml', 'r') as file:\n",
" config = yaml.safe_load(file)\n",
"print(config)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d7592da",
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"from strands.experimental.config_loader.agent import AgentConfigLoader\n",
"\n",
"# Create the config loader\n",
"loader = AgentConfigLoader()\n",
"\n",
"# Load agent from dictionary config\n",
"weather_agent = loader.load_agent(config)\n",
"\n",
"print(weather_agent)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b61d3792",
"metadata": {},
"outputs": [],
"source": [
"response = weather_agent(\"What is the weather today?\")\n",
"print(response)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "dev",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading