Skip to content

Commit 1686cc5

Browse files
wuliang229copybara-github
authored andcommitted
feat(config): implement configs and from_config() for CrewaiTool and LangchainTool
PiperOrigin-RevId: 791742964
1 parent 5380352 commit 1686cc5

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

src/google/adk/tools/crewai_tool.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from typing_extensions import override
1919

2020
from . import _automatic_function_calling_util
21+
from .base_tool import BaseToolConfig
22+
from .base_tool import ToolArgsConfig
2123
from .function_tool import FunctionTool
2224

2325
try:
@@ -70,3 +72,29 @@ def _get_declaration(self) -> types.FunctionDeclaration:
7072
self.tool.args_schema.model_json_schema(),
7173
)
7274
return function_declaration
75+
76+
@override
77+
@classmethod
78+
def from_config(
79+
cls: type[CrewaiTool], config: ToolArgsConfig, config_abs_path: str
80+
) -> CrewaiTool:
81+
from ..agents import config_agent_utils
82+
83+
crewai_tool_config = CrewaiToolConfig.model_validate(config.model_dump())
84+
tool = config_agent_utils.resolve_fully_qualified_name(
85+
crewai_tool_config.tool
86+
)
87+
name = crewai_tool_config.name
88+
description = crewai_tool_config.description
89+
return cls(tool, name=name, description=description)
90+
91+
92+
class CrewaiToolConfig(BaseToolConfig):
93+
tool: str
94+
"""The fully qualified path of the CrewAI tool instance."""
95+
96+
name: str = ""
97+
"""The name of the tool."""
98+
99+
description: str = ""
100+
"""The description of the tool."""

src/google/adk/tools/langchain_tool.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919

2020
from google.genai import types
2121
from langchain.agents import Tool
22-
from langchain_core.tools import BaseTool
22+
from langchain_core.tools import BaseTool as LangchainBaseTool
2323
from langchain_core.tools.structured import StructuredTool
2424
from typing_extensions import override
2525

2626
from . import _automatic_function_calling_util
27+
from .base_tool import BaseToolConfig
28+
from .base_tool import ToolArgsConfig
2729
from .function_tool import FunctionTool
2830

2931

@@ -50,12 +52,12 @@ class LangchainTool(FunctionTool):
5052
wrapped_tool = LangchainTool(search_tool)
5153
"""
5254

53-
_langchain_tool: Union[BaseTool, object]
55+
_langchain_tool: Union[LangchainBaseTool, object]
5456
"""The wrapped langchain tool."""
5557

5658
def __init__(
5759
self,
58-
tool: Union[BaseTool, object],
60+
tool: Union[LangchainBaseTool, object],
5961
name: Optional[str] = None,
6062
description: Optional[str] = None,
6163
):
@@ -114,7 +116,7 @@ def _get_declaration(self) -> types.FunctionDeclaration:
114116
# 2. Other tools: the tool doesn't inherit any class but follow some
115117
# conventions, like having a "run" method.
116118
# Handle BaseTool type (preferred Langchain approach)
117-
if isinstance(self._langchain_tool, BaseTool):
119+
if isinstance(self._langchain_tool, LangchainBaseTool):
118120
tool_wrapper = Tool(
119121
name=self.name,
120122
func=self.func,
@@ -148,3 +150,31 @@ def _get_declaration(self) -> types.FunctionDeclaration:
148150
raise ValueError(
149151
f'Failed to build function declaration for Langchain tool: {e}'
150152
) from e
153+
154+
@override
155+
@classmethod
156+
def from_config(
157+
cls: type[LangchainTool], config: ToolArgsConfig, config_abs_path: str
158+
) -> LangchainTool:
159+
from ..agents import config_agent_utils
160+
161+
langchain_tool_config = LangchainToolConfig.model_validate(
162+
config.model_dump()
163+
)
164+
tool = config_agent_utils.resolve_fully_qualified_name(
165+
langchain_tool_config.tool
166+
)
167+
name = langchain_tool_config.name
168+
description = langchain_tool_config.description
169+
return cls(tool, name=name, description=description)
170+
171+
172+
class LangchainToolConfig(BaseToolConfig):
173+
tool: str
174+
"""The fully qualified path of the Langchain tool instance."""
175+
176+
name: str = ''
177+
"""The name of the tool."""
178+
179+
description: str = ''
180+
"""The description of the tool."""

0 commit comments

Comments
 (0)