mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-10 00:28:31 +00:00
feat: support to initialize a tool from defined Tool attributes (#3023)
* feat: support to initialize a tool from defined Tool attributes * fix: ensure Agent is able to load a list of Tools dynamically
This commit is contained in:
@@ -476,7 +476,14 @@ def load_agent_from_repository(from_repository: str) -> Dict[str, Any]:
|
|||||||
try:
|
try:
|
||||||
module = importlib.import_module(tool["module"])
|
module = importlib.import_module(tool["module"])
|
||||||
tool_class = getattr(module, tool["name"])
|
tool_class = getattr(module, tool["name"])
|
||||||
attributes[key].append(tool_class())
|
|
||||||
|
tool_value = tool_class(**tool["init_params"])
|
||||||
|
|
||||||
|
if isinstance(tool_value, list):
|
||||||
|
attributes[key].extend(tool_value)
|
||||||
|
else:
|
||||||
|
attributes[key].append(tool_value)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise AgentRepositoryError(
|
raise AgentRepositoryError(
|
||||||
f"Tool {tool['name']} could not be loaded: {e}"
|
f"Tool {tool['name']} could not be loaded: {e}"
|
||||||
|
|||||||
@@ -2099,7 +2099,7 @@ def mock_get_auth_token():
|
|||||||
|
|
||||||
@patch("crewai.cli.plus_api.PlusAPI.get_agent")
|
@patch("crewai.cli.plus_api.PlusAPI.get_agent")
|
||||||
def test_agent_from_repository(mock_get_agent, mock_get_auth_token):
|
def test_agent_from_repository(mock_get_agent, mock_get_auth_token):
|
||||||
from crewai_tools import SerperDevTool, XMLSearchTool
|
from crewai_tools import SerperDevTool, XMLSearchTool, CSVSearchTool, EnterpriseActionTool
|
||||||
|
|
||||||
mock_get_response = MagicMock()
|
mock_get_response = MagicMock()
|
||||||
mock_get_response.status_code = 200
|
mock_get_response.status_code = 200
|
||||||
@@ -2108,19 +2108,42 @@ def test_agent_from_repository(mock_get_agent, mock_get_auth_token):
|
|||||||
"goal": "test goal",
|
"goal": "test goal",
|
||||||
"backstory": "test backstory",
|
"backstory": "test backstory",
|
||||||
"tools": [
|
"tools": [
|
||||||
{"module": "crewai_tools", "name": "SerperDevTool"},
|
{"module": "crewai_tools", "name": "SerperDevTool", "init_params": {"n_results": 30}},
|
||||||
{"module": "crewai_tools", "name": "XMLSearchTool"},
|
{"module": "crewai_tools", "name": "XMLSearchTool", "init_params": {"summarize": True}},
|
||||||
|
{"module": "crewai_tools", "name": "CSVSearchTool", "init_params": {}},
|
||||||
|
|
||||||
|
# using a tools that returns a list of BaseTools
|
||||||
|
{"module": "crewai_tools", "name": "CrewaiEnterpriseTools", "init_params": {"actions_list": [], "enterprise_token": "test_key"}},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
mock_get_agent.return_value = mock_get_response
|
mock_get_agent.return_value = mock_get_response
|
||||||
agent = Agent(from_repository="test_agent")
|
|
||||||
|
tool_action = EnterpriseActionTool(
|
||||||
|
name="test_name",
|
||||||
|
description="test_description",
|
||||||
|
enterprise_action_token="test_token",
|
||||||
|
action_name="test_action_name",
|
||||||
|
action_schema={"test": "test"},
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch("crewai_tools.CrewaiEnterpriseTools", return_value=[tool_action]):
|
||||||
|
agent = Agent(from_repository="test_agent")
|
||||||
|
|
||||||
assert agent.role == "test role"
|
assert agent.role == "test role"
|
||||||
assert agent.goal == "test goal"
|
assert agent.goal == "test goal"
|
||||||
assert agent.backstory == "test backstory"
|
assert agent.backstory == "test backstory"
|
||||||
assert len(agent.tools) == 2
|
assert len(agent.tools) == 4
|
||||||
|
|
||||||
assert isinstance(agent.tools[0], SerperDevTool)
|
assert isinstance(agent.tools[0], SerperDevTool)
|
||||||
|
assert agent.tools[0].n_results == 30
|
||||||
assert isinstance(agent.tools[1], XMLSearchTool)
|
assert isinstance(agent.tools[1], XMLSearchTool)
|
||||||
|
assert agent.tools[1].summarize
|
||||||
|
|
||||||
|
assert isinstance(agent.tools[2], CSVSearchTool)
|
||||||
|
assert not agent.tools[2].summarize
|
||||||
|
|
||||||
|
assert isinstance(agent.tools[3], EnterpriseActionTool)
|
||||||
|
assert agent.tools[3].name == "test_name"
|
||||||
|
|
||||||
|
|
||||||
@patch("crewai.cli.plus_api.PlusAPI.get_agent")
|
@patch("crewai.cli.plus_api.PlusAPI.get_agent")
|
||||||
@@ -2133,7 +2156,7 @@ def test_agent_from_repository_override_attributes(mock_get_agent, mock_get_auth
|
|||||||
"role": "test role",
|
"role": "test role",
|
||||||
"goal": "test goal",
|
"goal": "test goal",
|
||||||
"backstory": "test backstory",
|
"backstory": "test backstory",
|
||||||
"tools": [{"name": "SerperDevTool", "module": "crewai_tools"}],
|
"tools": [{"name": "SerperDevTool", "module": "crewai_tools", "init_params": {}}],
|
||||||
}
|
}
|
||||||
mock_get_agent.return_value = mock_get_response
|
mock_get_agent.return_value = mock_get_response
|
||||||
agent = Agent(from_repository="test_agent", role="Custom Role")
|
agent = Agent(from_repository="test_agent", role="Custom Role")
|
||||||
|
|||||||
Reference in New Issue
Block a user