parent
9f72701515
commit
fa1e92714b
@ -0,0 +1,89 @@
|
||||
from swarms import OpenAIChat, AgentRearrange, Agent
|
||||
from swarms.prompts.finance_agent_sys_prompt import (
|
||||
FINANCIAL_AGENT_SYS_PROMPT,
|
||||
)
|
||||
from swarms.utils.data_to_text import data_to_text
|
||||
|
||||
model = OpenAIChat(max_tokens=3000)
|
||||
|
||||
# Initialize the agent
|
||||
receipt_analyzer_agent = Agent(
|
||||
agent_name="Receipt Analyzer",
|
||||
system_prompt=FINANCIAL_AGENT_SYS_PROMPT,
|
||||
llm=model,
|
||||
max_loops=1,
|
||||
autosave=True,
|
||||
# dynamic_temperature_enabled=True,
|
||||
dashboard=False,
|
||||
verbose=True,
|
||||
streaming_on=True,
|
||||
# interactive=True, # Set to False to disable interactive mode
|
||||
dynamic_temperature_enabled=True,
|
||||
saved_state_path="finance_agent.json",
|
||||
# tools=[Add your functions here# ],
|
||||
# stopping_token="Stop!",
|
||||
# interactive=True,
|
||||
# docs_folder="docs", # Enter your folder name
|
||||
# pdf_path="docs/finance_agent.pdf",
|
||||
# sop="Calculate the profit for a company.",
|
||||
# sop_list=["Calculate the profit for a company."],
|
||||
user_name="swarms_corp",
|
||||
# # docs=
|
||||
# # docs_folder="docs",
|
||||
retry_attempts=3,
|
||||
# tool_schema = dict
|
||||
# agent_ops_on=True,
|
||||
# long_term_memory=ChromaDB(docs_folder="artifacts"),
|
||||
# multi_modal=True
|
||||
)
|
||||
|
||||
|
||||
# 2nd agent
|
||||
analyst_agent = Agent(
|
||||
agent_name="Analyst_Agent",
|
||||
system_prompt=FINANCIAL_AGENT_SYS_PROMPT,
|
||||
llm=model,
|
||||
max_loops=1,
|
||||
autosave=True,
|
||||
# dynamic_temperature_enabled=True,
|
||||
dashboard=False,
|
||||
verbose=True,
|
||||
streaming_on=True,
|
||||
# interactive=True, # Set to False to disable interactive mode
|
||||
dynamic_temperature_enabled=True,
|
||||
saved_state_path="finance_agent.json",
|
||||
# tools=[Add your functions here# ],
|
||||
# stopping_token="Stop!",
|
||||
# interactive=True,
|
||||
# docs_folder="docs", # Enter your folder name
|
||||
# pdf_path="docs/finance_agent.pdf",
|
||||
# sop="Calculate the profit for a company.",
|
||||
# sop_list=["Calculate the profit for a company."],
|
||||
user_name="swarms_corp",
|
||||
# # docs=
|
||||
# # docs_folder="docs",
|
||||
retry_attempts=3,
|
||||
# tool_schema = dict
|
||||
# agent_ops_on=True,
|
||||
# long_term_memory=ChromaDB(docs_folder="artifacts"),
|
||||
# multi_modal=True,
|
||||
)
|
||||
|
||||
|
||||
# sWARM
|
||||
agents = [receipt_analyzer_agent, analyst_agent]
|
||||
|
||||
# Flow
|
||||
flow = f"{receipt_analyzer_agent.agent_name} -> {analyst_agent.agent_name} -> H"
|
||||
pdf = data_to_text("receipt.pdf")
|
||||
|
||||
# Swarm
|
||||
swarm = AgentRearrange(
|
||||
agents=agents,
|
||||
flow=flow,
|
||||
)
|
||||
|
||||
# Run the swarm
|
||||
swarm.run(
|
||||
f"Analyze this PDF: {pdf} and return a summary of the expense and if it's necessary"
|
||||
)
|
Binary file not shown.
@ -1,58 +1,124 @@
|
||||
import pytest
|
||||
from swarms.structs.rearrange import AgentRearrange
|
||||
from unittest.mock import MagicMock
|
||||
from swarms import AgentRearrange
|
||||
|
||||
|
||||
# Mocking the Agent class
|
||||
class MockAgent:
|
||||
def __init__(self, agent_name):
|
||||
self.agent_name = agent_name
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
||||
def run(self, task):
|
||||
return f"Running {task}"
|
||||
def run(self, task, img=None, *args, **kwargs):
|
||||
return f"{self.name} processed {task}"
|
||||
|
||||
|
||||
# Test for AgentRearrange class
|
||||
class TestAgentRearrange:
|
||||
@pytest.fixture
|
||||
def agent_rearrange(self):
|
||||
agents = [MockAgent("agent1"), MockAgent("agent2")]
|
||||
return AgentRearrange(agents=agents)
|
||||
def mock_agents():
|
||||
return [
|
||||
MockAgent(name="Agent1"),
|
||||
MockAgent(name="Agent2"),
|
||||
MockAgent(name="Agent3"),
|
||||
]
|
||||
|
||||
def test_parse_pattern(self, agent_rearrange):
|
||||
assert agent_rearrange.parse_pattern("agent1->agent2") is True
|
||||
assert agent_rearrange.parse_pattern("agent3->agent4") is False
|
||||
|
||||
def test_self_find_agent_by_name(self, agent_rearrange):
|
||||
@pytest.fixture
|
||||
def agent_rearrange(mock_agents):
|
||||
return AgentRearrange(
|
||||
agents=mock_agents, flow="Agent1 -> Agent2 -> Agent3"
|
||||
)
|
||||
|
||||
|
||||
def test_initialization(mock_agents):
|
||||
agent_rearrange = AgentRearrange(
|
||||
agents=mock_agents, flow="Agent1 -> Agent2 -> Agent3"
|
||||
)
|
||||
assert len(agent_rearrange.agents) == 3
|
||||
assert agent_rearrange.flow == "Agent1 -> Agent2 -> Agent3"
|
||||
|
||||
|
||||
def test_add_agent(agent_rearrange):
|
||||
new_agent = MockAgent(name="Agent4")
|
||||
agent_rearrange.add_agent(new_agent)
|
||||
assert "Agent4" in agent_rearrange.agents
|
||||
|
||||
|
||||
def test_remove_agent(agent_rearrange):
|
||||
agent_rearrange.remove_agent("Agent2")
|
||||
assert "Agent2" not in agent_rearrange.agents
|
||||
|
||||
|
||||
def test_add_agents(agent_rearrange):
|
||||
new_agents = [MockAgent(name="Agent4"), MockAgent(name="Agent5")]
|
||||
agent_rearrange.add_agents(new_agents)
|
||||
assert "Agent4" in agent_rearrange.agents
|
||||
assert "Agent5" in agent_rearrange.agents
|
||||
|
||||
|
||||
def test_validate_flow_valid(agent_rearrange):
|
||||
assert agent_rearrange.validate_flow() == True
|
||||
|
||||
|
||||
def test_validate_flow_invalid(agent_rearrange):
|
||||
agent_rearrange.flow = "Agent1 -> Agent4"
|
||||
with pytest.raises(ValueError):
|
||||
agent_rearrange.validate_flow()
|
||||
|
||||
|
||||
def test_run(agent_rearrange):
|
||||
result = agent_rearrange.run("Test Task")
|
||||
assert (
|
||||
agent_rearrange.self_find_agent_by_name("agent1").agent_name
|
||||
== "agent1"
|
||||
result
|
||||
== "Agent1 processed Test Task; Agent2 processed Agent1 processed Test Task; Agent3 processed Agent2 processed Agent1 processed Test Task"
|
||||
)
|
||||
assert agent_rearrange.self_find_agent_by_name("agent3") is None
|
||||
|
||||
def test_agent_exists(self, agent_rearrange):
|
||||
assert agent_rearrange.agent_exists("agent1") is True
|
||||
assert agent_rearrange.agent_exists("agent3") is False
|
||||
|
||||
def test_parse_concurrent_flow(self, agent_rearrange):
|
||||
agent_rearrange.parse_concurrent_flow("agent1->agent2")
|
||||
assert "agent2" in agent_rearrange.flows["agent1"]
|
||||
def test_run_with_custom_tasks(agent_rearrange):
|
||||
custom_tasks = {"Agent2": "Custom Task"}
|
||||
result = agent_rearrange.run("Test Task", custom_tasks=custom_tasks)
|
||||
assert (
|
||||
result
|
||||
== "Agent1 processed Test Task; Agent2 processed Custom Task; Agent3 processed Agent2 processed Custom Task"
|
||||
)
|
||||
|
||||
def test_parse_sequential_flow(self, agent_rearrange):
|
||||
agent_rearrange.parse_sequential_flow("agent1", "agent2")
|
||||
assert "agent2" in agent_rearrange.flows["agent1"]
|
||||
|
||||
def test_execute_task(self, agent_rearrange):
|
||||
def test_run_with_human_intervention(agent_rearrange):
|
||||
agent_rearrange.human_in_the_loop = True
|
||||
agent_rearrange.custom_human_in_the_loop = MagicMock(
|
||||
return_value="Human processed Task"
|
||||
)
|
||||
agent_rearrange.flow = "Agent1 -> H -> Agent3"
|
||||
result = agent_rearrange.run("Test Task")
|
||||
assert (
|
||||
agent_rearrange.execute_task("agent1", "agent2", "task1", {})
|
||||
== "Running task1 (from agent2)"
|
||||
result
|
||||
== "Agent1 processed Test Task; Human processed Task; Agent3 processed Human processed Task"
|
||||
)
|
||||
|
||||
def test_process_flows(self, agent_rearrange):
|
||||
assert agent_rearrange.process_flows(
|
||||
"agent1->agent2", "task1", {}
|
||||
) == ["Running task1"]
|
||||
|
||||
def test_call(self, agent_rearrange):
|
||||
assert agent_rearrange(
|
||||
pattern="agent1->agent2", default_task="task1"
|
||||
) == ["Running task1"]
|
||||
def test_run_sub_swarm(agent_rearrange):
|
||||
sub_swarm_flow = "Agent1 -> Agent3"
|
||||
agent_rearrange.add_sub_swarm("SubSwarm1", sub_swarm_flow)
|
||||
result = agent_rearrange.run_sub_swarm("SubSwarm1", "Sub Task", None)
|
||||
assert (
|
||||
result
|
||||
== "Agent1 processed Sub Task; Agent3 processed Agent1 processed Sub Task"
|
||||
)
|
||||
|
||||
|
||||
def test_process_agent_or_swarm(agent_rearrange):
|
||||
result = agent_rearrange.process_agent_or_swarm(
|
||||
"Agent1", "Process Task", None
|
||||
)
|
||||
assert result == "Agent1 processed Process Task"
|
||||
|
||||
|
||||
def test_track_history(agent_rearrange):
|
||||
agent_rearrange.track_history("Agent1", "Task Result")
|
||||
assert agent_rearrange.swarm_history["Agent1"] == ["Task Result"]
|
||||
|
||||
|
||||
def test_human_intervention(agent_rearrange):
|
||||
agent_rearrange.human_in_the_loop = True
|
||||
agent_rearrange.custom_human_in_the_loop = MagicMock(
|
||||
return_value="Human processed Task"
|
||||
)
|
||||
result = agent_rearrange.human_intervention("Task")
|
||||
assert result == "Human processed Task"
|
||||
|
Loading…
Reference in new issue