Skip to content

Commit e73d71d

Browse files
wuliang229copybara-github
authored andcommitted
feat(config): implement config and from_config for ExampleTool
Only list[Example] is supported in config. BaseExampleProvider will need to be used in code. PiperOrigin-RevId: 791763913
1 parent e528749 commit e73d71d

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/google/adk/tools/example_tool.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
from ..examples.base_example_provider import BaseExampleProvider
2525
from ..examples.example import Example
2626
from .base_tool import BaseTool
27+
from .base_tool import BaseToolConfig
28+
from .base_tool import ToolArgsConfig
2729
from .tool_context import ToolContext
2830

2931
if TYPE_CHECKING:
@@ -60,3 +62,34 @@ async def process_llm_request(
6062
self.examples, parts[0].text, llm_request.model
6163
)
6264
])
65+
66+
@override
67+
@classmethod
68+
def from_config(
69+
cls: type[ExampleTool], config: ToolArgsConfig, config_abs_path: str
70+
) -> ExampleTool:
71+
from ..agents import config_agent_utils
72+
73+
example_tool_config = ExampleToolConfig.model_validate(config.model_dump())
74+
if isinstance(example_tool_config.examples, str):
75+
example_provider = config_agent_utils.resolve_fully_qualified_name(
76+
example_tool_config.examples
77+
)
78+
if not isinstance(example_provider, BaseExampleProvider):
79+
raise ValueError(
80+
'Example provider must be an instance of BaseExampleProvider.'
81+
)
82+
return cls(example_provider)
83+
elif isinstance(example_tool_config.examples, list):
84+
return cls(example_tool_config.examples)
85+
else:
86+
raise ValueError(
87+
'Example tool config must be a list of examples or a fully-qualified'
88+
' name to a BaseExampleProvider object in code.'
89+
)
90+
91+
92+
class ExampleToolConfig(BaseToolConfig):
93+
examples: Union[list[Example], str]
94+
"""The examples to add to the LLM request. User can either provide a list of
95+
examples or a fully-qualified name to a BaseExampleProvider object in code."""

0 commit comments

Comments
 (0)