-
Notifications
You must be signed in to change notification settings - Fork 2k
Fix test_change_tools_after_init test #1780
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1803,10 +1803,6 @@ def test_toolcalling_agent_api_misformatted_output(self, mock_inference_client): | |
assert "Error while parsing" in capture.get() | ||
assert len(agent.memory.steps) == 4 | ||
|
||
@pytest.mark.skip( | ||
reason="Test is not properly implemented (GH-1255) because fake_tools should have the same name. " | ||
"Additionally, it uses CodeAgent instead of ToolCallingAgent (GH-1409)" | ||
) | ||
def test_change_tools_after_init(self): | ||
from smolagents import tool | ||
|
||
|
@@ -1815,21 +1811,45 @@ def fake_tool_1() -> str: | |
"""Fake tool""" | ||
return "1" | ||
|
||
class FakeToolCallModel(Model): | ||
def generate(self, messages, stop_sequences=None, tools_to_call_from=None): | ||
if len(messages) < 3: | ||
return ChatMessage( | ||
role=MessageRole.ASSISTANT, | ||
content="I will call the fake tool.", | ||
tool_calls=[ | ||
ChatMessageToolCall( | ||
id="call_0", | ||
type="function", | ||
function=ChatMessageToolCallFunction(name="fake_tool_1", arguments={}), | ||
) | ||
], | ||
) | ||
else: | ||
return ChatMessage( | ||
role=MessageRole.ASSISTANT, | ||
content="I will return the final answer.", | ||
tool_calls=[ | ||
ChatMessageToolCall( | ||
id="call_1", | ||
type="function", | ||
function=ChatMessageToolCallFunction(name="final_answer", arguments={"answer": "2"}), | ||
) | ||
], | ||
) | ||
|
||
agent = ToolCallingAgent(tools=[fake_tool_1], model=FakeToolCallModel()) | ||
|
||
@tool | ||
def fake_tool_2() -> str: | ||
def fake_tool_1() -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For
Is this the expected change? Should I use the class method instead to create a fake tool with the same name? |
||
"""Fake tool""" | ||
return "2" | ||
|
||
class FakeCodeModel(Model): | ||
def generate(self, messages, stop_sequences=None): | ||
return ChatMessage(role=MessageRole.ASSISTANT, content="<code>\nfinal_answer(fake_tool_1())\n</code>") | ||
|
||
agent = CodeAgent(tools=[fake_tool_1], model=FakeCodeModel()) | ||
|
||
agent.tools["final_answer"] = CustomFinalAnswerTool() | ||
agent.tools["fake_tool_1"] = fake_tool_2 | ||
agent.tools["fake_tool_1"] = fake_tool_1 | ||
|
||
answer = agent.run("Fake task.") | ||
assert "2" in agent.memory.steps[1].observations | ||
assert answer == "2CUSTOM" | ||
|
||
def test_custom_final_answer_with_custom_inputs(self, test_tool): | ||
|
@@ -2094,9 +2114,6 @@ def test_end_code_appending(self): | |
assert messages | ||
assert all(m.content.endswith("</code>") for m in messages) | ||
|
||
@pytest.mark.skip( | ||
reason="Test is not properly implemented (GH-1255) because fake_tools should have the same name. " | ||
) | ||
def test_change_tools_after_init(self): | ||
from smolagents import tool | ||
|
||
|
@@ -2105,19 +2122,19 @@ def fake_tool_1() -> str: | |
"""Fake tool""" | ||
return "1" | ||
|
||
@tool | ||
def fake_tool_2() -> str: | ||
"""Fake tool""" | ||
return "2" | ||
|
||
class FakeCodeModel(Model): | ||
def generate(self, messages, stop_sequences=None): | ||
return ChatMessage(role=MessageRole.ASSISTANT, content="<code>\nfinal_answer(fake_tool_1())\n</code>") | ||
|
||
agent = CodeAgent(tools=[fake_tool_1], model=FakeCodeModel()) | ||
|
||
agent.tools["final_answer"] = CustomFinalAnswerTool() | ||
agent.tools["fake_tool_1"] = fake_tool_2 | ||
|
||
@tool | ||
def fake_tool_1() -> str: | ||
"""Fake tool""" | ||
return "2" | ||
|
||
agent.tools["fake_tool_1"] = fake_tool_1 | ||
|
||
answer = agent.run("Fake task.") | ||
assert answer == "2CUSTOM" | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In #1571, it was mentioned that the tests were flaky
Do you remember the error message in the failed runs? I ran the tests multiple times (Ran the entire
tests/test_agents.py
file a few times, mentioned details in the PR description).test_change_tools_after_init
used to always pass