|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | <
2925
td data-grid-cell-id="diff-8c6346f02016f8d1120d2707c272a1140b13c95750c83c15737bb8e074a67cfe-empty-16-2" data-line-anchor="diff-8c6346f02016f8d1120d2707c272a1140b13c95750c83c15737bb8e074a67cfeR16" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionLine-bgColor, var(--diffBlob-addition-bgColor-line));padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell pt-4 left-side">+
| 17 | +from typing import Optional |
| 18 | + |
| 19 | +from pydantic import BaseModel |
| 20 | +from pydantic import ConfigDict |
| 21 | + |
| 22 | +from ..utils.feature_decorator import working_in_progress |
| 23 | + |
| 24 | + |
| 25 | +@working_in_progress("BaseToolConfig is not ready for use.") |
| 26 | +class BaseToolConfig(BaseModel): |
| 27 | + """The base class for all tool configs.""" |
| 28 | + |
| 29 | + model_config = ConfigDict(extra="forbid") |
| 30 | + """Forbid extra fields.""" |
| 31 | + |
| 32 | + |
| 33 | +@working_in_progress("ToolArgsConfig is not ready for use.") |
| 34 | +class ToolArgsConfig(BaseModel): |
| 35 | + """Config to host free key-value pairs for the args in ToolConfig.""" |
| 36 | + |
| 37 | + model_config = ConfigDict(extra="allow") |
| 38 | + |
| 39 | + |
| 40 | +@working_in_progress("ToolConfig is not ready for use.") |
| 41 | +class ToolConfig(BaseModel): |
| 42 | + """The configuration for a tool. |
| 43 | +
|
| 44 | + The config supports these types of tools: |
| 45 | + 1. ADK built-in tools |
| 46 | + 2. User-defined tool instances |
| 47 | + 3. User-defined tool classes |
| 48 | + 4. User-defined functions that generate tool instances |
| 49 | + 5. User-defined function tools |
| 50 | +
|
| 51 | + For examples: |
| 52 | +
|
| 53 | + 1. For ADK built-in tool instances or classes in `google.adk.tools` package, |
| 54 | + they can be referenced directly with the `name` and optionally with |
| 55 | + `args`. |
| 56 | +
|
| 57 | + ``` |
| 58 | + tools: |
| 59 | + - name: google_search |
| 60 | + - name: AgentTool |
| 61 | + args: |
| 62 | + agent: ./another_agent.yaml |
| 63 | + skip_summarization: true |
| 64 | + ``` |
| 65 | +
|
| 66 | + 2. For user-defined tool instances, the `name` is the fully qualified path |
| 67 | + to the tool instance. |
| 68 | +
|
| 69 | + ``` |
| 70 | + tools: |
| 71 | + - name: my_package.my_module.my_tool |
| 72 | + ``` |
| 73 | +
|
| 74 | + 3. For user-defined tool classes (custom tools), the `name` is the fully |
| 75 | + qualified path to the tool class and `args` is the arguments for the tool. |
| 76 | +
|
| 77 | + ``` |
| 78 | + tools: |
| 79 | + - name: my_package.my_module.my_tool_class |
| 80 | + args: |
| 81 | + my_tool_arg1: value1 |
| 82 | + my_tool_arg2: value2 |
| 83 | + ``` |
| 84 | +
|
| 85 | + 4. For user-defined functions that generate tool instances, the `name` is |
| 86 | + the fully qualified path to the function and `args` is passed to the |
| 87 | + function as arguments. |
| 88 | +
|
| 89 | + ``` |
| 90 | + tools: |
| 91 | + - name: my_package.my_module.my_tool_function |
| 92 | + args: |
| 93 | + my_function_arg1: value1 |
| 94 | + my_function_arg2: value2 |
| 95 | + ``` |
| 96 | +
|
| 97 | + The function must have the following signature: |
| 98 | + ``` |
| 99 | + def my_function(args: ToolArgsConfig) -> BaseTool: |
| 100 | + ... |
| 101 | + ``` |
| 102 | +
|
| 103 | + 5. For user-defined function tools, the `name` is the fully qualified path |
| 104 | + to the function. |
| 105 | +
|
| 106 | + ``` |
| 107 | + tools: |
| 108 | + - name: my_package.my_module.my_function_tool |
| 109 | + ``` |
| 110 | +
|
| 111 | + If the above use cases don't suffice, users can define a custom tool config |
| 112 | + by extending BaseToolConfig and override from_config() in the custom tool. |
| 113 | + """ |
| 114 | + |
| 115 | + model_config = ConfigDict(extra="forbid") |
| 116 | + |
| 117 | + name: str |
| 118 | + """The name of the tool. |
| 119 | +
|
| 120 | + For ADK built-in tools, `name` is the name of the tool, e.g. `google_search` |
| 121 | + or `AgentTool`. |
| 122 | +
|
| 123 | + For user-defined tools, the name is the fully qualified path to the tool, e.g. |
| 124 | + `my_package.my_module.my_tool`. |
| 125 | + """ |
| 126 | + |
| 127 | + args: Optional[ToolArgsConfig] = None |
| 128 | + """The args for the tool.""" |
0 commit comments