|
| 1 | +# Config-based Agent Samples |
| 2 | + |
| 3 | +This directory contains several samples demonstrating ADK agent configuration using YAML files: |
| 4 | + |
| 5 | +1. **Core Basic Config** - The simplest possible agent configuration |
| 6 | +2. **Core Callback Config** - An agent with comprehensive callback hooks for lifecycle events |
| 7 | +3. **Core Generate Content Config** - An agent demonstrating generate_content_config settings |
| 8 | +4. **Tool Builtin Config** - An agent with built-in Google search tool |
| 9 | +5. **Tool Functions Config** - An agent with custom Java-based function tools |
| 10 | +6. **Multi Agent Basic Config** - Multiple specialized agents (coding tutor, math tutor) |
| 11 | +7. **Multi Agent LLM Config** - Multi-agent system with dice rolling and prime checking capabilities |
| 12 | +8. **Tool MCP Stdio File System Config** - An agent with MCP filesystem tools via stdio transport |
| 13 | +9. **Sub Agents Config** - Demonstrates programmatic sub-agent resolution using the `code` field |
| 14 | + |
| 15 | +## Project Structure |
| 16 | + |
| 17 | +``` |
| 18 | +├── core_basic_config/ |
| 19 | +│ └── root_agent.yaml # Basic agent configuration |
| 20 | +├── core_callback_config/ |
| 21 | +│ └── root_agent.yaml # Agent with callback hooks |
| 22 | +├── core_generate_content_config_config/ |
| 23 | +│ └── root_agent.yaml # Agent with generate_content_config settings |
| 24 | +├── tool_builtin_config/ |
| 25 | +│ └── root_agent.yaml # Agent with built-in search tool |
| 26 | +├── tool_functions_config/ |
| 27 | +│ └── root_agent.yaml # Agent with custom function tools |
| 28 | +├── multi_agent_basic_config/ |
| 29 | +│ ├── root_agent.yaml # Root coordinator agent |
| 30 | +│ ├── code_tutor_agent.yaml # Coding tutor sub-agent |
| 31 | +│ └── math_tutor_agent.yaml # Math tutor sub-agent |
| 32 | +├── multi_agent_llm_config/ |
| 33 | +│ ├── root_agent.yaml # Root agent with dice/prime delegation |
| 34 | +│ ├── roll_agent.yaml # Dice rolling sub-agent |
| 35 | +│ └── prime_agent.yaml # Prime checking sub-agent |
| 36 | +├── tool_mcp_stdio_file_system_config/ |
| 37 | +│ └── root_agent.yaml # Agent with MCP filesystem tools |
| 38 | +├── sub_agents_config/ |
| 39 | +│ ├── root_agent.yaml # Root agent using programmatic sub-agents |
| 40 | +│ ├── work_agent.yaml # Work-related sub-agent |
| 41 | +│ └── LifeAgent.java # Life agent implementation (registered via code) |
| 42 | +├── src/ |
| 43 | +│ ├── CustomDieTool.java # Custom tool implementation |
| 44 | +│ ├── CoreCallbacks.java # Callback implementations |
| 45 | +│ └── CustomDemoRegistry.java # Consolidated custom registry |
| 46 | +├── pom.xml # Maven configuration |
| 47 | +└── README.md # This file |
| 48 | +``` |
| 49 | + |
| 50 | +## How to Use |
| 51 | + |
| 52 | +### 1. Navigate to this sample directory |
| 53 | +```bash |
| 54 | +cd contrib/samples/configagent |
| 55 | +``` |
| 56 | + |
| 57 | +### 2. Run the ADK web server |
| 58 | +Run all agents simultaneously: |
| 59 | +```bash |
| 60 | +mvn clean compile google-adk:web -Dagents=. \ |
| 61 | + -Dregistry=com.example.CustomDemoRegistry |
| 62 | +``` |
| 63 | + |
| 64 | +Or run individual agents: |
| 65 | +```bash |
| 66 | +# Basic agent only |
| 67 | +mvn google-adk:web -Dagents=core_basic_config |
| 68 | + |
| 69 | +# Custom function tools agent (requires compilation and registry) |
| 70 | +mvn clean compile google-adk:web \ |
| 71 | + -Dagents=tool_functions_config \ |
| 72 | + -Dregistry=com.example.CustomDemoRegistry |
| 73 | +``` |
| 74 | + |
| 75 | +### 3. Access the Web UI |
| 76 | +Open your browser and navigate to: |
| 77 | +``` |
| 78 | +http://localhost:8000 |
| 79 | +``` |
| 80 | + |
| 81 | +You will see the configured agent(s) available in the UI. |
| 82 | + |
| 83 | +## Sample 1: Core Basic Config (`core_basic_config/`) |
| 84 | + |
| 85 | +Basic Q&A agent with essential fields: name, model, description, instruction. |
| 86 | + |
| 87 | +**Sample queries:** |
| 88 | +- "Hello, can you help me?" |
| 89 | +- "Explain what machine learning is" |
| 90 | + |
| 91 | +## Sample 2: Core Callback Config (`core_callback_config/`) |
| 92 | + |
| 93 | +Agent demonstrating comprehensive callback hooks for monitoring and debugging agent lifecycle events. This sample showcases how callbacks can be used to track agent execution, model interactions, and tool usage. |
| 94 | + |
| 95 | + |
| 96 | +**Sample queries:** |
| 97 | +- "Roll a 6-sided die" - Triggers tool callbacks |
| 98 | +- "Roll a 20-sided die and check if it's prime" - Demonstrates multiple callback types |
| 99 | +- "Check if 7, 11, and 13 are prime numbers" - Shows batch processing with callbacks |
| 100 | + |
| 101 | +**Callback Types:** |
| 102 | +- **Agent callbacks**: Execute before/after the agent processes a request |
| 103 | +- **Model callbacks**: Execute before/after LLM invocation |
| 104 | +- **Tool callbacks**: Execute before/after tool execution |
| 105 | + |
| 106 | +## Sample 3: Core Generate Content Config (`core_generate_content_config_config/`) |
| 107 | + |
| 108 | +Search agent demonstrating the use of `generate_content_config` to control LLM generation parameters. |
| 109 | + |
| 110 | +**Key Configuration:** |
| 111 | +```yaml |
| 112 | +generate_content_config: |
| 113 | + temperature: 0.1 # Lower temperature for more focused responses |
| 114 | + max_output_tokens: 2000 # Limit response length |
| 115 | +``` |
| 116 | +
|
| 117 | +**Sample queries that demonstrate the configuration:** |
| 118 | +- "Generate a creative story about a robot" - Run this multiple times; with temperature 0.1, you'll get very similar stories each time (low creativity/high consistency) |
| 119 | +- "List 3 uses for a paperclip" - Run multiple times; low temperature means you'll likely get the same common uses each time (writing, holding papers, reset button) |
| 120 | +- "Search for machine learning frameworks and provide a comprehensive comparison" - Tests the max_output_tokens limit of 2000; response will be truncated if it exceeds this limit |
| 121 | +
|
| 122 | +## Sample 4: Tool Builtin Config (`tool_builtin_config/`) |
| 123 | + |
| 124 | +Search agent with built-in Google search tool. |
| 125 | + |
| 126 | +**Sample queries:** |
| 127 | +- "Search for the latest news about artificial intelligence" |
| 128 | +- "What are the top Python frameworks in 2024?" |
| 129 | + |
| 130 | +## Sample 5: Tool Functions Config (`tool_functions_config/`) |
| 131 | + |
| 132 | +Agent with custom Java-based function tools for dice rolling and prime number checking. |
| 133 | + |
| 134 | +**Custom Tools:** |
| 135 | +- `roll_die(sides)` - Rolls a die with specified number of sides |
| 136 | +- `check_prime(nums)` - Checks if a list of numbers are prime |
| 137 | + |
| 138 | +**Sample queries:** |
| 139 | +- "Roll a 6-sided die" |
| 140 | +- "Roll a 20-sided die and check if the result is prime" |
| 141 | + |
| 142 | +## Sample 6: Multi Agent Basic Config (`multi_agent_basic_config/`) |
| 143 | + |
| 144 | +Learning assistant with specialized sub-agents for coding and math tutoring. The root agent coordinates between specialized tutors. |
| 145 | + |
| 146 | +**Sub-agents:** |
| 147 | +- `code_tutor_agent` - Programming concepts and code debugging |
| 148 | +- `math_tutor_agent` - Mathematical concepts and problem solving |
| 149 | + |
| 150 | +**Sample queries:** |
| 151 | +- "How do I write a for loop in Python?" |
| 152 | +- "Explain what recursion is with an example" |
| 153 | + |
| 154 | +## Sample 7: Multi Agent LLM Config (`multi_agent_llm_config/`) |
| 155 | + |
| 156 | +Multi-agent system demonstrating delegation between specialized agents for dice rolling and prime number checking. Includes few-shot examples and safety settings configuration. |
| 157 | + |
| 158 | +**Sub-agents:** |
| 159 | +- `roll_agent` - Handles dice rolling with different sizes |
| 160 | +- `prime_agent` - Checks whether numbers are prime |
| 161 | + |
| 162 | +**Features:** |
| 163 | +- Demonstrates multi-agent coordination and delegation |
| 164 | +- Uses custom Java tools (roll_die, check_prime) |
| 165 | +- Includes few-shot examples via ExampleTool |
| 166 | +- Shows safety settings configuration |
| 167 | +- Root agent delegates tasks to specialized sub-agents |
| 168 | + |
| 169 | +**Sample queries:** |
| 170 | +- "Roll a 6-sided die" |
| 171 | +- "Roll a 20-sided die and check if the result is prime" |
| 172 | + |
| 173 | +## Sample 8: Tool MCP Stdio File System Config (`tool_mcp_stdio_file_system_config/`) |
| 174 | + |
| 175 | +File system assistant using Model Context Protocol (MCP) stdio transport to manage files and directories. |
| 176 | + |
| 177 | +**Features:** |
| 178 | +- Connects to file system via MCP stdio server |
| 179 | +- Read, write, search, and manage files and directories |
| 180 | +- Uses `@modelcontextprotocol/server-filesystem` npm package |
| 181 | +- Works with `/tmp/mcp-demo` directory |
| 182 | + |
| 183 | +**Setup:** |
| 184 | +1. Ensure you have Node.js installed |
| 185 | +2. Create the demo directory: `mkdir -p /tmp/mcp-demo` |
| 186 | +3. Run the agent: |
| 187 | +```bash |
| 188 | +mvn google-adk:web -Dagents=tool_mcp_stdio_file_system_config -Dport=8001 |
| 189 | +``` |
| 190 | + |
| 191 | +**Sample queries:** |
| 192 | +- "List all files in the current directory" |
| 193 | +- "Create a new file called 'notes.txt' in /tmp/mcp-demo with some random content" |
| 194 | + |
| 195 | +## Sample 9: Sub Agents Config (`sub_agents_config/`) |
| 196 | + |
| 197 | +Demonstrates programmatic sub-agent resolution using the `code` field, which provides Python ADK compatibility. This sample shows how to reference agents registered in the ComponentRegistry. |
| 198 | + |
| 199 | +**Features:** |
| 200 | +- Root agent that routes queries to different sub-agents |
| 201 | +- Life agent loaded programmatically via `code` field |
| 202 | +- Work agent loaded from YAML config file |
| 203 | +- Uses CustomDemoRegistry to register the LifeAgent |
| 204 | + |
| 205 | +**Configuration Example:** |
| 206 | +```yaml |
| 207 | +sub_agents: |
| 208 | + - config_path: ./work_agent.yaml # Traditional YAML file reference |
| 209 | + - code: sub_agents_config.life_agent.agent # Programmatic reference via registry |
| 210 | +``` |
| 211 | + |
| 212 | +**Setup:** |
| 213 | +```bash |
| 214 | +mvn clean compile google-adk:web \ |
| 215 | + -Dagents=sub_agents_config \ |
| 216 | + -Dregistry=com.example.CustomDemoRegistry |
| 217 | +``` |
| 218 | + |
| 219 | +**Sample queries:** |
| 220 | +- "What is the meaning of life?" (routed to life agent) |
| 221 | +- "How can I be more productive at work?" (routed to work agent) |
| 222 | +- "Tell me about the weather" (handled by root agent) |
0 commit comments