forked from google/adk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm_agent_config.py
More file actions
230 lines (187 loc) · 6.8 KB
/
Copy pathllm_agent_config.py
File metadata and controls
230 lines (187 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import logging
from typing import Any
from typing import List
from typing import Literal
from typing import Optional
from google.genai import types
from pydantic import ConfigDict
from pydantic import Field
from pydantic import model_validator
from ..tools.tool_configs import ToolConfig
from .base_agent_config import BaseAgentConfig
from .common_configs import CodeConfig
logger = logging.getLogger('google_adk.' + __name__)
class LlmAgentConfig(BaseAgentConfig):
"""The config for the YAML schema of a LlmAgent."""
model_config = ConfigDict(
extra='forbid',
# Allow arbitrary types to support types.ContentUnion for static_instruction.
# ContentUnion includes PIL.Image.Image which doesn't have Pydantic schema
# support, but we validate it at runtime using google.genai._transformers.t_content()
arbitrary_types_allowed=True,
)
agent_class: str = Field(
default='LlmAgent',
description=(
'The value is used to uniquely identify the LlmAgent class. If it is'
' empty, it is by default an LlmAgent.'
),
)
model: Optional[str] = Field(
default=None,
description=(
'Optional. LlmAgent.model. Provide a model name string (e.g.'
' "gemini-2.5-flash"). If not set, the model will be inherited from'
' the ancestor or fall back to the system default (gemini-2.5-flash'
' unless overridden via LlmAgent.set_default_model). To construct a'
' model instance from code, use model_code.'
),
)
model_code: Optional[CodeConfig] = Field(
default=None,
description=(
'Optional. A CodeConfig that instantiates a BaseLlm implementation'
' such as LiteLlm with custom arguments (API base, fallbacks,'
' etc.). Cannot be set together with `model`.'
),
)
@model_validator(mode='before')
@classmethod
def _normalize_model_code(cls, data: Any) -> dict[str, Any] | Any:
if not isinstance(data, dict):
return data
model_value = data.get('model')
model_code = data.get('model_code')
if isinstance(model_value, dict) and model_code is None:
logger.warning(
'Detected legacy `model` mapping. Use `model_code` to provide a'
' CodeConfig for custom model construction.'
)
data = dict(data)
data['model_code'] = model_value
data['model'] = None
return data
@model_validator(mode='after')
def _validate_model_sources(self) -> LlmAgentConfig:
if self.model and self.model_code:
raise ValueError('Only one of `model` or `model_code` should be set.')
return self
instruction: str = Field(
description=(
'Required. LlmAgent.instruction. Dynamic instructions with'
' placeholder support. Behavior: if static_instruction is None, goes'
' to system_instruction; if static_instruction is set, goes to user'
' content after static content.'
)
)
static_instruction: Optional[types.ContentUnion] = Field(
default=None,
description=(
'Optional. LlmAgent.static_instruction. Static content sent literally'
' at position 0 without placeholder processing. When set, changes'
' instruction behavior to go to user content instead of'
' system_instruction. Supports context caching. Accepts'
' types.ContentUnion (str, types.Content, types.Part,'
' PIL.Image.Image, types.File, or list[PartUnion]).'
),
)
disallow_transfer_to_parent: Optional[bool] = Field(
default=None,
description='Optional. LlmAgent.disallow_transfer_to_parent.',
)
disallow_transfer_to_peers: Optional[bool] = Field(
default=None, description='Optional. LlmAgent.disallow_transfer_to_peers.'
)
input_schema: Optional[CodeConfig] = Field(
default=None, description='Optional. LlmAgent.input_schema.'
)
output_schema: Optional[CodeConfig] = Field(
default=None, description='Optional. LlmAgent.output_schema.'
)
output_key: Optional[str] = Field(
default=None, description='Optional. LlmAgent.output_key.'
)
include_contents: Literal['default', 'none'] = Field(
default='default', description='Optional. LlmAgent.include_contents.'
)
tools: Optional[list[ToolConfig]] = Field(
default=None,
description="""\
Optional. LlmAgent.tools.
Examples:
For ADK built-in tools in `google.adk.tools` package, they can be referenced
directly with the name:
```
tools:
- name: google_search
- name: load_memory
```
For user-defined tools, they can be referenced with fully qualified name:
```
tools:
- name: my_library.my_tools.my_tool
```
For tools that needs to be created via functions:
```
tools:
- name: my_library.my_tools.create_tool
args:
- name: param1
value: value1
- name: param2
value: value2
```
For more advanced tools, instead of specifying arguments in config, it's
recommended to define them in Python files and reference them. E.g.,
```
# tools.py
my_mcp_toolset = McpToolset(
connection_params=StdioServerParameters(
command="npx",
args=["-y", "@notionhq/notion-mcp-server"],
env={"OPENAPI_MCP_HEADERS": NOTION_HEADERS},
)
)
```
Then, reference the toolset in config:
```
tools:
- name: tools.my_mcp_toolset
```""",
)
before_model_callbacks: Optional[List[CodeConfig]] = Field(
default=None,
description="""\
Optional. LlmAgent.before_model_callbacks.
Example:
```
before_model_callbacks:
- name: my_library.callbacks.before_model_callback
```""",
)
after_model_callbacks: Optional[List[CodeConfig]] = Field(
default=None, description='Optional. LlmAgent.after_model_callbacks.'
)
before_tool_callbacks: Optional[List[CodeConfig]] = Field(
default=None, description='Optional. LlmAgent.before_tool_callbacks.'
)
after_tool_callbacks: Optional[List[CodeConfig]] = Field(
default=None, description='Optional. LlmAgent.after_tool_callbacks.'
)
generate_content_config: Optional[types.GenerateContentConfig] = Field(
default=None, description='Optional. LlmAgent.generate_content_config.'
)