Spaces:
Sleeping
Sleeping
File size: 7,315 Bytes
d8d14f1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | import datetime
from datetime import timedelta
from unittest.mock import Mock
import pytest
from dotenv import load_dotenv
from swarm_models.gpt4_vision_api import GPT4VisionAPI
from swarms.prompts.multi_modal_autonomous_instruction_prompt import (
MULTI_MODAL_AUTO_AGENT_SYSTEM_PROMPT_1,
)
from swarms.structs.agent import Agent
from swarms.structs.task import Task
load_dotenv()
@pytest.fixture
def llm():
return GPT4VisionAPI()
def test_agent_run_task(llm):
task = (
"Analyze this image of an assembly line and identify any"
" issues such as misaligned parts, defects, or deviations"
" from the standard assembly process. IF there is anything"
" unsafe in the image, explain why it is unsafe and how it"
" could be improved."
)
img = "assembly_line.jpg"
agent = Agent(
llm=llm,
max_loops="auto",
sop=MULTI_MODAL_AUTO_AGENT_SYSTEM_PROMPT_1,
dashboard=True,
)
result = agent.run(task=task, img=img)
# Add assertions here to verify the expected behavior of the agent's run method
assert isinstance(result, dict)
assert "response" in result
assert "dashboard_data" in result
# Add more assertions as needed
@pytest.fixture
def task():
agents = [Agent(llm=llm, id=f"Agent_{i}") for i in range(5)]
return Task(
id="Task_1", task="Task_Name", agents=agents, dependencies=[]
)
# Basic tests
def test_task_init(task):
assert task.id == "Task_1"
assert task.task == "Task_Name"
assert isinstance(task.agents, list)
assert len(task.agents) == 5
assert isinstance(task.dependencies, list)
def test_task_execute(task, mocker):
mocker.patch.object(Agent, "run", side_effect=[1, 2, 3, 4, 5])
parent_results = {}
task.execute(parent_results)
assert isinstance(task.results, list)
assert len(task.results) == 5
for result in task.results:
assert isinstance(result, int)
# Parameterized tests
@pytest.mark.parametrize("num_agents", [1, 3, 5, 10])
def test_task_num_agents(task, num_agents, mocker):
task.agents = [Agent(id=f"Agent_{i}") for i in range(num_agents)]
mocker.patch.object(Agent, "run", return_value=1)
parent_results = {}
task.execute(parent_results)
assert len(task.results) == num_agents
# Exception testing
def test_task_execute_with_dependency_error(task, mocker):
task.dependencies = ["NonExistentTask"]
mocker.patch.object(Agent, "run", return_value=1)
parent_results = {}
with pytest.raises(KeyError):
task.execute(parent_results)
# Mocking and monkeypatching tests
def test_task_execute_with_mocked_agents(task, mocker):
mock_agents = [Mock(spec=Agent) for _ in range(5)]
mocker.patch.object(task, "agents", mock_agents)
for mock_agent in mock_agents:
mock_agent.run.return_value = 1
parent_results = {}
task.execute(parent_results)
assert len(task.results) == 5
def test_task_creation():
agent = Agent()
task = Task(id="1", task="Task1", result=None, agents=[agent])
assert task.id == "1"
assert task.task == "Task1"
assert task.result is None
assert task.agents == [agent]
def test_task_with_dependencies():
agent = Agent()
task = Task(
id="2",
task="Task2",
result=None,
agents=[agent],
dependencies=["Task1"],
)
assert task.dependencies == ["Task1"]
def test_task_with_args():
agent = Agent()
task = Task(
id="3",
task="Task3",
result=None,
agents=[agent],
args=["arg1", "arg2"],
)
assert task.args == ["arg1", "arg2"]
def test_task_with_kwargs():
agent = Agent()
task = Task(
id="4",
task="Task4",
result=None,
agents=[agent],
kwargs={"kwarg1": "value1"},
)
assert task.kwargs == {"kwarg1": "value1"}
# ... continue creating tests for different scenarios
# Test execute method
def test_execute():
agent = Agent()
task = Task(id="5", task="Task5", result=None, agents=[agent])
# Assuming execute method returns True on successful execution
assert task.run() is True
def test_task_execute_with_agent(mocker):
mock_agent = mocker.Mock(spec=Agent)
mock_agent.run.return_value = "result"
task = Task(description="Test task", agent=mock_agent)
task.run()
assert task.result == "result"
assert task.history == ["result"]
def test_task_execute_with_callable(mocker):
mock_callable = mocker.Mock()
mock_callable.run.return_value = "result"
task = Task(description="Test task", agent=mock_callable)
task.run()
assert task.result == "result"
assert task.history == ["result"]
def test_task_execute_with_condition(mocker):
mock_agent = mocker.Mock(spec=Agent)
mock_agent.run.return_value = "result"
condition = mocker.Mock(return_value=True)
task = Task(
description="Test task", agent=mock_agent, condition=condition
)
task.run()
assert task.result == "result"
assert task.history == ["result"]
def test_task_execute_with_condition_false(mocker):
mock_agent = mocker.Mock(spec=Agent)
mock_agent.run.return_value = "result"
condition = mocker.Mock(return_value=False)
task = Task(
description="Test task", agent=mock_agent, condition=condition
)
task.run()
assert task.result is None
assert task.history == []
def test_task_execute_with_action(mocker):
mock_agent = mocker.Mock(spec=Agent)
mock_agent.run.return_value = "result"
action = mocker.Mock()
task = Task(
description="Test task", agent=mock_agent, action=action
)
task.run()
assert task.result == "result"
assert task.history == ["result"]
action.assert_called_once()
def test_task_handle_scheduled_task_now(mocker):
mock_agent = mocker.Mock(spec=Agent)
mock_agent.run.return_value = "result"
task = Task(
description="Test task",
agent=mock_agent,
schedule_time=datetime.now(),
)
task.handle_scheduled_task()
assert task.result == "result"
assert task.history == ["result"]
def test_task_handle_scheduled_task_future(mocker):
mock_agent = mocker.Mock(spec=Agent)
mock_agent.run.return_value = "result"
task = Task(
description="Test task",
agent=mock_agent,
schedule_time=datetime.now() + timedelta(days=1),
)
with mocker.patch.object(
task.scheduler, "enter"
) as mock_enter, mocker.patch.object(
task.scheduler, "run"
) as mock_run:
task.handle_scheduled_task()
mock_enter.assert_called_once()
mock_run.assert_called_once()
def test_task_set_trigger():
task = Task(description="Test task", agent=Agent())
def trigger():
return True
task.set_trigger(trigger)
assert task.trigger == trigger
def test_task_set_action():
task = Task(description="Test task", agent=Agent())
def action():
return True
task.set_action(action)
assert task.action == action
def test_task_set_condition():
task = Task(description="Test task", agent=Agent())
def condition():
return True
task.set_condition(condition)
assert task.condition == condition
|