Skip to content

Commit

Permalink
updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomdmoura committed Mar 3, 2024
1 parent 72a0d1e commit b856b21
Show file tree
Hide file tree
Showing 43 changed files with 43,126 additions and 32,950 deletions.
105 changes: 77 additions & 28 deletions tests/agent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ def test_agent_without_memory():
llm=ChatOpenAI(temperature=0, model="gpt-4"),
)

task = Task(description="How much is 1 + 1?", agent=no_memory_agent)
task = Task(
description="How much is 1 + 1?",
agent=no_memory_agent,
expected_output="the result of the math operation.",
)
result = no_memory_agent.execute_task(task)

assert result == "1 + 1 equals 2."
assert result == "The result of the math operation 1 + 1 is 2."
assert no_memory_agent.agent_executor.memory is None
assert memory_agent.agent_executor.memory is not None

Expand All @@ -83,10 +87,14 @@ def test_agent_execution():
allow_delegation=False,
)

task = Task(description="How much is 1 + 1?", agent=agent)
task = Task(
description="How much is 1 + 1?",
agent=agent,
expected_output="the result of the math operation.",
)

output = agent.execute_task(task)
assert output == "1 + 1 equals 2."
assert output == "The result of the math operation 1 + 1 is 2."


@pytest.mark.vcr(filter_headers=["authorization"])
Expand All @@ -104,7 +112,11 @@ def multiplier(first_number: int, second_number: int) -> float:
allow_delegation=False,
)

task = Task(description="What is 3 times 4?", agent=agent)
task = Task(
description="What is 3 times 4?",
agent=agent,
expected_output="The result of the multiplication.",
)
output = agent.execute_task(task)
assert output == "The result of 3 times 4 is 12."

Expand All @@ -126,14 +138,18 @@ def multiplier(first_number: int, second_number: int) -> float:
)

assert agent.tools_handler.last_used_tool == {}
task = Task(description="What is 3 times 4?", agent=agent)
task = Task(
description="What is 3 times 4?",
agent=agent,
expected_output="The result of the multiplication.",
)
# force cleaning cache
agent.tools_handler.cache = CacheHandler()
output = agent.execute_task(task)
tool_usage = InstructorToolCalling(
tool_name=multiplier.name, arguments={"first_number": 3, "second_number": 4}
)
assert output == "The result of multiplying 3 by 4 is 12."
assert output == "12"
assert agent.tools_handler.last_used_tool.tool_name == tool_usage.tool_name
assert agent.tools_handler.last_used_tool.arguments == tool_usage.arguments

Expand All @@ -157,8 +173,16 @@ def multiplier(first_number: int, second_number: int) -> float:
verbose=True,
)

task1 = Task(description="What is 2 times 6?", agent=agent)
task2 = Task(description="What is 3 times 3?", agent=agent)
task1 = Task(
description="What is 2 times 6?",
agent=agent,
expected_output="The result of the multiplication.",
)
task2 = Task(
description="What is 3 times 3?",
agent=agent,
expected_output="The result of the multiplication.",
)

output = agent.execute_task(task1)
output = agent.execute_task(task2)
Expand All @@ -168,7 +192,9 @@ def multiplier(first_number: int, second_number: int) -> float:
}

task = Task(
description="What is 2 times 6 times 3? Return only the number", agent=agent
description="What is 2 times 6 times 3? Return only the number",
agent=agent,
expected_output="The result of the multiplication.",
)
output = agent.execute_task(task)
assert output == "36"
Expand All @@ -184,6 +210,7 @@ def multiplier(first_number: int, second_number: int) -> float:
task = Task(
description="What is 2 times 6? Ignore correctness and just return the result of the multiplication tool.",
agent=agent,
expected_output="The result of the multiplication.",
)
output = agent.execute_task(task)
assert output == "0"
Expand All @@ -206,9 +233,13 @@ def multiplier(first_number: int, second_number: int) -> float:
allow_delegation=False,
)

task = Task(description="What is 3 times 4", agent=agent)
task = Task(
description="What is 3 times 4",
agent=agent,
expected_output="The result of the multiplication.",
)
output = agent.execute_task(task=task, tools=[multiplier])
assert output == "12"
assert output == "The result of the multiplication is 12."


@pytest.mark.vcr(filter_headers=["authorization"])
Expand All @@ -232,6 +263,7 @@ def get_final_answer(numbers) -> float:
) as private_mock:
task = Task(
description="The final answer is 42. But don't give it yet, instead keep using the `get_final_answer` tool.",
expected_output="The final answer",
)
agent.execute_task(
task=task,
Expand Down Expand Up @@ -259,7 +291,8 @@ def get_final_answer(anything: str) -> float:
)

task = Task(
description="The final answer is 42. But don't give it until I tell you so, instead keep using the `get_final_answer` tool."
description="The final answer is 42. But don't give it until I tell you so, instead keep using the `get_final_answer` tool.",
expected_output="The final answer",
)
# force cleaning cache
agent.tools_handler.cache = CacheHandler()
Expand All @@ -270,7 +303,7 @@ def get_final_answer(anything: str) -> float:

captured = capsys.readouterr()

assert "Final Answer: 42" in captured.out
assert "The final answer is 42." in captured.out


@pytest.mark.vcr(filter_headers=["authorization"])
Expand All @@ -290,7 +323,8 @@ def get_final_answer(numbers) -> float:
)

task = Task(
description="The final answer is 42. But don't give it yet, instead keep using the `get_final_answer` tool. Until you're told you could give my final answer if I'm ready."
description="The final answer is 42. But don't give it yet, instead keep using the `get_final_answer` tool over and over until you're told you can give yout final answer.",
expected_output="The final answer",
)
output = agent.execute_task(
task=task,
Expand Down Expand Up @@ -320,16 +354,14 @@ def get_final_answer(anything: str) -> float:
with patch.object(RPMController, "_wait_for_next_minute") as moveon:
moveon.return_value = True
task = Task(
description="Use tool logic for `get_final_answer` but fon't give you final answer yet, instead keep using it unless you're told to give your final answer"
description="Use tool logic for `get_final_answer` but fon't give you final answer yet, instead keep using it unless you're told to give your final answer",
expected_output="The final answer",
)
output = agent.execute_task(
task=task,
tools=[get_final_answer],
)
assert (
output
== 'The result of using the `get_final_answer` tool with the input "test input" is 42.'
)
assert output == "42"
captured = capsys.readouterr()
assert "Max RPM reached, waiting for next minute to start." in captured.out
moveon.assert_called()
Expand Down Expand Up @@ -357,7 +389,8 @@ def get_final_answer(numbers) -> float:
)

task = Task(
description="Don't give a Final Answer, instead keep using the `get_final_answer` tool.",
description="Use tool logic for `get_final_answer` but fon't give you final answer yet, instead keep using it unless you're told to give your final answer",
expected_output="The final answer",
tools=[get_final_answer],
agent=agent,
)
Expand Down Expand Up @@ -390,6 +423,7 @@ def get_final_answer(numbers) -> float:
backstory="test backstory",
max_rpm=10,
verbose=True,
allow_delegation=False,
)

agent2 = Agent(
Expand All @@ -398,15 +432,16 @@ def get_final_answer(numbers) -> float:
backstory="test backstory2",
max_iter=2,
verbose=True,
allow_delegation=False,
)

tasks = [
Task(
description="Just say hi.",
agent=agent1,
description="Just say hi.", agent=agent1, expected_output="Your greeting."
),
Task(
description="Don't give a Final Answer, instead keep using the `get_final_answer` tool non-stop",
description="NEVER give a Final Answer, instead keep using the `get_final_answer` tool non-stop",
expected_output="The final answer",
tools=[get_final_answer],
agent=agent2,
),
Expand All @@ -430,7 +465,7 @@ def test_agent_error_on_parsing_tool(capsys):
from langchain.tools import tool

@tool
def get_final_answer(anything: str) -> float:
def get_final_answer() -> float:
"""Get the final answer but don't give it yet, just re-use this
tool non-stop."""
return 42
Expand All @@ -444,12 +479,18 @@ def get_final_answer(anything: str) -> float:
tasks = [
Task(
description="Use the get_final_answer tool.",
expected_output="The final answer",
agent=agent1,
tools=[get_final_answer],
)
]

crew = Crew(agents=[agent1], tasks=tasks, verbose=2)
crew = Crew(
agents=[agent1],
tasks=tasks,
verbose=2,
function_calling_llm=ChatOpenAI(model="gpt-4-0125-preview"),
)

with patch.object(ToolUsage, "_render") as force_exception:
force_exception.side_effect = Exception("Error on parsing tool.")
Expand Down Expand Up @@ -480,6 +521,7 @@ def get_final_answer(anything: str) -> float:
tasks = [
Task(
description="Use tool logic for `get_final_answer` but fon't give you final answer yet, instead keep using it unless you're told to give your final answer",
expected_output="The final answer",
agent=agent1,
tools=[get_final_answer],
)
Expand All @@ -498,10 +540,15 @@ def test_agent_use_specific_tasks_output_as_context(capsys):

agent2 = Agent(role="test role2", goal="test goal2", backstory="test backstory2")

say_hi_task = Task(description="Just say hi.", agent=agent1)
say_bye_task = Task(description="Just say bye.", agent=agent1)
say_hi_task = Task(
description="Just say hi.", agent=agent1, expected_output="Your greeting."
)
say_bye_task = Task(
description="Just say bye.", agent=agent1, expected_output="Your farewell."
)
answer_task = Task(
description="Answer accordingly to the context you got.",
expected_output="Your answer.",
context=[say_hi_task],
agent=agent2,
)
Expand Down Expand Up @@ -537,6 +584,7 @@ def learn_about_AI(topic) -> float:

essay = Task(
description="Write and then review an small paragraph on AI until it's AMAZING",
expected_output="The final paragraph.",
agent=agent1,
)
tasks = [essay]
Expand Down Expand Up @@ -571,6 +619,7 @@ def learn_about_AI(topic) -> float:

essay = Task(
description="Write and then review an small paragraph on AI until it's AMAZING",
expected_output="The final paragraph.",
agent=agent1,
)
tasks = [essay]
Expand Down
Loading

0 comments on commit b856b21

Please sign in to comment.