Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test validate_tools_and_managed_agents #731

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion tests/test_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import tempfile
import unittest
import uuid
from contextlib import nullcontext as does_not_raise
from pathlib import Path
from unittest.mock import MagicMock

Expand All @@ -41,7 +42,7 @@
MessageRole,
TransformersModel,
)
from smolagents.tools import tool
from smolagents.tools import Tool, tool
from smolagents.utils import BASE_BUILTIN_MODULES


Expand Down Expand Up @@ -559,6 +560,23 @@ def check_always_fails(final_answer, agent_memory):
assert "Error raised in check" in str(agent.write_memory_to_messages())


class MockTool(Tool):
def __init__(self, name):
self.name = name
self.description = "Mock tool description"
self.inputs = {}
self.output_type = "string"

def forward(self):
return "Mock tool output"


class MockAgent:
def __init__(self, name, tools):
self.name = name
self.tools = {t.name: t for t in tools}


class TestMultiStepAgent:
def test_instantiation_disables_logging_to_terminal(self):
fake_model = MagicMock()
Expand Down Expand Up @@ -760,6 +778,44 @@ def test_provide_final_answer(self, images, expected_messages_list):
for content, expected_content in zip(message["content"], expected_message["content"]):
assert content == expected_content

@pytest.mark.parametrize(
"tools, managed_agents, expectation",
[
# Valid case: no duplicates
(
[MockTool("tool1"), MockTool("tool2")],
[MockAgent("agent1", [MockTool("tool3")])],
does_not_raise(),
),
# Invalid case: duplicate tool names
([MockTool("tool1"), MockTool("tool1")], [], pytest.raises(ValueError)),
# Invalid case: tool name same as managed agent name
([MockTool("tool1")], [MockAgent("tool1", [MockTool("final_answer")])], pytest.raises(ValueError)),
# Invalid case: tool name same as managed agent's tool name
([MockTool("tool1")], [MockAgent("agent1", [MockTool("tool1")])], pytest.raises(ValueError)),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this specific case should raise an error? For me the disambiguation is to save the model a confusion : since the manager agent's model will never see the managed agent tools, this would not be a problem.

# Invalid case: duplicate managed agent name and managed agent tool name
([], [MockAgent("tool1", [MockTool("tool1")])], pytest.raises(ValueError)),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, I'm not sure that this should raise an error.

# Invalid case: duplicate tool names across managed agents
(
[MockTool("tool1")],
[
MockAgent("agent1", [MockTool("tool2"), MockTool("final_answer")]),
MockAgent("agent2", [MockTool("tool2"), MockTool("final_answer")]),
],
pytest.raises(ValueError),
),
],
)
def test_validate_tools_and_managed_agents(self, tools, managed_agents, expectation):
# Mock MultiStepAgent class
class MockMultiStepAgent(MultiStepAgent):
def __init__(self):
pass

agent = MockMultiStepAgent()
with expectation:
agent._validate_tools_and_managed_agents(tools, managed_agents)


class TestCodeAgent:
@pytest.mark.parametrize("provide_run_summary", [False, True])
Expand Down