You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
swarms/tests/agents/agents.py

145 lines
5.3 KiB

1 year ago
import pytest
from unittest.mock import Mock, patch
1 year ago
from swarms.agents.agents import (
AgentNodeInitializer,
AgentNode,
agent,
) # replace with actual import
1 year ago
# For initializing AgentNodeInitializer in multiple tests
@pytest.fixture
def mock_agent_node_initializer():
1 year ago
with patch("swarms.agents.agents.ChatOpenAI") as mock_llm, patch(
"swarms.agents.agents.AutoGPT"
) as mock_agent:
initializer = AgentNodeInitializer(
model_type="openai",
model_id="test",
openai_api_key="test_key",
temperature=0.5,
)
1 year ago
initializer.llm = mock_llm
initializer.tools = [Mock(spec=BaseTool)]
initializer.vectorstore = Mock()
initializer.agent = mock_agent
return initializer
# Test initialize_llm method of AgentNodeInitializer class
1 year ago
@pytest.mark.parametrize("model_type", ["openai", "huggingface", "invalid"])
1 year ago
def test_agent_node_initializer_initialize_llm(model_type, mock_agent_node_initializer):
1 year ago
with patch("swarms.agents.agents.ChatOpenAI") as mock_openai, patch(
"swarms.agents.agents.HuggingFaceLLM"
) as mock_huggingface:
if model_type == "invalid":
1 year ago
with pytest.raises(ValueError):
1 year ago
mock_agent_node_initializer.initialize_llm(
model_type, "model_id", "openai_api_key", 0.5
)
1 year ago
else:
1 year ago
mock_agent_node_initializer.initialize_llm(
model_type, "model_id", "openai_api_key", 0.5
)
if model_type == "openai":
1 year ago
mock_openai.assert_called_once()
1 year ago
elif model_type == "huggingface":
1 year ago
mock_huggingface.assert_called_once()
# Test add_tool method of AgentNodeInitializer class
def test_agent_node_initializer_add_tool(mock_agent_node_initializer):
1 year ago
with patch("swarms.agents.agents.BaseTool") as mock_base_tool:
1 year ago
mock_agent_node_initializer.add_tool(mock_base_tool)
assert mock_base_tool in mock_agent_node_initializer.tools
# Test run method of AgentNodeInitializer class
1 year ago
@pytest.mark.parametrize("prompt", ["valid prompt", ""])
1 year ago
def test_agent_node_initializer_run(prompt, mock_agent_node_initializer):
1 year ago
if prompt == "":
1 year ago
with pytest.raises(ValueError):
mock_agent_node_initializer.run(prompt)
else:
assert mock_agent_node_initializer.run(prompt) == "Task completed by AgentNode"
1 year ago
1 year ago
# For initializing AgentNode in multiple tests
@pytest.fixture
def mock_agent_node():
1 year ago
with patch("swarms.agents.agents.ChatOpenAI") as mock_llm, patch(
"swarms.agents.agents.AgentNodeInitializer"
) as mock_agent_node_initializer:
mock_agent_node = AgentNode("test_key")
1 year ago
mock_agent_node.llm_class = mock_llm
mock_agent_node.vectorstore = Mock()
mock_agent_node_initializer.llm = mock_llm
return mock_agent_node
1 year ago
1 year ago
# Test initialize_llm method of AgentNode class
1 year ago
@pytest.mark.parametrize("llm_class", ["openai", "huggingface"])
1 year ago
def test_agent_node_initialize_llm(llm_class, mock_agent_node):
1 year ago
with patch("swarms.agents.agents.ChatOpenAI") as mock_openai, patch(
"swarms.agents.agents.HuggingFaceLLM"
) as mock_huggingface:
1 year ago
mock_agent_node.initialize_llm(llm_class)
1 year ago
if llm_class == "openai":
1 year ago
mock_openai.assert_called_once()
1 year ago
elif llm_class == "huggingface":
1 year ago
mock_huggingface.assert_called_once()
1 year ago
1 year ago
# Test initialize_tools method of AgentNode class
def test_agent_node_initialize_tools(mock_agent_node):
1 year ago
with patch("swarms.agents.agents.DuckDuckGoSearchRun") as mock_ddg, patch(
"swarms.agents.agents.WriteFileTool"
) as mock_write_file, patch(
"swarms.agents.agents.ReadFileTool"
) as mock_read_file, patch(
"swarms.agents.agents.process_csv"
) as mock_process_csv, patch(
"swarms.agents.agents.WebpageQATool"
) as mock_webpage_qa:
mock_agent_node.initialize_tools("openai")
1 year ago
assert mock_ddg.called
assert mock_write_file.called
assert mock_read_file.called
assert mock_process_csv.called
assert mock_webpage_qa.called
# Test create_agent method of AgentNode class
def test_agent_node_create_agent(mock_agent_node):
1 year ago
with patch.object(mock_agent_node, "initialize_llm"), patch.object(
mock_agent_node, "initialize_tools"
), patch.object(mock_agent_node, "initialize_vectorstore"), patch(
"swarms.agents.agents.AgentNodeInitializer"
) as mock_agent_node_initializer:
1 year ago
mock_agent_node.create_agent()
mock_agent_node_initializer.assert_called_once()
mock_agent_node_initializer.return_value.create_agent.assert_called_once()
# Test agent function
1 year ago
@pytest.mark.parametrize(
"openai_api_key,objective",
[("valid_key", "valid_objective"), ("", "valid_objective"), ("valid_key", "")],
)
1 year ago
def test_agent(openai_api_key, objective):
1 year ago
if openai_api_key == "" or objective == "":
1 year ago
with pytest.raises(ValueError):
agent(openai_api_key, objective)
else:
1 year ago
with patch(
"swarms.agents.agents.AgentNodeInitializer"
) as mock_agent_node_initializer:
mock_agent_node = (
mock_agent_node_initializer.return_value.create_agent.return_value
)
mock_agent_node.run.return_value = "Agent output"
1 year ago
result = agent(openai_api_key, objective)
1 year ago
assert result == "Agent output"